Firebase provider tests

This commit is contained in:
Eugene 2017-10-10 18:33:12 +03:00 committed by Evgeny Danienko
parent 7195fe3f92
commit c36a51d0cf
No known key found for this signature in database
GPG Key ID: BC8C34D8B45BECBF
3 changed files with 175 additions and 49 deletions

View File

@ -7,6 +7,7 @@ package common
import (
go_fcm "github.com/NaySoftware/go-fcm"
gomock "github.com/golang/mock/gomock"
message "github.com/status-im/status-go/geth/notification/message"
reflect "reflect"
)
@ -34,66 +35,110 @@ func (m *MockNotification) EXPECT() *MockNotificationMockRecorder {
}
// Notify mocks base method
func (m *MockNotification) Notify(token string) string {
ret := m.ctrl.Call(m, "Notify", token)
func (m *MockNotification) Notify(token string, msg *message.Message) (string, error) {
ret := m.ctrl.Call(m, "Notify", token, msg)
ret0, _ := ret[0].(string)
return ret0
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Notify indicates an expected call of Notify
func (mr *MockNotificationMockRecorder) Notify(token interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Notify", reflect.TypeOf((*MockNotification)(nil).Notify), token)
func (mr *MockNotificationMockRecorder) Notify(token, msg interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Notify", reflect.TypeOf((*MockNotification)(nil).Notify), token, msg)
}
// MockMessagingProvider is a mock of MessagingProvider interface
type MockMessagingProvider struct {
ctrl *gomock.Controller
recorder *MockMessagingProviderMockRecorder
}
// MockMessagingProviderMockRecorder is the mock recorder for MockMessagingProvider
type MockMessagingProviderMockRecorder struct {
mock *MockMessagingProvider
}
// NewMockMessagingProvider creates a new mock instance
func NewMockMessagingProvider(ctrl *gomock.Controller) *MockMessagingProvider {
mock := &MockMessagingProvider{ctrl: ctrl}
mock.recorder = &MockMessagingProviderMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockMessagingProvider) EXPECT() *MockMessagingProviderMockRecorder {
return m.recorder
}
// SetMessage mocks base method
func (m *MockMessagingProvider) SetMessage(ids []string, body interface{}) {
m.ctrl.Call(m, "SetMessage", ids, body)
}
// SetMessage indicates an expected call of SetMessage
func (mr *MockMessagingProviderMockRecorder) SetMessage(ids, body interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetMessage", reflect.TypeOf((*MockMessagingProvider)(nil).SetMessage), ids, body)
}
// SetPayload mocks base method
func (m *MockMessagingProvider) SetPayload(payload *message.Payload) {
m.ctrl.Call(m, "SetPayload", payload)
}
// SetPayload indicates an expected call of SetPayload
func (mr *MockMessagingProviderMockRecorder) SetPayload(payload interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPayload", reflect.TypeOf((*MockMessagingProvider)(nil).SetPayload), payload)
}
// Send mocks base method
func (m *MockNotification) Send() error {
func (m *MockMessagingProvider) Send() error {
ret := m.ctrl.Call(m, "Send")
ret0, _ := ret[0].(error)
return ret0
}
// Send indicates an expected call of Send
func (mr *MockNotificationMockRecorder) Send() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockNotification)(nil).Send))
func (mr *MockMessagingProviderMockRecorder) Send() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockMessagingProvider)(nil).Send))
}
// MockMessaging is a mock of Messaging interface
type MockMessaging struct {
// MockFirebaseClient is a mock of FirebaseClient interface
type MockFirebaseClient struct {
ctrl *gomock.Controller
recorder *MockMessagingMockRecorder
recorder *MockFirebaseClientMockRecorder
}
// MockMessagingMockRecorder is the mock recorder for MockMessaging
type MockMessagingMockRecorder struct {
mock *MockMessaging
// MockFirebaseClientMockRecorder is the mock recorder for MockFirebaseClient
type MockFirebaseClientMockRecorder struct {
mock *MockFirebaseClient
}
// NewMockMessaging creates a new mock instance
func NewMockMessaging(ctrl *gomock.Controller) *MockMessaging {
mock := &MockMessaging{ctrl: ctrl}
mock.recorder = &MockMessagingMockRecorder{mock}
// NewMockFirebaseClient creates a new mock instance
func NewMockFirebaseClient(ctrl *gomock.Controller) *MockFirebaseClient {
mock := &MockFirebaseClient{ctrl: ctrl}
mock.recorder = &MockFirebaseClientMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockMessaging) EXPECT() *MockMessagingMockRecorder {
func (m *MockFirebaseClient) EXPECT() *MockFirebaseClientMockRecorder {
return m.recorder
}
// NewFcmRegIdsMsg mocks base method
func (m *MockMessaging) NewFcmRegIdsMsg(list []string, body interface{}) *go_fcm.FcmClient {
func (m *MockFirebaseClient) NewFcmRegIdsMsg(list []string, body interface{}) *go_fcm.FcmClient {
ret := m.ctrl.Call(m, "NewFcmRegIdsMsg", list, body)
ret0, _ := ret[0].(*go_fcm.FcmClient)
return ret0
}
// NewFcmRegIdsMsg indicates an expected call of NewFcmRegIdsMsg
func (mr *MockMessagingMockRecorder) NewFcmRegIdsMsg(list, body interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewFcmRegIdsMsg", reflect.TypeOf((*MockMessaging)(nil).NewFcmRegIdsMsg), list, body)
func (mr *MockFirebaseClientMockRecorder) NewFcmRegIdsMsg(list, body interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewFcmRegIdsMsg", reflect.TypeOf((*MockFirebaseClient)(nil).NewFcmRegIdsMsg), list, body)
}
// Send mocks base method
func (m *MockMessaging) Send() (*go_fcm.FcmResponseStatus, error) {
func (m *MockFirebaseClient) Send() (*go_fcm.FcmResponseStatus, error) {
ret := m.ctrl.Call(m, "Send")
ret0, _ := ret[0].(*go_fcm.FcmResponseStatus)
ret1, _ := ret[1].(error)
@ -101,18 +146,18 @@ func (m *MockMessaging) Send() (*go_fcm.FcmResponseStatus, error) {
}
// Send indicates an expected call of Send
func (mr *MockMessagingMockRecorder) Send() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockMessaging)(nil).Send))
func (mr *MockFirebaseClientMockRecorder) Send() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Send", reflect.TypeOf((*MockFirebaseClient)(nil).Send))
}
// SetNotificationPayload mocks base method
func (m *MockMessaging) SetNotificationPayload(payload *go_fcm.NotificationPayload) *go_fcm.FcmClient {
func (m *MockFirebaseClient) SetNotificationPayload(payload *go_fcm.NotificationPayload) *go_fcm.FcmClient {
ret := m.ctrl.Call(m, "SetNotificationPayload", payload)
ret0, _ := ret[0].(*go_fcm.FcmClient)
return ret0
}
// SetNotificationPayload indicates an expected call of SetNotificationPayload
func (mr *MockMessagingMockRecorder) SetNotificationPayload(payload interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNotificationPayload", reflect.TypeOf((*MockMessaging)(nil).SetNotificationPayload), payload)
func (mr *MockFirebaseClientMockRecorder) SetNotificationPayload(payload interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetNotificationPayload", reflect.TypeOf((*MockFirebaseClient)(nil).SetNotificationPayload), payload)
}

View File

@ -1,22 +1,81 @@
package notification
import (
"errors"
"testing"
"github.com/NaySoftware/go-fcm"
"github.com/golang/mock/gomock"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/notification/message"
t "github.com/status-im/status-go/geth/testing"
"github.com/stretchr/testify/suite"
)
func TestFCMClientTestSuite(t *testing.T) {
suite.Run(t, new(FCMClientTestSuite))
suite.Run(t, new(FCMProviderTestSuite))
}
type FCMClientTestSuite struct {
type FCMProviderTestSuite struct {
t.BaseTestSuite
fcmClientMock *common.MockFirebaseClient
fcmClientMockCtrl *gomock.Controller
}
func (s *FCMClientTestSuite) TestNewFCMClient() {
fcmClient := NewFCMClient()
func (s *FCMProviderTestSuite) SetupTest() {
s.fcmClientMockCtrl = gomock.NewController(s.T())
s.fcmClientMock = common.NewMockFirebaseClient(s.fcmClientMockCtrl)
}
func (s *FCMProviderTestSuite) TearDownTest() {
s.fcmClientMockCtrl.Finish()
}
func (s *FCMProviderTestSuite) TestNewFCMClient() {
fcmClient := NewFCMProvider(s.fcmClientMock)
s.Require().NotNil(fcmClient)
}
}
func (s *FCMProviderTestSuite) TestSetMessage() {
ids := []string{"1"}
body := interface{}("body")
s.fcmClientMock.EXPECT().NewFcmRegIdsMsg(ids, body).Times(1)
fcmClient := NewFCMProvider(s.fcmClientMock)
fcmClient.SetMessage(ids, body)
}
func (s *FCMProviderTestSuite) TestSetPayload() {
title := "title"
body := "body"
payload := &message.Payload{Title: title, Body: body}
fcmPayload := &fcm.NotificationPayload{Title: title, Body: body}
s.fcmClientMock.EXPECT().SetNotificationPayload(fcmPayload).Times(1)
fcmClient := NewFCMProvider(s.fcmClientMock)
fcmClient.SetPayload(payload)
}
func (s *FCMProviderTestSuite) TestSendSuccess() {
s.fcmClientMock.EXPECT().Send().Return(nil, nil).Times(1)
fcmClient := NewFCMProvider(s.fcmClientMock)
err := fcmClient.Send()
s.Require().NoError(err)
}
func (s *FCMProviderTestSuite) TestSendError() {
expectedError := errors.New("error")
s.fcmClientMock.EXPECT().Send().Return(nil, expectedError).Times(1)
fcmClient := NewFCMProvider(s.fcmClientMock)
err := fcmClient.Send()
s.Require().Equal(expectedError, err)
}

View File

@ -3,8 +3,10 @@ package notification
import (
"testing"
"errors"
"github.com/golang/mock/gomock"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/notification/message"
t "github.com/status-im/status-go/geth/testing"
"github.com/stretchr/testify/suite"
)
@ -15,13 +17,13 @@ func TestNotificationTestSuite(t *testing.T) {
type NotificationTestSuite struct {
t.BaseTestSuite
messagingMock *common.MockMessaging
messagingMock *common.MockMessagingProvider
messagingMockCtrl *gomock.Controller
}
func (s *NotificationTestSuite) SetupTest() {
s.messagingMockCtrl = gomock.NewController(s.T())
s.messagingMock = common.NewMockMessaging(s.messagingMockCtrl)
s.messagingMock = common.NewMockMessagingProvider(s.messagingMockCtrl)
}
func (s *NotificationTestSuite) TearDownTest() {
@ -33,24 +35,44 @@ func (s *NotificationTestSuite) TestNewNotification() {
s.Require().NotNil(manager)
}
func (s *NotificationTestSuite) TestNotify() {
func (s *NotificationTestSuite) TestNotifySuccess() {
token := "test"
s.messagingMock.EXPECT().NewFcmRegIdsMsg([]string{token}, map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
})
msg := getMessage()
s.messagingMock.EXPECT().SetMessage([]string{token}, msg.Body).Times(1)
s.messagingMock.EXPECT().SetPayload(msg.Payload).Times(1)
s.messagingMock.EXPECT().Send().Return(nil).Times(1)
manager := New(s.messagingMock)
res := manager.Notify(token)
res, err := manager.Notify(token, msg)
s.Require().Equal(token, res)
}
func (s *NotificationTestSuite) TestSend() {
s.messagingMock.EXPECT().Send().Times(1).Return(nil)
manager := New(s.messagingMock)
err := manager.Send()
s.Require().NoError(err)
}
func (s *NotificationTestSuite) TestNotifyError() {
token := "test"
msg := getMessage()
expectedError := errors.New("error")
s.messagingMock.EXPECT().SetMessage([]string{token}, msg.Body).Times(1)
s.messagingMock.EXPECT().SetPayload(msg.Payload).Times(1)
s.messagingMock.EXPECT().Send().Return(expectedError).Times(1)
manager := New(s.messagingMock)
_, err := manager.Notify(token, msg)
s.Require().Equal(expectedError, err)
}
func getMessage() *message.Message {
return &message.Message{
Body: map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
},
Payload: &message.Payload{
Title: "test notification",
},
}
}