|
1 | 1 | import path from 'path';
|
| 2 | +import log from 'npmlog'; |
2 | 3 | import FCM from '../src/FCM.js';
|
3 | 4 |
|
| 5 | +const testArgs = { |
| 6 | + firebaseServiceAccount: path.join( |
| 7 | + __dirname, |
| 8 | + '..', |
| 9 | + 'spec', |
| 10 | + 'support', |
| 11 | + 'fakeServiceAccount.json', |
| 12 | + ), |
| 13 | +}; |
| 14 | + |
4 | 15 | describe('FCM', () => {
|
5 | 16 | it('can initialize', () => {
|
6 |
| - const args = { |
7 |
| - firebaseServiceAccount: path.join( |
8 |
| - __dirname, |
9 |
| - '..', |
10 |
| - 'spec', |
11 |
| - 'support', |
12 |
| - 'fakeServiceAccount.json', |
13 |
| - ), |
14 |
| - }; |
15 |
| - const fcm = new FCM(args); |
| 17 | + const fcm = new FCM(testArgs); |
16 | 18 | expect(fcm).toBeDefined();
|
17 | 19 | });
|
18 | 20 |
|
| 21 | + it('can throw on initializing with invalid args', () => { |
| 22 | + expect(function() { new FCM(123); }).toThrow(); |
| 23 | + expect(function() { new FCM({}); }).toThrow(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('does log on invalid payload', async () => { |
| 27 | + const spy = spyOn(log, 'warn'); |
| 28 | + const fcm = new FCM(testArgs); |
| 29 | + fcm.send(); |
| 30 | + expect(spy).toHaveBeenCalled(); |
| 31 | + expect(spy).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'invalid push payload'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('can send successful FCM android request', async () => { |
| 35 | + const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {}); |
| 36 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 37 | + const fcm = new FCM(testArgs); |
| 38 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 39 | + return Promise.resolve({ |
| 40 | + responses: [{ success: true }], |
| 41 | + }); |
| 42 | + }); |
| 43 | + fcm.pushType = 'android'; |
| 44 | + const data = { data: { alert: 'alert' } }; |
| 45 | + const devices = [{ deviceToken: 'token' }]; |
| 46 | + const response = await fcm.send(data, devices); |
| 47 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 48 | + const args = fcm.sender.sendEachForMulticast.calls.first().args; |
| 49 | + expect(args.length).toEqual(1); |
| 50 | + expect(args[0].android.priority).toEqual('high'); |
| 51 | + expect(args[0].android.data.data).toEqual('{"alert":"alert"}'); |
| 52 | + expect(args[0].tokens).toEqual(['token']); |
| 53 | + expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]'); |
| 54 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 55 | + expect(response).toEqual([[{ |
| 56 | + device: { deviceToken: 'token', deviceType: undefined }, |
| 57 | + transmitted: true |
| 58 | + }]]); |
| 59 | + }); |
| 60 | + |
| 61 | + it('can send successful FCM android request with apns integer keys', async () => { |
| 62 | + const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {}); |
| 63 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 64 | + const fcm = new FCM(testArgs); |
| 65 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 66 | + return Promise.resolve({ |
| 67 | + responses: [{ success: true }], |
| 68 | + }); |
| 69 | + }); |
| 70 | + fcm.pushType = 'android'; |
| 71 | + const data = { data: { alert: 'alert', badge: 1 } }; |
| 72 | + const devices = [{ deviceToken: 'token' }]; |
| 73 | + const response = await fcm.send(data, devices); |
| 74 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 75 | + const args = fcm.sender.sendEachForMulticast.calls.first().args; |
| 76 | + expect(args.length).toEqual(1); |
| 77 | + expect(args[0].android.priority).toEqual('high'); |
| 78 | + // Should not include badge key in data |
| 79 | + expect(args[0].android.data.data).toEqual('{"alert":"alert"}'); |
| 80 | + expect(args[0].tokens).toEqual(['token']); |
| 81 | + expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]'); |
| 82 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 83 | + expect(response).toEqual([[{ |
| 84 | + device: { deviceToken: 'token', deviceType: undefined }, |
| 85 | + transmitted: true |
| 86 | + }]]); |
| 87 | + }); |
| 88 | + |
| 89 | + it('can send successful FCM apple request with alert', async () => { |
| 90 | + const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {}); |
| 91 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 92 | + const fcm = new FCM(testArgs); |
| 93 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 94 | + return Promise.resolve({ |
| 95 | + responses: [{ success: true }], |
| 96 | + }); |
| 97 | + }); |
| 98 | + fcm.pushType = 'apple'; |
| 99 | + const data = { data: { alert: 'alert' } }; |
| 100 | + const devices = [{ deviceToken: 'token' }]; |
| 101 | + const response = await fcm.send(data, devices); |
| 102 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 103 | + const args = fcm.sender.sendEachForMulticast.calls.first().args; |
| 104 | + expect(args.length).toEqual(1); |
| 105 | + expect(args[0].apns.payload).toEqual({ aps: { alert: { body: 'alert' } } }); |
| 106 | + expect(args[0].apns.headers).toEqual({ 'apns-push-type': 'alert' }); |
| 107 | + expect(args[0].tokens).toEqual(['token']); |
| 108 | + expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]'); |
| 109 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 110 | + expect(response).toEqual([[{ |
| 111 | + device: { deviceToken: 'token', deviceType: undefined }, |
| 112 | + transmitted: true |
| 113 | + }]]); |
| 114 | + }); |
| 115 | + |
| 116 | + it('can send successful FCM apple request with title', async () => { |
| 117 | + const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {}); |
| 118 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 119 | + const fcm = new FCM(testArgs); |
| 120 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 121 | + return Promise.resolve({ |
| 122 | + responses: [{ success: true }], |
| 123 | + }); |
| 124 | + }); |
| 125 | + fcm.pushType = 'apple'; |
| 126 | + const data = { data: { title: 'title' } }; |
| 127 | + const devices = [{ deviceToken: 'token' }]; |
| 128 | + const response = await fcm.send(data, devices); |
| 129 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 130 | + const args = fcm.sender.sendEachForMulticast.calls.first().args; |
| 131 | + expect(args.length).toEqual(1); |
| 132 | + expect(args[0].apns.payload).toEqual({ aps: { alert: { title: 'title' } } }); |
| 133 | + expect(args[0].apns.headers).toEqual({ 'apns-push-type': 'alert' }); |
| 134 | + expect(args[0].tokens).toEqual(['token']); |
| 135 | + expect(spyVerbose).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'tokens with successful pushes: ["token"]'); |
| 136 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 137 | + expect(response).toEqual([[{ |
| 138 | + device: { deviceToken: 'token', deviceType: undefined }, |
| 139 | + transmitted: true |
| 140 | + }]]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('can send failed FCM request', async () => { |
| 144 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 145 | + const spyError = spyOn(log, 'error').and.callFake(() => {}); |
| 146 | + const fcm = new FCM(testArgs); |
| 147 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 148 | + return Promise.resolve({ |
| 149 | + responses: [{ success: false, error: 'testing failed' }], |
| 150 | + }); |
| 151 | + }); |
| 152 | + fcm.pushType = 'android'; |
| 153 | + const data = { data: { alert: 'alert' } }; |
| 154 | + const devices = [{ deviceToken: 'token', deviceType: 'apple' }]; |
| 155 | + const response = await fcm.send(data, devices); |
| 156 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 157 | + const args = fcm.sender.sendEachForMulticast.calls.first().args; |
| 158 | + expect(args.length).toEqual(1); |
| 159 | + expect(args[0].android.priority).toEqual('high'); |
| 160 | + expect(args[0].android.data.data).toEqual('{"alert":"alert"}'); |
| 161 | + expect(args[0].tokens).toEqual(['token']); |
| 162 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 163 | + expect(spyError.calls.all()[0].args).toEqual(['parse-server-push-adapter FCM', 'failed to send to token with error: "testing failed"']); |
| 164 | + expect(spyError.calls.all()[1].args).toEqual(['parse-server-push-adapter FCM', 'tokens with failed pushes: ["token"]']); |
| 165 | + expect(response).toEqual([[{ |
| 166 | + device: { deviceToken: 'token', deviceType: 'apple' }, |
| 167 | + response: { error: 'testing failed'}, |
| 168 | + transmitted: false, |
| 169 | + }]]); |
| 170 | + }); |
| 171 | + |
| 172 | + it('can handle FCM request error', async () => { |
| 173 | + const spyInfo = spyOn(log, 'info').and.callFake(() => {}); |
| 174 | + const spyError = spyOn(log, 'error').and.callFake(() => {}); |
| 175 | + const fcm = new FCM(testArgs); |
| 176 | + spyOn(fcm.sender, 'sendEachForMulticast').and.callFake(() => { |
| 177 | + return Promise.reject('testing error abort'); |
| 178 | + }); |
| 179 | + fcm.pushType = 'android'; |
| 180 | + const data = { data: { alert: 'alert' } }; |
| 181 | + const devices = [{ deviceToken: 'token' }]; |
| 182 | + await fcm.send(data, devices); |
| 183 | + expect(fcm.sender.sendEachForMulticast).toHaveBeenCalled(); |
| 184 | + expect(spyInfo).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'sending push to 1 devices'); |
| 185 | + expect(spyError).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'error sending push: testing error abort'); |
| 186 | + }); |
| 187 | + |
| 188 | + it('FCM request invalid push type', async () => { |
| 189 | + const fcm = new FCM(testArgs); |
| 190 | + fcm.pushType = 'invalid'; |
| 191 | + const data = { data: { alert: 'alert' } }; |
| 192 | + const devices = [{ deviceToken: 'token' }]; |
| 193 | + try { |
| 194 | + await fcm.send(data, devices); |
| 195 | + expect(true).toBe(false); |
| 196 | + } catch (e) { |
| 197 | + expect(e.message).toBe('Unsupported push type, apple or android only.'); |
| 198 | + } |
| 199 | + }); |
| 200 | + |
19 | 201 | it('can use a raw FCM payload', () => {
|
20 | 202 | // If the payload is wrapped inside a key named 'rawPayload', a user can use the raw FCM payload structure
|
21 | 203 | // See: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
|
|
0 commit comments