mirror of
https://github.com/pushbits/server.git
synced 2025-05-09 15:06:59 +02:00
enhance application testing
This commit is contained in:
parent
e87f775b1d
commit
ee212e11c3
2 changed files with 127 additions and 10 deletions
|
@ -1,20 +1,25 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pushbits/server/internal/configuration"
|
||||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/tests"
|
||||
"github.com/pushbits/server/tests/mockups"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
var TestApplicationHandler *ApplicationHandler
|
||||
var TestConfig *configuration.Configuration
|
||||
var TestUser *model.User
|
||||
|
||||
// Collect all created applications to check & delete them later
|
||||
var SuccessAplications []model.Application
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
// Get main config and adapt
|
||||
|
@ -27,10 +32,9 @@ func TestMain(m *testing.M) {
|
|||
|
||||
config.Database.Connection = "pushbits-test.db"
|
||||
config.Database.Dialect = "sqlite3"
|
||||
TestConfig = config
|
||||
|
||||
// Set up test environment
|
||||
appHandler, err := getApplicationHandler(&TestConfig.Matrix)
|
||||
appHandler, err := getApplicationHandler(&config.Matrix)
|
||||
if err != nil {
|
||||
cleanUp()
|
||||
log.Println("Can not set up application handler: ", err)
|
||||
|
@ -39,8 +43,12 @@ func TestMain(m *testing.M) {
|
|||
|
||||
TestApplicationHandler = appHandler
|
||||
|
||||
// Run
|
||||
m.Run()
|
||||
// Run for each user
|
||||
for _, user := range mockups.GetUsers(config) {
|
||||
SuccessAplications = []model.Application{}
|
||||
TestUser = user
|
||||
m.Run()
|
||||
}
|
||||
cleanUp()
|
||||
}
|
||||
|
||||
|
@ -67,7 +75,44 @@ func TestApi_RgisterApplication(t *testing.T) {
|
|||
testCases[400] = 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"}}
|
||||
testCases[200] = tests.Request{Name: "Valid JSON Data", Method: "POST", Endpoint: "/application", Data: `{"name": "test2", "strict_compatibility": true}`, Headers: map[string]string{"Content-Type": "application/json"}}
|
||||
|
||||
user := mockups.GetAdminUser(TestConfig)
|
||||
for statusCode, req := range testCases {
|
||||
var application model.Application
|
||||
w, c, err := req.GetRequest()
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", TestUser)
|
||||
TestApplicationHandler.CreateApplication(c)
|
||||
|
||||
// Parse body only for successful requests
|
||||
if statusCode >= 200 && statusCode < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = json.Unmarshal(body, &application)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
SuccessAplications = append(SuccessAplications, application)
|
||||
}
|
||||
|
||||
assert.Equalf(w.Code, statusCode, "CreateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, statusCode, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_GetApplications(t *testing.T) {
|
||||
var applications []model.Application
|
||||
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
testCases := make(map[int]tests.Request)
|
||||
testCases[200] = tests.Request{Name: "Valid Request", Method: "GET", Endpoint: "/application"}
|
||||
|
||||
for statusCode, req := range testCases {
|
||||
w, c, err := req.GetRequest()
|
||||
|
@ -75,14 +120,45 @@ func TestApi_RgisterApplication(t *testing.T) {
|
|||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
c.Set("user", user)
|
||||
c.Set("user", TestUser)
|
||||
TestApplicationHandler.GetApplications(c)
|
||||
|
||||
TestApplicationHandler.CreateApplication(c)
|
||||
// Parse body only for successful requests
|
||||
if statusCode >= 200 && statusCode < 300 {
|
||||
body, err := ioutil.ReadAll(w.Body)
|
||||
assert.NoErrorf(err, "Can not read request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
err = json.Unmarshal(body, &applications)
|
||||
assert.NoErrorf(err, "Can not unmarshal request body")
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
assert.Equalf(w.Code, statusCode, fmt.Sprintf("CreateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, statusCode, w.Code))
|
||||
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.Equalf(w.Code, statusCode, "GetApplications (Test case: \"%s\") should return status code %v but is %v.", req.Name, statusCode, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApi_GetApplicationsWithoutUser(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
testCase := tests.Request{Name: "Valid Request", Method: "GET", Endpoint: "/application"}
|
||||
|
||||
_, c, err := testCase.GetRequest()
|
||||
if err != nil {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
|
||||
assert.Panicsf(func() { TestApplicationHandler.GetApplications(c) }, "GetApplications did not panic altough user is not in context")
|
||||
|
||||
}
|
||||
|
||||
// GetApplicationHandler creates and returns an application handler
|
||||
func getApplicationHandler(c *configuration.Matrix) (*ApplicationHandler, error) {
|
||||
db, err := mockups.GetEmptyDatabase()
|
||||
|
@ -100,6 +176,25 @@ func getApplicationHandler(c *configuration.Matrix) (*ApplicationHandler, error)
|
|||
}, nil
|
||||
}
|
||||
|
||||
// True if all created applications are in list
|
||||
func validateAllApplications(apps []model.Application) bool {
|
||||
for _, successApp := range SuccessAplications {
|
||||
foundApp := false
|
||||
for _, app := range apps {
|
||||
if app.ID == successApp.ID {
|
||||
foundApp = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !foundApp {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func cleanUp() {
|
||||
os.Remove("pushbits-test.db")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue