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/apidomain_test.go

168 lines
4.6 KiB
Go

package host
import (
"net/http"
"strconv"
"testing"
"dev.sum7.eu/sum7/warehost/system"
"dev.sum7.eu/sum7/warehost/test"
)
func TestAPIDomain(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)
loginTest(session, assertion)
result, w := session.JSONRequest("DELETE", "/host/delete", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
// 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)
/*
* TEST domainList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/domain", nil)
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/domain", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
result, w = session.JSONRequest("GET", "/host/domain?filter=all", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
/*
* TEST domainAdd
*/
session.Clean()
result, w = session.JSONRequest("POST", "/host/domain", Domain{
FQDN: "example.de",
})
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/domain", []byte{2, 3})
assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data)
result, w = session.JSONRequest("POST", "/host/domain", Domain{
FQDN: "example.de",
})
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
result, w = session.JSONRequest("POST", "/host/domain", Domain{
FQDN: "example.de",
})
assertion.Equal(http.StatusInternalServerError, w.StatusCode)
assertion.Equal(false, result.Data)
/*
* TEST domainEdit
*/
result, w = session.JSONRequest("GET", "/host/domain", nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
var domain int
for _, obj := range result.Data.([]interface{}) {
item := obj.(map[string]interface{})
if item["fqdn"] == "example.de" {
domain = int(item["ID"].(float64))
break
}
}
session.Clean()
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain), Domain{
Mail: true,
})
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
loginTest(session, assertion)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain), Domain{
Mail: true,
})
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(-1), Domain{
Mail: true,
})
assertion.Equal(http.StatusNotFound, w.StatusCode)
assertion.Equal(false, result.Data)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain), []byte{2, 3})
assertion.Equal(http.StatusBadRequest, w.StatusCode)
assertion.Equal(false, result.Data)
/*
* TEST domainShow
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain), nil)
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(-1), nil)
assertion.Equal(http.StatusNotFound, w.StatusCode)
assertion.Equal(false, result.Data)
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain), nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.NotEqual(false, result.Data)
/*
* TEST domainDelete
*/
session.Clean()
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain), nil)
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
loginTest(session, assertion)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(-1), nil)
assertion.Equal(http.StatusNotFound, w.StatusCode)
assertion.Equal(false, result.Data)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain), nil)
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain), nil)
assertion.Equal(http.StatusNotFound, w.StatusCode)
assertion.Equal(false, result.Data)
// CLEANUP
}