From ea13efca023c9ffd8d07c24c5d8eb56bfc26dd56 Mon Sep 17 00:00:00 2001 From: Cubicroot Date: Thu, 22 Jul 2021 19:22:51 +0200 Subject: [PATCH] add unittest for create notification --- internal/api/application_test.go | 5 +++ internal/api/notification_test.go | 69 +++++++++++++++++++++++++++++++ tests/mockups/dispatcher.go | 8 ++++ tests/mockups/helper.go | 14 +++++++ tests/request.go | 1 + 5 files changed, 97 insertions(+) create mode 100644 internal/api/notification_test.go create mode 100644 tests/mockups/helper.go diff --git a/internal/api/application_test.go b/internal/api/application_test.go index ad2ba69..a808b0b 100644 --- a/internal/api/application_test.go +++ b/internal/api/application_test.go @@ -20,6 +20,7 @@ import ( var TestApplicationHandler *ApplicationHandler var TestUsers []*model.User var TestDatabase *database.Database +var TestNotificationHandler *NotificationHandler // Collect all created applications to check & delete them later var SuccessAplications map[uint][]model.Application @@ -56,6 +57,10 @@ func TestMain(m *testing.M) { TestUsers = mockups.GetUsers(config) SuccessAplications = make(map[uint][]model.Application) + TestNotificationHandler = &NotificationHandler{ + DB: TestDatabase, + DP: &mockups.MockDispatcher{}, + } // Run m.Run() cleanUp() diff --git a/internal/api/notification_test.go b/internal/api/notification_test.go new file mode 100644 index 0000000..d5f00ca --- /dev/null +++ b/internal/api/notification_test.go @@ -0,0 +1,69 @@ +package api + +import ( + "encoding/json" + "io/ioutil" + "testing" + + "github.com/gin-gonic/gin" + "github.com/pushbits/server/internal/model" + "github.com/pushbits/server/tests" + "github.com/stretchr/testify/assert" +) + +func TestApi_CreateNotification(t *testing.T) { + assert := assert.New(t) + gin.SetMode(gin.TestMode) + + testApplication := model.Application{ + ID: 1, + Token: "123456", + UserID: 1, + Name: "Test Application", + MatrixID: "@testuser:test.de", + } + + testCases := make([]tests.Request, 0) + testCases = append(testCases, tests.Request{Name: "Valid with message", Method: "POST", Endpoint: "/message?token=123456&message=testmessage", ShouldStatus: 200, ShouldReturn: model.Notification{Message: "testmessage", Title: "Test Application"}}) + testCases = append(testCases, tests.Request{Name: "Valid with message and title", Method: "POST", Endpoint: "/message?token=123456&message=testmessage&title=abcdefghijklmnop", ShouldStatus: 200, ShouldReturn: model.Notification{Message: "testmessage", Title: "abcdefghijklmnop"}}) + testCases = append(testCases, tests.Request{Name: "Valid with message, title and priority", Method: "POST", Endpoint: "/message?token=123456&message=testmessage&title=abcdefghijklmnop&priority=3", ShouldStatus: 200, ShouldReturn: model.Notification{Message: "testmessage", Title: "abcdefghijklmnop", Priority: 3}}) + testCases = append(testCases, tests.Request{Name: "Invalid with wrong field message2", Method: "POST", Endpoint: "/message?token=123456&message2=testmessage", ShouldStatus: 400}) + testCases = append(testCases, tests.Request{Name: "No form data", Method: "POST", Endpoint: "/message", ShouldStatus: 400}) + + for _, req := range testCases { + var notification model.Notification + w, c, err := req.GetRequest() + if err != nil { + t.Fatalf(err.Error()) + } + + c.Set("app", &testApplication) + TestNotificationHandler.CreateNotification(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, ¬ification) + assert.NoErrorf(err, "Can not unmarshal request body") + if err != nil { + continue + } + + shouldNotification, ok := req.ShouldReturn.(model.Notification) + assert.Truef(ok, "(Test case %s) Type mismatch can not test further", req.Name) + + assert.Greaterf(len(notification.ID), 1, "(Test case %s) Notification id is not set correctly with \"%s\"", req.Name, notification.ID) + + assert.Equalf(shouldNotification.Message, notification.Message, "(Test case %s) Notification message should be %s but is %s", req.Name, shouldNotification.Message, notification.Message) + assert.Equalf(shouldNotification.Title, notification.Title, "(Test case %s) Notification title should be %s but is %s", req.Name, shouldNotification.Title, notification.Title) + assert.Equalf(shouldNotification.Priority, notification.Priority, "(Test case %s) Notification priority should be %s but is %s", req.Name, shouldNotification.Priority, notification.Priority) + } + + assert.Equalf(w.Code, req.ShouldStatus, "(Test case: \"%s\") should return status code %v but is %v.", req.Name, req.ShouldStatus, w.Code) + } + +} diff --git a/tests/mockups/dispatcher.go b/tests/mockups/dispatcher.go index b0accab..af66dce 100644 --- a/tests/mockups/dispatcher.go +++ b/tests/mockups/dispatcher.go @@ -21,3 +21,11 @@ func (d *MockDispatcher) DeregisterApplication(a *model.Application, u *model.Us func (d *MockDispatcher) UpdateApplication(a *model.Application) error { return nil } + +func (d *MockDispatcher) SendNotification(a *model.Application, n *model.Notification) (id string, err error) { + return randStr(15), nil +} + +func (d *MockDispatcher) DeleteNotification(a *model.Application, n *model.DeleteNotification) error { + return nil +} diff --git a/tests/mockups/helper.go b/tests/mockups/helper.go new file mode 100644 index 0000000..4ed28c6 --- /dev/null +++ b/tests/mockups/helper.go @@ -0,0 +1,14 @@ +package mockups + +import ( + "crypto/rand" + "encoding/base64" +) + +func randStr(len int) string { + buff := make([]byte, len) + rand.Read(buff) + str := base64.StdEncoding.EncodeToString(buff) + // Base 64 can be longer than len + return str[:len] +} diff --git a/tests/request.go b/tests/request.go index bb786af..40c659e 100644 --- a/tests/request.go +++ b/tests/request.go @@ -17,6 +17,7 @@ type Request struct { Data interface{} Headers map[string]string ShouldStatus int + ShouldReturn interface{} } // GetRequest returns a ResponseRecorder and gin context according to the data set in the Request.