mirror of
https://github.com/pushbits/server.git
synced 2025-05-12 08:26:58 +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
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pushbits/server/internal/configuration"
|
"github.com/pushbits/server/internal/configuration"
|
||||||
|
"github.com/pushbits/server/internal/model"
|
||||||
"github.com/pushbits/server/tests"
|
"github.com/pushbits/server/tests"
|
||||||
"github.com/pushbits/server/tests/mockups"
|
"github.com/pushbits/server/tests/mockups"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var TestApplicationHandler *ApplicationHandler
|
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) {
|
func TestMain(m *testing.M) {
|
||||||
// Get main config and adapt
|
// Get main config and adapt
|
||||||
|
@ -27,10 +32,9 @@ func TestMain(m *testing.M) {
|
||||||
|
|
||||||
config.Database.Connection = "pushbits-test.db"
|
config.Database.Connection = "pushbits-test.db"
|
||||||
config.Database.Dialect = "sqlite3"
|
config.Database.Dialect = "sqlite3"
|
||||||
TestConfig = config
|
|
||||||
|
|
||||||
// Set up test environment
|
// Set up test environment
|
||||||
appHandler, err := getApplicationHandler(&TestConfig.Matrix)
|
appHandler, err := getApplicationHandler(&config.Matrix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanUp()
|
cleanUp()
|
||||||
log.Println("Can not set up application handler: ", err)
|
log.Println("Can not set up application handler: ", err)
|
||||||
|
@ -39,8 +43,12 @@ func TestMain(m *testing.M) {
|
||||||
|
|
||||||
TestApplicationHandler = appHandler
|
TestApplicationHandler = appHandler
|
||||||
|
|
||||||
// Run
|
// Run for each user
|
||||||
|
for _, user := range mockups.GetUsers(config) {
|
||||||
|
SuccessAplications = []model.Application{}
|
||||||
|
TestUser = user
|
||||||
m.Run()
|
m.Run()
|
||||||
|
}
|
||||||
cleanUp()
|
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[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"}}
|
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 {
|
for statusCode, req := range testCases {
|
||||||
w, c, err := req.GetRequest()
|
w, c, err := req.GetRequest()
|
||||||
|
@ -75,12 +120,43 @@ func TestApi_RgisterApplication(t *testing.T) {
|
||||||
t.Fatalf(err.Error())
|
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 {
|
||||||
assert.Equalf(w.Code, statusCode, fmt.Sprintf("CreateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, statusCode, w.Code))
|
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.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
|
// GetApplicationHandler creates and returns an application handler
|
||||||
|
@ -100,6 +176,25 @@ func getApplicationHandler(c *configuration.Matrix) (*ApplicationHandler, error)
|
||||||
}, nil
|
}, 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() {
|
func cleanUp() {
|
||||||
os.Remove("pushbits-test.db")
|
os.Remove("pushbits-test.db")
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,3 +19,25 @@ func GetAdminUser(c *configuration.Configuration) *model.User {
|
||||||
MatrixID: c.Admin.MatrixID,
|
MatrixID: c.Admin.MatrixID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetUser returns an user
|
||||||
|
func GetUser(c *configuration.Configuration) *model.User {
|
||||||
|
credentialsManager := credentials.CreateManager(false, c.Crypto)
|
||||||
|
hash, _ := credentialsManager.CreatePasswordHash(c.Admin.Password)
|
||||||
|
|
||||||
|
return &model.User{
|
||||||
|
ID: 2,
|
||||||
|
Name: c.Admin.Name + "-normalo",
|
||||||
|
PasswordHash: hash,
|
||||||
|
IsAdmin: false,
|
||||||
|
MatrixID: c.Admin.MatrixID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUsers returns a list of users
|
||||||
|
func GetUsers(c *configuration.Configuration) []*model.User {
|
||||||
|
var users []*model.User
|
||||||
|
users = append(users, GetAdminUser(c))
|
||||||
|
users = append(users, GetUser(c))
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue