Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@

// for testing, normally this is set on (temporary) in the database client
OneSignal._isNewVisitor = true;
console.log('_isNewVisitor', OneSignal._isNewVisitor);

OneSignal.init({
appId,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
},
{
"path": "./build/releases/OneSignalSDK.page.es6.js",
"limit": "43.77 kB",
"limit": "43.75 kB",
"gzip": true
},
{
Expand Down
12 changes: 6 additions & 6 deletions src/core/CoreModuleDirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ export class CoreModuleDirector {
logMethodCall(
'CoreModuleDirector.getPushSubscriptionModelByLastKnownToken',
);
const lastKnownPushToken = await getPushToken();
if (lastKnownPushToken) {

// Checking '' in case we create a temp/fake subscription on logi
const lastKnownPushToken = (await getPushToken()) ?? '';
if (lastKnownPushToken !== null) {
return this._getSubscriptionOfTypeWithToken(
SubscriptionChannel._Push,
lastKnownPushToken,
Expand All @@ -151,10 +153,8 @@ export class CoreModuleDirector {
SubscriptionModel | undefined
> {
logMethodCall('CoreModuleDirector.getPushSubscriptionModel');
return (
(await this._getPushSubscriptionModelByCurrentToken()) ||
(await this._getPushSubscriptionModelByLastKnownToken())
);
const sub = await this._getPushSubscriptionModelByLastKnownToken();
return (await this._getPushSubscriptionModelByCurrentToken()) || sub;
}

public _getIdentityModel(): IdentityModel {
Expand Down
50 changes: 30 additions & 20 deletions src/onesignal/OneSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
setGetUserResponse,
setSendCustomEventResponse,
setTransferSubscriptionResponse,
setUpdateSubscriptionResponse,
setUpdateUserResponse,
transferSubscriptionFn,
updateSubscriptionFn,
updateUserFn,
} from '__test__/support/helpers/requests';
import {
Expand Down Expand Up @@ -713,7 +715,16 @@
external_id: externalId,
},
...BASE_IDENTITY,
subscriptions: [],
subscriptions: [
{
device_model: '',
device_os: DEVICE_OS,
enabled: false,
sdk: expect.any(String),
token: '',
type: 'ChromePush',
},
],
});
});

Expand Down Expand Up @@ -747,7 +758,7 @@
});
});

test('login then accept web push permissions - it should make two user calls', async () => {
test('login then accept web push permissions - it should create user then update subscription', async () => {
const { promise, resolve } = Promise.withResolvers();
OneSignal._emitter.on(OneSignal.EVENTS.SUBSCRIPTION_CHANGED, resolve);
setGetUserResponse();
Expand All @@ -760,6 +771,7 @@
},
],
});
setUpdateSubscriptionResponse({ subscriptionId: SUB_ID });

OneSignal._coreDirector._subscriptionModelStore._replaceAll(
[],
Expand All @@ -783,38 +795,36 @@
};
registerForPushNotifications();

// first call just sets the external id
// create user call includes the empty subscription
await vi.waitUntil(() => createUserFn.mock.calls.length === 1, {
interval: 0,
});
expect(createUserFn).toHaveBeenCalledWith({
identity: {
external_id: externalId,
},
...BASE_IDENTITY,
subscriptions: [],
});
await promise;

// second call creates the subscription
await vi.waitUntil(() => createUserFn.mock.calls.length === 2, {
interval: 1,
});
expect(createUserFn).toHaveBeenCalledTimes(1);
expect(createUserFn).toHaveBeenCalledWith({
identity: {
external_id: externalId,
},
...BASE_IDENTITY,
subscriptions: [
{
...BASE_SUB,
token: PUSH_TOKEN,
device_model: '',
device_os: DEVICE_OS,
enabled: false,
sdk: expect.any(String),
token: '',
type: 'ChromePush',
web_auth: 'w3cAuth',
web_p256: 'w3cP256dh',
},
],
});
await promise;

// accepting permissions patches the existing subscription with the token
await vi.waitUntil(
() => updateSubscriptionFn.mock.calls.length >= 1,
{
interval: 1,
},
);

let pushSub: SubscriptionSchema | undefined;
await vi.waitUntil(
Expand Down Expand Up @@ -1287,4 +1297,4 @@
);

const showLocalNotificationSpy = vi.spyOn(MainHelper, 'showLocalNotification');
showLocalNotificationSpy.mockImplementation(async () => {});

