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/modul/host/api_test.go

106 lines
2.8 KiB
Go

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(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
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(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/signup", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
result, w = session.JSONRequest("POST", "/host/signup", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
/*
* TEST checksignup
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/signup", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/signup", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
/*
* TEST delete
*/
session.Clean()
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
/*
* TEST profil
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/profil", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/profil", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
// Need a Profile for Next tests
result, w = session.JSONRequest("POST", "/host/signup", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
result, w = session.JSONRequest("GET", "/host/profil", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.NotEqual(result.Data, false)
}