From 46dd19b07d5bad316fdf5971a935a4118a8fc08e Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Thu, 17 Jun 2021 17:33:07 +0200 Subject: [PATCH] getApplication testing --- internal/api/context_test.go | 50 +++++++++++++++++++++++++++++++++++- tests/mockups/application.go | 32 +++++++++++++++++++++++ tests/mockups/database.go | 13 ++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/mockups/application.go diff --git a/internal/api/context_test.go b/internal/api/context_test.go index 4432c9d..7f9975f 100644 --- a/internal/api/context_test.go +++ b/internal/api/context_test.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" "github.com/pushbits/server/internal/model" "github.com/pushbits/server/tests" + "github.com/pushbits/server/tests/mockups" "github.com/stretchr/testify/assert" ) @@ -32,8 +33,55 @@ func TestApi_getID(t *testing.T) { } c.Set("id", id) - getID(c) + idReturned, err := getID(c) + + if req.ShouldStatus >= 200 && req.ShouldStatus < 300 { + idUint, ok := id.(uint) + if ok { + assert.Equalf(idReturned, idUint, "getApi id was set to %d but result is %d", idUint, idReturned) + } + assert.NoErrorf(err, "getId with id %v (%t) returned an error altough it should not: %v", id, id, err) + } else { + assert.Errorf(err, "getId with id %v (%t) returned no error altough it should", id, id) + } 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) } } + +func TestApi_getApplication(t *testing.T) { + assert := assert.New(t) + gin.SetMode(gin.TestMode) + + // Generate a mock database + db, err := mockups.GetEmptyDatabase() + assert.NoErrorf(err, "Could not get database: ", err) + + applications := mockups.GetAllApplications() + mockups.AddApplicationsToDb(db, applications) + + testCases := make(map[uint]tests.Request) + testCases[500] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 404} + testCases[1] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200} + testCases[2] = tests.Request{Name: "-", Method: "GET", Endpoint: "/", Data: "", ShouldStatus: 200} + + for id, req := range testCases { + w, c, err := req.GetRequest() + if err != nil { + t.Fatalf(err.Error()) + } + + c.Set("id", id) + app, err := getApplication(c, db) + + if req.ShouldStatus >= 200 && req.ShouldStatus < 300 { + assert.Equalf(app.ID, id, "getApplication id was set to %d but resulting app id is %d", id, app.ID) + assert.NoErrorf(err, "getApplication with id %v (%t) returned an error altough it should not: %v", id, id, err) + } else { + assert.Errorf(err, "getApplication with id %v (%t) returned no error altough it should", id, id) + } + + assert.Equalf(w.Code, req.ShouldStatus, "getApplication id was set to %v (%T) and should result in status code %d but code is %d", id, id, req.ShouldStatus, w.Code) + + } +} diff --git a/tests/mockups/application.go b/tests/mockups/application.go new file mode 100644 index 0000000..754b37e --- /dev/null +++ b/tests/mockups/application.go @@ -0,0 +1,32 @@ +package mockups + +import "github.com/pushbits/server/internal/model" + +// GetApplication1 returns an application with id 1 +func GetApplication1() *model.Application { + return &model.Application{ + ID: 1, + Token: "1234567890abcdefghijklmn", + UserID: 1, + Name: "App1", + } +} + +// GetApplication2 returns an application with id 2 +func GetApplication2() *model.Application { + return &model.Application{ + ID: 2, + Token: "0987654321xyzabcdefghij", + UserID: 1, + Name: "App2", + } +} + +// GetAllApplications returns all mock-applications as a list +func GetAllApplications() []*model.Application { + applications := make([]*model.Application, 0) + applications = append(applications, GetApplication1()) + applications = append(applications, GetApplication2()) + + return applications +} diff --git a/tests/mockups/database.go b/tests/mockups/database.go index f2b76fa..6914d3c 100644 --- a/tests/mockups/database.go +++ b/tests/mockups/database.go @@ -4,6 +4,7 @@ import ( "github.com/pushbits/server/internal/authentication/credentials" "github.com/pushbits/server/internal/configuration" "github.com/pushbits/server/internal/database" + "github.com/pushbits/server/internal/model" ) // GetEmptyDatabase returns an empty sqlite database object @@ -11,3 +12,15 @@ func GetEmptyDatabase() (*database.Database, error) { cm := credentials.CreateManager(false, configuration.CryptoConfig{}) return database.Create(cm, "sqlite3", "pushbits-test.db") } + +// AddApplicationsToDb inserts the applications apps into the database db +func AddApplicationsToDb(db *database.Database, apps []*model.Application) error { + for _, app := range apps { + err := db.CreateApplication(app) + if err != nil { + return err + } + } + + return nil +}