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