@amedia/user 0.12.18 → 0.12.19

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 (43) hide show
  1. package/dist/actions/authentication.d.ts +0 -1
  2. package/dist/actions/authorization.d.ts +8 -3
  3. package/dist/clients/jupiter.d.ts +1 -1
  4. package/dist/eventHandlers/eventMap.d.ts +25 -0
  5. package/dist/eventHandlers/state.d.ts +1 -0
  6. package/dist/eventHandlers/state.spec.d.ts +4 -0
  7. package/dist/eventHandlers/storage.d.ts +1 -0
  8. package/dist/eventHandlers/storage.spec.d.ts +4 -0
  9. package/dist/eventHandlers/userAttributes.d.ts +0 -23
  10. package/dist/exceptions/PaywallError.d.ts +1 -0
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -2
  13. package/dist/lib/PaywallUnlockRequest.d.ts +16 -0
  14. package/dist/lib/PaywallUnlockRequest.spec.d.ts +4 -0
  15. package/dist/lib/UserDataRequest.d.ts +1 -0
  16. package/dist/lib/UserDataRequest.spec.d.ts +4 -0
  17. package/dist/store/index.d.ts +1 -1
  18. package/package.json +1 -1
  19. package/src/actions/authentication.ts +0 -3
  20. package/src/actions/authorization.ts +17 -9
  21. package/src/clients/jupiter.spec.ts +4 -4
  22. package/src/clients/jupiter.ts +1 -1
  23. package/src/eventHandlers/eventMap.ts +29 -0
  24. package/src/eventHandlers/state.spec.ts +59 -0
  25. package/src/eventHandlers/state.ts +23 -0
  26. package/src/eventHandlers/storage.spec.ts +106 -0
  27. package/src/eventHandlers/storage.ts +35 -0
  28. package/src/eventHandlers/userAttributes.spec.ts +5 -103
  29. package/src/eventHandlers/userAttributes.ts +1 -66
  30. package/src/exceptions/PaywallError.ts +3 -0
  31. package/src/index-storage.spec.ts +2 -2
  32. package/src/index-subscription.spec.ts +2 -0
  33. package/src/index.ts +3 -4
  34. package/src/janitor/jobs/accessCookieRefresher.spec.ts +6 -2
  35. package/src/janitor/jobs/removeUserStorage.spec.ts +4 -1
  36. package/src/lib/PaywallUnlockRequest.spec.ts +343 -0
  37. package/src/lib/PaywallUnlockRequest.ts +121 -0
  38. package/src/lib/UserDataRequest.spec.ts +58 -0
  39. package/src/lib/UserDataRequest.ts +39 -3
  40. package/src/store/index.ts +1 -1
  41. package/amedia-user-js.iml +0 -9
  42. package/src/lib/PaywallUnlocker.spec.ts +0 -196
  43. package/src/lib/PaywallUnlocker.ts +0 -113
@@ -2,7 +2,7 @@ import fetch from 'jest-fetch-mock';
2
2
  import log from '../utils/log';
