|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { assert } from 'chai'; |
| 17 | +import * as sinon from 'sinon'; |
| 18 | +import makeFakeApp from './make-fake-app'; |
| 19 | +import makeFakeSWReg from './make-fake-sw-reg'; |
| 20 | +import dbTMHelper from './testing-utils/db-token-manager'; |
| 21 | +import { deleteDatabase } from './testing-utils/db-helper'; |
| 22 | +import Errors from '../src/models/errors'; |
| 23 | +import TokenManager from '../src/models/token-manager'; |
| 24 | +import WindowController from '../src/controllers/window-controller'; |
| 25 | +import SWController from '../src/controllers/sw-controller'; |
| 26 | + |
| 27 | +const EXAMPLE_TOKEN_SAVE = { |
| 28 | + fcmToken: 'ExampleFCMToken1337', |
| 29 | + fcmSenderId: '1234567890', |
| 30 | + endpoint: 'https://example.google.com/1234', |
| 31 | + swScope: 'firebase-messaging-sw-scope', |
| 32 | + auth: '12345', |
| 33 | + p256dh: '123456789098765642421' |
| 34 | +}; |
| 35 | + |
| 36 | +describe('Firebase Messaging > *Controller.deleteToken()', function() { |
| 37 | + const sandbox = sinon.sandbox.create(); |
| 38 | + |
| 39 | + const app = makeFakeApp({ |
| 40 | + messagingSenderId: EXAMPLE_TOKEN_SAVE.fcmSenderId |
| 41 | + }); |
| 42 | + |
| 43 | + const configureRegistrationMocks = (ServiceClass, fakeReg) => { |
| 44 | + sandbox.stub( |
| 45 | + ServiceClass.prototype, |
| 46 | + 'getSWRegistration_' |
| 47 | + ).callsFake(() => { |
| 48 | + return fakeReg; |
| 49 | + }); |
| 50 | + }; |
| 51 | + |
| 52 | + const generateFakeReg = getSubResult => { |
| 53 | + const registration = makeFakeSWReg(); |
| 54 | + Object.defineProperty(registration, 'pushManager', { |
| 55 | + value: { |
| 56 | + getSubscription: () => { |
| 57 | + if (typeof getSubResult === 'function') { |
| 58 | + return getSubResult(); |
| 59 | + } |
| 60 | + |
| 61 | + return getSubResult; |
| 62 | + }, |
| 63 | + } |
| 64 | + }); |
| 65 | + return Promise.resolve(registration); |
| 66 | + }; |
| 67 | + |
| 68 | + let globalMessagingService; |
| 69 | + const cleanUp = () => { |
| 70 | + sandbox.restore(); |
| 71 | + |
| 72 | + const deletePromises = [dbTMHelper.closeDatabase()]; |
| 73 | + if (globalMessagingService) { |
| 74 | + deletePromises.push(globalMessagingService.delete()); |
| 75 | + } |
| 76 | + return Promise.all(deletePromises) |
| 77 | + .then(() => deleteDatabase(TokenManager.DB_NAME)) |
| 78 | + .then(() => globalMessagingService = null); |
| 79 | + } |
| 80 | + |
| 81 | + beforeEach(function() { |
| 82 | + return cleanUp(); |
| 83 | + }); |
| 84 | + |
| 85 | + after(function() { |
| 86 | + return cleanUp(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should handle no token to delete', function() { |
| 90 | + globalMessagingService = new WindowController(app); |
| 91 | + return globalMessagingService.deleteToken() |
| 92 | + .then( |
| 93 | + () => { |
| 94 | + throw new Error('Expected error to be thrown.'); |
| 95 | + }, |
| 96 | + err => { |
| 97 | + assert.equal( |
| 98 | + 'messaging/' + Errors.codes.INVALID_DELETE_TOKEN, |
| 99 | + err.code |
| 100 | + ); |
| 101 | + } |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should handle no registration', function() { |
| 106 | + configureRegistrationMocks(WindowController, Promise.resolve(null)); |
| 107 | + |
| 108 | + return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE) |
| 109 | + .then(() => { |
| 110 | + globalMessagingService = new WindowController(app); |
| 111 | + return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle get subscription error', function() { |
| 116 | + configureRegistrationMocks(WindowController, |
| 117 | + generateFakeReg(() => Promise.reject(new Error('Unknown error'))) |
| 118 | + ); |
| 119 | + |
| 120 | + dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE); |
| 121 | + |
| 122 | + globalMessagingService = new WindowController(app); |
| 123 | + return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken).then( |
| 124 | + () => { |
| 125 | + throw new Error('Expected this to reject'); |
| 126 | + }, |
| 127 | + err => { |
| 128 | + assert.equal('Unknown error', err.message); |
| 129 | + } |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + [WindowController, SWController].forEach((ServiceClass) => { |
| 134 | + it(`should handle null getSubscription() ${ServiceClass.name}`, function() { |
| 135 | + configureRegistrationMocks(ServiceClass, generateFakeReg(Promise.resolve(null))); |
| 136 | + |
| 137 | + return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE) |
| 138 | + .then(() => { |
| 139 | + globalMessagingService = new ServiceClass(app); |
| 140 | + return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it(`should handle error on unsubscribe ${ServiceClass.name}`, function() { |
| 145 | + const errorMsg = 'unsubscribe-error-1234567890'; |
| 146 | + const fakeSubscription = { |
| 147 | + endpoint: EXAMPLE_TOKEN_SAVE.endpoint, |
| 148 | + unsubscribe: () => Promise.reject(new Error(errorMsg)) |
| 149 | + }; |
| 150 | + |
| 151 | + configureRegistrationMocks( |
| 152 | + ServiceClass, |
| 153 | + generateFakeReg(Promise.resolve(fakeSubscription)) |
| 154 | + ); |
| 155 | + |
| 156 | + return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE) |
| 157 | + .then(() => { |
| 158 | + globalMessagingService = new ServiceClass(app); |
| 159 | + return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken) |
| 160 | + .then( |
| 161 | + () => { |
| 162 | + throw new Error('Expected this to reject'); |
| 163 | + }, |
| 164 | + err => { |
| 165 | + assert.equal(errorMsg, err.message); |
| 166 | + } |
| 167 | + ); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + it(`should delete with valid unsubscribe ${ServiceClass.name}`, function() { |
| 172 | + const fakeSubscription = { |
| 173 | + endpoint: EXAMPLE_TOKEN_SAVE.endpoint, |
| 174 | + unsubscribe: () => Promise.resolve() |
| 175 | + }; |
| 176 | + |
| 177 | + configureRegistrationMocks( |
| 178 | + ServiceClass, |
| 179 | + generateFakeReg(Promise.resolve(fakeSubscription)) |
| 180 | + ); |
| 181 | + |
| 182 | + return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE) |
| 183 | + .then(() => { |
| 184 | + globalMessagingService = new ServiceClass(app); |
| 185 | + return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken); |
| 186 | + }); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments