@amedia/user 0.13.34 → 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 (180) hide show
  1. package/dist/user.cjs +15 -0
  2. package/dist/{index.js.map → user.cjs.map} +3 -3
  3. package/dist/{index.d.ts → user.d.ts} +12 -0
  4. package/dist/user.js +15 -0
  5. package/dist/{index.esm.js.map → user.js.map} +3 -3
  6. package/package.json +9 -6
  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.esm.js +0 -15
  61. package/dist/index.js +0 -15
  62. package/dist/janitor/index.d.ts +0 -10
  63. package/dist/janitor/index.spec.d.ts +0 -1
  64. package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
  65. package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
  66. package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
  67. package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
  68. package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
  69. package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
  70. package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
  71. package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
  72. package/dist/lib/UserDataRequest.d.ts +0 -22
  73. package/dist/lib/UserDataRequest.spec.d.ts +0 -1
  74. package/dist/lib/cookie.d.ts +0 -9
  75. package/dist/lib/rpc.d.ts +0 -6
  76. package/dist/store/index.d.ts +0 -23
  77. package/dist/store/index.spec.d.ts +0 -1
  78. package/dist/types.d.ts +0 -22
  79. package/dist/utils/customEventWrapper.d.ts +0 -19
  80. package/dist/utils/debouncer.d.ts +0 -26
  81. package/dist/utils/debouncer.spec.d.ts +0 -1
  82. package/dist/utils/deepEquals.d.ts +0 -1
  83. package/dist/utils/expectBootLogs.d.ts +0 -2
  84. package/dist/utils/getOrUpdate.d.ts +0 -6
  85. package/dist/utils/getOrUpdate.spec.d.ts +0 -1
  86. package/dist/utils/localhostCheck.d.ts +0 -1
  87. package/dist/utils/location.d.ts +0 -3
  88. package/dist/utils/log.d.ts +0 -9
  89. package/dist/utils/log.spec.d.ts +0 -1
  90. package/dist/utils/loginState.d.ts +0 -2
  91. package/dist/utils/networkError.d.ts +0 -1
  92. package/dist/utils/queue.d.ts +0 -12
  93. package/dist/utils/type.d.ts +0 -6
  94. package/dist/utils/url.d.ts +0 -6
  95. package/dist/utils/url.spec.d.ts +0 -1
  96. package/eik.json +0 -10
  97. package/global.d.ts +0 -1
  98. package/jest.config.js +0 -7
  99. package/setupJest.js +0 -1
  100. package/src/actions/authentication.spec.ts +0 -51
  101. package/src/actions/authentication.ts +0 -43
  102. package/src/actions/authorization.spec.ts +0 -369
  103. package/src/actions/authorization.ts +0 -117
  104. package/src/actions/preferences.spec.ts +0 -41
  105. package/src/actions/preferences.ts +0 -41
  106. package/src/actions/storage.ts +0 -22
  107. package/src/actions/userData.spec.ts +0 -133
  108. package/src/actions/userData.ts +0 -68
  109. package/src/autologin/index.spec.ts +0 -239
  110. package/src/autologin/index.ts +0 -92
  111. package/src/autologin/thirdPartyCookie.spec.ts +0 -95
  112. package/src/autologin/thirdPartyCookie.ts +0 -55
  113. package/src/cache/engines/global.spec.ts +0 -62
  114. package/src/cache/engines/global.ts +0 -48
  115. package/src/cache/index.spec.ts +0 -130
  116. package/src/cache/index.ts +0 -48
  117. package/src/cache/types.ts +0 -12
  118. package/src/clients/europa.spec.ts +0 -209
  119. package/src/clients/europa.ts +0 -183
  120. package/src/clients/jupiter.spec.ts +0 -179
  121. package/src/clients/jupiter.ts +0 -119
  122. package/src/clients/mercury.spec.ts +0 -59
  123. package/src/clients/mercury.ts +0 -48
  124. package/src/clients/mercuryLog.spec.ts +0 -20
  125. package/src/clients/mercuryLog.ts +0 -67
  126. package/src/clients/nebula.spec.ts +0 -70
  127. package/src/clients/nebula.ts +0 -83
  128. package/src/clients/pluto.spec.ts +0 -165
  129. package/src/clients/pluto.ts +0 -118
  130. package/src/eventHandlers/activation.ts +0 -35
  131. package/src/eventHandlers/authentication.ts +0 -34
  132. package/src/eventHandlers/eventMap.ts +0 -34
  133. package/src/eventHandlers/state.spec.ts +0 -48
  134. package/src/eventHandlers/state.ts +0 -23
  135. package/src/eventHandlers/storage.spec.ts +0 -114
  136. package/src/eventHandlers/storage.ts +0 -45
  137. package/src/eventHandlers/userAttributes.spec.ts +0 -93
  138. package/src/eventHandlers/userAttributes.ts +0 -31
  139. package/src/exceptions/PaywallError.ts +0 -16
  140. package/src/index-authentcation.spec.ts +0 -26
  141. package/src/index-storage.spec.ts +0 -144
  142. package/src/index-subscription.spec.ts +0 -213
  143. package/src/index.ts +0 -59
  144. package/src/janitor/index.spec.ts +0 -18
  145. package/src/janitor/index.ts +0 -59
  146. package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
  147. package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
  148. package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
  149. package/src/janitor/jobs/daxsubCookie.ts +0 -38
  150. package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
  151. package/src/janitor/jobs/removeUserStorage.ts +0 -12
  152. package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
  153. package/src/lib/PaywallUnlockRequest.ts +0 -143
  154. package/src/lib/UserDataRequest.spec.ts +0 -77
  155. package/src/lib/UserDataRequest.ts +0 -213
  156. package/src/lib/cookie.ts +0 -121
  157. package/src/lib/rpc.ts +0 -19
  158. package/src/store/index.spec.ts +0 -49
  159. package/src/store/index.ts +0 -64
  160. package/src/types.ts +0 -22
  161. package/src/utils/customEventWrapper.ts +0 -58
  162. package/src/utils/debouncer.spec.ts +0 -252
  163. package/src/utils/debouncer.ts +0 -123
  164. package/src/utils/deepEquals.ts +0 -42
  165. package/src/utils/expectBootLogs.ts +0 -9
  166. package/src/utils/getOrUpdate.spec.ts +0 -166
  167. package/src/utils/getOrUpdate.ts +0 -66
  168. package/src/utils/localhostCheck.ts +0 -9
  169. package/src/utils/location.ts +0 -31
  170. package/src/utils/log.spec.ts +0 -148
  171. package/src/utils/log.ts +0 -124
  172. package/src/utils/loginState.ts +0 -24
  173. package/src/utils/networkError.ts +0 -11
  174. package/src/utils/queue.ts +0 -32
  175. package/src/utils/type.ts +0 -19
  176. package/src/utils/url.spec.ts +0 -33
  177. package/src/utils/url.ts +0 -35
  178. package/testSetup.js +0 -19
  179. package/tsconfig.eslint.json +0 -24
  180. package/tsconfig.json +0 -18
