From ef5409eb35bd82075c8a39df27b4a5aece9229b5 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Sun, 13 Jun 2021 18:09:57 +0200 Subject: [PATCH] add Tests for getID --- internal/api/application_test.go | 290 ++++++++++++++++--------------- internal/api/context_test.go | 39 +++++ 2 files changed, 193 insertions(+), 136 deletions(-) create mode 100644 internal/api/context_test.go diff --git a/internal/api/application_test.go b/internal/api/application_test.go index 19d429d..01f0d6a 100644 --- a/internal/api/application_test.go +++ b/internal/api/application_test.go @@ -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() - } + 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,33 +73,36 @@ 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 _, 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 req.ShouldStatus >= 200 && req.ShouldStatus < 300 { - body, err := ioutil.ReadAll(w.Body) - assert.NoErrorf(err, "Can not read request body") + for _, user := range TestUsers { + SuccessAplications[user.ID] = make([]model.Application, 0) + for _, req := range testCases { + var application model.Application + w, c, err := req.GetRequest() if err != nil { - continue - } - err = json.Unmarshal(body, &application) - assert.NoErrorf(err, "Can not unmarshal request body") - if err != nil { - continue + t.Fatalf(err.Error()) } - SuccessAplications = append(SuccessAplications, application) - } + c.Set("user", user) + TestApplicationHandler.CreateApplication(c) - assert.Equalf(w.Code, req.ShouldStatus, "CreateApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) + // Parse body only for successful requests + if req.ShouldStatus >= 200 && req.ShouldStatus < 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[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) + } } } @@ -115,33 +115,35 @@ 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 _, req := range testCases { - w, c, err := req.GetRequest() - if err != nil { - t.Fatalf(err.Error()) - } - - c.Set("user", TestUser) - TestApplicationHandler.GetApplications(c) - - // Parse body only for successful requests - if req.ShouldStatus >= 200 && req.ShouldStatus < 300 { - body, err := ioutil.ReadAll(w.Body) - assert.NoErrorf(err, "Can not read request body") + for _, user := range TestUsers { + for _, req := range testCases { + w, c, err := req.GetRequest() if err != nil { - continue - } - err = json.Unmarshal(body, &applications) - assert.NoErrorf(err, "Can not unmarshal request body") - if err != nil { - continue + t.Fatalf(err.Error()) } - 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)) - } + c.Set("user", user) + TestApplicationHandler.GetApplications(c) - assert.Equalf(w.Code, req.ShouldStatus, "GetApplications (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) + // Parse body only for successful requests + if req.ShouldStatus >= 200 && req.ShouldStatus < 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.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) + } } } @@ -170,17 +172,19 @@ 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 id, req := range testCases { - w, c, err := req.GetRequest() - if err != nil { - t.Fatalf(err.Error()) + for _, user := range TestUsers { + for id, req := range testCases { + w, c, err := req.GetRequest() + if err != nil { + t.Fatalf(err.Error()) + } + + 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) } - - c.Set("user", TestUser) - 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) } } @@ -191,38 +195,40 @@ func TestApi_GetApplication(t *testing.T) { gin.SetMode(gin.TestMode) // Previously generated applications - for _, app := range SuccessAplications { - 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} + 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() - if err != nil { - t.Fatalf(err.Error()) - } - - c.Set("user", TestUser) - c.Set("id", app.ID) - TestApplicationHandler.GetApplication(c) - - // Parse body only for successful requests - if req.ShouldStatus >= 200 && req.ShouldStatus < 300 { - body, err := ioutil.ReadAll(w.Body) - assert.NoErrorf(err, "Can not read request body") + w, c, err := req.GetRequest() if err != nil { - continue - } - err = json.Unmarshal(body, &application) - assert.NoErrorf(err, "Can not unmarshal request body: %v", err) - if err != nil { - continue + t.Fatalf(err.Error()) } - assert.Equalf(application.ID, app.ID, "Application ID should be %d but is %d", app.ID, application.ID) - assert.Equalf(application.Name, app.Name, "Application Name should be %s but is %s", app.Name, application.Name) - assert.Equalf(application.UserID, app.UserID, "Application user ID should be %d but is %d", app.UserID, application.UserID) + c.Set("user", user) + c.Set("id", app.ID) + TestApplicationHandler.GetApplication(c) + // Parse body only for successful requests + if req.ShouldStatus >= 200 && req.ShouldStatus < 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: %v", err) + if err != nil { + continue + } + + assert.Equalf(application.ID, app.ID, "Application ID should be %d but is %d", app.ID, application.ID) + assert.Equalf(application.Name, app.Name, "Application Name should be %s but is %s", app.Name, application.Name) + assert.Equalf(application.UserID, app.UserID, "Application user ID should be %d but is %d", app.UserID, application.UserID) + + } + + assert.Equalf(w.Code, req.ShouldStatus, "GetApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) } - - assert.Equalf(w.Code, req.ShouldStatus, "GetApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) } } @@ -230,36 +236,38 @@ func TestApi_UpdateApplication(t *testing.T) { assert := assert.New(t) gin.SetMode(gin.TestMode) - testCases := make(map[uint]tests.Request) - // Previously generated applications - for _, app := range SuccessAplications { - newName := app.Name + "-new_name" - updateApp := model.UpdateApplication{ - Name: &newName, + for _, user := range TestUsers { + testCases := make(map[uint]tests.Request) + // Previously generated applications + for _, app := range SuccessAplications[user.ID] { + newName := app.Name + "-new_name" + updateApp := model.UpdateApplication{ + Name: &newName, + } + updateAppBytes, err := json.Marshal(updateApp) + assert.NoErrorf(err, "Error on marshaling updateApplication struct") + + // Valid + testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Update application (valid) %s (%d)", app.Name, app.ID), Method: "PUT", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200, Data: string(updateAppBytes), Headers: map[string]string{"Content-Type": "application/json"}} + // Invalid + testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Update application (invalid) %s (%d)", app.Name, app.ID), Method: "PUT", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200, Data: "{}", Headers: map[string]string{"Content-Type": "application/json"}} } - updateAppBytes, err := json.Marshal(updateApp) - assert.NoErrorf(err, "Error on marshaling updateApplication struct") + // Arbitrary test cases + testCases[5555] = tests.Request{Name: "Update application 5555", Method: "PUT", Endpoint: "/application/5555", ShouldStatus: 404, Data: "random data"} + testCases[5556] = tests.Request{Name: "Update application 5556", Method: "PUT", Endpoint: "/application/5556", ShouldStatus: 404, Data: `{"new_name": "new name"}`, Headers: map[string]string{"Content-Type": "application/json"}} - // Valid - testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Update application (valid) %s (%d)", app.Name, app.ID), Method: "PUT", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200, Data: string(updateAppBytes), Headers: map[string]string{"Content-Type": "application/json"}} - // Invalid - testCases[app.ID] = tests.Request{Name: fmt.Sprintf("Update application (invalid) %s (%d)", app.Name, app.ID), Method: "PUT", Endpoint: fmt.Sprintf("/application/%d", app.ID), ShouldStatus: 200, Data: "{}", Headers: map[string]string{"Content-Type": "application/json"}} - } - // Arbitrary test cases - testCases[5555] = tests.Request{Name: "Update application 5555", Method: "PUT", Endpoint: "/application/5555", ShouldStatus: 404, Data: "random data"} - testCases[5556] = tests.Request{Name: "Update application 5556", Method: "PUT", Endpoint: "/application/5556", ShouldStatus: 404, Data: `{"new_name": "new name"}`, Headers: map[string]string{"Content-Type": "application/json"}} + for id, req := range testCases { + w, c, err := req.GetRequest() + if err != nil { + t.Fatalf(err.Error()) + } - for id, req := range testCases { - w, c, err := req.GetRequest() - if err != nil { - t.Fatalf(err.Error()) + 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) } - - c.Set("user", TestUser) - 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) } } @@ -267,25 +275,27 @@ func TestApi_DeleteApplication(t *testing.T) { assert := assert.New(t) gin.SetMode(gin.TestMode) - testCases := make(map[uint]tests.Request) - // Previously generated applications - for _, app := range SuccessAplications { - 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 - testCases[5555] = tests.Request{Name: "Delete application 5555", Method: "DELETE", Endpoint: "/application/5555", ShouldStatus: 404} - - for id, req := range testCases { - w, c, err := req.GetRequest() - if err != nil { - t.Fatalf(err.Error()) + for _, user := range TestUsers { + testCases := make(map[uint]tests.Request) + // Previously generated applications + 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 + testCases[5555] = tests.Request{Name: "Delete application 5555", Method: "DELETE", Endpoint: "/application/5555", ShouldStatus: 404} - c.Set("user", TestUser) - c.Set("id", id) - TestApplicationHandler.DeleteApplication(c) + for id, req := range testCases { + w, c, err := req.GetRequest() + if err != nil { + t.Fatalf(err.Error()) + } - assert.Equalf(w.Code, req.ShouldStatus, "DeleteApplication (Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) + 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) + } } } @@ -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") } diff --git a/internal/api/context_test.go b/internal/api/context_test.go new file mode 100644 index 0000000..4432c9d --- /dev/null +++ b/internal/api/context_test.go @@ -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) + } +}