3
3
  import {
4
4
  checkForAccess,
5
- getVstoken,
5
+ getUserAccess,
6
6
  AccessFeatures,
7
7
  CheckAccessForPaper,
8
8
  GetVstoken,
@@ -79,7 +79,7 @@ describe('jupiter service', () => {
79
79
 
80
80
  fetch.mockResponseOnce(JSON.stringify(response));
81
81
 
82
- await expect(getVstoken(publicationDomain)).resolves.toEqual(response);
82
+ await expect(getUserAccess(publicationDomain)).resolves.toEqual(response);
83
83
 
84
84
  expect(fetch).toHaveBeenCalledWith(
85
85
  `/api/jupiter/v1/get_vstoken/${publicationDomain}`,
@@ -94,7 +94,7 @@ describe('jupiter service', () => {
94
94
  status: 400,
95
95
  });
96
96
 
97
- await expect(getVstoken(publicationDomain)).rejects.toThrow(
97
+ await expect(getUserAccess(publicationDomain)).rejects.toThrow(
98
98
  'Request not ok (400: Bad Request).'
99
99
  );
100
100
  });
@@ -103,7 +103,7 @@ describe('jupiter service', () => {
103
103
  fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
104
104
  log.error = jest.fn();
105
105
 
106
- await expect(getVstoken(publicationDomain)).rejects.toThrow(
106
+ await expect(getUserAccess(publicationDomain)).rejects.toThrow(
107
107
  'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
108
108
  );
109
109
  });
@@ -75,7 +75,7 @@ export const checkForAccess = (
75
75
  throw undefined;
76
76
  });
77
77
 
78
- export const getVstoken = (publicationDomain: string): Promise<GetVstoken> =>
78
+ export const getUserAccess = (publicationDomain: string): Promise<GetVstoken> =>
79
79
  fetch(`/api/jupiter/v1/get_vstoken/${publicationDomain}`, { method: 'post' })
80
80
  .then((response) => {
81
81
  if (!response.ok) {
@@ -0,0 +1,29 @@
1
+ import { State, UserAttributes } from '../types';
2
+
3
+ export enum AmediaUserEvents {
4
+ REQUEST_STATE = 'amedia-user.request-state',
5
+ REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
6
+ REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
7
+ UPDATE_STORAGE = 'amedua-user.update-storage-namespaces',
8
+ ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
9
+ ON_STATE_UPDATED = 'amedia-user.on-state-updated',
10
+ ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
11
+ }
12
+
13
+ declare global {
14
+ interface WindowEventMap {
15
+ [AmediaUserEvents.REQUEST_STATE]: CustomEvent;
16
+ [AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<
17
+ Array<keyof UserAttributes>
18
+ >;
19
+ [AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
20
+ [AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
21
+ [namespace: string]: object;
22
+ }>;
23
+ [AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
24
+ [AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
25
+ [AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
26
+ [index: string]: unknown;
27
+ }>;
28
+ }
29
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+
5
+ import {
6
+ dispatch,
7
+ listenForAllEvents,
8
+ setRootElement,
9
+ } from '../utils/customEventWrapper';
10
+ import store from '../store';
11
+ import { AmediaUserEvents } from './eventMap';
12
+ import { listenForStateRequests } from './state';
13
+ import restoreAllMocks = jest.restoreAllMocks;
14
+
15
+ beforeEach(() => {
16
+ setRootElement(new Document());
17
+ restoreAllMocks();
18
+ store.clear();
19
+ });
20
+
21
+ describe('listenForUserAttributeRequests', () => {
22
+ let currentState;
23
+ beforeEach(() => {
24
+ listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
25
+ currentState = evt.detail;
26
+ });
27
+ listenForStateRequests();
28
+ });
29
+
30
+ describe('when empty initial state', () => {
31
+ beforeEach(() => {
32
+ dispatch(new CustomEvent(AmediaUserEvents.REQUEST_STATE));
33
+ });
34
+ it('should not return empty state', () => {
35
+ expect(currentState).toBeUndefined();
36
+ });
37
+ });
38
+
39
+ describe('with initial state', () => {
40
+ beforeEach(() => {
41
+ store.set('state', { isLoggedIn: false });
42
+ dispatch(new CustomEvent(AmediaUserEvents.REQUEST_STATE));
43
+ });
44
+ it('should return expected state', () => {
45
+ expect(currentState).toEqual({ isLoggedIn: false });
46
+ });
47
+ describe('when state changes', () => {
48
+ beforeEach(() => {
49
+ store.merge('state', { emergencyMode: ['aid'] });
50
+ });
51
+ it('should have updated state', () => {
52
+ expect(currentState).toEqual({
53
+ isLoggedIn: false,
54
+ emergencyMode: ['aid'],
55
+ });
56
+ });
57
+ });
58
+ });
59
+ });
@@ -0,0 +1,23 @@
1
+ import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
2
+ import store from '../store';
3
+ import { AmediaUserEvents } from './eventMap';
4
+
5
+ export const listenForStateRequests = () => {
6
+ listenWithExclusivity(AmediaUserEvents.REQUEST_STATE, () => {
7
+ const state = store.get('state');
8
+ if (state && Object.keys(state).length > 0) {
9
+ dispatch(
10
+ new CustomEvent(AmediaUserEvents.ON_STATE_UPDATED, {
11
+ detail: state,
12
+ })
13
+ );
14
+ }
15
+ store.on('state', (updatedState) => {
16
+ dispatch(
17
+ new CustomEvent(AmediaUserEvents.ON_STATE_UPDATED, {
18
+ detail: updatedState,
19
+ })
20
+ );
21
+ });
22
+ });
23
+ };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
4
+
5
+ import {
6
+ dispatch,
7
+ listenForAllEvents,
8
+ setRootElement,
9
+ } from '../utils/customEventWrapper';
10
+ import * as userData from '../actions/userData';
11
+ import * as storage from '../actions/storage';
12
+ import store from '../store';
13
+ import { listenForStorageRequests } from './storage';
14
+ import { AmediaUserEvents } from './eventMap';
15
+ import restoreAllMocks = jest.restoreAllMocks;
16
+
17
+ beforeEach(() => {
18
+ setRootElement(new Document());
19
+ restoreAllMocks();
20
+ store.clear();
21
+ });
22
+
23
+ describe('listenForStorageRequests', () => {
24
+ let currentStorage;
25
+ beforeEach(() => {
26
+ listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
27
+ currentStorage = evt.detail;
28
+ });
29
+ listenForStorageRequests();
30
+ jest
31
+ .spyOn(userData, 'requestAttributes')
32
+ .mockReturnValue(Promise.resolve());
33
+ jest.spyOn(storage, 'requestStorage').mockReturnValue();
34
+ });
35
+
36
+ describe('AmediaUserEvents.REQUEST_STORAGE_NAMESPACES received', () => {
37
+ describe('no previous namespaces', () => {
38
+ beforeEach(() => {
39
+ dispatch(
40
+ new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
41
+ detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
42
+ })
43
+ );
44
+ });
45
+ it('calls requestAttributes with requested attributes', () => {
46
+ expect(storage.requestStorage).toHaveBeenCalledWith([
47
+ 'privacy_preferences',
48
+ 'euucp',
49
+ 'favorite_teams',
50
+ ]);
51
+ });
52
+ it('does not receive a storage event', () => {
53
+ expect(currentStorage).toBeUndefined();
54
+ });
55
+ });
56
+
57
+ describe('with some namespaces', () => {
58
+ beforeEach(() => {
59
+ store.set('storage', {
60
+ favorite_teams: { stabaek: true, sf: true },
61
+ });
62
+ dispatch(
63
+ new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
64
+ detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
65
+ })
66
+ );
67
+ });
68
+ it('calls requestStorage with only namespaces we did not already have', () => {
69
+ expect(storage.requestStorage).toHaveBeenCalledWith([
70
+ 'privacy_preferences',
71
+ 'euucp',
72
+ ]);
73
+ });
74
+ it('receives initial event with previous namespaces', () => {
75
+ expect(currentStorage).toEqual({
76
+ favorite_teams: { stabaek: true, sf: true },
77
+ });
78
+ });
79
+ });
80
+
81
+ describe('with previous state and all attributes', () => {
82
+ beforeEach(() => {
83
+ store.set('storage', {
84
+ favorite_teams: { stabaek: true, sf: true },
85
+ privacy_preferences: { ads: true, tracking: true },
86
+ euucp: { what_is: 'this' },
87
+ });
88
+ dispatch(
89
+ new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
90
+ detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
91
+ })
92
+ );
93
+ });
94
+ it('does not call requestAttributes', () => {
95
+ expect(storage.requestStorage).not.toHaveBeenCalled();
96
+ });
97
+ it('receives initial event with all namespaces', () => {
98
+ expect(currentStorage).toEqual({
99
+ favorite_teams: { stabaek: true, sf: true },
100
+ privacy_preferences: { ads: true, tracking: true },
101
+ euucp: { what_is: 'this' },
102
+ });
103
+ });
104
+ });
105
+ });
106
+ });
@@ -0,0 +1,35 @@
1
+ import store from '../store';
2
+ import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
3
+ import { AmediaUserEvents } from './eventMap';
4
+ import { requestStorage, updateStorage } from '../actions/storage';
5
+
6
+ export const listenForStorageRequests = () => {
7
+ store.on('storage', (storage) => {
8
+ dispatch(
9
+ new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, { detail: storage })
10
+ );
11
+ });
12
+
13
+ listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
14
+ const requestedNamespaces = evt.detail;
15
+ const currentStorage = store.get('storage') || {};
16
+ if (Object.keys(currentStorage).length > 0) {
17
+ dispatch(
18
+ new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, {
19
+ detail: currentStorage,
20
+ })
21
+ );
22
+ }
23
+ const missingNamespaces = requestedNamespaces.filter(
24
+ (namespace) => currentStorage[namespace] === undefined
25
+ );
26
+ if (missingNamespaces.length > 0) {
27
+ requestStorage(missingNamespaces);
28
+ }
29
+ });
30
+
31
+ listenWithExclusivity(AmediaUserEvents.UPDATE_STORAGE, (evt) => {
32
+ updateStorage(evt.detail);
33
+ });
34
+ };
35
+ listenForStorageRequests();
@@ -7,13 +7,10 @@ import {
7
7
  listenForAllEvents,
8
8
  setRootElement,
9
9
  } from '../utils/customEventWrapper';
