mirror of
https://github.com/pushbits/server.git
synced 2025-05-07 14:16:15 +02:00
add Tests for getID
This commit is contained in:
parent
49ec908fce
commit
ef5409eb35
2 changed files with 193 additions and 136 deletions
|
@ -17,10 +17,10 @@ import (
|
|||
)
|
||||
|
||||
var TestApplicationHandler *ApplicationHandler
|
||||
var TestUser *model.User
|
||||
var TestUsers []*model.User
|
||||
|
||||
// Collect all created applications to check & delete them later
|
||||
var SuccessAplications []model.Application
|
||||
var SuccessAplications map[uint][]model.Application
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// Get main config and adapt
|
||||
|
@ -43,13 +43,10 @@ func TestMain(m *testing.M) {
|
|||
}
|
||||
|
||||
TestApplicationHandler = appHandler
|
||||
TestUsers = mockups.GetUsers(config)
|
||||
SuccessAplications = make(map[uint][]model.Application)
|
||||
|
||||
// Run for each user
|
||||
for _, user := range mockups.GetUsers(config) {
|
||||
SuccessAplications = []model.Application{}
|
||||
TestUser = user
|
||||
m.Run()
|
||||
}
|
||||
cleanUp()
|
||||
}
|
||||
|
||||
|
@ -67,7 +64,7 @@ func TestApi_RegisterApplicationWithoutUser(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestApi_RgisterApplication(t *testing.T) {
|
||||
func TestApi_RegisterApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
|
@ -76,6 +73,8 @@ func TestApi_RgisterApplication(t *testing.T) {
|
|||
testCases = append(testCases, tests.Request{Name: "Invalid JSON Data", Method: "POST", Endpoint: "/application", Data: `{"name": "test1", "strict_compatibility": "oh yes"}`, Headers: map[string]string{"Content-Type": "application/json"}, ShouldStatus: 400})
|
||||
testCases = append(testCases, tests.Request{Name: "Valid JSON Data", Method: "POST", Endpoint: "/application", Data: `{"name": "test2", "strict_compatibility": true}`, Headers: map[string]string{"Content-Type": "application/json"}, ShouldStatus: 200})
|
||||
|
||||
for _, user := range TestUsers {
|
||||
SuccessAplications[user.ID] = make([]model.Application, 0)
|
||||
for _, req := range testCases {
|
||||
var application model.Application
|
||||
w, c, err := req.GetRequest()
|
||||
|
@ -83,7 +82,7 @@ func TestApi_RgisterApplication(t *testing.T) {
|
|||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
TestApplicationHandler.CreateApplication(c)
|
||||
|
||||
// Parse body only for successful requests
|
||||
|
@ -99,11 +98,12 @@ func TestApi_RgisterApplication(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
|
||||
SuccessAplications = append(SuccessAplications, application)
|
||||
SuccessAplications[user.ID] = append(SuccessAplications[user.ID], application)
|
||||
}
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "CreateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_GetApplications(t *testing.T) {
|
||||
|
@ -115,13 +115,14 @@ func TestApi_GetApplications(t *testing.T) {
|
|||
testCases := make([]tests.Request, 0)
|
||||
testCases = append(testCases, tests.Request{Name: "Valid Request", Method: "GET", Endpoint: "/application", ShouldStatus: 200})
|
||||
|
||||
for _, user := range TestUsers {
|
||||
for _, req := range testCases {
|
||||
w, c, err := req.GetRequest()
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
TestApplicationHandler.GetApplications(c)
|
||||
|
||||
// Parse body only for successful requests
|
||||
|
@ -137,12 +138,13 @@ func TestApi_GetApplications(t *testing.T) {
|
|||
continue
|
||||
}
|
||||
|
||||
assert.Truef(validateAllApplications(applications), "Did not find application created previously")
|
||||
assert.Equalf(len(applications), len(SuccessAplications), "Created %d application(s) but got %d back", len(SuccessAplications), len(applications))
|
||||
assert.Truef(validateAllApplications(user, applications), "Did not find application created previously")
|
||||
assert.Equalf(len(applications), len(SuccessAplications[user.ID]), "Created %d application(s) but got %d back", len(SuccessAplications[user.ID]), len(applications))
|
||||
}
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "GetApplications (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_GetApplicationsWithoutUser(t *testing.T) {
|
||||
|
@ -170,18 +172,20 @@ func TestApi_GetApplicationErrors(t *testing.T) {
|
|||
testCases[5555] = tests.Request{Name: "Requesting unknown application 5555", Method: "GET", Endpoint: "/application/5555", ShouldStatus: 404}
|
||||
testCases[99999999999999999] = tests.Request{Name: "Requesting unknown application 99999999999999999", Method: "GET", Endpoint: "/application/99999999999999999", ShouldStatus: 404}
|
||||
|
||||
for _, user := range TestUsers {
|
||||
for id, req := range testCases {
|
||||
w, c, err := req.GetRequest()
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
c.Set("id", id)
|
||||
TestApplicationHandler.GetApplication(c)
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "GetApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_GetApplication(t *testing.T) {
|
||||
|
@ -191,7 +195,8 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
gin.SetMode(gin.TestMode)
|
||||
|
||||
// Previously generated applications
|
||||
for _, app := range SuccessAplications {
|
||||
for _, user := range TestUsers {
|
||||
for _, app := range SuccessAplications[user.ID] {
|
||||
req := tests.Request{Name: fmt.Sprintf("Requesting application %s (%d)", app.Name, app.ID), Method: "GET", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200}
|
||||
|
||||
w, c, err := req.GetRequest()
|
||||
|
@ -199,7 +204,7 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
c.Set("id", app.ID)
|
||||
TestApplicationHandler.GetApplication(c)
|
||||
|
||||
|
@ -224,15 +229,17 @@ func TestApi_GetApplication(t *testing.T) {
|
|||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "GetApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_UpdateApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
for _, user := range TestUsers {
|
||||
testCases := make(map[uint]tests.Request)
|
||||
// Previously generated applications
|
||||
for _, app := range SuccessAplications {
|
||||
for _, app := range SuccessAplications[user.ID] {
|
||||
newName := app.Name + "-new_name"
|
||||
updateApp := model.UpdateApplication{
|
||||
Name: &newName,
|
||||
|
@ -255,21 +262,23 @@ func TestApi_UpdateApplication(t *testing.T) {
|
|||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
c.Set("id", id)
|
||||
TestApplicationHandler.UpdateApplication(c)
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "UpdateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_DeleteApplication(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
for _, user := range TestUsers {
|
||||
testCases := make(map[uint]tests.Request)
|
||||
// Previously generated applications
|
||||
for _, app := range SuccessAplications {
|
||||
for _, app := range SuccessAplications[user.ID] {
|
||||
testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Delete application %s (%d)", app.Name, app.ID), Method: "DELETE", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200}
|
||||
}
|
||||
// Arbitrary test cases
|
||||
|
@ -281,12 +290,13 @@ func TestApi_DeleteApplication(t *testing.T) {
|
|||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
c.Set("user", user)
|
||||
c.Set("id", id)
|
||||
TestApplicationHandler.DeleteApplication(c)
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "DeleteApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetApplicationHandler creates and returns an application handler
|
||||
|
@ -307,8 +317,16 @@ func getApplicationHandler(c *configuration.Matrix) (*ApplicationHandler, error)
|
|||
}
|
||||
|
||||
// True if all created applications are in list
|
||||
func validateAllApplications(apps []model.Application) bool {
|
||||
for _, successApp := range SuccessAplications {
|
||||
func validateAllApplications(user *model.User, apps []model.Application) bool {
|
||||
if _, ok := SuccessAplications[user.ID]; !ok {
|
||||
if len(apps) == 0 {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
for _, successApp := range SuccessAplications[user.ID] {
|
||||
foundApp := false
|
||||
for _, app := range apps {
|
||||
if app.ID == successApp.ID {
|
||||
|
@ -326,5 +344,5 @@ func validateAllApplications(apps []model.Application) bool {
|
|||
}
|
||||
|
||||
func cleanUp() {
|
||||
//os.Remove("pushbits-test.db")
|
||||
os.Remove("pushbits-test.db")
|
||||
}
|
||||
|
|
39
internal/api/context_test.go
Normal file
39
internal/api/context_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestApi_getID(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
testValue := uint(1337)
|
||||
|
||||
testCases := make(map[interface{}]tests.Request)
|
||||
testCases[-1] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
testCases[uint(1)] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
|
||||
testCases[uint(0)] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
|
||||
testCases[uint(500)] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200}
|
||||
testCases[500] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
testCases["test"] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
testCases[model.Application{}] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
testCases[&model.Application{}] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
testCases[&testValue] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 500}
|
||||
|
||||
for id, req := range testCases {
|
||||
w, c, err := req.GetRequest()
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("id", id)
|
||||
getID(c)
|
||||
|
||||
assert.Equalf(w.Code, req.ShouldStatus, "getApi id was set to %v (%T) and should result in status code %d but code is %d", id, id, req.ShouldStatus, w.Code)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue