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

174 lines
5.1 KiB
Go

package host
import (
"net/http"
"strconv"
"testing"
"dev.sum7.eu/sum7/warehost/system"
"dev.sum7.eu/sum7/warehost/test"
)
func TestAPIWeb(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(w.StatusCode, http.StatusOK)
// 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)
// Need a Domain for next tests
result, w = session.JSONRequest("POST", "/host/domain", Domain{
FQDN: "example.de",
})
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
// Get id von domain
result, w = session.JSONRequest("GET", "/host/domain", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.NotEqual(result.Data, false)
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
}
}
/*
* TEST webList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(-1)+"/web", nil)
assertion.Equal(w.StatusCode, http.StatusNotFound)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.NotEqual(result.Data, false)
/*
* TEST webAdd
*/
session.Clean()
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{
Subdomain: "",
})
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(-1)+"/web", Web{
Subdomain: "",
})
assertion.Equal(w.StatusCode, http.StatusNotFound)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{
Subdomain: "",
})
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
result, w = session.JSONRequest("POST", "/host/domain/"+strconv.Itoa(domain)+"/web", Web{
Subdomain: "",
})
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
/*
* TEST webEdit
*/
result, w = session.JSONRequest("GET", "/host/domain/"+strconv.Itoa(domain)+"/web", nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.NotEqual(result.Data, false)
var web int
for _, obj := range result.Data.([]interface{}) {
item := obj.(map[string]interface{})
if item["subdomain"] == "" {
web = int(item["ID"].(float64))
break
}
}
session.Clean()
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{
Subdomain: "test-bug-auth",
})
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), []byte{2, 3})
assertion.Equal(w.StatusCode, http.StatusBadRequest)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), Web{
Subdomain: "test-bug",
})
assertion.Equal(w.StatusCode, http.StatusNotFound)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("PATCH", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), Web{
Subdomain: "test",
})
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
/*
* TEST domainDelete
*/
session.Clean()
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil)
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
assertion.Equal(result.Data, false)
loginTest(session, assertion)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(-1), nil)
assertion.Equal(w.StatusCode, http.StatusNotFound)
assertion.Equal(result.Data, false)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil)
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
result, w = session.JSONRequest("DELETE", "/host/domain/"+strconv.Itoa(domain)+"/web/"+strconv.Itoa(web), nil)
assertion.Equal(w.StatusCode, http.StatusNotFound)
assertion.Equal(result.Data, false)
}