10
- import {
11
- AmediaUserEvents,
12
- listenForUserAttributeRequests,
13
- } from './userAttributes';
10
+ import { listenForUserAttributeRequests } from './userAttributes';
14
11
  import * as userData from '../actions/userData';
15
- import * as storage from '../actions/storage';
16
12
  import store from '../store';
13
+ import { AmediaUserEvents } from './eventMap';
17
14
  import restoreAllMocks = jest.restoreAllMocks;
18
15
 
19
16
  beforeEach(() => {
@@ -23,28 +20,19 @@ beforeEach(() => {
23
20
  });
24
21
 
25
22
  describe('listenForUserAttributeRequests', () => {
26
- let currentState;
27
23
  let currentAttributes;
28
- let currentStorage;
29
24
  beforeEach(() => {
30
- listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
31
- currentState = evt.detail;
32
- });
33
25
  listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
34
26
  currentAttributes = evt.detail;
35
27
  });
36
- listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
37
- currentStorage = evt.detail;
38
- });
39
28
  listenForUserAttributeRequests();
40
29
  jest
41
30
  .spyOn(userData, 'requestAttributes')
42
31
  .mockReturnValue(Promise.resolve());
43
- jest.spyOn(storage, 'requestStorage').mockReturnValue();
44
32
  });
