Skip to content

Commit

Permalink
WIP Add login tests for externalId
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherd-l committed Sep 20, 2024
1 parent 119b5a5 commit 937d585
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 20 deletions.
1 change: 1 addition & 0 deletions __test__/support/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const APP_ID = '34fcbe85-278d-4fd2-a4ec-0f80e95072c5';
export const DUMMY_PUSH_TOKEN =
'https://fcm.googleapis.com/fcm/send/01010101010101';
export const DUMMY_ONESIGNAL_ID = '1111111111-2222222222-3333333333';
export const DUMMY_ONESIGNAL_ID_2 = '2222222222-3333333333-4444444444';
export const DUMMY_EXTERNAL_ID = 'rodrigo';
export const DUMMY_EXTERNAL_ID_2 = 'iryna';
export const DUMMY_SUBSCRIPTION_ID = '4444444444-5555555555-6666666666';
Expand Down
112 changes: 92 additions & 20 deletions __test__/unit/user/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { setupLoginStubs } from '../../support/helpers/login';
import { RequestService } from '../../../src/core/requestService/RequestService';
import { getDummyIdentityOSModel } from '../../support/helpers/core';
import { ModelName } from '../../../src/core/models/SupportedModels';
import { DUMMY_EXTERNAL_ID, DUMMY_ONESIGNAL_ID } from '../../support/constants';
import {
DUMMY_EXTERNAL_ID,
DUMMY_EXTERNAL_ID_2,
DUMMY_ONESIGNAL_ID,
} from '../../support/constants';
import { IdentityExecutor } from '../../../src/core/executors/IdentityExecutor';
import { PropertiesExecutor } from '../../../src/core/executors/PropertiesExecutor';
import { SubscriptionExecutor } from '../../../src/core/executors/SubscriptionExecutor';
Expand Down Expand Up @@ -88,12 +92,6 @@ describe('Login tests', () => {
const identifyOrUpsertUserSpy = test.stub(
LoginManager,
'identifyOrUpsertUser',
Promise.resolve({
identity: {
external_id: DUMMY_EXTERNAL_ID,
onesignal_id: '1234567890',
},
}),
);

await LoginManager.login(DUMMY_EXTERNAL_ID);
Expand Down Expand Up @@ -230,23 +228,97 @@ describe('Login tests', () => {
});

test('If login with JWT token, save it to the database', async () => {});
});

test('Login called before any Subscriptions, should save external_id but not create User', async () => {
setupLoginStubs();
await TestEnvironment.initialize();
test.nock({});
test('Login called before any Subscriptions, should save external_id but not create User', async () => {
setupLoginStubs();
await TestEnvironment.initialize();
test.nock({});

OneSignal.coreDirector.add(ModelName.Identity, getDummyIdentityOSModel());

const createUserSpy = jest.spyOn(RequestService, 'createUser');

await LoginManager.login(DUMMY_EXTERNAL_ID);

expect(OneSignal.coreDirector.getIdentityModel().data.external_id).toBe(
DUMMY_EXTERNAL_ID,
);

// User should NOT be created, as we have no subscriptions yet.
expect(createUserSpy.mock.calls.length).toBe(0);
});

test('Login updates externalId on all models', async () => {
setupLoginStubs();
await TestEnvironment.initialize({
useMockIdentityModel: true,
useMockPushSubscriptionModel: true,
});

test.stub(
LoginManager,
'identifyOrUpsertUser',
Promise.resolve({
identity: {
external_id: DUMMY_EXTERNAL_ID,
onesignal_id: DUMMY_ONESIGNAL_ID,
},
}),
);

await LoginManager.login(DUMMY_EXTERNAL_ID);

expect(OneSignal.coreDirector.getIdentityModel().externalId).toBe(
DUMMY_EXTERNAL_ID,
);

expect(OneSignal.coreDirector.getPropertiesModel().externalId).toBe(
DUMMY_EXTERNAL_ID,
);

const subs = await OneSignal.coreDirector.getAllSubscriptionsModels();

expect(
subs.every(
(sub: { externalId: string }) => sub.externalId === DUMMY_EXTERNAL_ID,
),
).toBe(true);
});

test('Login twice updates current externalId on all models', async () => {
setupLoginStubs();
await TestEnvironment.initialize({
useMockIdentityModel: true,
});

test.stub(
LoginManager,
'identifyOrUpsertUser',
Promise.resolve({
identity: {
external_id: DUMMY_EXTERNAL_ID,
onesignal_id: DUMMY_ONESIGNAL_ID,
},
}),
);

OneSignal.coreDirector.add(ModelName.Identity, getDummyIdentityOSModel());
await LoginManager.login(DUMMY_EXTERNAL_ID);
await LoginManager.login(DUMMY_EXTERNAL_ID_2);

const createUserSpy = jest.spyOn(RequestService, 'createUser');
expect(OneSignal.coreDirector.getIdentityModel().externalId).toBe(
DUMMY_EXTERNAL_ID_2,
);

await LoginManager.login(DUMMY_EXTERNAL_ID);
expect(OneSignal.coreDirector.getPropertiesModel().externalId).toBe(
DUMMY_EXTERNAL_ID_2,
);

expect(OneSignal.coreDirector.getIdentityModel().data.external_id).toBe(
DUMMY_EXTERNAL_ID,
);
const subs = await OneSignal.coreDirector.getAllSubscriptionsModels();

// User should NOT be created, as we have no subscriptions yet.
expect(createUserSpy.mock.calls.length).toBe(0);
expect(
subs.every(
(sub: { externalId: string }) => sub.externalId === DUMMY_EXTERNAL_ID_2,
),
).toBe(true);
});
});

0 comments on commit 937d585

Please sign in to comment.