From ee212e11c36c0b1f195535da3e22a815e05b796e Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Fri, 11 Jun 2021 19:03:04 +0200 Subject: [PATCH] enhance application testing --- internal/api/application_test.go | 115 ++++++++++++++++++++++++++++--- tests/mockups/user.go | 22 ++++++ 2 files changed, 127 insertions(+), 10 deletions(-) diff --git a/internal/api/application_test.go b/internal/api/application_test.go index f5efcb9..2dcb368 100644 --- a/internal/api/application_test.go +++ b/internal/api/application_test.go @@ -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") } diff --git a/tests/mockups/user.go b/tests/mockups/user.go index a541cbd..2e75639 100644 --- a/tests/mockups/user.go +++ b/tests/mockups/user.go @@ -19,3 +19,25 @@ func GetAdminUser(c *configuration.Configuration) *model.User { 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 +}