45
33
 
46
34
  describe('AmediaUserEvents.REQUEST_USER_ATTRIBUTES received', () => {
47
- describe('no previous state or attributes', () => {
35
+ describe('no previous attributes', () => {
48
36
  beforeEach(() => {
49
37
  dispatch(
50
38
  new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
@@ -59,19 +47,13 @@ describe('listenForUserAttributeRequests', () => {
59
47
  'email',
60
48
  ]);
61
49
  });
62
- it('does not receive a state event', () => {
63
- expect(currentState).toBeUndefined();
64
- });
65
50
  it('does not receive an attribute event', () => {
66
51
  expect(currentAttributes).toBeUndefined();
67
52
  });
68
53
  });
69
54
 
70
- describe('with previous state and some attributes', () => {
55
+ describe('with some attributes present already', () => {
71
56
  beforeEach(() => {
72
- store.set('state', {
73
- isLoggedIn: true,
74
- });
75
57
  store.set('userAttributes', {
76
58
  uuid: 'someuuid',
77
59
  });
@@ -87,9 +69,6 @@ describe('listenForUserAttributeRequests', () => {
87
69
  'email',
88
70
  ]);
89
71
  });
90
- it('receives initial event with previous state', () => {
91
- expect(currentState).toEqual({ isLoggedIn: true });
92
- });
93
72
  it('receives initial event with previous attributes', () => {
94
73
  expect(currentAttributes).toEqual({
95
74
  uuid: 'someuuid',
@@ -97,11 +76,8 @@ describe('listenForUserAttributeRequests', () => {
97
76
  });
98
77
  });
99
78
 
100
- describe('with previous state and all attributes', () => {
79
+ describe('with all attributes present already', () => {
101
80
  beforeEach(() => {
102
- store.set('state', {
103
- isLoggedIn: true,
104
- });
105
81
  store.set('userAttributes', {
106
82
  uuid: 'someuuid',
107
83
  name: 'Some Name',
@@ -116,9 +92,6 @@ describe('listenForUserAttributeRequests', () => {
116
92
  it('does not call requestAttributes', () => {
117
93
  expect(userData.requestAttributes).not.toHaveBeenCalled();
118
94
  });
119
- it('receives initial event with previous state', () => {
120
- expect(currentState).toEqual({ isLoggedIn: true });
121
- });
122
95
  it('receives initial event with all attributes', () => {
123
96
  expect(currentAttributes).toEqual({
124
97
  uuid: 'someuuid',
@@ -128,75 +101,4 @@ describe('listenForUserAttributeRequests', () => {
128
101
  });
129
102
  });
130
103
  });
131
-
132
- describe('AmediaUserEvents.REQUEST_STORAGE_NAMESPACES received', () => {
133
- describe('no previous namespaces', () => {
134
- beforeEach(() => {
135
- dispatch(
136
- new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
137
- detail: ['privacy_preferences', 'uucp', 'favorite_teams'],
138
- })
139
- );
140
- });
141
- it('calls requestAttributes with requested attributes', () => {
142
- expect(storage.requestStorage).toHaveBeenCalledWith([
143
- 'privacy_preferences',
144
- 'uucp',
145
- 'favorite_teams',
146
- ]);
147
- });
148
- it('does not receive a storage event', () => {
149
- expect(currentStorage).toBeUndefined();
150
- });
151
- });
152
-
153
- describe('with some namespaces', () => {
154
- beforeEach(() => {
155
- store.set('storage', {
156
- favorite_teams: { stabaek: true, sf: true },
157
- });
158
- dispatch(
159
- new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
160
- detail: ['privacy_preferences', 'uucp', 'favorite_teams'],
161
- })
162
- );
163
- });
164
- it('calls requestStorage with only namespaces we did not already have', () => {
165
- expect(storage.requestStorage).toHaveBeenCalledWith([
166
- 'privacy_preferences',
167
- 'uucp',
168
- ]);
169
- });
170
- it('receives initial event with previous namespaces', () => {
171
- expect(currentStorage).toEqual({
172
- favorite_teams: { stabaek: true, sf: true },
173
- });
174
- });
175
- });
176
-
177
- describe('with previous state and all attributes', () => {
178
- beforeEach(() => {
179
- store.set('storage', {
180
- favorite_teams: { stabaek: true, sf: true },
181
- privacy_preferences: { ads: true, tracking: true },
182
- uucp: { what_is: 'this' },
183
- });
184
- dispatch(
185
- new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
186
- detail: ['privacy_preferences', 'uucp', 'favorite_teams'],
187
- })
188
- );
189
- });
190
- it('does not call requestAttributes', () => {
191
- expect(storage.requestStorage).not.toHaveBeenCalled();
192
- });
193
- it('receives initial event with all namespaces', () => {
194
- expect(currentStorage).toEqual({
195
- favorite_teams: { stabaek: true, sf: true },
196
- privacy_preferences: { ads: true, tracking: true },
197
- uucp: { what_is: 'this' },
198
- });
199
- });
200
- });
201
- });
202
104
  });
@@ -1,34 +1,7 @@
1
1
  import { requestAttributes } from '../actions/userData';
2
2
  import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
3
- import { State, UserAttributes } from '../types';
4
3
  import store from '../store';
5
- import { requestStorage, updateStorage } from '../actions/storage';
6
-
7
- export enum AmediaUserEvents {
8
- REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
9
- REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
10
- UPDATE_STORAGE = 'amedua-user.update-storage-namespaces',
11
- ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
12
- ON_STATE_UPDATED = 'amedia-user.on-state-updated',
13
- ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
14
- }
15
-
16
- declare global {
17
- interface WindowEventMap {
18
- [AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<
19
- Array<keyof UserAttributes>
20
- >;
21
- [AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
22
- [AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
23
- [namespace: string]: object;
24
- }>;
25
- [AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
26
- [AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
27
- [AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
28
- [index: string]: unknown;
29
- }>;
30
- }
31
- }
4
+ import { AmediaUserEvents } from './eventMap';
32
5
 
33
6
  export const listenForUserAttributeRequests = () => {
34
7
  store.on('userAttributes', (user) => {
@@ -38,26 +11,8 @@ export const listenForUserAttributeRequests = () => {
38
11
  })
39
12
  );
40
13
  });
41
- store.on('state', (state) => {
42
- dispatch(
43
- new CustomEvent(AmediaUserEvents.ON_STATE_UPDATED, { detail: state })
44
- );
45
- });
46
- store.on('storage', (storage) => {
47
- dispatch(
48
- new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, { detail: storage })
49
- );
50
- });
51
14
  listenWithExclusivity(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, (evt) => {
52
15
  const requestedAttributes = evt.detail;
53
- const currentState = store.get('state');
54
- if (currentState !== undefined) {
55
- dispatch(
56
- new CustomEvent(AmediaUserEvents.ON_STATE_UPDATED, {
57
- detail: currentState,
58
- })
59
- );
60
- }
61
16
  const currentAttributes = store.get('userAttributes') || {};
62
17
  if (Object.keys(currentAttributes).length > 0) {
63
18
  dispatch(
@@ -73,26 +28,6 @@ export const listenForUserAttributeRequests = () => {
73
28
  requestAttributes(missingAttributes);
74
29
  }
75
30
  });
76
- listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
77
- const requestedNamespaces = evt.detail;
78
- const currentStorage = store.get('storage') || {};
79
- if (Object.keys(currentStorage).length > 0) {
80
- dispatch(
81
- new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, {
82
- detail: currentStorage,
83
- })
84
- );
85
- }
86
- const missingNamespaces = requestedNamespaces.filter(
87
- (namespace) => currentStorage[namespace] === undefined
88
- );
89
- if (missingNamespaces.length > 0) {
90
- requestStorage(missingNamespaces);
91
- }
92
- });
93
- listenWithExclusivity(AmediaUserEvents.UPDATE_STORAGE, (evt) => {
94
- updateStorage(evt.detail);
95
- });
96
31
  };
97
32
 
98
33
  listenForUserAttributeRequests();
@@ -3,6 +3,9 @@ class PaywallError extends Error {
3
3
 
4
4
  static ERROR_ENABLING_ACCESS_FAILED = 'ENABLING_ACCESS_FAILED';
5
5
 
6
+ static ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES =
7
+ 'NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES';
8
+
6
9
  public code: string;
7
10
 
8
11
  constructor(message: string, code: string) {
@@ -8,7 +8,7 @@ import * as storage from './actions/storage';
8
8
  import store from './store';
9
9
  import UserDataRequest from './lib/UserDataRequest';
10
10
  import { setRootElement } from './utils/customEventWrapper';
11
- import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
11
+ import { listenForStorageRequests } from './eventHandlers/storage';
12
12
  import restoreAllMocks = jest.restoreAllMocks;
13
13
 
14
14
  beforeEach(() => {
@@ -23,7 +23,7 @@ beforeEach(() => {
23
23
 
24
24
  // Also, since we keep creating new root elements, we also
25
25
  // need to attach to these new roots.
26
- listenForUserAttributeRequests();
26
+ listenForStorageRequests();
27
27
  });
28
28
 
29
29
  describe('Storage', () => {
@@ -7,6 +7,7 @@ import * as userData from './actions/userData';
7
7
  import { setRootElement } from './utils/customEventWrapper';
8
8
  import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
9
9
  import store from './store';
10
+ import { listenForStateRequests } from './eventHandlers/state';
10
11
  import restoreAllMocks = jest.restoreAllMocks;
11
12
 
12
13
  beforeEach(() => {
@@ -22,6 +23,7 @@ beforeEach(() => {
22
23
  // Also, since we keep creating new root elements, we also
23
24
  // need to attach to these new roots.
24
25
  listenForUserAttributeRequests();
26
+ listenForStateRequests();
25
27
  });
26
28
 
27
29
  describe('Public endpoint - userdata', () => {
package/src/index.ts CHANGED
@@ -2,8 +2,8 @@ import { ActivationEvents } from './eventHandlers/activation';
2
2
  import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
3
3
  import * as authentication from './actions/authentication';
4
4
  import UserDataRequest from './lib/UserDataRequest';
5
- import PaywallUnlocker from './lib/PaywallUnlocker';
6
- import { AmediaUserEvents } from './eventHandlers/userAttributes';
5
+ import PaywallUnlockRequest from './lib/PaywallUnlockRequest';
6
+ import { AmediaUserEvents } from './eventHandlers/eventMap';
7
7
  import { getCurrentUrl } from './utils/url';
8
8
  import { runJanitor } from './janitor';
9
9
 
@@ -12,6 +12,7 @@ import { runJanitor } from './janitor';
12
12
  * get notified when something changes
13
13
  */
14
14
  export const user = () => new UserDataRequest();
15
+ export const paywall = () => new PaywallUnlockRequest();
15
16
 
16
17
  /**
17
18
  * This will, if necessary, trigger a redirect to www.aid.no and return
@@ -37,8 +38,6 @@ export const tryActivateSubscriptionAccess = (
37
38
  );
38
39
  });
39
40
 
40
- export const paywall = () => new PaywallUnlocker();
41
-
42
41
  export const updateStorage = (storage: { [index: string]: Object }): void =>
43
42
  dispatch(
44
43
  new CustomEvent(AmediaUserEvents.UPDATE_STORAGE, { detail: storage })
@@ -21,10 +21,14 @@ beforeEach(() => {
21
21
  url.setCurrentHostname('www.avisnavn.no');
22
22
  jest
23
23
  .spyOn(authorization, 'setUserAccessCookie')
24
- .mockImplementation(() => Promise.resolve(true));
24
+ .mockImplementation(() =>
25
+ Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
26
+ );
25
27
  jest
26
28
  .spyOn(authorization, 'setNonUserAccessCookie')
27
- .mockImplementation(() => Promise.resolve(true));
29
+ .mockImplementation(() =>
30
+ Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
31
+ );
28
32
  });
29
33
 
30
34
  function mockAccessCookie(validHours, uuid, accessFeatures, domain) {
@@ -3,16 +3,19 @@
3
3
  */
4
4
  import store from '../../store';
5
5
  import { removeUserStorage } from './removeUserStorage';
6
+ import { listenForStateRequests } from '../../eventHandlers/state';
6
7
  import restoreAllMocks = jest.restoreAllMocks;
7
8
 
8
9
  describe('removeUserStorage', () => {
9
10
  beforeEach(() => {
10
11
  restoreAllMocks();
12
+ store.clear();
11
13
  spyOn(store, 'set').and.callThrough();
14
+ listenForStateRequests();
12
15
  removeUserStorage();
13
16
  });
14
17
 
15
- describe('when not logged in', () => {
18
+ describe('when logged in', () => {
16
19
  beforeEach(() => {
17
20
  store.set('state', { isLoggedIn: true });
18
21
  });