Skip to content

Commit 5c8d0ed

Browse files
committed
add tests cases for update subscription method
1 parent 04c9170 commit 5c8d0ed

File tree

2 files changed

+145
-4
lines changed

2 files changed

+145
-4
lines changed

src/shared/managers/SubscriptionManager.test.ts

Lines changed: 140 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
import { APP_ID } from '__test__/support/constants';
1+
import { APP_ID, DUMMY_EXTERNAL_ID } from '__test__/support/constants';
22
import TestContext from '__test__/support/environment/TestContext';
33
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';
413
import ContextSW from '../models/ContextSW';
514
import { RawPushSubscription } from '../models/RawPushSubscription';
15+
import { IDManager } from './IDManager';
616
import {
717
SubscriptionManager,
818
SubscriptionManagerConfig,
@@ -24,26 +34,38 @@ const getRawSubscription = (): RawPushSubscription => {
2434
};
2535

2636
describe('SubscriptionManager', () => {
27-
beforeAll(async () => {
37+
beforeEach(async () => {
38+
vi.resetModules();
2839
await TestEnvironment.initialize();
2940
});
3041

3142
describe('updatePushSubscriptionModelWithRawSubscription', () => {
3243
test('should create the push subscription model if it does not exist', async () => {
44+
setCreateUserResponse();
3345
const context = new ContextSW(TestContext.getFakeMergedConfig());
3446
const subscriptionManager = new SubscriptionManager(context, subConfig);
3547
const rawSubscription = getRawSubscription();
3648

3749
let subModels =
3850
await OneSignal.coreDirector.subscriptionModelStore.list();
3951
expect(subModels.length).toBe(0);
52+
53+
// mimicing the event helper checkAndTriggerSubscriptionChanged
54+
await OneSignal.database.setPushToken(
55+
rawSubscription.w3cEndpoint?.toString(),
56+
);
57+
4058
await subscriptionManager._updatePushSubscriptionModelWithRawSubscription(
4159
rawSubscription,
4260
);
4361

4462
subModels = await OneSignal.coreDirector.subscriptionModelStore.list();
4563
expect(subModels.length).toBe(1);
64+
65+
const id = subModels[0].id;
66+
expect(IDManager.isLocalId(id)).toBe(true);
4667
expect(subModels[0].toJSON()).toEqual({
68+
id,
4769
device_model: '',
4870
device_os: 56,
4971
enabled: true,
@@ -54,6 +76,122 @@ describe('SubscriptionManager', () => {
5476
web_auth: rawSubscription.w3cAuth,
5577
web_p256: rawSubscription.w3cP256dh,
5678
});
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);
57190
});
58191
});
59192
});
193+
194+
Object.defineProperty(global.navigator, 'serviceWorker', {
195+
value: new MockServiceWorker(),
196+
writable: true,
197+
});

src/shared/managers/SubscriptionManager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,19 @@ export class SubscriptionManager {
174174
rawPushSubscription: RawPushSubscription,
175175
) {
176176
const pushModel = await OneSignal.coreDirector.getPushSubscriptionModel();
177-
177+
// EventHelper checkAndTriggerSubscriptionChanged is called before this function when permission is granted and so
178+
// it will save the push token/id to the database so we don't need to save the token afer generating
178179
if (!pushModel) {
179180
OneSignal.coreDirector.generatePushSubscriptionModel(rawPushSubscription);
180181
return UserDirector.createUserOnServer();
181182

182183
// Bug w/ v160400 release where isCreatingUser was improperly set and never reset
183184
// so a check if pushModel id is needed to recreate the user
184-
} else if (!pushModel.id) {
185+
}
186+
if (!pushModel.id) {
185187
return UserDirector.createUserOnServer();
186188
}
189+
187190
// resubscribing. update existing push subscription model
188191
const serializedSubscriptionRecord = new FuturePushSubscriptionRecord(
189192
rawPushSubscription,

0 commit comments

Comments
 (0)