package system import ( "net/http" "testing" "github.com/stretchr/testify/assert" "dev.sum7.eu/sum7/warehost/test" ) func loginTest(session *test.Request, assertion *assert.Assertions) { result, w := session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) } func TestAPI(t *testing.T) { assertion, db, router := test.Init(t) defer db.Close() //load system Models to database SyncModels(db) // Bind API BindAPI(db, router, "") session := test.NewSession(router) /* * TEST status */ result, w := session.JSONRequest("GET", "/status", nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Nil(result.Error) assertion.Equal(result.Data, true) /* * TEST login */ result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) assertion.Equal(result.Error.Fields[0], "password") result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) assertion.Equal(result.Error.Fields[0], "username") result, w = session.JSONRequest("POST", "/login", []byte{2, 3}) assertion.Equal(w.StatusCode, http.StatusBadRequest) //login before loginTest(session, assertion) /* * TEST logout */ result, w = session.JSONRequest("GET", "/logout", nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) // Test if crash on if not login in result, w = session.JSONRequest("GET", "/logout", nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) /* * TEST password */ result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) //login before loginTest(session, assertion) result, w = session.JSONRequest("POST", "/password", []byte{2, 3}) assertion.Equal(w.StatusCode, http.StatusBadRequest) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-wrong", NewPassword: "root-bug"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) assertion.Equal(result.Error.Fields[0], "currentpassword") result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: ""}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) assertion.Equal(result.Error.Fields[0], "newpassword") result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-tmp"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-tmp", NewPassword: "root"}) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, true) /* * TEST inviteList */ session.Clean() result, w = session.JSONRequest("GET", "/invite", nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) //login before loginTest(session, assertion) result, w = session.JSONRequest("GET", "/invite", nil) assertion.Equal(w.StatusCode, http.StatusOK) /* * TEST loginList */ session.Clean() result, w = session.JSONRequest("GET", "/user", nil) assertion.Equal(w.StatusCode, http.StatusOK) assertion.Equal(result.Data, false) //login before loginTest(session, assertion) result, w = session.JSONRequest("GET", "/user", nil) assertion.Equal(w.StatusCode, http.StatusOK) }