Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Update trial logic to be more generic for multiple products #2653

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions src/chrome/create-chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { PUBLIC_EVENTS } from '../utils/consts';
import { usePendoFeedback } from '../components/Feedback';
import { middlewareListener } from '../redux/redux-config';
import { clearAnsibleTrialFlag, isAnsibleTrialFlagActive, setAnsibleTrialFlag } from '../utils/isAnsibleTrialFlagActive';
import { clearProductTrialFlag, isProductTrialFlagActive, setProductTrialFlag } from '../utils/isProductTrialFlagActive';
import chromeHistory from '../utils/chromeHistory';
import { ReduxState } from '../redux/store';
import { STORE_INITIAL_HASH } from '../redux/action-types';
Expand Down Expand Up @@ -162,6 +163,9 @@ export const createChromeContext = ({
clearAnsibleTrialFlag,
isAnsibleTrialFlagActive,
setAnsibleTrialFlag,
clearProductTrialFlag,
isProductTrialFlagActive,
setProductTrialFlag,
chromeHistory,
analytics: analytics!,
// FIXME: Update types once merged
Expand Down
18 changes: 14 additions & 4 deletions src/jwt/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logger from './logger';
import { SSOParsedToken } from './Priv';
import { ChromeUser } from '@redhat-cloud-services/types';
import { isAnsibleTrialFlagActive } from '../utils/isAnsibleTrialFlagActive';
import { isProductTrialFlagActive } from '../utils/isProductTrialFlagActive';
import chromeHistory from '../utils/chromeHistory';
import { createUser } from '../cognito/auth';

Expand Down Expand Up @@ -31,6 +32,7 @@ const pathMapper = {
settings: 'settings',
'user-preferences': 'user_preferences',
internal: 'internal',
'application-services': 'application_services',
};

const REDIRECT_BASE = `${document.location.origin}${isBeta() ? getRouterBasename() : ''}`;
Expand Down Expand Up @@ -100,10 +102,11 @@ export function tryBounceIfUnentitled(
| {
[key: string]: SSOServiceDetails;
},
section: string
pathName: string[]
) {
// only test this on the apps that are in valid sections
// we need to keep /apps and other things functional
const section = pathName[0];
if (
section !== 'insights' &&
section !== 'openshift' &&
Expand All @@ -112,7 +115,8 @@ export function tryBounceIfUnentitled(
section !== 'ansible' &&
section !== 'subscriptions' &&
section !== 'user-preferences' &&
section !== 'internal'
section !== 'internal' &&
section !== 'application-services'
) {
return;
}
Expand All @@ -123,6 +127,12 @@ export function tryBounceIfUnentitled(
return;
}

const productActive = pathName.some((path) => isProductTrialFlagActive(path));
// test temporary product trial flag
if (productActive) {
return;
}

// do not show not entitled modal repeadly for the same section
if (bounceInvocationLock[section]) {
return;
Expand Down Expand Up @@ -225,7 +235,7 @@ export default async (token: SSOParsedToken): Promise<ChromeUser | void> => {
// we "force" a bounce here because the entitlements API
// was never called
if (!isValidAccountNumber(user.identity.account_number)) {
tryBounceIfUnentitled(true, pathName[0]);
tryBounceIfUnentitled(true, pathName);
// always return user regardless of the entitlements result
// required for insights accounts with invalid account number
return {
Expand All @@ -234,7 +244,7 @@ export default async (token: SSOParsedToken): Promise<ChromeUser | void> => {
};
}

tryBounceIfUnentitled(data as unknown as { [key: string]: SSOServiceDetails }, pathName[0]);
tryBounceIfUnentitled(data as unknown as { [key: string]: SSOServiceDetails }, pathName);

return {
...user,
Expand Down
20 changes: 10 additions & 10 deletions src/jwt/user.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ describe('User', () => {

test('should *not* bounce if the section is unkown', () => {
ents.insights.is_entitled = false;
user.tryBounceIfUnentitled(ents, 'apps');
user.tryBounceIfUnentitled(ents, 'foo');
user.tryBounceIfUnentitled(ents, 'test');
user.tryBounceIfUnentitled(ents, ['apps']);
user.tryBounceIfUnentitled(ents, ['foo']);
user.tryBounceIfUnentitled(ents, ['test']);
expect(replaceMock).not.toBeCalled();
});

test('should bounce if unentitled', () => {
const historySpy = jest.spyOn(chromeHistory.default, 'replace');
user.tryBounceIfUnentitled(ents, 'insights');
user.tryBounceIfUnentitled(ents, ['insights']);
expect(historySpy).lastCalledWith({ pathname: '/', search: '?not_entitled=insights' });

user.tryBounceIfUnentitled(ents, 'cost-management');
user.tryBounceIfUnentitled(ents, ['cost-management']);
expect(historySpy).lastCalledWith({ pathname: '/', search: '?not_entitled=cost_management' });

user.tryBounceIfUnentitled(ents, 'ansible');
user.tryBounceIfUnentitled(ents, ['ansible']);
expect(historySpy).lastCalledWith({ pathname: '/ansible/ansible-dashboard/trial', search: '' });
historySpy.mockRestore();
});
Expand All @@ -78,12 +78,12 @@ describe('User', () => {
setAnsibleTrialFlag(Date.now());
// advance time by one minute. user should not be bounced
jest.advanceTimersByTime(1 * 60 * 1000);
user.tryBounceIfUnentitled(ents, 'ansible');
user.tryBounceIfUnentitled(ents, ['ansible']);
expect(historySpy).not.toBeCalled();

// advace time by additional 10 minutes. user should be bounced to /trial/expired
jest.advanceTimersByTime(10 * 60 * 1000);
user.tryBounceIfUnentitled(ents, 'ansible');
user.tryBounceIfUnentitled(ents, ['ansible']);
expect(historySpy).toBeCalledTimes(1);
expect(historySpy).toHaveBeenLastCalledWith({ pathname: '/ansible/ansible-dashboard/trial/expired', search: '' });
historySpy.mockClear();
Expand All @@ -95,7 +95,7 @@ describe('User', () => {
is_entitled: true,
},
},
'ansible'
['ansible']
);
expect(historySpy).not.toBeCalled();

Expand All @@ -105,7 +105,7 @@ describe('User', () => {

test('should *not* bounce if entitled', () => {
ents.insights.is_entitled = true;
user.tryBounceIfUnentitled(ents, 'insights');
user.tryBounceIfUnentitled(ents, ['insights']);
expect(replaceMock).not.toBeCalled();
});
});
Expand Down
32 changes: 5 additions & 27 deletions src/utils/isAnsibleTrialFlagActive.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import logger from '../jwt/logger';
import * as productTrialFlagUtils from './isProductTrialFlagActive';

export const ANSIBLE_TRIAL_FLAG = 'chrome.ansible.trial';
const TRIAL_DURATION = 10 * 60 * 1000; // 10 minutes
export const ANSIBLE_TRIAL_FLAG = 'ansible';

const log = logger('Ansible trial invalidation');
export const isAnsibleTrialFlagActive = () => productTrialFlagUtils.isProductTrialFlagActive(ANSIBLE_TRIAL_FLAG);

export const isAnsibleTrialFlagActive = () => {
let expiration: string | number | null = localStorage.getItem(ANSIBLE_TRIAL_FLAG);
if (expiration) {
try {
expiration = parseInt(expiration);
if (isNaN(expiration)) {
// expiration is not a valid number
return false;
}
export const setAnsibleTrialFlag = () => productTrialFlagUtils.setProductTrialFlag(ANSIBLE_TRIAL_FLAG);

return expiration + TRIAL_DURATION > Date.now();
} catch (error) {
log(`Enable to parse ansible trial flag expiration: ${error}`);
}
}
};

export const setAnsibleTrialFlag = () => {
localStorage.setItem(ANSIBLE_TRIAL_FLAG, Date.now().toString());
};

export const clearAnsibleTrialFlag = () => {
localStorage.removeItem(ANSIBLE_TRIAL_FLAG);
};
export const clearAnsibleTrialFlag = () => productTrialFlagUtils.clearProductTrialFlag(ANSIBLE_TRIAL_FLAG);
34 changes: 34 additions & 0 deletions src/utils/isProductTrialFlagActive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import logger from '../jwt/logger';

const TRIAL_DURATION = 10 * 60 * 1000; // 10 minutes

const log = logger('Product trial invalidation');

export const isProductTrialFlagActive = (flag: string) => {
let expiration: string | number | null = localStorage.getItem(`chrome.${flag}.trial`);
if (expiration) {
try {
expiration = parseInt(expiration);
if (isNaN(expiration)) {
// expiration is not a valid number
return false;
}

return expiration + TRIAL_DURATION > Date.now();
} catch (error) {
log(`Unable to parse ${flag} trial expiration: ${error}`);
}
}
};

/**
* Sets a product trial flag to the current date/time in localstorage
* @param flag string representation of the product this flag pertains to. This exact string should appear in the URL path of the product being enable
*/
export const setProductTrialFlag = (flag: string) => {
localStorage.setItem(`chrome.${flag}.trial`, Date.now().toString());
};

export const clearProductTrialFlag = (flag: string) => {
localStorage.removeItem(`chrome.${flag}.trial`);
};