status-go/geth/notification/fcm/notifier_test.go

66 lines
1.6 KiB
Go
Raw Normal View History

2017-10-11 13:51:43 +00:00
package fcm
import (
2017-10-10 15:33:12 +00:00
"errors"
"testing"
2017-10-10 15:33:12 +00:00
"github.com/NaySoftware/go-fcm"
"github.com/golang/mock/gomock"
"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-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
2017-10-11 13:59:58 +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())
2017-10-11 13:59:58 +00:00
s.fcmClientMock = NewMockfirebaseClient(s.fcmClientMockCtrl)
}
2017-10-11 13:52:11 +00:00
func (s *NotifierTestSuite) TearDownTest() {
2017-10-10 15:33:12 +00:00
s.fcmClientMockCtrl.Finish()
}
2017-10-11 13:52:11 +00:00
func (s *NotifierTestSuite) TestNotifySuccess() {
2017-10-11 13:51:43 +00:00
fcmPayload := getPayload()
2017-10-10 15:33:12 +00:00
ids := []string{"1"}
body := interface{}("body")
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
2017-10-11 13:51:43 +00:00
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
2017-10-10 15:33:12 +00:00
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
2017-10-11 13:51:43 +00:00
fcmClient := Notifier{s.fcmClientMock}
2017-10-10 15:33:12 +00:00
2017-10-18 20:17:40 +00:00
err := fcmClient.Send(body, 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
}
2017-10-11 13:52:11 +00:00
func (s *NotifierTestSuite) TestNotifyError() {
2017-10-10 15:33:12 +00:00
expectedError := errors.New("error")
2017-10-11 13:51:43 +00:00
fcmPayload := getPayload()
ids := []string{"1"}
body := interface{}("body")
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
2017-10-10 15:33:12 +00:00
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
2017-10-11 13:51:43 +00:00
fcmClient := Notifier{s.fcmClientMock}
2017-10-10 15:33:12 +00:00
2017-10-18 20:17:40 +00:00
err := fcmClient.Send(body, 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
func getPayload() *fcm.NotificationPayload {
return &fcm.NotificationPayload{Title: "Status - new message", Body: "ping"}
}