2016-10-17 14:07:17 +02:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-17 19:21:17 +02:00
|
|
|
"strconv"
|
2016-10-17 14:07:17 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"dev.sum7.eu/sum7/warehost/system"
|
|
|
|
"dev.sum7.eu/sum7/warehost/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPIDatabase(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)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TEST databaseList
|
|
|
|
*/
|
|
|
|
session.Clean()
|
|
|
|
result, w = session.JSONRequest("GET", "/host/database", nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
|
|
|
loginTest(session, assertion)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("GET", "/host/database", nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.NotEqual(result.Data, false)
|
|
|
|
|
2016-10-18 19:28:57 +02:00
|
|
|
result, w = session.JSONRequest("GET", "/host/database?filter=all", nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.NotEqual(result.Data, false)
|
|
|
|
|
2016-10-17 14:07:17 +02:00
|
|
|
/*
|
|
|
|
* TEST databaseAdd
|
|
|
|
*/
|
|
|
|
result, w = session.JSONRequest("POST", "/host/database", Database{
|
|
|
|
Password: "example.de",
|
|
|
|
Comment: "test",
|
|
|
|
})
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.NotEqual(result.Data, false)
|
2016-10-17 19:21:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TEST databaseEdit
|
|
|
|
*/
|
|
|
|
result, w = session.JSONRequest("GET", "/host/database", nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.NotEqual(result.Data, false)
|
|
|
|
var database int
|
|
|
|
for _, obj := range result.Data.([]interface{}) {
|
|
|
|
item := obj.(map[string]interface{})
|
|
|
|
if item["password"] == "example.de" {
|
|
|
|
database = int(item["ID"].(float64))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.Clean()
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(database), Database{
|
|
|
|
Comment: "test-bug",
|
|
|
|
})
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
|
|
|
loginTest(session, assertion)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(database), Database{
|
|
|
|
Comment: "test2",
|
|
|
|
})
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.Equal(result.Data, true)
|
|
|
|
|
2016-10-21 21:32:30 +02:00
|
|
|
result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(-1), Database{
|
|
|
|
Comment: "test-bug",
|
|
|
|
})
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusNotFound)
|
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
result, w = session.JSONRequest("PATCH", "/host/database/"+strconv.Itoa(database), []byte{2, 3})
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusBadRequest)
|
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TEST databaseDelete
|
|
|
|
*/
|
|
|
|
session.Clean()
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("DELETE", "/host/database/"+strconv.Itoa(database), nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
|
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
|
|
|
loginTest(session, assertion)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("DELETE", "/host/database/"+strconv.Itoa(database), nil)
|
|
|
|
assertion.Equal(w.StatusCode, http.StatusOK)
|
|
|
|
assertion.Equal(result.Data, true)
|
|
|
|
|
|
|
|
result, w = session.JSONRequest("DELETE", "/host/database/"+strconv.Itoa(database), nil)
|
2016-10-21 21:32:30 +02:00
|
|
|
assertion.Equal(w.StatusCode, http.StatusNotFound)
|
2016-10-17 19:21:17 +02:00
|
|
|
assertion.Equal(result.Data, false)
|
|
|
|
|
2016-10-17 14:07:17 +02:00
|
|
|
}
|