|
| 1 | +import { APP_ID, DUMMY_EXTERNAL_ID } from '__test__/support/constants'; |
| 2 | +import TestContext from '__test__/support/environment/TestContext'; |
| 3 | +import { TestEnvironment } from '__test__/support/environment/TestEnvironment'; |
| 4 | +import { setupSubModelStore } from '__test__/support/environment/TestEnvironmentHelpers'; |
| 5 | +import { |
| 6 | + createUserFn, |
| 7 | + setCreateUserResponse, |
| 8 | +} from '__test__/support/helpers/requests'; |
| 9 | +import { |
| 10 | + getSubscriptionFn, |
| 11 | + MockServiceWorker, |
| 12 | +} from '__test__/support/mocks/MockServiceWorker'; |
| 13 | +import ContextSW from '../models/ContextSW'; |
| 14 | +import { RawPushSubscription } from '../models/RawPushSubscription'; |
| 15 | +import { IDManager } from './IDManager'; |
| 16 | +import { |
| 17 | + SubscriptionManager, |
| 18 | + SubscriptionManagerConfig, |
| 19 | +} from './SubscriptionManager'; |
| 20 | + |
| 21 | +const subConfig: SubscriptionManagerConfig = { |
| 22 | + appId: APP_ID, |
| 23 | + vapidPublicKey: '', |
| 24 | + onesignalVapidPublicKey: '', |
| 25 | +}; |
| 26 | + |
| 27 | +const getRawSubscription = (): RawPushSubscription => { |
| 28 | + const rawSubscription = new RawPushSubscription(); |
| 29 | + rawSubscription.w3cAuth = 'auth'; |
| 30 | + rawSubscription.w3cP256dh = 'p256dh'; |
| 31 | + rawSubscription.w3cEndpoint = new URL('https://example.com/endpoint'); |
| 32 | + rawSubscription.safariDeviceToken = 'safariDeviceToken'; |
| 33 | + return rawSubscription; |
| 34 | +}; |
| 35 | + |
| 36 | +describe('SubscriptionManager', () => { |
| 37 | + beforeEach(async () => { |
| 38 | + vi.resetModules(); |
| 39 | + await TestEnvironment.initialize(); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('updatePushSubscriptionModelWithRawSubscription', () => { |
| 43 | + test('should create the push subscription model if it does not exist', async () => { |
| 44 | + setCreateUserResponse(); |
| 45 | + const context = new ContextSW(TestContext.getFakeMergedConfig()); |
| 46 | + const subscriptionManager = new SubscriptionManager(context, subConfig); |
| 47 | + const rawSubscription = getRawSubscription(); |
| 48 | + |
| 49 | + let subModels = |
| 50 | + await OneSignal.coreDirector.subscriptionModelStore.list(); |
| 51 | + expect(subModels.length).toBe(0); |
| 52 | + |
| 53 | + // mimicing the event helper checkAndTriggerSubscriptionChanged |
| 54 | + await OneSignal.database.setPushToken( |
| 55 | + rawSubscription.w3cEndpoint?.toString(), |
| 56 | + ); |
| 57 | + |
| 58 | + await subscriptionManager._updatePushSubscriptionModelWithRawSubscription( |
| 59 | + rawSubscription, |
| 60 | + ); |
| 61 | + |
| 62 | + subModels = await OneSignal.coreDirector.subscriptionModelStore.list(); |
| 63 | + expect(subModels.length).toBe(1); |
| 64 | + |
| 65 | + const id = subModels[0].id; |
| 66 | + expect(IDManager.isLocalId(id)).toBe(true); |
| 67 | + expect(subModels[0].toJSON()).toEqual({ |
| 68 | + id, |
| 69 | + device_model: '', |
| 70 | + device_os: 56, |
| 71 | + enabled: true, |
| 72 | + notification_types: 1, |
| 73 | + sdk: '1', |
| 74 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 75 | + type: 'ChromePush', |
| 76 | + web_auth: rawSubscription.w3cAuth, |
| 77 | + web_p256: rawSubscription.w3cP256dh, |
| 78 | + }); |
| 79 | + |
| 80 | + await vi.waitUntil(() => createUserFn.mock.calls.length > 0); |
| 81 | + expect(createUserFn).toHaveBeenCalledWith({ |
| 82 | + identity: {}, |
| 83 | + properties: { |
| 84 | + language: 'en', |
| 85 | + timezone_id: 'America/Los_Angeles', |
| 86 | + }, |
| 87 | + refresh_device_metadata: true, |
| 88 | + subscriptions: [ |
| 89 | + { |
| 90 | + device_model: '', |
| 91 | + device_os: 56, |
| 92 | + enabled: true, |
| 93 | + notification_types: 1, |
| 94 | + sdk: '1', |
| 95 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 96 | + type: 'ChromePush', |
| 97 | + web_auth: rawSubscription.w3cAuth, |
| 98 | + web_p256: rawSubscription.w3cP256dh, |
| 99 | + }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + test('should create user if push subscription model does not have an id', async () => { |
| 105 | + const generatePushSubscriptionModelSpy = vi.spyOn( |
| 106 | + OneSignal.coreDirector, |
| 107 | + 'generatePushSubscriptionModel', |
| 108 | + ); |
| 109 | + const rawSubscription = getRawSubscription(); |
| 110 | + getSubscriptionFn.mockResolvedValue({ |
| 111 | + endpoint: rawSubscription.w3cEndpoint?.toString(), |
| 112 | + }); |
| 113 | + setCreateUserResponse({ |
| 114 | + externalId: 'some-external-id', |
| 115 | + }); |
| 116 | + |
| 117 | + const context = new ContextSW(TestContext.getFakeMergedConfig()); |
| 118 | + const subscriptionManager = new SubscriptionManager(context, subConfig); |
| 119 | + |
| 120 | + // create push sub with no id |
| 121 | + const identityModel = OneSignal.coreDirector.getIdentityModel(); |
| 122 | + identityModel.onesignalId = IDManager.createLocalId(); |
| 123 | + identityModel.externalId = 'some-external-id'; |
| 124 | + |
| 125 | + await setupSubModelStore({ |
| 126 | + id: '', |
| 127 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 128 | + onesignalId: identityModel.onesignalId, |
| 129 | + }); |
| 130 | + |
| 131 | + await subscriptionManager._updatePushSubscriptionModelWithRawSubscription( |
| 132 | + rawSubscription, |
| 133 | + ); |
| 134 | + |
| 135 | + // should not call generatePushSubscriptionModelSpy |
| 136 | + expect(generatePushSubscriptionModelSpy).not.toHaveBeenCalled(); |
| 137 | + |
| 138 | + expect(createUserFn).toHaveBeenCalledWith({ |
| 139 | + identity: { |
| 140 | + external_id: 'some-external-id', |
| 141 | + }, |
| 142 | + properties: { |
| 143 | + language: 'en', |
| 144 | + timezone_id: 'America/Los_Angeles', |
| 145 | + }, |
| 146 | + refresh_device_metadata: true, |
| 147 | + subscriptions: [ |
| 148 | + { |
| 149 | + device_model: '', |
| 150 | + device_os: 56, |
| 151 | + enabled: true, |
| 152 | + notification_types: 1, |
| 153 | + sdk: '1', |
| 154 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 155 | + type: 'ChromePush', |
| 156 | + }, |
| 157 | + ], |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + test('should update the push subscription model if it already exists', async () => { |
| 162 | + setCreateUserResponse(); |
| 163 | + const context = new ContextSW(TestContext.getFakeMergedConfig()); |
| 164 | + const subscriptionManager = new SubscriptionManager(context, subConfig); |
| 165 | + const rawSubscription = getRawSubscription(); |
| 166 | + |
| 167 | + await OneSignal.database.setPushToken( |
| 168 | + rawSubscription.w3cEndpoint?.toString(), |
| 169 | + ); |
| 170 | + |
| 171 | + const pushModel = await setupSubModelStore({ |
| 172 | + id: '123', |
| 173 | + token: rawSubscription.w3cEndpoint?.toString(), |
| 174 | + onesignalId: DUMMY_EXTERNAL_ID, |
| 175 | + }); |
| 176 | + pushModel.web_auth = 'old-web-auth'; |
| 177 | + pushModel.web_p256 = 'old-web-p256'; |
| 178 | + |
| 179 | + await subscriptionManager._updatePushSubscriptionModelWithRawSubscription( |
| 180 | + rawSubscription, |
| 181 | + ); |
| 182 | + |
| 183 | + const updatedPushModel = |
| 184 | + (await OneSignal.coreDirector.getPushSubscriptionModel())!; |
| 185 | + expect(updatedPushModel.token).toBe( |
| 186 | + rawSubscription.w3cEndpoint?.toString(), |
| 187 | + ); |
| 188 | + expect(updatedPushModel.web_auth).toBe(rawSubscription.w3cAuth); |
| 189 | + expect(updatedPushModel.web_p256).toBe(rawSubscription.w3cP256dh); |
| 190 | + }); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +Object.defineProperty(global.navigator, 'serviceWorker', { |
| 195 | + value: new MockServiceWorker(), |
| 196 | + writable: true, |
| 197 | +}); |
0 commit comments