sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/system/api_test.go

54 lines
1.4 KiB
Go
Raw Normal View History

2016-10-07 15:13:31 +02:00
package system
import (
"net/http"
"testing"
2016-10-11 20:16:24 +02:00
"dev.sum7.eu/sum7/warehost/test"
2016-10-07 15:13:31 +02:00
)
func TestAPI(t *testing.T) {
assert, config, sessions, db, router := test.Init(t)
defer db.Close()
//load system Models to database
SyncModels(db)
// Bind API
2016-10-11 20:16:24 +02:00
BindAPI(db, router, "")
2016-10-07 15:13:31 +02:00
session := test.NewSession(router)
// Test status
result, w := session.JSONRequest("GET", "/status", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Nil(result.Error)
assert.Equal(result.Data, true)
// Test login
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
assert.Equal(w.StatusCode, http.StatusBadRequest)
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, true)
// Test logout
result, w = session.JSONRequest("GET", "/logout", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, true)
// Test if crash on if not login in
result, w = session.JSONRequest("GET", "/logout", nil)
assert.Equal(w.StatusCode, http.StatusOK)
assert.Equal(result.Data, false)
}