@amedia/user 0.12.17 → 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.
- package/dist/actions/authentication.d.ts +0 -1
- package/dist/actions/authorization.d.ts +8 -3
- package/dist/clients/jupiter.d.ts +1 -1
- package/dist/eventHandlers/eventMap.d.ts +25 -0
- package/dist/eventHandlers/state.d.ts +1 -0
- package/dist/eventHandlers/state.spec.d.ts +4 -0
- package/dist/eventHandlers/storage.d.ts +1 -0
- package/dist/eventHandlers/storage.spec.d.ts +4 -0
- package/dist/eventHandlers/userAttributes.d.ts +0 -23
- package/dist/exceptions/PaywallError.d.ts +1 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -2
- package/dist/lib/PaywallUnlockRequest.d.ts +16 -0
- package/dist/lib/PaywallUnlockRequest.spec.d.ts +4 -0
- package/dist/lib/UserDataRequest.d.ts +22 -0
- package/dist/lib/UserDataRequest.spec.d.ts +4 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/utils/log.d.ts +4 -16
- package/package.json +1 -1
- package/setupJest.js +3 -0
- package/src/actions/authentication.ts +0 -3
- package/src/actions/authorization.ts +17 -9
- package/src/actions/userData.spec.ts +0 -31
- package/src/actions/userData.ts +1 -1
- package/src/clients/europa.spec.ts +2 -0
- package/src/clients/jupiter.spec.ts +6 -32
- package/src/clients/jupiter.ts +29 -30
- package/src/clients/mercury.spec.ts +2 -0
- package/src/clients/nebula.spec.ts +2 -0
- package/src/clients/pluto.spec.ts +18 -44
- package/src/clients/pluto.ts +4 -14
- package/src/eventHandlers/eventMap.ts +29 -0
- package/src/eventHandlers/state.spec.ts +59 -0
- package/src/eventHandlers/state.ts +23 -0
- package/src/eventHandlers/storage.spec.ts +106 -0
- package/src/eventHandlers/storage.ts +35 -0
- package/src/eventHandlers/userAttributes.spec.ts +5 -103
- package/src/eventHandlers/userAttributes.ts +1 -66
- package/src/exceptions/PaywallError.ts +3 -0
- package/src/index-storage.spec.ts +5 -5
- package/src/index-subscription.spec.ts +21 -11
- package/src/index.ts +5 -6
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +27 -19
- package/src/janitor/jobs/accessCookieRefresher.ts +2 -2
- package/src/janitor/jobs/removeUserStorage.spec.ts +4 -1
- package/src/janitor/jobs/removeUserStorage.ts +3 -2
- package/src/lib/PaywallUnlockRequest.spec.ts +343 -0
- package/src/lib/PaywallUnlockRequest.ts +121 -0
- package/src/lib/UserDataRequest.spec.ts +58 -0
- package/src/lib/{User.ts → UserDataRequest.ts} +45 -11
- package/src/store/index.ts +1 -1
- package/src/utils/log.ts +12 -16
- package/src/utils/logValidationError.ts +6 -6
- package/amedia-user-js.iml +0 -9
- package/src/lib/PaywallUnlocker.spec.ts +0 -196
- package/src/lib/PaywallUnlocker.ts +0 -113
|
@@ -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
|
|
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
|
|
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
|
|
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 {
|
|
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) {
|
|
@@ -6,9 +6,9 @@ import * as europa from './clients/europa';
|
|
|
6
6
|
import { updateStorage } from './index';
|
|
7
7
|
import * as storage from './actions/storage';
|
|
8
8
|
import store from './store';
|
|
9
|
-
import
|
|
9
|
+
import UserDataRequest from './lib/UserDataRequest';
|
|
10
10
|
import { setRootElement } from './utils/customEventWrapper';
|
|
11
|
-
import {
|
|
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
|
-
|
|
26
|
+
listenForStorageRequests();
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
describe('Storage', () => {
|
|
@@ -51,7 +51,7 @@ describe('Storage', () => {
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
const onUpdate = jest.fn();
|
|
54
|
-
new
|
|
54
|
+
new UserDataRequest().withStorage(['namespace']).subscribe(onUpdate);
|
|
55
55
|
|
|
56
56
|
expect(onUpdate).toHaveBeenCalledWith({
|
|
57
57
|
storage: {
|
|
@@ -79,7 +79,7 @@ describe('Storage', () => {
|
|
|
79
79
|
});
|
|
80
80
|
|
|
81
81
|
const onUpdate = jest.fn();
|
|
82
|
-
new
|
|
82
|
+
new UserDataRequest().withStorage(['one', 'three']).subscribe(onUpdate);
|
|
83
83
|
|
|
84
84
|
expect(onUpdate).toHaveBeenCalledWith({
|
|
85
85
|
storage: {
|
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import UserDataRequest from './lib/UserDataRequest';
|
|
6
6
|
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', () => {
|
|
@@ -32,7 +34,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
const onUserUpdate = jest.fn();
|
|
35
|
-
new
|
|
37
|
+
new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
|
|
36
38
|
|
|
37
39
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
38
40
|
attributes: {
|
|
@@ -50,7 +52,9 @@ describe('Public endpoint - userdata', () => {
|
|
|
50
52
|
});
|
|
51
53
|
|
|
52
54
|
const onUserUpdate = jest.fn();
|
|
53
|
-
new
|
|
55
|
+
new UserDataRequest()
|
|
56
|
+
.withAttributes(['name', 'uuid'])
|
|
57
|
+
.subscribe(onUserUpdate);
|
|
54
58
|
|
|
55
59
|
expect(onUserUpdate).nthCalledWith(1, {
|
|
56
60
|
attributes: {
|
|
@@ -74,10 +78,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
74
78
|
});
|
|
75
79
|
|
|
76
80
|
const onUserUpdate1 = jest.fn();
|
|
77
|
-
new
|
|
81
|
+
new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate1);
|
|
78
82
|
|
|
79
83
|
const onUserUpdate2 = jest.fn();
|
|
80
|
-
new
|
|
84
|
+
new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate2);
|
|
81
85
|
|
|
82
86
|
expect(onUserUpdate1).toHaveBeenCalledTimes(1);
|
|
83
87
|
expect(onUserUpdate1).toHaveBeenCalledWith({
|
|
@@ -103,7 +107,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
103
107
|
});
|
|
104
108
|
|
|
105
109
|
const firstOnUserUpdate = jest.fn();
|
|
106
|
-
new
|
|
110
|
+
new UserDataRequest().withAttributes(['name']).subscribe(firstOnUserUpdate);
|
|
107
111
|
|
|
108
112
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
109
113
|
expect(firstOnUserUpdate).nthCalledWith(1, {
|
|
@@ -132,7 +136,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
132
136
|
});
|
|
133
137
|
|
|
134
138
|
const onUserUpdate = jest.fn();
|
|
135
|
-
new
|
|
139
|
+
new UserDataRequest().withAttributes(['name']).subscribe(onUserUpdate);
|
|
136
140
|
|
|
137
141
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
138
142
|
attributes: {
|
|
@@ -152,7 +156,9 @@ describe('Public endpoint - userdata', () => {
|
|
|
152
156
|
});
|
|
153
157
|
|
|
154
158
|
const onUserUpdate = jest.fn();
|
|
155
|
-
new
|
|
159
|
+
new UserDataRequest()
|
|
160
|
+
.withAttributes(['name', 'email'])
|
|
161
|
+
.subscribe(onUserUpdate);
|
|
156
162
|
|
|
157
163
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
158
164
|
attributes: {
|
|
@@ -173,10 +179,14 @@ describe('Public endpoint - userdata', () => {
|
|
|
173
179
|
});
|
|
174
180
|
|
|
175
181
|
const firstOnUserUpdate = jest.fn();
|
|
176
|
-
new
|
|
182
|
+
new UserDataRequest()
|
|
183
|
+
.withAttributes(['name', 'email'])
|
|
184
|
+
.subscribe(firstOnUserUpdate);
|
|
177
185
|
|
|
178
186
|
const secondOnUserUpdate = jest.fn();
|
|
179
|
-
new
|
|
187
|
+
new UserDataRequest()
|
|
188
|
+
.withAttributes(['email'])
|
|
189
|
+
.subscribe(secondOnUserUpdate);
|
|
180
190
|
|
|
181
191
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
182
192
|
expect(firstOnUserUpdate).nthCalledWith(1, {
|
|
@@ -207,7 +217,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
207
217
|
|
|
208
218
|
const onUpdate = jest.fn();
|
|
209
219
|
|
|
210
|
-
new
|
|
220
|
+
new UserDataRequest().withAttributes(['name']).subscribe(onUpdate);
|
|
211
221
|
|
|
212
222
|
expect(onUpdate).toHaveBeenCalledWith({
|
|
213
223
|
attributes: { name: 'Donald Duck' },
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { ActivationEvents } from './eventHandlers/activation';
|
|
2
2
|
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
3
3
|
import * as authentication from './actions/authentication';
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import { AmediaUserEvents } from './eventHandlers/
|
|
4
|
+
import UserDataRequest from './lib/UserDataRequest';
|
|
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
|
|
|
@@ -11,7 +11,8 @@ import { runJanitor } from './janitor';
|
|
|
11
11
|
* Enables you to subscribe to the user attributes you care about and
|
|
12
12
|
* get notified when something changes
|
|
13
13
|
*/
|
|
14
|
-
export const user = () => new
|
|
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 })
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import accessCookieRefresher from './accessCookieRefresher';
|
|
6
6
|
import * as url from '../../utils/url';
|
|
7
|
-
import
|
|
7
|
+
import UserDataRequest from '../../lib/UserDataRequest';
|
|
8
8
|
import * as cookie from '../../lib/cookie';
|
|
9
9
|
import * as authorization from '../../actions/authorization';
|
|
10
10
|
import { setRootElement } from '../../utils/customEventWrapper';
|
|
@@ -21,10 +21,14 @@ beforeEach(() => {
|
|
|
21
21
|
url.setCurrentHostname('www.avisnavn.no');
|
|
22
22
|
jest
|
|
23
23
|
.spyOn(authorization, 'setUserAccessCookie')
|
|
24
|
-
.mockImplementation(() =>
|
|
24
|
+
.mockImplementation(() =>
|
|
25
|
+
Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
|
|
26
|
+
);
|
|
25
27
|
jest
|
|
26
28
|
.spyOn(authorization, 'setNonUserAccessCookie')
|
|
27
|
-
.mockImplementation(() =>
|
|
29
|
+
.mockImplementation(() =>
|
|
30
|
+
Promise.resolve({ success: true, accessFeatures: ['newspaper'] })
|
|
31
|
+
);
|
|
28
32
|
});
|
|
29
33
|
|
|
30
34
|
function mockAccessCookie(validHours, uuid, accessFeatures, domain) {
|
|
@@ -38,16 +42,18 @@ function mockAccessCookie(validHours, uuid, accessFeatures, domain) {
|
|
|
38
42
|
describe('accessCookieRefresher', () => {
|
|
39
43
|
describe('User is logged in', () => {
|
|
40
44
|
beforeEach(() => {
|
|
41
|
-
jest
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
jest
|
|
46
|
+
.spyOn(UserDataRequest.prototype, 'subscribe')
|
|
47
|
+
.mockImplementation((callback) => {
|
|
48
|
+
callback({
|
|
49
|
+
attributes: {
|
|
50
|
+
uuid: mockUuid,
|
|
51
|
+
},
|
|
52
|
+
state: {
|
|
53
|
+
isLoggedIn: true,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
49
56
|
});
|
|
50
|
-
});
|
|
51
57
|
store.set('state', { isLoggedIn: true });
|
|
52
58
|
});
|
|
53
59
|
|
|
@@ -166,14 +172,16 @@ describe('accessCookieRefresher', () => {
|
|
|
166
172
|
});
|
|
167
173
|
describe('User is not logged in', () => {
|
|
168
174
|
beforeEach(() => {
|
|
169
|
-
jest
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
+
jest
|
|
176
|
+
.spyOn(UserDataRequest.prototype, 'subscribe')
|
|
177
|
+
.mockImplementation((callback) => {
|
|
178
|
+
callback({
|
|
179
|
+
attributes: {},
|
|
180
|
+
state: {
|
|
181
|
+
isLoggedIn: false,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
175
184
|
});
|
|
176
|
-
});
|
|
177
185
|
store.set('state', { isLoggedIn: false });
|
|
178
186
|
});
|
|
179
187
|
describe('without any cookies', () => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getCookie, removeCookie } from '../../lib/cookie';
|
|
2
|
-
import
|
|
2
|
+
import UserDataRequest from '../../lib/UserDataRequest';
|
|
3
3
|
import {
|
|
4
4
|
setNonUserAccessCookie,
|
|
5
5
|
setUserAccessCookie,
|
|
@@ -15,7 +15,7 @@ async function verifyAndSetUserAccessCookie() {
|
|
|
15
15
|
const cookie = getCookie(cookieName);
|
|
16
16
|
let hasVerifiedVstoken = false;
|
|
17
17
|
|
|
18
|
-
new
|
|
18
|
+
new UserDataRequest()
|
|
19
19
|
.withAttributes(['uuid'])
|
|
20
20
|
.subscribe(async ({ attributes, state }) => {
|
|
21
21
|
if (state.isLoggedIn === false) {
|
|
@@ -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
|
|
18
|
+
describe('when logged in', () => {
|
|
16
19
|
beforeEach(() => {
|
|
17
20
|
store.set('state', { isLoggedIn: true });
|
|
18
21
|
});
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import UserDataRequest from '../../lib/UserDataRequest';
|
|
2
2
|
import store from '../../store';
|
|
3
3
|
|
|
4
4
|
export function removeUserStorage() {
|
|
5
|
-
new
|
|
5
|
+
new UserDataRequest().subscribe(({ state }) => {
|
|
6
|
+
// Need to explicitly check for false here until we have then()
|
|
6
7
|
if (state.isLoggedIn === false) {
|
|
7
8
|
store.set('userAttributes', {});
|
|
8
9
|
store.set('storage', {});
|