forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcm_test.go
224 lines (194 loc) · 5.7 KB
/
fcm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package fcm
import (
"context"
"errors"
"testing"
"github.com/appleboy/go-fcm"
"github.com/stretchr/testify/require"
)
func TestFCM_New(t *testing.T) {
t.Parallel()
assert := require.New(t)
service, err := New("server-api-key")
assert.NotNil(service)
assert.Nil(err)
}
func TestFCM_AddReceivers(t *testing.T) {
t.Parallel()
assert := require.New(t)
svc := &Service{
deviceTokens: []string{},
}
deviceTokens := []string{"Token1", "Token2", "Token3"}
svc.AddReceivers(deviceTokens...)
assert.Equal(svc.deviceTokens, deviceTokens)
}
func TestFCM_Send(t *testing.T) {
t.Parallel()
assert := require.New(t)
svc := &Service{
deviceTokens: []string{},
}
mockToken := "deviceToken"
mockData := map[string]interface{}{
"foo": "bar",
}
// test fcm client send
mockClient := newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 0).Return(&fcm.Response{Success: 1}, nil)
svc.client = mockClient
svc.AddReceivers(mockToken)
ctx := context.Background()
err := svc.Send(ctx, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
// test fcm client send with data
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 0).Return(&fcm.Response{Success: 1}, nil)
svc.client = mockClient
svc.AddReceivers(mockToken)
ctx = context.Background()
ctxWithData := context.WithValue(ctx, DataKey, mockData)
err = svc.Send(ctxWithData, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
// test fcm client send with data and retries
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 3).Return(&fcm.Response{Success: 1}, nil)
svc.client = mockClient
svc.AddReceivers(mockToken)
ctx = context.Background()
ctxWithData = context.WithValue(ctx, DataKey, mockData)
ctxWithDataAndRetries := context.WithValue(ctxWithData, RetriesKey, 3)
err = svc.Send(ctxWithDataAndRetries, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
// test fcm client returning error
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 3).Return(nil, errors.New("some error"))
svc.client = mockClient
svc.AddReceivers(mockToken)
ctx = context.Background()
ctxWithData = context.WithValue(ctx, DataKey, mockData)
ctxWithDataAndRetries = context.WithValue(ctxWithData, RetriesKey, 3)
err = svc.Send(ctxWithDataAndRetries, "subject", "message")
assert.NotNil(err)
mockClient.AssertExpectations(t)
// test fcm client multiple receivers
anotherMockToken := "another_device_token"
mockClient = newMockFcmClient(t)
mockClient.On("SendWithRetry", &fcm.Message{
To: mockToken,
Data: mockData,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 3).Return(&fcm.Response{Success: 1}, nil)
mockClient.On("SendWithRetry", &fcm.Message{
To: anotherMockToken,
Data: mockData,
Notification: &fcm.Notification{
Title: "subject",
Body: "message",
},
}, 3).Return(&fcm.Response{Success: 1}, nil)
svc.client = mockClient
svc.AddReceivers(mockToken, anotherMockToken)
ctx = context.Background()
ctxWithData = context.WithValue(ctx, DataKey, mockData)
ctxWithDataAndRetries = context.WithValue(ctxWithData, RetriesKey, 3)
err = svc.Send(ctxWithDataAndRetries, "subject", "message")
assert.Nil(err)
mockClient.AssertExpectations(t)
}
func TestFCM_GetMessageData(t *testing.T) {
t.Parallel()
assert := require.New(t)
mockData := map[string]interface{}{
"foo": "bar",
}
// test without data
ctx := context.Background()
data, ok := getMessageData(ctx)
assert.Nil(data)
assert.False(ok)
// test with invalid type of data
ctx = context.Background()
ctxWithInvalidTypeOfData := context.WithValue(ctx, DataKey, "invalid_type_of_data")
data, ok = getMessageData(ctxWithInvalidTypeOfData)
assert.Nil(data)
assert.False(ok)
// test with invalid data key
ctx = context.Background()
invalidDataKey := struct {
Key string
}{
Key: "invalid_data_key",
}
ctxWithInvalidDataKey := context.WithValue(ctx, invalidDataKey, mockData)
data, ok = getMessageData(ctxWithInvalidDataKey)
assert.Nil(data)
assert.False(ok)
// test with data
ctx = context.Background()
ctxWithData := context.WithValue(ctx, DataKey, mockData)
data, ok = getMessageData(ctxWithData)
assert.Equal(mockData, data)
assert.True(ok)
}
func TestFCM_GetMessageRetryAttempts(t *testing.T) {
t.Parallel()
assert := require.New(t)
// test without retries
ctx := context.Background()
retryAttempts := getMessageRetryAttempts(ctx)
assert.Equal(0, retryAttempts)
// test with invalid type of retries
ctx = context.Background()
ctxWithInvalidTypeOfRetries := context.WithValue(ctx, RetriesKey, "invalid_type_of_retries")
retryAttempts = getMessageRetryAttempts(ctxWithInvalidTypeOfRetries)
assert.Equal(0, retryAttempts)
// test with invalid retries key
ctx = context.Background()
invalidRetriesKey := struct {
Key string
}{
Key: "invalid_retries_key",
}
ctxWithInvalidRetriesKey := context.WithValue(ctx, invalidRetriesKey, 3)
retryAttempts = getMessageRetryAttempts(ctxWithInvalidRetriesKey)
assert.Equal(0, retryAttempts)
// test with retries
ctx = context.Background()
ctxWithRetries := context.WithValue(ctx, RetriesKey, 3)
retryAttempts = getMessageRetryAttempts(ctxWithRetries)
assert.Equal(3, retryAttempts)
}