Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

svcid checks for enabling notifications #152

Merged
merged 7 commits into from
Jul 29, 2020
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
91 changes: 78 additions & 13 deletions src/omnibar/omnibar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//#region imports

import {
BBOmnibar
} from './omnibar';
Expand Down Expand Up @@ -64,8 +62,6 @@ import {
BBOmnibarToastContainerInitArgs
} from './omnibar-toast-container-init-args';

//#endregion

describe('Omnibar', () => {
const BASE_URL = 'about:blank';

Expand All @@ -85,6 +81,12 @@ describe('Omnibar', () => {
BBOmnibar.load(config);
}

function loadOmnibarWithNotifications(): void {
loadOmnibar({
svcId: 'renxt'
});
}

function getIframeEl(): HTMLIFrameElement {
return document.querySelector('.sky-omnibar-iframe') as HTMLIFrameElement;
}
Expand Down Expand Up @@ -651,7 +653,7 @@ describe('Omnibar', () => {

getTokenFake = () => Promise.resolve(testTokenWithNotificationEntitlement);

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'ready'
Expand Down Expand Up @@ -1166,7 +1168,7 @@ describe('Omnibar', () => {
cb(testNotifications);
});

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'ready'
Expand All @@ -1178,7 +1180,7 @@ describe('Omnibar', () => {
refreshUserCallback();
});

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'get-token',
Expand Down Expand Up @@ -1214,7 +1216,7 @@ describe('Omnibar', () => {
});
});

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'get-token',
Expand All @@ -1240,7 +1242,7 @@ describe('Omnibar', () => {
done();
});

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'get-token',
Expand All @@ -1261,7 +1263,7 @@ describe('Omnibar', () => {

getTokenFake = () => Promise.resolve(testTokenWithoutNotificationEntitlement);

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'get-token',
Expand All @@ -1284,14 +1286,77 @@ describe('Omnibar', () => {

getTokenFake = () => Promise.resolve(testTokenWithNotificationEntitlement);

loadOmnibar();
loadOmnibarWithNotifications();

fireMessageEvent({
messageType: 'get-token',
tokenRequestId: 123
});
});

describe('when connecting to notifications', () => {

function testSvcId(
svcId: string,
tokenSpyCalled: boolean,
notificationsInitialized: boolean
): Promise<void> {
return new Promise((resolve) => {
startTrackingSpy.and.callFake((refreshUserCallback: () => void) => {
refreshUserCallback();

setTimeout(() => {
let allArgsExpectation = expect(getTokenSpy.calls.allArgs());
if (!tokenSpyCalled) {
allArgsExpectation = allArgsExpectation.not;
}
allArgsExpectation.toContain(
[{
disableRedirect: true,
envId: 'envid',
leId: 'leid',
permissionScope: 'Notifications'
}]
);

let toastContainerExpectation = expect(toastContainerInitSpy);
if (!notificationsInitialized) {
toastContainerExpectation = toastContainerExpectation.not;
}
toastContainerExpectation.toHaveBeenCalled();

getTokenSpy.calls.reset();
toastContainerInitSpy.calls.reset();
destroyOmnibar();

resolve();
});
});

loadOmnibar({
envId: 'envid',
leId: 'leid',
svcId
});

fireMessageEvent({
messageType: 'get-token',
tokenRequestId: 123
});
});
}

it('should respect the notification settings for a given service ID', async () => {
await testSvcId('renxt', true, true);
await testSvcId('fenxt', true, true);
await testSvcId('skydev', false, true);
await testSvcId('skydevhome', false, true);
await testSvcId('skyux', false, true);
await testSvcId('other', false, false);
});

});

});

describe('pushNotificationsEnabled() method', () => {
Expand All @@ -1305,7 +1370,7 @@ describe('Omnibar', () => {
});

it('should check the token for the notif entitlement', (done) => {
loadOmnibar();
loadOmnibarWithNotifications();

getTokenFake = () => Promise.resolve(testTokenWithoutNotificationEntitlement);

Expand All @@ -1324,7 +1389,7 @@ describe('Omnibar', () => {
});

it('should return false if retrieving a token fails', (done) => {
loadOmnibar();
loadOmnibarWithNotifications();

getTokenFake = () => Promise.reject('The user is not logged in');

Expand Down
72 changes: 49 additions & 23 deletions src/omnibar/omnibar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//#region imports

import * as jwtDecode from 'jwt-decode';

import {
Expand Down Expand Up @@ -62,11 +60,31 @@ import {
BBOmnibarThemeAccent
} from './theming';

//#endregion

const CLS_EXPANDED = 'sky-omnibar-iframe-expanded';
const CLS_LOADING = 'sky-omnibar-loading';

const notificationSvcIds: {
[key: string]: {
requiresNotif: boolean
}
} = {
fenxt: {
requiresNotif: true
},
renxt: {
requiresNotif: true
},
skydev: {
requiresNotif: false
},
skydevhome: {
requiresNotif: false
},
skyux: {
requiresNotif: false
}
};

let envEl: HTMLDivElement;
let placeholderEl: HTMLDivElement;
let styleEl: HTMLStyleElement;
Expand Down Expand Up @@ -633,26 +651,34 @@ export class BBOmnibar {
return Promise.resolve(false);
}

return BBAuth.getToken({
disableRedirect: true,
envId: omnibarConfig.envId,
leId: omnibarConfig.leId,
permissionScope: 'Notifications'
}).then(
(token) => {
const decodedToken: any = jwtDecode(token);
let entitlements: string | string[] = decodedToken['1bb.entitlements'];

if (entitlements) {
entitlements = Array.isArray(entitlements) ? entitlements : [entitlements];
return (entitlements as string[]).indexOf('notif') > -1;
}

return false;
if (notificationSvcIds[omnibarConfig.svcId]) {
if (notificationSvcIds[omnibarConfig.svcId].requiresNotif) {
return BBAuth.getToken({
disableRedirect: true,
envId: omnibarConfig.envId,
leId: omnibarConfig.leId,
permissionScope: 'Notifications'
}).then(
(token) => {
const decodedToken: any = jwtDecode(token);
let entitlements: string | string[] = decodedToken['1bb.entitlements'];

if (entitlements) {
entitlements = Array.isArray(entitlements) ? entitlements : [entitlements];
return (entitlements as string[]).indexOf('notif') > -1;
}

return false;
}
).catch(() => {
return false;
});
} else {
return Promise.resolve(true);
}
).catch(() => {
return false;
});
}

return Promise.resolve(false);
}

public static destroy(): void {
Expand Down