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
Raw Permalink Normal View History

2016-10-16 19:00:53 +02:00
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"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-16 19:00:53 +02:00
}
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)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-16 19:00:53 +02:00
loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/signup", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-16 19:00:53 +02:00
result, w = session.JSONRequest("POST", "/host/signup", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-16 19:00:53 +02:00
2016-10-17 11:54:35 +02:00
/*
* TEST checksignup
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/signup", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-17 11:54:35 +02:00
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/signup", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-17 11:54:35 +02:00
2016-10-16 19:00:53 +02:00
/*
* TEST delete
*/
session.Clean()
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-16 19:00:53 +02:00
loginTest(session, assertion)
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-16 19:00:53 +02:00
2016-10-17 14:07:17 +02:00
result, w = session.JSONRequest("DELETE", "/host/delete", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-17 14:07:17 +02:00
2016-10-17 11:54:35 +02:00
/*
* TEST profil
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/profil", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-17 11:54:35 +02:00
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/profil", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-17 11:54:35 +02:00
// Need a Profile for Next tests
result, w = session.JSONRequest("POST", "/host/signup", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-17 11:54:35 +02:00
result, w = session.JSONRequest("GET", "/host/profil", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
2016-10-16 19:00:53 +02:00
}