@@ -1,369 +0,0 @@
1
- /**
2
- * @jest-environment jsdom
3
- */
4
-
5
- import {
6
- activate,
7
- getAccess,
8
- hasAccess,
9
- hasRecentlyPerformedPaywallReload,
10
- setAccessCookies,
11
- setHasPerformedPaywallReload,
12
- setNonUserAccessCookie,
13
- setUserAccessCookie,
14
- } from './authorization';
15
- import * as jupiter from '../clients/jupiter';
16
- import * as cookie from '../lib/cookie';
17
- import * as url from '../utils/url';
18
- import * as mercury from '../clients/mercury';
19
- import * as authentication from './authentication';
20
- import { removeAllCookies } from '../lib/cookie';
21
- import { AccessFeatures } from '../clients/jupiter';
22
-
23
- const publicationDomain = 'www.avisnavn.no';
24
-
25
- beforeEach(() => {
26
- removeAllCookies();
27
- jest.spyOn(cookie, 'setCookie');
28
- });
29
-
30
- describe('getAccess', () => {
31
- beforeEach(() => {
32
- url.setCurrentHostname('www.avisnavn.no');
33
- jest
34
- .spyOn(jupiter, 'checkForAccess')
35
- .mockResolvedValue({ access: ['newspaper', 'plussalt'] });
36
- });
37
- it('returns list of access', async () => {
38
- expect(await getAccess()).toEqual({
39
- access: ['newspaper', 'plussalt'],
40
- });
41
- });
42
- });
43
-
44
- describe('activate', () => {
45
- describe('when all is ok', () => {
46
- beforeEach(() => {
47
- jest
48
- .spyOn(mercury, 'activateSubscription')
49
- .mockResolvedValue(['whatever']);
50
- });
51
- it('Returns whatever mercury returns', async () => {
52
- expect(await activate(['newspaper'])).toEqual(['whatever']);
53
- expect(mercury.activateSubscription).toHaveBeenCalledWith(['newspaper']);
54
- });
55
- });
56
-
57
- describe('when user is deactivated', () => {
58
- beforeEach(() => {
59
- jest
60
- .spyOn(mercury, 'activateSubscription')
61
- .mockRejectedValue(new mercury.UserDeactivatedError());
62
- jest.spyOn(authentication, 'logout').mockResolvedValue(undefined);
63
- });
64
- it('Returns whatever mercury returns', async () => {
65
- expect(await activate([])).toEqual([]);
66
- expect(mercury.activateSubscription).toHaveBeenCalledWith([]);
67
- expect(authentication.logout).toHaveBeenCalled();
68
- });
69
- });
70
- });
71
-
72
- describe('hasAccess', () => {
73
- let access = false;
74
- function itReturnsTrue() {
75
- it('returns true', () => {
76
- expect(access).toBeTruthy();
77
- });
78
- }
79
- function itReturnsFalse() {
80
- it('returns false', () => {
81
- expect(access).not.toBeTruthy();
82
- });
83
- }
84
- describe('with no access requested', () => {
85
- beforeEach(() => {
86
- access = hasAccess(['newspaper'], []);
87
- });
88
- itReturnsTrue();
89
- });
90
-
91
- describe('with access requested without access', () => {
92
- beforeEach(() => {
93
- access = hasAccess([], ['newspaper']);
94
- });
95
- itReturnsFalse();
96
- });
97
-
98
- describe('with access to some requested access feature', () => {
99
- beforeEach(() => {
100
- access = hasAccess(['newspaper'], ['all', 'plussalt', 'newspaper']);
101
- });
102
- itReturnsTrue();
103
- });
104
-
105
- describe('with access to other access features than requested requested access feature', () => {
106
- beforeEach(() => {
107
- access = hasAccess(['pluss', 'sport'], ['all', 'plussalt', 'newspaper']);
108
- });
109
- itReturnsFalse();
110
- });
111
- });
112
-
113
- let cookieResponse: any;
114
-
115
- function itReturnsCookieResponse(success: any, accessFeatures: AccessFeatures) {
116
- it(`returns cookie response with success = ${success}`, () => {
117
- expect(cookieResponse.success).toEqual(success);
118
- });
119
- it(`returns cookie response with accessFeatures = ${accessFeatures}`, () => {
120
- expect(cookieResponse.accessFeatures).toEqual(accessFeatures);
121
- });
122
- }
123
-
124
- function itSetsNoCookies() {
125
- it('does not set any cookies', () => {
126
- expect(cookie.setCookie).not.toHaveBeenCalled();
127
- });
128
- }
129
-
130
- function itDoesNotSetVstokenCookie() {
131
- it('does not set VSTOKEN cookie', () => {
132
- expect(cookie.setCookie).not.toHaveBeenCalledWith('VSTOKEN');
133
- });
134
- }
135
-
136
- function itSetsVstokenCookie() {
137
- it('sets VSTOKEN cookie', () => {
138
- expect(cookie.setCookie).toHaveBeenCalledWith(
139
- 'VSTOKEN',
140
- 'COOKIE_VALUE',
141
- 365,
142
- '/'
143
- );
144
- });
145
- }
146
-
147
- function itDoesNotSetVstoken2Cookie() {
148
- it('does not set VSTOKEN2 cookie', () => {
149
- expect(cookie.setCookie).not.toHaveBeenCalledWith('VSTOKEN2');
150
- });
151
- }
152
-
153
- function itSetsVstoken2Cookie() {
154
- it('sets VSTOKEN2 cookie', () => {
155
- expect(cookie.setCookie).toHaveBeenCalledWith(
156
- 'VSTOKEN2',
157
- 'COOKIE2_VALUE',
158
- 1,
159
- '/'
160
- );
161
- });
162
- }
163
-
164
- function mockGetUserAccess(status: any, accessFeatures: AccessFeatures) {
165
- beforeEach(async () => {
166
- jest.spyOn(jupiter, 'getUserAccess').mockResolvedValue({
167
- status,
168
- cookie: 'COOKIE_VALUE',
169
- access_features: accessFeatures,
170
- });
171
- });
172
- }
173
-
174
- function getUserAccessSucceeds(accessFeatures: AccessFeatures) {
175
- mockGetUserAccess('success', accessFeatures);
176
- }
177
-
178
- function getUserAccessFails(accessFeatures: AccessFeatures) {
179
- mockGetUserAccess('failure', accessFeatures);
180
- }
181
-
182
- function mockGetNonUserAccessSucceeds(
183
- status: any,
184
- accessFeatures: AccessFeatures
185
- ) {
186
- beforeEach(async () => {
187
- jest.spyOn(jupiter, 'getNonUserAccess').mockResolvedValue({
188
- status,
189
- uuid: 'mycompany',
190
- vstoken_cookie: 'COOKIE2_VALUE',
191
- access_features: accessFeatures,
192
- });
193
- });
194
- }
195
-
196
- function getNonUserAccessSucceeds(accessFeatures: AccessFeatures) {
197
- mockGetNonUserAccessSucceeds('success', accessFeatures);
198
- }
199
-
200
- function getNonUserAccessFails(accessFeatures: AccessFeatures) {
201
- mockGetNonUserAccessSucceeds('failure', accessFeatures);
202
- }
203
-
204
- describe('setUserAccessCookie', () => {
205
- describe('when getUserAccess is successful', () => {
206
- getUserAccessSucceeds(['newspaper', 'plussalt']);
207
- beforeEach(async () => {
208
- cookieResponse = await setUserAccessCookie(publicationDomain);
209
- });
210
- itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
211
- itSetsVstokenCookie();
212
- });
213
-
214
- describe('when getUserAccess is not successful', () => {
215
- getUserAccessFails(['newspaper', 'plussalt']);
216
- beforeEach(async () => {
217
- cookieResponse = await setUserAccessCookie(publicationDomain);
218
- });
219
- itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
220
- itSetsNoCookies();
221
- });
222
- });
223
-
224
- describe('setNonUserAccessCookie', () => {
225
- describe('when getNonUserAccess is successful', () => {
226
- getNonUserAccessSucceeds(['newspaper', 'plussalt']);
227
- beforeEach(async () => {
228
- cookieResponse = await setNonUserAccessCookie(publicationDomain);
229
- });
230
- itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
231
- itSetsVstoken2Cookie();
232
- });
233
-
234
- describe('when getNonUserAccess is not successful', () => {
235
- getNonUserAccessFails(['newspaper', 'plussalt']);
236
- beforeEach(async () => {
237
- cookieResponse = await setNonUserAccessCookie(publicationDomain);
238
- });
239
- itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
240
- itSetsNoCookies();
241
- });
242
- });
243
-
244
- describe('setAccessCookies', () => {
245
- describe('when both VSTOKEN and VSTOKEN2 requests succeed', () => {
246
- getUserAccessSucceeds(['newspaper', 'plussalt']);
247
- getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
248
- beforeEach(async () => {
249
- cookieResponse = await setAccessCookies(publicationDomain);
250
- });
251
- itReturnsCookieResponse(true, ['newspaper', 'pluss', 'plussalt', 'sport']);
252
- itSetsVstokenCookie();
253
- itSetsVstoken2Cookie();
254
- });
255
-
256
- describe('when VSTOKEN fails and VSTOKEN2 succeeds', () => {
257
- getUserAccessFails(['newspaper']);
258
- getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
259
- beforeEach(async () => {
260
- cookieResponse = await setAccessCookies(publicationDomain);
261
- });
262
- itReturnsCookieResponse(false, ['newspaper', 'pluss', 'sport']);
263
- itDoesNotSetVstokenCookie();
264
- itSetsVstoken2Cookie();
265
- });
266
-
267
- describe('when VSTOKEN succeeds and VSTOKEN2 fails', () => {
268
- getUserAccessSucceeds(['newspaper', 'plussalt']);
269
- getNonUserAccessFails(['newspaper']);
270
- beforeEach(async () => {
271
- cookieResponse = await setAccessCookies(publicationDomain);
272
- });
273
- itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
274
- itSetsVstokenCookie();
275
- itDoesNotSetVstoken2Cookie();
276
- });
277
-
278
- describe('when both VSTOKEN and VSTOKEN2 fails', () => {
279
- getUserAccessFails(['newspaper', 'plussalt']);
280
- getNonUserAccessFails(['newspaper']);
281
- beforeEach(async () => {
282
- cookieResponse = await setAccessCookies(publicationDomain);
283
- });
284
- itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
285
- itSetsNoCookies();
286
- });
287
- });
288
-
289
- describe('reload loop protection', () => {
290
- const currentUrl = 'http://www.avisnavn.no/myAwesomeArticle';
291
- const otherUrl = 'https://www.avisnavn.no/someBoringArticle';
292
-
293
- beforeEach(() => {
294
- jest.spyOn(url, 'getCurrentUrl').mockReturnValue(currentUrl);
295
- });
296
-
297
- describe('setHasPerformedPaywallReload', () => {
298
- describe('without requestedUrl', () => {
299
- beforeEach(() => {
300
- setHasPerformedPaywallReload();
301
- });
302
- it('sets aid-paywall-reload-performed with current URL as value', () => {
303
- const twoMinutesFromNow = new Date().getTime() + 2 * 60 * 1000;
304
- expect(cookie.setCookie).toHaveBeenCalledWith(
305
- 'aid-paywall-reload-performed',
306
- currentUrl,
307
- expect.toBeWithin(twoMinutesFromNow - 5000, twoMinutesFromNow + 5000)
308
- );
309
- });
310
- });
311
- describe('with requestedUrl', () => {
312
- beforeEach(() => {
313
- setHasPerformedPaywallReload(otherUrl);
314
- });
315
- it('sets aid-paywall-reload-performed with current URL as value', () => {
316
- const twoMinutesFromNow = new Date().getTime() + 2 * 60 * 1000;
317
- expect(cookie.setCookie).toHaveBeenCalledWith(
318
- 'aid-paywall-reload-performed',
319
- otherUrl,
320
- expect.toBeWithin(twoMinutesFromNow - 5000, twoMinutesFromNow + 5000)
321
- );
322
- });
323
- });
324
- });
325
-
326
- describe('hasRecentlyPerformedPaywallReload', () => {
327
- function itReturnsTrue() {
328
- it('returns true', () => {
329
- expect(hasRecentlyPerformedPaywallReload()).toBeTruthy();
330
- });
331
- }
332
- function itReturnsFalse() {
333
- it('returns false', () => {
334
- expect(hasRecentlyPerformedPaywallReload()).toBeFalsy();
335
- });
336
- }
337
- describe('when has recently tried to unlock paywall on same URL', () => {
338
- beforeEach(() => {
339
- setHasPerformedPaywallReload();
340
- });
341
- itReturnsTrue();
342
- });
343
- describe('when has recently tried to unlock paywall on different URL', () => {
344
- beforeEach(() => {
345
- jest
346
- .spyOn(url, 'getCurrentUrl')
347
- .mockReturnValueOnce(otherUrl)
348
- .mockReturnValue(currentUrl);
349
- setHasPerformedPaywallReload();
350
- });
351
- itReturnsFalse();
352
- });
353
- describe('when has recently tried to unlock paywall on different URL, and this URL is specified', () => {
354
- beforeEach(() => {
355
- jest
356
- .spyOn(url, 'getCurrentUrl')
357
- .mockReturnValueOnce(otherUrl) // This will be the value in our cookie
358
- .mockReturnValue(currentUrl);
359
- setHasPerformedPaywallReload();
360
- });
361
- it('returns true', () => {
362
- expect(hasRecentlyPerformedPaywallReload(otherUrl)).toBeTruthy();
363
- });
364
- });
365
- describe('when aid-paywall-reload-performed is not present', () => {
366
- itReturnsFalse();
367
- });
368
- });
369
- });
@@ -1,117 +0,0 @@
1
- import {
2
- checkForAccess,
3
- getNonUserAccess,
4
- getUserAccess,
5
- } from '../clients/jupiter';
6
- import { getCurrentPublicationDomain, getCurrentUrl } from '../utils/url';
7
- import { UserAttributes } from '../types';
8
- import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
9
- import { logout } from './authentication';
10
- import { getCookie, setCookie } from '../lib/cookie';
11
-
12
- type SetCookieResponse = { success: boolean; accessFeatures: string[] };
13
-
14
- export const getAccess = (
15
- accessFeatures: string[] = ['newspaper']
16
- ): Promise<Pick<UserAttributes, 'access'>> =>
17
- checkForAccess(getCurrentPublicationDomain(), accessFeatures);
18
-
19
- export const activate = async (accessFeatures: string[]): Promise<string[]> => {
20
- try {
21
- return await activateSubscription(accessFeatures);
22
- } catch (error) {
23
- if (error instanceof UserDeactivatedError) {
24
- await logout();
25
- return [];
26
- }
27
- throw error;
28
- }
29
- };
30
-
31
- export const hasAccess = (
32
- currentAccessFeatures: string[],
33
- requestedAccessFeatures: string[]
34
- ): boolean =>
35
- requestedAccessFeatures.length < 1 ||
36
- requestedAccessFeatures.filter((requestedAccessFeature) =>
37
- currentAccessFeatures.includes(requestedAccessFeature)
38
- ).length > 0;
39
-
40
- export const setUserAccessCookie = async (
41
- publicationDomain: string
42
- ): Promise<SetCookieResponse> => {
43
- const vstoken = await getUserAccess(publicationDomain);
44
- if (vstoken.status === 'success') {
45
- /**
46
- * The VSTOKEN cookie will live for 365 days, but a timestamp in the payload will ensure that it cannot
47
- * be used to gain access for that long. The reason we want the cookie to live long is that it's also
48
- * used to keep subscribers from seeing ads for buying the subscription they already have.
49
- */
50
- setCookie('VSTOKEN', vstoken.cookie, 365, '/');
51
- }
52
- return {
53
- success: vstoken.status === 'success',
54
- accessFeatures: vstoken.access_features,
55
- };
56
- };
57
-
58
- export const setNonUserAccessCookie = async (
59
- publicationDomain: string
60
- ): Promise<SetCookieResponse> => {
61
- const vstoken2 = await getNonUserAccess(publicationDomain);
62
- if (vstoken2.status === 'success') {
63
- setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
64
- }
65
- return {
66
- success: vstoken2.status === 'success',
67
- accessFeatures: vstoken2.access_features,
68
- };
69
- };
70
-
71
- export const setAccessCookies = async (
72
- publicationDomain: string
73
- ): Promise<SetCookieResponse> => {
74
- const setters: Array<Promise<SetCookieResponse>> = [];
75
- setters.push(
76
- setUserAccessCookie(publicationDomain).catch(() =>
77
- Promise.resolve({ success: false, accessFeatures: [] })
78
- )
79
- );
80
- setters.push(
81
- setNonUserAccessCookie(publicationDomain).catch(() =>
82
- Promise.resolve({ success: false, accessFeatures: [] })
83
- )
84
- );
85
- return Promise.all(setters).then((results) =>
86
- results.reduce(
87
- (accumulatedResult, result) => ({
88
- success: accumulatedResult.success && result.success,
89
- accessFeatures: accumulatedResult.accessFeatures
90
- .concat(result.accessFeatures)
91
- .filter((v, i, a) => a.indexOf(v) === i) // Remove duplicates by keeping only first copy
92
- .sort(), // We want to keep result deterministic for easy testing
93
- }),
94
- {
95
- success: true,
96
- accessFeatures: [],
97
- }
98
- )
99
- );
100
- };
101
-
102
- export const setHasPerformedPaywallReload = (requestedUrl?: string): void => {
103
- const expires = new Date();
104
- expires.setTime(expires.getTime() + 2 * 60 * 1000); // Add two minutes
105
-
106
- setCookie(
107
- 'aid-paywall-reload-performed',
108
- requestedUrl || getCurrentUrl(),
109
- expires
110
- );
111
- };
112
-
113
- export const hasRecentlyPerformedPaywallReload = (
114
- requestedUrl?: string
115
- ): boolean =>
116
- getCookie('aid-paywall-reload-performed') ===
117
- (requestedUrl || getCurrentUrl());
@@ -1,41 +0,0 @@
1
- import fetch from 'jest-fetch-mock';
2
- import cache from '../cache';
3
- import { getPrivacyPreferences } from './preferences';
4
-
5
- beforeEach(async () => {
6
- await cache.clear();
7
- });
8
-
9
- const europaResponse = {
10
- namespaces: {
11
- privacy_preferences: {
12
- allow_research_usage: 'true',
13
- connect_with_surveys: true,
14
- personalized_ads: 'false',
15
- personalized_content: false,
16
- },
17
- },
18
- };
19
-
20
- describe('privacyPreferences', () => {
21
- it('should return preferences', async () => {
22
- fetch.mockResponse(JSON.stringify(europaResponse));
23
- await getPrivacyPreferences();
24
- expect(fetch).toHaveBeenCalledTimes(1);
25
- });
26
-
27
- it('should use cache if available', async () => {
28
- fetch.mockResponse(JSON.stringify(europaResponse));
29
-
30
- jest.spyOn(cache, 'get').mockReturnValue(
31
- Promise.resolve({
32
- allowResearchUsage: true,
33
- connectWithSurveys: true,
34
- personalizedAds: false,
35
- personalizedContent: false,
36
- })
37
- );
38
- await Promise.all([getPrivacyPreferences(), getPrivacyPreferences()]);
39
- expect(fetch).toHaveBeenCalledTimes(0);
40
- });
41
- });
@@ -1,41 +0,0 @@
1
- import { Record, Boolean } from 'runtypes';
2
- import { getNamespaces } from '../clients/europa';
3
- import { UserAttributes } from '../types';
4
- import getOrUpdate from '../utils/getOrUpdate';
5
-
6
- const CACHE_DURATION = 5 * 60000; // 5 minutes
7
- const PRIVACY_PREFERENCES_CACHE_KEY = 'privacyPreferences';
8
-
9
- export const PrivacyPreferencesSchema = Record({
10
- allowResearchUsage: Boolean,
11
- connectWithSurveys: Boolean,
12
- personalizedAds: Boolean,
13
- personalizedContent: Boolean,
14
- });
15
-
16
- const toBoolean = (val: string | boolean) => val === 'true' || val === true;
17
-
18
- export const getPrivacyPreferences = async (): Promise<
19
- Pick<UserAttributes, 'privacyPreferences'>
20
- > =>
21
- getOrUpdate(PRIVACY_PREFERENCES_CACHE_KEY, PrivacyPreferencesSchema, () =>
22
- getNamespaces(['privacy_preferences']).then((namespaces) => {
23
- const privacyPreferences: any = namespaces.privacy_preferences;
24
-
25
- return {
26
- ttl: CACHE_DURATION,
27
- data: {
28
- allowResearchUsage: toBoolean(
29
- privacyPreferences.allow_research_usage
30
- ),
31
- connectWithSurveys: toBoolean(
32
- privacyPreferences.connect_with_surveys
33
- ),
34
- personalizedAds: toBoolean(privacyPreferences.personalized_ads),
35
- personalizedContent: toBoolean(
36
- privacyPreferences.personalized_content
37
- ),
38
- },
39
- };
40
- })
41
- ).then((data) => ({ privacyPreferences: data }));
@@ -1,22 +0,0 @@
1
- import {
2
- getNamespaces,
3
- saveNamespace,
4
- updateNamespace,
5
- } from '../clients/europa';
6
- import store from '../store';
7
-
8
- export const requestStorage = (namespaces: string[]) => {
9
- getNamespaces(namespaces).then((storage) => {
10
- store.merge('storage', storage);
11
- });
12
- };
13
-
14
- export const updateStorage = (storage: { [namespace: string]: object }) =>
15
- Object.keys(storage).forEach((namespace) =>
16
- updateNamespace(namespace, storage[namespace])
17
- );
18
-
19
- export const saveStorage = (storage: { [namespace: string]: object }) =>
20
- Object.keys(storage).forEach((namespace) =>
21
- saveNamespace(namespace, storage[namespace])
22
- );