@amedia/user 0.13.35 → 0.13.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/user.cjs +1 -1
- package/dist/user.cjs.map +3 -3
- package/dist/user.d.ts +12 -0
- package/dist/user.js +1 -1
- package/dist/user.js.map +3 -3
- package/package.json +5 -2
- package/.codeclimate.yml +0 -8
- package/.eslintignore +0 -2
- package/.eslintrc +0 -11
- package/.prettierrc.json +0 -1
- package/@types/amedia-frontend-rpc.d.ts +0 -11
- package/@types/browserid.d.ts +0 -4
- package/build.js +0 -28
- package/checkVersion.cjs +0 -12
- package/cloudbuild.json +0 -29
- package/cop.json +0 -8
- package/dist/actions/authentication.d.ts +0 -4
- package/dist/actions/authentication.spec.d.ts +0 -1
- package/dist/actions/authorization.d.ts +0 -14
- package/dist/actions/authorization.spec.d.ts +0 -4
- package/dist/actions/preferences.d.ts +0 -9
- package/dist/actions/preferences.spec.d.ts +0 -1
- package/dist/actions/storage.d.ts +0 -7
- package/dist/actions/userData.d.ts +0 -2
- package/dist/actions/userData.spec.d.ts +0 -1
- package/dist/autologin/index.d.ts +0 -7
- package/dist/autologin/index.spec.d.ts +0 -1
- package/dist/autologin/thirdPartyCookie.d.ts +0 -3
- package/dist/autologin/thirdPartyCookie.spec.d.ts +0 -1
- package/dist/cache/engines/global.d.ts +0 -24
- package/dist/cache/engines/global.spec.d.ts +0 -1
- package/dist/cache/index.d.ts +0 -11
- package/dist/cache/index.spec.d.ts +0 -1
- package/dist/cache/types.d.ts +0 -11
- package/dist/clients/europa.d.ts +0 -24
- package/dist/clients/europa.spec.d.ts +0 -1
- package/dist/clients/jupiter.d.ts +0 -30
- package/dist/clients/jupiter.spec.d.ts +0 -1
- package/dist/clients/mercury.d.ts +0 -3
- package/dist/clients/mercury.spec.d.ts +0 -1
- package/dist/clients/mercuryLog.d.ts +0 -8
- package/dist/clients/mercuryLog.spec.d.ts +0 -1
- package/dist/clients/nebula.d.ts +0 -2
- package/dist/clients/nebula.spec.d.ts +0 -1
- package/dist/clients/pluto.d.ts +0 -4
- package/dist/clients/pluto.spec.d.ts +0 -1
- package/dist/eventHandlers/activation.d.ts +0 -15
- package/dist/eventHandlers/authentication.d.ts +0 -17
- package/dist/eventHandlers/eventMap.d.ts +0 -29
- package/dist/eventHandlers/state.d.ts +0 -1
- package/dist/eventHandlers/state.spec.d.ts +0 -1
- package/dist/eventHandlers/storage.d.ts +0 -1
- package/dist/eventHandlers/storage.spec.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.spec.d.ts +0 -1
- package/dist/exceptions/PaywallError.d.ts +0 -8
- package/dist/index-authentcation.spec.d.ts +0 -1
- package/dist/index-storage.spec.d.ts +0 -1
- package/dist/index-subscription.spec.d.ts +0 -1
- package/dist/index.d.ts +0 -20
- package/dist/janitor/index.d.ts +0 -10
- package/dist/janitor/index.spec.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
- package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
- package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
- package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
- package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
- package/dist/lib/UserDataRequest.d.ts +0 -22
- package/dist/lib/UserDataRequest.spec.d.ts +0 -1
- package/dist/lib/cookie.d.ts +0 -9
- package/dist/lib/rpc.d.ts +0 -6
- package/dist/store/index.d.ts +0 -23
- package/dist/store/index.spec.d.ts +0 -1
- package/dist/types.d.ts +0 -22
- package/dist/utils/customEventWrapper.d.ts +0 -19
- package/dist/utils/debouncer.d.ts +0 -26
- package/dist/utils/debouncer.spec.d.ts +0 -1
- package/dist/utils/deepEquals.d.ts +0 -1
- package/dist/utils/expectBootLogs.d.ts +0 -2
- package/dist/utils/getOrUpdate.d.ts +0 -6
- package/dist/utils/getOrUpdate.spec.d.ts +0 -1
- package/dist/utils/localhostCheck.d.ts +0 -1
- package/dist/utils/location.d.ts +0 -3
- package/dist/utils/log.d.ts +0 -9
- package/dist/utils/log.spec.d.ts +0 -1
- package/dist/utils/loginState.d.ts +0 -2
- package/dist/utils/networkError.d.ts +0 -1
- package/dist/utils/queue.d.ts +0 -12
- package/dist/utils/type.d.ts +0 -6
- package/dist/utils/url.d.ts +0 -6
- package/dist/utils/url.spec.d.ts +0 -1
- package/eik.json +0 -10
- package/global.d.ts +0 -1
- package/jest.config.js +0 -7
- package/setupJest.js +0 -1
- package/src/actions/authentication.spec.ts +0 -51
- package/src/actions/authentication.ts +0 -43
- package/src/actions/authorization.spec.ts +0 -369
- package/src/actions/authorization.ts +0 -117
- package/src/actions/preferences.spec.ts +0 -41
- package/src/actions/preferences.ts +0 -41
- package/src/actions/storage.ts +0 -22
- package/src/actions/userData.spec.ts +0 -133
- package/src/actions/userData.ts +0 -68
- package/src/autologin/index.spec.ts +0 -239
- package/src/autologin/index.ts +0 -92
- package/src/autologin/thirdPartyCookie.spec.ts +0 -95
- package/src/autologin/thirdPartyCookie.ts +0 -55
- package/src/cache/engines/global.spec.ts +0 -62
- package/src/cache/engines/global.ts +0 -48
- package/src/cache/index.spec.ts +0 -130
- package/src/cache/index.ts +0 -48
- package/src/cache/types.ts +0 -12
- package/src/clients/europa.spec.ts +0 -209
- package/src/clients/europa.ts +0 -183
- package/src/clients/jupiter.spec.ts +0 -179
- package/src/clients/jupiter.ts +0 -119
- package/src/clients/mercury.spec.ts +0 -59
- package/src/clients/mercury.ts +0 -48
- package/src/clients/mercuryLog.spec.ts +0 -20
- package/src/clients/mercuryLog.ts +0 -67
- package/src/clients/nebula.spec.ts +0 -70
- package/src/clients/nebula.ts +0 -83
- package/src/clients/pluto.spec.ts +0 -165
- package/src/clients/pluto.ts +0 -118
- package/src/eventHandlers/activation.ts +0 -35
- package/src/eventHandlers/authentication.ts +0 -34
- package/src/eventHandlers/eventMap.ts +0 -34
- package/src/eventHandlers/state.spec.ts +0 -48
- package/src/eventHandlers/state.ts +0 -23
- package/src/eventHandlers/storage.spec.ts +0 -114
- package/src/eventHandlers/storage.ts +0 -45
- package/src/eventHandlers/userAttributes.spec.ts +0 -93
- package/src/eventHandlers/userAttributes.ts +0 -31
- package/src/exceptions/PaywallError.ts +0 -16
- package/src/index-authentcation.spec.ts +0 -26
- package/src/index-storage.spec.ts +0 -144
- package/src/index-subscription.spec.ts +0 -213
- package/src/index.ts +0 -59
- package/src/janitor/index.spec.ts +0 -18
- package/src/janitor/index.ts +0 -59
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
- package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
- package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
- package/src/janitor/jobs/daxsubCookie.ts +0 -38
- package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
- package/src/janitor/jobs/removeUserStorage.ts +0 -12
- package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
- package/src/lib/PaywallUnlockRequest.ts +0 -143
- package/src/lib/UserDataRequest.spec.ts +0 -77
- package/src/lib/UserDataRequest.ts +0 -213
- package/src/lib/cookie.ts +0 -121
- package/src/lib/rpc.ts +0 -19
- package/src/store/index.spec.ts +0 -49
- package/src/store/index.ts +0 -64
- package/src/types.ts +0 -22
- package/src/utils/customEventWrapper.ts +0 -58
- package/src/utils/debouncer.spec.ts +0 -252
- package/src/utils/debouncer.ts +0 -123
- package/src/utils/deepEquals.ts +0 -42
- package/src/utils/expectBootLogs.ts +0 -9
- package/src/utils/getOrUpdate.spec.ts +0 -166
- package/src/utils/getOrUpdate.ts +0 -66
- package/src/utils/localhostCheck.ts +0 -9
- package/src/utils/location.ts +0 -31
- package/src/utils/log.spec.ts +0 -148
- package/src/utils/log.ts +0 -124
- package/src/utils/loginState.ts +0 -24
- package/src/utils/networkError.ts +0 -11
- package/src/utils/queue.ts +0 -32
- package/src/utils/type.ts +0 -19
- package/src/utils/url.spec.ts +0 -33
- package/src/utils/url.ts +0 -35
- package/testSetup.js +0 -19
- package/tsconfig.eslint.json +0 -24
- package/tsconfig.json +0 -18
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import daxSubCookieHandler from './daxsubCookie';
|
|
2
|
-
import * as cookie from '../../lib/cookie';
|
|
3
|
-
import store from '../../store';
|
|
4
|
-
import { listenForStateRequests } from '../../eventHandlers/state';
|
|
5
|
-
import { listenForUserAttributeRequests } from '../../eventHandlers/userAttributes';
|
|
6
|
-
import UserDataRequest from '../../lib/UserDataRequest';
|
|
7
|
-
import log from '../../utils/log';
|
|
8
|
-
|
|
9
|
-
describe('daxSubCookieHandler', () => {
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
store.clear();
|
|
12
|
-
spyOn(cookie, 'setCookie');
|
|
13
|
-
spyOn(cookie, 'removeCookie');
|
|
14
|
-
|
|
15
|
-
listenForStateRequests();
|
|
16
|
-
listenForUserAttributeRequests();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe('when fetching user data fails', () => {
|
|
20
|
-
beforeEach(async () => {
|
|
21
|
-
spyOn(UserDataRequest.prototype, 'fetch').and.returnValue(
|
|
22
|
-
Promise.reject(new Error('Could not fetch'))
|
|
23
|
-
);
|
|
24
|
-
spyOn(log, 'error');
|
|
25
|
-
await daxSubCookieHandler();
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should log error when fetch fails', () => {
|
|
29
|
-
expect(log.error).toHaveBeenCalledWith(
|
|
30
|
-
`Setting daxsub cookie failed. Error: Could not fetch`,
|
|
31
|
-
new Error('Could not fetch')
|
|
32
|
-
);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe('when user is not logged in', () => {
|
|
37
|
-
beforeEach(async () => {
|
|
38
|
-
store.set('state', { isLoggedIn: false });
|
|
39
|
-
store.set('userAttributes', {
|
|
40
|
-
uuid: null,
|
|
41
|
-
trackingKey: null,
|
|
42
|
-
extraData: {},
|
|
43
|
-
});
|
|
44
|
-
await daxSubCookieHandler();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('Should call removeCookie', () => {
|
|
48
|
-
expect(cookie.removeCookie).toHaveBeenCalled();
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('should not call setCookie', () => {
|
|
52
|
-
expect(cookie.setCookie).not.toHaveBeenCalled();
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe('when user is logged in', () => {
|
|
57
|
-
beforeEach(async () => {
|
|
58
|
-
store.set('state', { isLoggedIn: true });
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
describe('when has subscription', () => {
|
|
62
|
-
beforeEach(async () => {
|
|
63
|
-
store.set('userAttributes', {
|
|
64
|
-
uuid: 'uuid',
|
|
65
|
-
extraData: {
|
|
66
|
-
customer_keys: 'someString',
|
|
67
|
-
},
|
|
68
|
-
trackingKey: '148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
69
|
-
});
|
|
70
|
-
await daxSubCookieHandler();
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('Should not call removeCookie', () => {
|
|
74
|
-
expect(cookie.removeCookie).not.toHaveBeenCalled();
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('Should set cookie with a_sub_status active', () => {
|
|
78
|
-
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
79
|
-
'daxsub',
|
|
80
|
-
'a_sub_status%3Dactive%26a_user_key%3D148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
81
|
-
1
|
|
82
|
-
);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
describe('when has no subscription', () => {
|
|
87
|
-
beforeEach(async () => {
|
|
88
|
-
store.set('userAttributes', {
|
|
89
|
-
uuid: 'uuid',
|
|
90
|
-
extraData: {},
|
|
91
|
-
trackingKey: '148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
92
|
-
});
|
|
93
|
-
await daxSubCookieHandler();
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('Should not call removeCookie', () => {
|
|
97
|
-
expect(cookie.removeCookie).not.toHaveBeenCalled();
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('Should set cookie with a_sub_status null', () => {
|
|
101
|
-
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
102
|
-
'daxsub',
|
|
103
|
-
'a_sub_status%3Dnull%26a_user_key%3D148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
104
|
-
1
|
|
105
|
-
);
|
|
106
|
-
});
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { setCookie, removeCookie } from '../../lib/cookie';
|
|
2
|
-
import UserDataRequest from '../../lib/UserDataRequest';
|
|
3
|
-
import log from '../../utils/log';
|
|
4
|
-
import { UserAttributes } from '../../types';
|
|
5
|
-
|
|
6
|
-
export function getCookieValue(attributes: UserAttributes) {
|
|
7
|
-
// If user has the customer_keys field set, assume its a paying subscriber and set subscriptionStatus = active.
|
|
8
|
-
const subscriptionStatus = attributes.extraData?.customer_keys
|
|
9
|
-
? 'active'
|
|
10
|
-
: 'null';
|
|
11
|
-
|
|
12
|
-
const cookieValue = `a_sub_status=${subscriptionStatus}&a_user_key=${attributes.trackingKey}`;
|
|
13
|
-
return cookieValue;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function setDaxsubCookie(attributes: UserAttributes) {
|
|
17
|
-
const name = 'daxsub';
|
|
18
|
-
const cookieValue = getCookieValue(attributes);
|
|
19
|
-
const cookieValueEncoded = encodeURIComponent(cookieValue);
|
|
20
|
-
const validTime = 1; // 1 day (24 hours);
|
|
21
|
-
setCookie(name, cookieValueEncoded, validTime);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export default function daxSubCookieHandler() {
|
|
25
|
-
return new UserDataRequest()
|
|
26
|
-
.withAttributes(['trackingKey', 'extraData'])
|
|
27
|
-
.fetch()
|
|
28
|
-
.then(({ attributes, state }) => {
|
|
29
|
-
if (!state.isLoggedIn) {
|
|
30
|
-
removeCookie('daxsub');
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
setDaxsubCookie(attributes);
|
|
34
|
-
})
|
|
35
|
-
.catch((e) => {
|
|
36
|
-
log.error(`Setting daxsub cookie failed. Error: ${e.message}`, e);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import store from '../../store';
|
|
2
|
-
import { removeUserStorage } from './removeUserStorage';
|
|
3
|
-
import { listenForStateRequests } from '../../eventHandlers/state';
|
|
4
|
-
|
|
5
|
-
describe('removeUserStorage', () => {
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
store.clear();
|
|
8
|
-
spyOn(store, 'set').and.callThrough();
|
|
9
|
-
listenForStateRequests();
|
|
10
|
-
removeUserStorage();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe('when logged in', () => {
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
store.set('state', { isLoggedIn: true });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('should not reset stores', () => {
|
|
19
|
-
expect(store.set).not.toHaveBeenCalledWith('userAttributes', {});
|
|
20
|
-
expect(store.set).not.toHaveBeenCalledWith('storage', {});
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe('when not logged in', () => {
|
|
25
|
-
beforeEach(() => {
|
|
26
|
-
store.set('state', { isLoggedIn: false });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should reset stores', () => {
|
|
30
|
-
expect(store.set).toHaveBeenCalledWith('userAttributes', {});
|
|
31
|
-
expect(store.set).toHaveBeenCalledWith('storage', {});
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import UserDataRequest from '../../lib/UserDataRequest';
|
|
2
|
-
import store from '../../store';
|
|
3
|
-
|
|
4
|
-
export function removeUserStorage() {
|
|
5
|
-
new UserDataRequest().subscribe(({ state }) => {
|
|
6
|
-
// Need to explicitly check for false here until we have then()
|
|
7
|
-
if (state.isLoggedIn === false) {
|
|
8
|
-
store.set('userAttributes', {});
|
|
9
|
-
store.set('storage', {});
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
}
|
|
@@ -1,363 +0,0 @@
|
|
|
1
|
-
import PaywallUnlockRequest, { UnlockResponse } from './PaywallUnlockRequest';
|
|
2
|
-
import * as authorization from '../actions/authorization';
|
|
3
|
-
import * as location from '../utils/location';
|
|
4
|
-
import * as autologin from '../autologin';
|
|
5
|
-
import * as localhostCheck from '../utils/localhostCheck';
|
|
6
|
-
import store from '../store';
|
|
7
|
-
import PaywallError from '../exceptions/PaywallError';
|
|
8
|
-
import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
|
|
9
|
-
import { listenForStateRequests } from '../eventHandlers/state';
|
|
10
|
-
import { removeAllCookies } from './cookie';
|
|
11
|
-
import spyOn = jest.spyOn;
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
store.clear();
|
|
15
|
-
|
|
16
|
-
removeAllCookies();
|
|
17
|
-
|
|
18
|
-
listenForStateRequests();
|
|
19
|
-
listenForUserAttributeRequests();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
function itShouldNotReload() {
|
|
23
|
-
it('should not reload', () => {
|
|
24
|
-
expect(location.reload).not.toHaveBeenCalled();
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function itShouldNotRedirect() {
|
|
29
|
-
it('should not redirect', () => {
|
|
30
|
-
expect(location.replace).not.toHaveBeenCalled();
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function itShouldNotAttemptActivation() {
|
|
35
|
-
it('should not attempt activation', () => {
|
|
36
|
-
expect(authorization.activate).not.toHaveBeenCalled();
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function itShouldAttemptActivationWithAccessFeatures(accessFeatures: string[]) {
|
|
41
|
-
it('should attempt activation with access features', () => {
|
|
42
|
-
expect(authorization.activate).toHaveBeenCalledWith(accessFeatures);
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
describe('PaywallUnlocker', () => {
|
|
47
|
-
beforeEach(() => {
|
|
48
|
-
spyOn(autologin, 'attemptPaywallAutoLogin').mockResolvedValue();
|
|
49
|
-
spyOn(authorization, 'setHasPerformedPaywallReload');
|
|
50
|
-
spyOn(authorization, 'activate');
|
|
51
|
-
spyOn(location, 'reload').mockReturnValue();
|
|
52
|
-
spyOn(location, 'replace').mockReturnValue();
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('When running on localhost', () => {
|
|
56
|
-
beforeEach(() => {
|
|
57
|
-
jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('should fail with expected error', async () => {
|
|
61
|
-
try {
|
|
62
|
-
await new PaywallUnlockRequest().tryUnlock();
|
|
63
|
-
} catch (e) {
|
|
64
|
-
expect(e).toEqual(
|
|
65
|
-
new Error('@amedia/user cannot unlock paywall on localhost')
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe('When not logged in', () => {
|
|
72
|
-
let unlockResponse: UnlockResponse;
|
|
73
|
-
beforeEach(async () => {
|
|
74
|
-
store.set('state', { isLoggedIn: false });
|
|
75
|
-
store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
|
|
76
|
-
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('should attempt autologin', async () => {
|
|
80
|
-
expect(autologin.attemptPaywallAutoLogin).toHaveBeenCalledTimes(1);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should return no access', () => {
|
|
84
|
-
expect(unlockResponse).toEqual({
|
|
85
|
-
isLoggedIn: false,
|
|
86
|
-
hasAccess: false,
|
|
87
|
-
accessFeatures: [],
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
itShouldNotRedirect();
|
|
92
|
-
itShouldNotReload();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
describe('When loggen in', () => {
|
|
96
|
-
beforeEach(() => {
|
|
97
|
-
store.set('state', { isLoggedIn: true });
|
|
98
|
-
store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it('should not attempt autologin', () => {
|
|
102
|
-
expect(autologin.attemptPaywallAutoLogin).not.toHaveBeenCalled();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
itShouldNotRedirect();
|
|
106
|
-
itShouldNotReload();
|
|
107
|
-
|
|
108
|
-
describe('when has access', () => {
|
|
109
|
-
let unlockResponse: UnlockResponse;
|
|
110
|
-
beforeEach(async () => {
|
|
111
|
-
store.merge('userAttributes', { access: ['newspaper'] });
|
|
112
|
-
spyOn(authorization, 'setAccessCookies').mockResolvedValue({
|
|
113
|
-
success: true,
|
|
114
|
-
accessFeatures: ['newspaper'],
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe('When called with reload and already tried to reload', () => {
|
|
121
|
-
let error: Error | undefined;
|
|
122
|
-
beforeEach(async () => {
|
|
123
|
-
spyOn(
|
|
124
|
-
authorization,
|
|
125
|
-
'hasRecentlyPerformedPaywallReload'
|
|
126
|
-
).mockReturnValue(true);
|
|
127
|
-
error = undefined;
|
|
128
|
-
try {
|
|
129
|
-
await new PaywallUnlockRequest().reloadOnAccess().tryUnlock();
|
|
130
|
-
} catch (e) {
|
|
131
|
-
error = e;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
it('should throw exception', () => {
|
|
135
|
-
expect(error).toEqual(
|
|
136
|
-
new PaywallError(
|
|
137
|
-
'Unlock check already performed.',
|
|
138
|
-
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
139
|
-
)
|
|
140
|
-
);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('should not store that we tried, since we perform no reload', () => {
|
|
145
|
-
expect(
|
|
146
|
-
authorization.setHasPerformedPaywallReload
|
|
147
|
-
).not.toHaveBeenCalled();
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('should give access with access features', () => {
|
|
151
|
-
expect(unlockResponse).toEqual({
|
|
152
|
-
isLoggedIn: true,
|
|
153
|
-
hasAccess: true,
|
|
154
|
-
accessFeatures: ['newspaper'],
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
itShouldNotAttemptActivation();
|
|
159
|
-
itShouldNotRedirect();
|
|
160
|
-
itShouldNotReload();
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
describe('when no access', () => {
|
|
164
|
-
describe('and activation give access', () => {
|
|
165
|
-
let unlockResponse: UnlockResponse;
|
|
166
|
-
beforeEach(() => {
|
|
167
|
-
spyOn(authorization, 'setAccessCookies')
|
|
168
|
-
.mockResolvedValueOnce({
|
|
169
|
-
success: true,
|
|
170
|
-
accessFeatures: [],
|
|
171
|
-
})
|
|
172
|
-
.mockResolvedValueOnce({
|
|
173
|
-
success: true,
|
|
174
|
-
accessFeatures: ['newspaper'],
|
|
175
|
-
});
|
|
176
|
-
spyOn(authorization, 'activate').mockResolvedValue(['newspaper']);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
describe('with no post access actions', () => {
|
|
180
|
-
beforeEach(async () => {
|
|
181
|
-
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('should give access with access features', () => {
|
|
185
|
-
expect(unlockResponse).toEqual({
|
|
186
|
-
isLoggedIn: true,
|
|
187
|
-
hasAccess: true,
|
|
188
|
-
accessFeatures: ['newspaper'],
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
193
|
-
itShouldNotRedirect();
|
|
194
|
-
itShouldNotReload();
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
describe('with redirect requested', () => {
|
|
198
|
-
beforeEach(async () => {
|
|
199
|
-
unlockResponse = await new PaywallUnlockRequest()
|
|
200
|
-
.redirectOnAccess('https://redirect.pls')
|
|
201
|
-
.tryUnlock();
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should give access with access features', () => {
|
|
205
|
-
expect(unlockResponse).toEqual({
|
|
206
|
-
isLoggedIn: true,
|
|
207
|
-
hasAccess: true,
|
|
208
|
-
accessFeatures: ['newspaper'],
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it('should navigate to expected url', () => {
|
|
213
|
-
expect(location.replace).toHaveBeenCalledWith(
|
|
214
|
-
'https://redirect.pls'
|
|
215
|
-
);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
219
|
-
itShouldNotReload();
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
describe('with reload requested', () => {
|
|
223
|
-
beforeEach(async () => {
|
|
224
|
-
unlockResponse = await new PaywallUnlockRequest()
|
|
225
|
-
.reloadOnAccess()
|
|
226
|
-
.tryUnlock();
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it('should give access with access features', () => {
|
|
230
|
-
expect(unlockResponse).toEqual({
|
|
231
|
-
isLoggedIn: true,
|
|
232
|
-
hasAccess: true,
|
|
233
|
-
accessFeatures: ['newspaper'],
|
|
234
|
-
});
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('should reload on access', () => {
|
|
238
|
-
expect(location.reload).toHaveBeenCalled();
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
242
|
-
itShouldNotRedirect();
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
describe('and activation does not give access', () => {
|
|
247
|
-
let unlockResponse: UnlockResponse;
|
|
248
|
-
beforeEach(async () => {
|
|
249
|
-
spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
|
|
250
|
-
success: true,
|
|
251
|
-
accessFeatures: [],
|
|
252
|
-
});
|
|
253
|
-
spyOn(authorization, 'activate').mockResolvedValue([]);
|
|
254
|
-
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it('should not give access', () => {
|
|
258
|
-
expect(unlockResponse).toEqual({
|
|
259
|
-
isLoggedIn: true,
|
|
260
|
-
hasAccess: false,
|
|
261
|
-
accessFeatures: [],
|
|
262
|
-
});
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
266
|
-
itShouldNotRedirect();
|
|
267
|
-
itShouldNotReload();
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
describe("and activation don't give enough access", () => {
|
|
271
|
-
let unlockResponse: UnlockResponse;
|
|
272
|
-
beforeEach(async () => {
|
|
273
|
-
spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
|
|
274
|
-
success: true,
|
|
275
|
-
accessFeatures: ['newspaper'],
|
|
276
|
-
});
|
|
277
|
-
spyOn(authorization, 'activate').mockResolvedValue(['newspaper']);
|
|
278
|
-
unlockResponse = await new PaywallUnlockRequest()
|
|
279
|
-
.withAccessFeatures(['plussalt'])
|
|
280
|
-
.tryUnlock();
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
it('should not give access, but return users access features', () => {
|
|
284
|
-
expect(unlockResponse).toEqual({
|
|
285
|
-
isLoggedIn: true,
|
|
286
|
-
hasAccess: false,
|
|
287
|
-
accessFeatures: ['newspaper'],
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
itShouldAttemptActivationWithAccessFeatures(['plussalt']);
|
|
292
|
-
itShouldNotRedirect();
|
|
293
|
-
itShouldNotReload();
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
describe('when setting access cookies fails', () => {
|
|
298
|
-
let paywallError: any;
|
|
299
|
-
beforeEach(async () => {
|
|
300
|
-
spyOn(authorization, 'setAccessCookies').mockResolvedValue({
|
|
301
|
-
success: false,
|
|
302
|
-
accessFeatures: [],
|
|
303
|
-
});
|
|
304
|
-
try {
|
|
305
|
-
await new PaywallUnlockRequest().tryUnlock();
|
|
306
|
-
} catch (error) {
|
|
307
|
-
paywallError = error;
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
it('should throw error', () => {
|
|
312
|
-
expect(paywallError).toEqual(
|
|
313
|
-
new PaywallError(
|
|
314
|
-
'Could not set access cookies',
|
|
315
|
-
PaywallError.ERROR_ENABLING_ACCESS_FAILED
|
|
316
|
-
)
|
|
317
|
-
);
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
itShouldNotAttemptActivation();
|
|
321
|
-
itShouldNotRedirect();
|
|
322
|
-
itShouldNotReload();
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
describe('when internal system disagree about users access', () => {
|
|
326
|
-
let paywallError: any;
|
|
327
|
-
beforeEach(async () => {
|
|
328
|
-
spyOn(authorization, 'setAccessCookies')
|
|
329
|
-
.mockResolvedValueOnce({
|
|
330
|
-
success: true,
|
|
331
|
-
accessFeatures: ['newspaper'],
|
|
332
|
-
})
|
|
333
|
-
.mockResolvedValueOnce({
|
|
334
|
-
success: true,
|
|
335
|
-
accessFeatures: ['newspaper'], // Notice not changed
|
|
336
|
-
});
|
|
337
|
-
spyOn(authorization, 'activate').mockResolvedValue([
|
|
338
|
-
'newspaper',
|
|
339
|
-
'plussalt',
|
|
340
|
-
]);
|
|
341
|
-
try {
|
|
342
|
-
await new PaywallUnlockRequest()
|
|
343
|
-
.withAccessFeatures(['plussalt'])
|
|
344
|
-
.tryUnlock();
|
|
345
|
-
} catch (error) {
|
|
346
|
-
paywallError = error;
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('should throw error about bad access state', () => {
|
|
351
|
-
expect(paywallError).toEqual(
|
|
352
|
-
new PaywallError(
|
|
353
|
-
'New subscription was attached and checked successfully, but internal systems disagree about current newspaper access.',
|
|
354
|
-
PaywallError.ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES
|
|
355
|
-
)
|
|
356
|
-
);
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
itShouldNotReload();
|
|
360
|
-
itShouldNotRedirect();
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
});
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
activate,
|
|
3
|
-
hasAccess,
|
|
4
|
-
hasRecentlyPerformedPaywallReload,
|
|
5
|
-
setAccessCookies,
|
|
6
|
-
setHasPerformedPaywallReload,
|
|
7
|
-
} from '../actions/authorization';
|
|
8
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
9
|
-
import * as location from '../utils/location';
|
|
10
|
-
import PaywallError from '../exceptions/PaywallError';
|
|
11
|
-
import UserDataRequest from './UserDataRequest';
|
|
12
|
-
import { attemptPaywallAutoLogin } from '../autologin';
|
|
13
|
-
import { AccessFeatures } from '../clients/jupiter';
|
|
14
|
-
import { isRunningOnLocalhost } from '../utils/localhostCheck';
|
|
15
|
-
|
|
16
|
-
export type UnlockResponse = {
|
|
17
|
-
isLoggedIn: boolean;
|
|
18
|
-
hasAccess: boolean;
|
|
19
|
-
accessFeatures: string[];
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
class PaywallUnlockRequest {
|
|
23
|
-
private requestedAccessFeatures: string[] = ['newspaper'];
|
|
24
|
-
|
|
25
|
-
private reload: boolean = false;
|
|
26
|
-
|
|
27
|
-
private requestedUrl: string | undefined;
|
|
28
|
-
|
|
29
|
-
public withAccessFeatures(accessFeatures: string[]): this {
|
|
30
|
-
this.requestedAccessFeatures = accessFeatures;
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public reloadOnAccess(): this {
|
|
35
|
-
this.reload = true;
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public redirectOnAccess(requestedUrl: string): this {
|
|
40
|
-
this.requestedUrl = requestedUrl;
|
|
41
|
-
return this;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async tryUnlock(): Promise<UnlockResponse> {
|
|
45
|
-
if (isRunningOnLocalhost()) {
|
|
46
|
-
return Promise.reject(
|
|
47
|
-
new Error('@amedia/user cannot unlock paywall on localhost')
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
const {
|
|
51
|
-
state: { isLoggedIn },
|
|
52
|
-
} = await new UserDataRequest().withAttributes(['name']).fetch();
|
|
53
|
-
if (!isLoggedIn) {
|
|
54
|
-
// Will attempt one or more strategies for creating a first party session
|
|
55
|
-
// Redirects/reloads might occur
|
|
56
|
-
await attemptPaywallAutoLogin();
|
|
57
|
-
return {
|
|
58
|
-
isLoggedIn: false,
|
|
59
|
-
hasAccess: false,
|
|
60
|
-
accessFeatures: [],
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
let currentAccess = await this.setAccessCookiesAndGetAccessFeatures(
|
|
65
|
-
this.requestedAccessFeatures
|
|
66
|
-
);
|
|
67
|
-
if (currentAccess.denied) {
|
|
68
|
-
// Check if we actually have a valid subscription that's not properly connected to the user yet
|
|
69
|
-
currentAccess = await this.activateAccess(this.requestedAccessFeatures);
|
|
70
|
-
if (currentAccess.denied) {
|
|
71
|
-
return {
|
|
72
|
-
isLoggedIn: true,
|
|
73
|
-
hasAccess: false,
|
|
74
|
-
accessFeatures: currentAccess.accessFeatures,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
currentAccess = await this.setAccessCookiesAndGetAccessFeatures(
|
|
78
|
-
this.requestedAccessFeatures
|
|
79
|
-
);
|
|
80
|
-
if (currentAccess.denied) {
|
|
81
|
-
throw new PaywallError(
|
|
82
|
-
'New subscription was attached and checked successfully, but internal systems disagree about current newspaper access.',
|
|
83
|
-
PaywallError.ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
if (this.reload || this.requestedUrl) {
|
|
88
|
-
// Ensures we don't get stuck in a reload/redirect loop
|
|
89
|
-
if (hasRecentlyPerformedPaywallReload(this.requestedUrl)) {
|
|
90
|
-
throw new PaywallError(
|
|
91
|
-
'Unlock check already performed.',
|
|
92
|
-
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
setHasPerformedPaywallReload(this.requestedUrl);
|
|
97
|
-
if (this.reload) {
|
|
98
|
-
location.reload();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (this.requestedUrl) {
|
|
102
|
-
location.replace(this.requestedUrl);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return {
|
|
106
|
-
isLoggedIn: true,
|
|
107
|
-
hasAccess: true,
|
|
108
|
-
accessFeatures: currentAccess.accessFeatures,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// eslint-disable-next-line class-methods-use-this
|
|
113
|
-
private setAccessCookiesAndGetAccessFeatures(
|
|
114
|
-
requestedAccessFeatures: AccessFeatures
|
|
115
|
-
) {
|
|
116
|
-
return setAccessCookies(getCurrentPublicationDomain()).then((response) => {
|
|
117
|
-
const accessDenied = !hasAccess(
|
|
118
|
-
response.accessFeatures,
|
|
119
|
-
requestedAccessFeatures
|
|
120
|
-
);
|
|
121
|
-
if (!response.success && accessDenied) {
|
|
122
|
-
throw new PaywallError(
|
|
123
|
-
'Could not set access cookies',
|
|
124
|
-
PaywallError.ERROR_ENABLING_ACCESS_FAILED
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
denied: accessDenied,
|
|
129
|
-
accessFeatures: response.accessFeatures,
|
|
130
|
-
};
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
private async activateAccess(requestedAccessFeatures: AccessFeatures) {
|
|
135
|
-
const accessFeatures = await activate(this.requestedAccessFeatures);
|
|
136
|
-
return {
|
|
137
|
-
denied: !hasAccess(accessFeatures, requestedAccessFeatures),
|
|
138
|
-
accessFeatures,
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export default PaywallUnlockRequest;
|