Check warning on line 1300 in src/onesignal/OneSignal.test.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected empty async arrow function
66 changes: 0 additions & 66 deletions src/onesignal/userDirector.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/page/bell/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ export default class Message {
}

async _display(type: string, content: string, duration = 0) {
console.log('zzzzzdisplay', type, content, duration);
Log._debug(`Calling display(${type}, ${content}, ${duration}).`);
if (this._shown) await this._hide();
this._content = decodeHtmlEntities(content);
Expand Down
75 changes: 60 additions & 15 deletions src/page/managers/LoginManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
import { updateIdentityModel } from '__test__/support/helpers/setup';
import { SubscriptionModel } from 'src/core/models/SubscriptionModel';
import { BaseSubscriptionOperation } from 'src/core/operations/BaseSubscriptionOperation';
import { db } from 'src/shared/database/client';
import Log from 'src/shared/libraries/Log';
import * as userDirector from '../../onesignal/userDirector';
import LoginManager from './LoginManager';

const createUserOnServerSpy = vi.spyOn(userDirector, 'createUserOnServer');

describe('LoginManager', () => {
beforeEach(() => {
TestEnvironment.initialize();
Expand Down Expand Up @@ -52,6 +50,48 @@ describe('LoginManager', () => {
expect(enqueueAndWaitSpy).toHaveBeenCalled();
});

test('login: with existing push sub enqueues transfer operation', async () => {
const mockPushSub = { id: 'push-sub-id' } as SubscriptionModel;
vi.spyOn(
OneSignal._coreDirector,
'_getPushSubscriptionModel',
).mockResolvedValue(mockPushSub);
const enqueueSpy = vi.spyOn(
OneSignal._coreDirector._operationRepo,
'_enqueue',
);
vi.spyOn(
OneSignal._coreDirector._operationRepo,
'_enqueueAndWait',
).mockResolvedValue(undefined);

await LoginManager.login('new-id');

expect(enqueueSpy).toHaveBeenCalled();
const transferOp = enqueueSpy.mock.calls[0][0] as BaseSubscriptionOperation;
expect(transferOp._subscriptionId).toBe('push-sub-id');
});

test('login: without push sub creates new subscription model', async () => {
vi.spyOn(
OneSignal._coreDirector,
'_getPushSubscriptionModel',
).mockResolvedValue(undefined);
vi.spyOn(
OneSignal._coreDirector._operationRepo,
'_enqueueAndWait',
).mockResolvedValue(undefined);
const addSpy = vi.spyOn(
OneSignal._coreDirector._subscriptionModelStore,
'_add',
);

await LoginManager.login('new-id');

expect(addSpy).toHaveBeenCalled();
expect(addSpy.mock.calls[0][0].token).toBe('');
});

test('logout: no external id logs debug and returns', async () => {
const debugSpy = vi
.spyOn(Log, '_debug')
Expand All @@ -63,21 +103,26 @@ describe('LoginManager', () => {
);
});

test('logout: with external id and no subscriptions resets models and skips user creation', async () => {
test('logout: with external id and push sub enqueues transfer and login operations', async () => {
await updateIdentityModel('external_id', 'abc');
await LoginManager.logout();
expect(createUserOnServerSpy).not.toHaveBeenCalled();
});

test('logout: with external id and a subscription resets models and creates anonymous user', async () => {
await updateIdentityModel('external_id', 'abc');
const mockSub = { id: 'sub-id' } as SubscriptionModel;
const mockPushSub = { id: 'sub-id' } as SubscriptionModel;
vi.spyOn(
OneSignal._coreDirector._subscriptionModelStore,
'_list',
).mockReturnValue([mockSub]);
OneSignal._coreDirector,
'_getPushSubscriptionModel',
).mockResolvedValue(mockPushSub);
const enqueueSpy = vi.spyOn(
OneSignal._coreDirector._operationRepo,
'_enqueue',
);
vi.spyOn(
OneSignal._coreDirector._operationRepo,
'_enqueueAndWait',
).mockResolvedValue(undefined);

await LoginManager.logout();
expect(createUserOnServerSpy).toHaveBeenCalled();

expect(enqueueSpy).toHaveBeenCalled();
const transferOp = enqueueSpy.mock.calls[0][0] as BaseSubscriptionOperation;
expect(transferOp._subscriptionId).toBe('sub-id');
});
});
Loading
Loading