package host import ( "net/http" "testing" "github.com/stretchr/testify/assert" "dev.sum7.eu/sum7/warehost/system" "dev.sum7.eu/sum7/warehost/test" ) func loginTest(session *test.Request, assertion *assert.Assertions) { result, w := session.JSONRequest("POST", "/login", system.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 system.SyncModels(db) db.Unscoped().Delete(Profil{}) SyncModels(db) // Bind API system.BindAPI(db, router, "") BindAPI(db, router, "/host") session := test.NewSession(router) /* * TEST signup */ result, w := session.JSONRequest("POST", "/host/signup", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) loginTest(session, assertion) result, w = session.JSONRequest("POST", "/host/signup", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) result, w = session.JSONRequest("POST", "/host/signup", nil) assertion.Equal(http.StatusInternalServerError, w.StatusCode) assertion.Equal(false, result.Data) /* * TEST checksignup */ session.Clean() result, w = session.JSONRequest("GET", "/host/signup", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) loginTest(session, assertion) result, w = session.JSONRequest("GET", "/host/signup", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) /* * TEST delete */ session.Clean() result, w = session.JSONRequest("DELETE", "/host/delete", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) loginTest(session, assertion) result, w = session.JSONRequest("DELETE", "/host/delete", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) result, w = session.JSONRequest("DELETE", "/host/delete", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) /* * TEST profil */ session.Clean() result, w = session.JSONRequest("GET", "/host/profil", nil) assertion.Equal(http.StatusUnauthorized, w.StatusCode) assertion.Equal(false, result.Data) loginTest(session, assertion) result, w = session.JSONRequest("GET", "/host/profil", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(false, result.Data) // Need a Profile for Next tests result, w = session.JSONRequest("POST", "/host/signup", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.Equal(true, result.Data) result, w = session.JSONRequest("GET", "/host/profil", nil) assertion.Equal(http.StatusOK, w.StatusCode) assertion.NotEqual(false, result.Data) }