@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.
Files changed (179) hide show
  1. package/dist/user.cjs +1 -1
  2. package/dist/user.cjs.map +3 -3
  3. package/dist/user.d.ts +12 -0
  4. package/dist/user.js +1 -1
  5. package/dist/user.js.map +3 -3
  6. package/package.json +5 -2
  7. package/.codeclimate.yml +0 -8
  8. package/.eslintignore +0 -2
  9. package/.eslintrc +0 -11
  10. package/.prettierrc.json +0 -1
  11. package/@types/amedia-frontend-rpc.d.ts +0 -11
  12. package/@types/browserid.d.ts +0 -4
  13. package/build.js +0 -28
  14. package/checkVersion.cjs +0 -12
  15. package/cloudbuild.json +0 -29
  16. package/cop.json +0 -8
  17. package/dist/actions/authentication.d.ts +0 -4
  18. package/dist/actions/authentication.spec.d.ts +0 -1
  19. package/dist/actions/authorization.d.ts +0 -14
  20. package/dist/actions/authorization.spec.d.ts +0 -4
  21. package/dist/actions/preferences.d.ts +0 -9
  22. package/dist/actions/preferences.spec.d.ts +0 -1
  23. package/dist/actions/storage.d.ts +0 -7
  24. package/dist/actions/userData.d.ts +0 -2
  25. package/dist/actions/userData.spec.d.ts +0 -1
  26. package/dist/autologin/index.d.ts +0 -7
  27. package/dist/autologin/index.spec.d.ts +0 -1
  28. package/dist/autologin/thirdPartyCookie.d.ts +0 -3
  29. package/dist/autologin/thirdPartyCookie.spec.d.ts +0 -1
  30. package/dist/cache/engines/global.d.ts +0 -24
  31. package/dist/cache/engines/global.spec.d.ts +0 -1
  32. package/dist/cache/index.d.ts +0 -11
  33. package/dist/cache/index.spec.d.ts +0 -1
  34. package/dist/cache/types.d.ts +0 -11
  35. package/dist/clients/europa.d.ts +0 -24
  36. package/dist/clients/europa.spec.d.ts +0 -1
  37. package/dist/clients/jupiter.d.ts +0 -30
  38. package/dist/clients/jupiter.spec.d.ts +0 -1
  39. package/dist/clients/mercury.d.ts +0 -3
  40. package/dist/clients/mercury.spec.d.ts +0 -1
  41. package/dist/clients/mercuryLog.d.ts +0 -8
  42. package/dist/clients/mercuryLog.spec.d.ts +0 -1
  43. package/dist/clients/nebula.d.ts +0 -2
  44. package/dist/clients/nebula.spec.d.ts +0 -1
  45. package/dist/clients/pluto.d.ts +0 -4
  46. package/dist/clients/pluto.spec.d.ts +0 -1
  47. package/dist/eventHandlers/activation.d.ts +0 -15
  48. package/dist/eventHandlers/authentication.d.ts +0 -17
  49. package/dist/eventHandlers/eventMap.d.ts +0 -29
  50. package/dist/eventHandlers/state.d.ts +0 -1
  51. package/dist/eventHandlers/state.spec.d.ts +0 -1
  52. package/dist/eventHandlers/storage.d.ts +0 -1
  53. package/dist/eventHandlers/storage.spec.d.ts +0 -1
  54. package/dist/eventHandlers/userAttributes.d.ts +0 -1
  55. package/dist/eventHandlers/userAttributes.spec.d.ts +0 -1
  56. package/dist/exceptions/PaywallError.d.ts +0 -8
  57. package/dist/index-authentcation.spec.d.ts +0 -1
  58. package/dist/index-storage.spec.d.ts +0 -1
  59. package/dist/index-subscription.spec.d.ts +0 -1
  60. package/dist/index.d.ts +0 -20
  61. package/dist/janitor/index.d.ts +0 -10
  62. package/dist/janitor/index.spec.d.ts +0 -1
  63. package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
  64. package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
  65. package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
  66. package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
  67. package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
  68. package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
  69. package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
  70. package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
  71. package/dist/lib/UserDataRequest.d.ts +0 -22
  72. package/dist/lib/UserDataRequest.spec.d.ts +0 -1
  73. package/dist/lib/cookie.d.ts +0 -9
  74. package/dist/lib/rpc.d.ts +0 -6
  75. package/dist/store/index.d.ts +0 -23
  76. package/dist/store/index.spec.d.ts +0 -1
  77. package/dist/types.d.ts +0 -22
  78. package/dist/utils/customEventWrapper.d.ts +0 -19
  79. package/dist/utils/debouncer.d.ts +0 -26
  80. package/dist/utils/debouncer.spec.d.ts +0 -1
  81. package/dist/utils/deepEquals.d.ts +0 -1
  82. package/dist/utils/expectBootLogs.d.ts +0 -2
  83. package/dist/utils/getOrUpdate.d.ts +0 -6
  84. package/dist/utils/getOrUpdate.spec.d.ts +0 -1
  85. package/dist/utils/localhostCheck.d.ts +0 -1
  86. package/dist/utils/location.d.ts +0 -3
  87. package/dist/utils/log.d.ts +0 -9
  88. package/dist/utils/log.spec.d.ts +0 -1
  89. package/dist/utils/loginState.d.ts +0 -2
  90. package/dist/utils/networkError.d.ts +0 -1
  91. package/dist/utils/queue.d.ts +0 -12
  92. package/dist/utils/type.d.ts +0 -6
  93. package/dist/utils/url.d.ts +0 -6
  94. package/dist/utils/url.spec.d.ts +0 -1
  95. package/eik.json +0 -10
  96. package/global.d.ts +0 -1
  97. package/jest.config.js +0 -7
  98. package/setupJest.js +0 -1
  99. package/src/actions/authentication.spec.ts +0 -51
  100. package/src/actions/authentication.ts +0 -43
  101. package/src/actions/authorization.spec.ts +0 -369
  102. package/src/actions/authorization.ts +0 -117
  103. package/src/actions/preferences.spec.ts +0 -41
  104. package/src/actions/preferences.ts +0 -41
  105. package/src/actions/storage.ts +0 -22
  106. package/src/actions/userData.spec.ts +0 -133
  107. package/src/actions/userData.ts +0 -68
  108. package/src/autologin/index.spec.ts +0 -239
  109. package/src/autologin/index.ts +0 -92
  110. package/src/autologin/thirdPartyCookie.spec.ts +0 -95
  111. package/src/autologin/thirdPartyCookie.ts +0 -55
  112. package/src/cache/engines/global.spec.ts +0 -62
  113. package/src/cache/engines/global.ts +0 -48
  114. package/src/cache/index.spec.ts +0 -130
  115. package/src/cache/index.ts +0 -48
  116. package/src/cache/types.ts +0 -12
  117. package/src/clients/europa.spec.ts +0 -209
  118. package/src/clients/europa.ts +0 -183
  119. package/src/clients/jupiter.spec.ts +0 -179
  120. package/src/clients/jupiter.ts +0 -119
  121. package/src/clients/mercury.spec.ts +0 -59
  122. package/src/clients/mercury.ts +0 -48
  123. package/src/clients/mercuryLog.spec.ts +0 -20
  124. package/src/clients/mercuryLog.ts +0 -67
  125. package/src/clients/nebula.spec.ts +0 -70
  126. package/src/clients/nebula.ts +0 -83
  127. package/src/clients/pluto.spec.ts +0 -165
  128. package/src/clients/pluto.ts +0 -118
  129. package/src/eventHandlers/activation.ts +0 -35
  130. package/src/eventHandlers/authentication.ts +0 -34
  131. package/src/eventHandlers/eventMap.ts +0 -34
  132. package/src/eventHandlers/state.spec.ts +0 -48
  133. package/src/eventHandlers/state.ts +0 -23
  134. package/src/eventHandlers/storage.spec.ts +0 -114
  135. package/src/eventHandlers/storage.ts +0 -45
  136. package/src/eventHandlers/userAttributes.spec.ts +0 -93
  137. package/src/eventHandlers/userAttributes.ts +0 -31
  138. package/src/exceptions/PaywallError.ts +0 -16
  139. package/src/index-authentcation.spec.ts +0 -26
  140. package/src/index-storage.spec.ts +0 -144
  141. package/src/index-subscription.spec.ts +0 -213
  142. package/src/index.ts +0 -59
  143. package/src/janitor/index.spec.ts +0 -18
  144. package/src/janitor/index.ts +0 -59
  145. package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
  146. package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
  147. package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
  148. package/src/janitor/jobs/daxsubCookie.ts +0 -38
  149. package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
  150. package/src/janitor/jobs/removeUserStorage.ts +0 -12
  151. package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
  152. package/src/lib/PaywallUnlockRequest.ts +0 -143
  153. package/src/lib/UserDataRequest.spec.ts +0 -77
  154. package/src/lib/UserDataRequest.ts +0 -213
  155. package/src/lib/cookie.ts +0 -121
  156. package/src/lib/rpc.ts +0 -19
  157. package/src/store/index.spec.ts +0 -49
  158. package/src/store/index.ts +0 -64
  159. package/src/types.ts +0 -22
  160. package/src/utils/customEventWrapper.ts +0 -58
  161. package/src/utils/debouncer.spec.ts +0 -252
  162. package/src/utils/debouncer.ts +0 -123
  163. package/src/utils/deepEquals.ts +0 -42
  164. package/src/utils/expectBootLogs.ts +0 -9
  165. package/src/utils/getOrUpdate.spec.ts +0 -166
  166. package/src/utils/getOrUpdate.ts +0 -66
  167. package/src/utils/localhostCheck.ts +0 -9
  168. package/src/utils/location.ts +0 -31
  169. package/src/utils/log.spec.ts +0 -148
  170. package/src/utils/log.ts +0 -124
  171. package/src/utils/loginState.ts +0 -24
  172. package/src/utils/networkError.ts +0 -11
  173. package/src/utils/queue.ts +0 -32
  174. package/src/utils/type.ts +0 -19
  175. package/src/utils/url.spec.ts +0 -33
  176. package/src/utils/url.ts +0 -35
  177. package/testSetup.js +0 -19
  178. package/tsconfig.eslint.json +0 -24
  179. 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;