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(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) } 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(http.StatusOK, w.StatusCode) assertion.Nil(result.Error) assertion.Equal(true, result.Data) /* * TEST login */ result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) assertion.Equal("password", result.Error.Fields[0]) result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) assertion.Equal("username", result.Error.Fields[0]) result, w = session.JSONRequest("POST", "/login", []byte{2, 3}) assertion.Equal(http.StatusBadRequest, w.StatusCode) //login before loginTest(session, assertion) /* * TEST logout */ result, w = session.JSONRequest("GET", "/logout", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) // Test if crash on if not login in result, w = session.JSONRequest("GET", "/logout", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) /* * TEST password */ session.Clean() result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"}) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) //login before loginTest(session, assertion) result, w = session.JSONRequest("POST", "/password", []byte{2, 3}) assertion.Equal(http.StatusBadRequest, w.StatusCode) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-wrong", NewPassword: "root-bug"}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) assertion.Equal("currentpassword", result.Error.Fields[0]) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: ""}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) assertion.Equal("newpassword", result.Error.Fields[0]) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-tmp"}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-tmp", NewPassword: "root"}) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) /* * TEST inviteList */ session.Clean() result, w = session.JSONRequest("GET", "/invite", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) //login before loginTest(session, assertion) result, w = session.JSONRequest("GET", "/invite", nil) assertion.Equal(http.StatusOK, w.StatusCode) /* * TEST loginList */ session.Clean() result, w = session.JSONRequest("GET", "/user", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) //login before loginTest(session, assertion) result, w = session.JSONRequest("GET", "/user", nil) assertion.Equal(http.StatusOK, w.StatusCode) }