@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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
checkForAccess,
|
|
3
3
|
getNonUserAccess,
|
|
4
|
-
|
|
4
|
+
getUserAccess,
|
|
5
5
|
} from '../clients/jupiter';
|
|
6
6
|
import { getCurrentPublicationDomain, getCurrentUrl } from '../utils/url';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
@@ -9,6 +9,8 @@ import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
|
9
9
|
import { logout } from './authentication';
|
|
10
10
|
import { getCookie, setCookie } from '../lib/cookie';
|
|
11
11
|
|
|
12
|
+
type SetCookieResponse = { success: boolean; accessFeatures: string[] };
|
|
13
|
+
|
|
12
14
|
export const getAccess = (
|
|
13
15
|
accessFeatures: string[] = ['newspaper']
|
|
14
16
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
@@ -36,8 +38,8 @@ export const hasAccess = (
|
|
|
36
38
|
|
|
37
39
|
export const setUserAccessCookie = async (
|
|
38
40
|
publicationDomain
|
|
39
|
-
): Promise<
|
|
40
|
-
const vstoken = await
|
|
41
|
+
): Promise<SetCookieResponse> => {
|
|
42
|
+
const vstoken = await getUserAccess(publicationDomain);
|
|
41
43
|
if (vstoken.status === 'success') {
|
|
42
44
|
/**
|
|
43
45
|
* The VSTOKEN cookie will live for 365 days, but a timestamp in the payload will ensure that it cannot
|
|
@@ -45,20 +47,24 @@ export const setUserAccessCookie = async (
|
|
|
45
47
|
* used to keep subscribers from seeing ads for buying the subscription they already have.
|
|
46
48
|
*/
|
|
47
49
|
setCookie('VSTOKEN', vstoken.cookie, 365, '/');
|
|
48
|
-
return true;
|
|
49
50
|
}
|
|
50
|
-
return
|
|
51
|
+
return {
|
|
52
|
+
success: vstoken.status === 'success',
|
|
53
|
+
accessFeatures: vstoken.access_features,
|
|
54
|
+
};
|
|
51
55
|
};
|
|
52
56
|
|
|
53
57
|
export const setNonUserAccessCookie = async (
|
|
54
58
|
publicationDomain
|
|
55
|
-
): Promise<
|
|
59
|
+
): Promise<SetCookieResponse> => {
|
|
56
60
|
const vstoken2 = await getNonUserAccess(publicationDomain);
|
|
57
61
|
if (vstoken2.status === 'success') {
|
|
58
62
|
setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
|
|
59
|
-
return true;
|
|
60
63
|
}
|
|
61
|
-
return
|
|
64
|
+
return {
|
|
65
|
+
success: vstoken2.status === 'success',
|
|
66
|
+
accessFeatures: vstoken2.access_features,
|
|
67
|
+
};
|
|
62
68
|
};
|
|
63
69
|
|
|
64
70
|
export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
@@ -73,7 +79,9 @@ export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
|
73
79
|
return cookie === currentUrl;
|
|
74
80
|
};
|
|
75
81
|
|
|
76
|
-
export const setAccessCookies = async (
|
|
82
|
+
export const setAccessCookies = async (
|
|
83
|
+
publicationDomain
|
|
84
|
+
): Promise<SetCookieResponse> =>
|
|
77
85
|
setUserAccessCookie(publicationDomain) &&
|
|
78
86
|
setNonUserAccessCookie(publicationDomain);
|
|
79
87
|
|
|
@@ -47,37 +47,6 @@ describe('when fetching userData', () => {
|
|
|
47
47
|
});
|
|
48
48
|
});
|
|
49
49
|
});
|
|
50
|
-
|
|
51
|
-
describe('in quick succession', () => {
|
|
52
|
-
beforeEach(async () => {
|
|
53
|
-
plutoFetchMock.mockResponse(
|
|
54
|
-
JSON.stringify({
|
|
55
|
-
user_uuid: '1234-123-1234',
|
|
56
|
-
user_name: 'Donald',
|
|
57
|
-
tracking_key: '4321-321-4321',
|
|
58
|
-
})
|
|
59
|
-
);
|
|
60
|
-
await Promise.all([
|
|
61
|
-
requestAttributes(['name', 'uuid']),
|
|
62
|
-
requestAttributes(['uuid']),
|
|
63
|
-
requestAttributes(['name']),
|
|
64
|
-
requestAttributes(['name', 'uuid']),
|
|
65
|
-
requestAttributes(['uuid', 'name']),
|
|
66
|
-
]);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('should ask only once', () => {
|
|
70
|
-
expect(plutoFetchMock).toHaveBeenCalledTimes(1);
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('should have expected data in store', () => {
|
|
74
|
-
expect(store.get('userAttributes')).toEqual({
|
|
75
|
-
uuid: '1234-123-1234',
|
|
76
|
-
name: 'Donald',
|
|
77
|
-
trackingKey: '4321-321-4321',
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
50
|
});
|
|
82
51
|
|
|
83
52
|
describe('when querying for attributes that need login state to be resolved', () => {
|
package/src/actions/userData.ts
CHANGED
|
@@ -53,7 +53,7 @@ export const requestAttributes = (attributes: Array<keyof UserAttributes>) => {
|
|
|
53
53
|
store.merge('userAttributes', data);
|
|
54
54
|
})
|
|
55
55
|
.catch((error) => {
|
|
56
|
-
log.
|
|
56
|
+
log.warn('Call rejected', service, error);
|
|
57
57
|
})
|
|
58
58
|
);
|
|
59
59
|
});
|
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
2
|
import log from '../utils/log';
|
|
4
3
|
import {
|
|
5
4
|
checkForAccess,
|
|
6
|
-
|
|
5
|
+
getUserAccess,
|
|
7
6
|
AccessFeatures,
|
|
8
7
|
CheckAccessForPaper,
|
|
9
8
|
GetVstoken,
|
|
10
9
|
GetNonUserAccess,
|
|
11
10
|
getNonUserAccess,
|
|
12
11
|
} from './jupiter';
|
|
13
|
-
import
|
|
12
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
14
13
|
|
|
15
14
|
beforeEach(async () => {
|
|
15
|
+
restoreAllMocks();
|
|
16
16
|
fetch.resetMocks();
|
|
17
|
-
await cache.clear();
|
|
18
17
|
});
|
|
19
18
|
|
|
20
19
|
describe('jupiter service', () => {
|
|
@@ -66,31 +65,6 @@ describe('jupiter service', () => {
|
|
|
66
65
|
checkForAccess(publicationDomain, accessFeatures)
|
|
67
66
|
).rejects.toBeUndefined();
|
|
68
67
|
});
|
|
69
|
-
|
|
70
|
-
it('should not cache when access denied', (done) => {
|
|
71
|
-
fetch.mockResponseOnce(
|
|
72
|
-
JSON.stringify({
|
|
73
|
-
status: 'success',
|
|
74
|
-
access: 'denied',
|
|
75
|
-
publication: {
|
|
76
|
-
domain: 'www.rb.no',
|
|
77
|
-
id: 95,
|
|
78
|
-
},
|
|
79
|
-
access_features: [],
|
|
80
|
-
})
|
|
81
|
-
);
|
|
82
|
-
const cacheSetSpy = spyOn(cache, 'set');
|
|
83
|
-
expect(checkForAccess(publicationDomain, accessFeatures))
|
|
84
|
-
.resolves.toEqual({ access: [] })
|
|
85
|
-
.then(() => {
|
|
86
|
-
expect(cacheSetSpy).toHaveBeenCalledWith(
|
|
87
|
-
'checkForAccess',
|
|
88
|
-
anything(),
|
|
89
|
-
0
|
|
90
|
-
);
|
|
91
|
-
})
|
|
92
|
-
.then(done);
|
|
93
|
-
});
|
|
94
68
|
});
|
|
95
69
|
|
|
96
70
|
describe('POST /get_vstoken', () => {
|
|
@@ -105,7 +79,7 @@ describe('jupiter service', () => {
|
|
|
105
79
|
|
|
106
80
|
fetch.mockResponseOnce(JSON.stringify(response));
|
|
107
81
|
|
|
108
|
-
await expect(
|
|
82
|
+
await expect(getUserAccess(publicationDomain)).resolves.toEqual(response);
|
|
109
83
|
|
|
110
84
|
expect(fetch).toHaveBeenCalledWith(
|
|
111
85
|
`/api/jupiter/v1/get_vstoken/${publicationDomain}`,
|
|
@@ -120,7 +94,7 @@ describe('jupiter service', () => {
|
|
|
120
94
|
status: 400,
|
|
121
95
|
});
|
|
122
96
|
|
|
123
|
-
await expect(
|
|
97
|
+
await expect(getUserAccess(publicationDomain)).rejects.toThrow(
|
|
124
98
|
'Request not ok (400: Bad Request).'
|
|
125
99
|
);
|
|
126
100
|
});
|
|
@@ -129,7 +103,7 @@ describe('jupiter service', () => {
|
|
|
129
103
|
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
130
104
|
log.error = jest.fn();
|
|
131
105
|
|
|
132
|
-
await expect(
|
|
106
|
+
await expect(getUserAccess(publicationDomain)).rejects.toThrow(
|
|
133
107
|
'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
|
|
134
108
|
);
|
|
135
109
|
});
|
package/src/clients/jupiter.ts
CHANGED
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
Number,
|
|
8
8
|
Union,
|
|
9
9
|
} from 'runtypes';
|
|
10
|
-
import getOrUpdate from '../utils/getOrUpdate';
|
|
11
10
|
import { UserAttributes } from '../types';
|
|
12
11
|
import logValidationError from '../utils/logValidationError';
|
|
13
12
|
|
|
@@ -47,36 +46,36 @@ export const checkForAccess = (
|
|
|
47
46
|
publicationDomain: string,
|
|
48
47
|
accessFeatures: AccessFeatures
|
|
49
48
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
49
|
+
fetch(
|
|
50
|
+
`/api/jupiter/v1/check_access_for_paper/${publicationDomain}/?require=any&access_features=${accessFeatures.join(
|
|
51
|
+
','
|
|
52
|
+
)}`
|
|
53
|
+
)
|
|
54
|
+
.then((response) => {
|
|
55
|
+
if (!response.ok) {
|
|
56
|
+
// TODO Handle this.
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Request not ok (${response.status}: ${response.statusText}).`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return response.json();
|
|
62
|
+
})
|
|
63
|
+
.then((json) => {
|
|
64
|
+
try {
|
|
65
|
+
const data = CheckAccessForPaperSchema.check(json);
|
|
66
|
+
return data;
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logValidationError('CheckAccessForPaperSchema', error, { json });
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
.then((data) => ({ access: data.access_features }))
|
|
73
|
+
.catch(() => {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
|
|
75
|
+
throw undefined;
|
|
76
|
+
});
|
|
78
77
|
|
|
79
|
-
export const
|
|
78
|
+
export const getUserAccess = (publicationDomain: string): Promise<GetVstoken> =>
|
|
80
79
|
fetch(`/api/jupiter/v1/get_vstoken/${publicationDomain}`, { method: 'post' })
|
|
81
80
|
.then((response) => {
|
|
82
81
|
if (!response.ok) {
|
|
@@ -2,9 +2,11 @@ import fetch from 'jest-fetch-mock';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import log from '../utils/log';
|
|
4
4
|
import { getInfo } from './nebula';
|
|
5
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
5
6
|
|
|
6
7
|
describe('nebula service', () => {
|
|
7
8
|
beforeEach(async () => {
|
|
9
|
+
restoreAllMocks();
|
|
8
10
|
fetch.resetMocks();
|
|
9
11
|
await cache.clear();
|
|
10
12
|
});
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
2
|
import store from '../store';
|
|
4
3
|
import { getCurrentSession, removeSession } from './pluto';
|
|
5
4
|
import restoreAllMocks = jest.restoreAllMocks;
|
|
@@ -7,7 +6,6 @@ import restoreAllMocks = jest.restoreAllMocks;
|
|
|
7
6
|
beforeEach(async () => {
|
|
8
7
|
restoreAllMocks();
|
|
9
8
|
fetch.resetMocks();
|
|
10
|
-
await cache.clear();
|
|
11
9
|
});
|
|
12
10
|
|
|
13
11
|
const userUUID = '3-1-33-7';
|
|
@@ -55,58 +53,34 @@ describe('Pluto service', () => {
|
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
describe('when user is logged in ', () => {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
currentSession = await getCurrentSession();
|
|
62
|
-
});
|
|
63
|
-
it('should call Pluto', () => {
|
|
64
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
65
|
-
});
|
|
66
|
-
itShouldReturnSessionDataAndSetLoggedInTrueInState();
|
|
56
|
+
beforeEach(async () => {
|
|
57
|
+
fetch.mockResponseOnce(JSON.stringify(plutoLoggedInResponse));
|
|
58
|
+
currentSession = await getCurrentSession();
|
|
67
59
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
cache.set('getCurrentSession', plutoLoggedInResponse, 60000);
|
|
71
|
-
currentSession = await getCurrentSession();
|
|
72
|
-
});
|
|
73
|
-
it('should not call Pluto', () => {
|
|
74
|
-
expect(fetch).toHaveBeenCalledTimes(0);
|
|
75
|
-
});
|
|
76
|
-
itShouldReturnSessionDataAndSetLoggedInTrueInState();
|
|
60
|
+
it('should call Pluto', () => {
|
|
61
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
77
62
|
});
|
|
63
|
+
itShouldReturnSessionDataAndSetLoggedInTrueInState();
|
|
78
64
|
});
|
|
79
65
|
describe('when user is not logged in', () => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
currentSession = await getCurrentSession();
|
|
89
|
-
});
|
|
90
|
-
it('should call Pluto', () => {
|
|
91
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
92
|
-
});
|
|
93
|
-
itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
|
|
66
|
+
beforeEach(async () => {
|
|
67
|
+
fetch.mockResponseOnce(() =>
|
|
68
|
+
Promise.resolve({
|
|
69
|
+
init: { status: 403 },
|
|
70
|
+
body: '{"code": "session.not_found"}',
|
|
71
|
+
})
|
|
72
|
+
);
|
|
73
|
+
currentSession = await getCurrentSession();
|
|
94
74
|
});
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
cache.set('getCurrentSession', {}, 60000);
|
|
98
|
-
currentSession = await getCurrentSession();
|
|
99
|
-
});
|
|
100
|
-
it('should not call Pluto', () => {
|
|
101
|
-
expect(fetch).toHaveBeenCalledTimes(0);
|
|
102
|
-
});
|
|
103
|
-
itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
|
|
75
|
+
it('should call Pluto', () => {
|
|
76
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
104
77
|
});
|
|
78
|
+
itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
|
|
105
79
|
});
|
|
106
80
|
});
|
|
107
81
|
|
|
108
82
|
describe('removeSession()', () => {
|
|
109
|
-
it('should call
|
|
83
|
+
it('should call Pluto session delete', () => {
|
|
110
84
|
removeSession('www.rb.no');
|
|
111
85
|
expect(
|
|
112
86
|
fetch
|
package/src/clients/pluto.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Partial, Static, String } from 'runtypes';
|
|
2
2
|
import log from '../utils/log';
|
|
3
|
-
import getOrUpdate from '../utils/getOrUpdate';
|
|
4
3
|
import store from '../store';
|
|
5
4
|
import { UserAttributes } from '../types';
|
|
6
5
|
import logValidationError from '../utils/logValidationError';
|
|
@@ -35,29 +34,20 @@ const fetchCurrentSession = () =>
|
|
|
35
34
|
return response.json();
|
|
36
35
|
})
|
|
37
36
|
.then((json) => {
|
|
38
|
-
const nodata = { ttl: -1, data: json };
|
|
39
|
-
|
|
40
37
|
// We are probably logged out in this case.
|
|
41
38
|
if (Object.keys(json).length === 0) {
|
|
42
|
-
return
|
|
39
|
+
return {};
|
|
43
40
|
}
|
|
44
41
|
try {
|
|
45
|
-
return
|
|
46
|
-
ttl: 10000,
|
|
47
|
-
data: ServiceSessionSchema.check(json),
|
|
48
|
-
};
|
|
42
|
+
return ServiceSessionSchema.check(json);
|
|
49
43
|
} catch (error) {
|
|
50
44
|
logValidationError('ServiceSessionSchema', error, { json });
|
|
51
|
-
return
|
|
45
|
+
return {};
|
|
52
46
|
}
|
|
53
47
|
});
|
|
54
48
|
|
|
55
49
|
export const getCurrentSession = async (): Promise<UserAttributes> =>
|
|
56
|
-
|
|
57
|
-
'getCurrentSession',
|
|
58
|
-
ServiceSessionSchema,
|
|
59
|
-
fetchCurrentSession
|
|
60
|
-
).then((currentSession) => {
|
|
50
|
+
fetchCurrentSession().then((currentSession) => {
|
|
61
51
|
store.merge('state', {
|
|
62
52
|
isLoggedIn: currentSession.user_uuid !== undefined,
|
|
63
53
|
});
|
|
@@ -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
|
+
});
|