mirror of
https://github.com/pushbits/server.git
synced 2025-05-10 15:37:00 +02:00
add unittest for create notification
This commit is contained in:
parent
c1631dd53e
commit
ea13efca02
5 changed files with 97 additions and 0 deletions
|
@ -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()
|
||||
|
|
69
internal/api/notification_test.go
Normal file
69
internal/api/notification_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
14
tests/mockups/helper.go
Normal file
14
tests/mockups/helper.go
Normal file
|
@ -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]
|
||||
}
|
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue