|
1 | 1 | package firebase
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "slices" |
4 | 7 | "testing"
|
5 | 8 | "time"
|
6 | 9 |
|
| 10 | + "firebase.google.com/go/v4/messaging" |
7 | 11 | "github.com/nyaruka/courier"
|
8 | 12 | . "github.com/nyaruka/courier/handlers"
|
9 | 13 | "github.com/nyaruka/courier/test"
|
@@ -36,13 +40,39 @@ var testChannels = []courier.Channel{
|
36 | 40 | map[string]any{
|
37 | 41 | configKey: "FCMKey",
|
38 | 42 | configTitle: "FCMTitle",
|
| 43 | + configCredentialsFile: `{ |
| 44 | + "type": "service_account", |
| 45 | + "project_id": "foo-project-id", |
| 46 | + "private_key_id": "123", |
| 47 | + "private_key": "BLAH", |
| 48 | + "client_email": "[email protected]", |
| 49 | + "client_id": "123123", |
| 50 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 51 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 52 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 53 | + "client_x509_cert_url": "", |
| 54 | + "universe_domain": "googleapis.com" |
| 55 | + }`, |
39 | 56 | }),
|
40 | 57 | test.NewMockChannel("8eb23e93-5ecb-45ba-b726-3b064e0c568c", "FCM", "1234", "",
|
41 | 58 | []string{urns.Firebase.Prefix},
|
42 | 59 | map[string]any{
|
43 | 60 | configKey: "FCMKey",
|
44 | 61 | configNotification: true,
|
45 | 62 | configTitle: "FCMTitle",
|
| 63 | + configCredentialsFile: `{ |
| 64 | + "type": "service_account", |
| 65 | + "project_id": "foo-project-id", |
| 66 | + "private_key_id": "123", |
| 67 | + "private_key": "BLAH", |
| 68 | + "client_email": "[email protected]", |
| 69 | + "client_id": "123123", |
| 70 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 71 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 72 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 73 | + "client_x509_cert_url": "", |
| 74 | + "universe_domain": "googleapis.com" |
| 75 | + }`, |
46 | 76 | }),
|
47 | 77 | }
|
48 | 78 |
|
@@ -99,20 +129,11 @@ func BenchmarkHandler(b *testing.B) {
|
99 | 129 |
|
100 | 130 | var notificationSendTestCases = []OutgoingTestCase{
|
101 | 131 | {
|
102 |
| - Label: "Plain Send", |
103 |
| - MsgText: "Simple Message", |
104 |
| - MsgURN: "fcm:250788123123", |
105 |
| - MsgURNAuth: "auth1", |
106 |
| - MockResponses: map[string][]*httpx.MockResponse{ |
107 |
| - "https://fcm.googleapis.com/fcm/send": { |
108 |
| - httpx.NewMockResponse(200, nil, []byte(`{"success":1, "multicast_id": 123456}`)), |
109 |
| - }, |
110 |
| - }, |
111 |
| - ExpectedRequests: []ExpectedRequest{{ |
112 |
| - Headers: map[string]string{"Authorization": "key=FCMKey"}, |
113 |
| - Body: `{"data":{"type":"rapidpro","title":"FCMTitle","message":"Simple Message","message_id":10,"session_status":""},"notification":{"title":"FCMTitle","body":"Simple Message"},"content_available":true,"to":"auth1","priority":"high"}`, |
114 |
| - }}, |
115 |
| - ExpectedExtIDs: []string{"123456"}, |
| 132 | + Label: "Plain Send", |
| 133 | + MsgText: "Simple Message", |
| 134 | + MsgURN: "fcm:250788123123", |
| 135 | + MsgURNAuth: "auth1", |
| 136 | + ExpectedExtIDs: []string{"123456-a"}, |
116 | 137 | },
|
117 | 138 | }
|
118 | 139 |
|
@@ -224,7 +245,40 @@ var sendTestCases = []OutgoingTestCase{
|
224 | 245 | },
|
225 | 246 | }
|
226 | 247 |
|
| 248 | +type MockFCMClient struct { |
| 249 | + // list of valid FCM tokens |
| 250 | + ValidTokens []string |
| 251 | + |
| 252 | + // log of messages sent to this client |
| 253 | + Messages []*messaging.Message |
| 254 | +} |
| 255 | + |
| 256 | +func (fc *MockFCMClient) Send(ctx context.Context, message *messaging.Message) (string, error) { |
| 257 | + var err error |
| 258 | + result := "" |
| 259 | + |
| 260 | + fc.Messages = append(fc.Messages, message) |
| 261 | + if slices.Contains(fc.ValidTokens, message.Token) { |
| 262 | + return "projects/foo-project-id/messages/123456-a", err |
| 263 | + } |
| 264 | + return result, errors.New("401 error: 401 Unauthorized") |
| 265 | +} |
| 266 | + |
| 267 | +type FCMHandler struct { |
| 268 | + courier.ChannelHandler |
| 269 | + FCMClient FCMClient |
| 270 | +} |
| 271 | + |
| 272 | +func newFCMHandler(FCMClient FCMClient) *FCMHandler { |
| 273 | + return &FCMHandler{test.NewMockHandler(), FCMClient} |
| 274 | +} |
| 275 | + |
| 276 | +func (h *FCMHandler) GetFCMClient(ctx context.Context, channel courier.Channel, clog *courier.ChannelLog) (FCMClient, string, error) { |
| 277 | + return h.FCMClient, "foo-project-id", nil |
| 278 | +} |
| 279 | + |
227 | 280 | func TestOutgoing(t *testing.T) {
|
228 |
| - RunOutgoingTestCases(t, testChannels[0], newHandler(), sendTestCases, []string{"FCMKey"}, nil) |
229 |
| - RunOutgoingTestCases(t, testChannels[1], newHandler(), notificationSendTestCases, []string{"FCMKey"}, nil) |
| 281 | + |
| 282 | + RunOutgoingTestCases(t, testChannels[0], newFCMHandler(&MockFCMClient{ValidTokens: []string{"auth1"}}), sendTestCases, []string{"FCMKey"}, nil) |
| 283 | + RunOutgoingTestCases(t, testChannels[1], newFCMHandler(&MockFCMClient{ValidTokens: []string{"auth1"}}), notificationSendTestCases, []string{"FCMKey"}, nil) |
230 | 284 | }
|
0 commit comments