2017-10-11 13:51:43 +00:00
|
|
|
package fcm
|
2017-10-09 16:23:09 +00:00
|
|
|
|
|
|
|
import (
|
2019-01-18 13:03:32 +00:00
|
|
|
"encoding/json"
|
2017-10-10 15:33:12 +00:00
|
|
|
"errors"
|
2017-10-09 16:23:09 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-10-10 15:33:12 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2017-10-09 16:23:09 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFCMClientTestSuite(t *testing.T) {
|
2017-10-11 13:52:11 +00:00
|
|
|
suite.Run(t, new(NotifierTestSuite))
|
2017-10-09 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 13:52:11 +00:00
|
|
|
type NotifierTestSuite struct {
|
2017-10-12 14:31:14 +00:00
|
|
|
suite.Suite
|
2017-10-10 15:33:12 +00:00
|
|
|
|
2018-04-05 13:14:47 +00:00
|
|
|
fcmClientMock *MockFirebaseClient
|
2017-10-10 15:33:12 +00:00
|
|
|
fcmClientMockCtrl *gomock.Controller
|
|
|
|
}
|
|
|
|
|
2017-10-11 13:52:11 +00:00
|
|
|
func (s *NotifierTestSuite) SetupTest() {
|
2017-10-10 15:33:12 +00:00
|
|
|
s.fcmClientMockCtrl = gomock.NewController(s.T())
|
2018-04-05 13:14:47 +00:00
|
|
|
s.fcmClientMock = NewMockFirebaseClient(s.fcmClientMockCtrl)
|
2017-10-09 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 13:52:11 +00:00
|
|
|
func (s *NotifierTestSuite) TearDownTest() {
|
2017-10-10 15:33:12 +00:00
|
|
|
s.fcmClientMockCtrl.Finish()
|
|
|
|
}
|
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
func (s *NotifierTestSuite) TestSendSuccess() {
|
2017-10-10 15:33:12 +00:00
|
|
|
ids := []string{"1"}
|
2019-01-18 13:03:32 +00:00
|
|
|
dataPayload := make(map[string]string)
|
|
|
|
dataPayload["from"] = "a"
|
|
|
|
dataPayload["to"] = "b"
|
|
|
|
dataPayloadByteArray, err := json.Marshal(dataPayload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
dataPayloadJSON := string(dataPayloadByteArray)
|
|
|
|
|
|
|
|
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
|
2017-10-10 15:33:12 +00:00
|
|
|
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
|
2017-10-18 20:56:39 +00:00
|
|
|
fcmClient := Notification{s.fcmClientMock}
|
2017-10-10 15:33:12 +00:00
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
err = fcmClient.Send(dataPayloadJSON, ids...)
|
2017-10-10 15:33:12 +00:00
|
|
|
|
2017-10-11 13:52:11 +00:00
|
|
|
s.NoError(err)
|
2017-10-10 15:33:12 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
func (s *NotifierTestSuite) TestSendError() {
|
2017-10-10 15:33:12 +00:00
|
|
|
expectedError := errors.New("error")
|
2019-01-18 13:03:32 +00:00
|
|
|
ids := []string{"2"}
|
|
|
|
dataPayload := make(map[string]string)
|
|
|
|
dataPayload["from"] = "c"
|
|
|
|
dataPayload["to"] = "d"
|
|
|
|
dataPayloadByteArray, err := json.Marshal(dataPayload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
dataPayloadJSON := string(dataPayloadByteArray)
|
|
|
|
|
|
|
|
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, dataPayload).Times(1)
|
2017-10-10 15:33:12 +00:00
|
|
|
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
|
2017-10-18 20:56:39 +00:00
|
|
|
fcmClient := Notification{s.fcmClientMock}
|
2017-10-10 15:33:12 +00:00
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
err = fcmClient.Send(dataPayloadJSON, ids...)
|
2017-10-10 15:33:12 +00:00
|
|
|
|
2017-10-11 13:52:11 +00:00
|
|
|
s.Equal(expectedError, err)
|
2017-10-10 15:33:12 +00:00
|
|
|
}
|
2017-10-11 13:51:43 +00:00
|
|
|
|
2019-01-18 13:03:32 +00:00
|
|
|
func (s *NotifierTestSuite) TestSendWithInvalidJSON() {
|
|
|
|
ids := []string{"3"}
|
|
|
|
dataPayloadJSON := "{a=b}"
|
|
|
|
|
|
|
|
fcmClient := Notification{s.fcmClientMock}
|
|
|
|
|
|
|
|
err := fcmClient.Send(dataPayloadJSON, ids...)
|
|
|
|
s.Require().Error(err)
|
|
|
|
|
|
|
|
_, ok := err.(*json.SyntaxError)
|
|
|
|
s.True(ok)
|
2017-10-11 13:51:43 +00:00
|
|
|
}
|