@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.
- package/dist/user.cjs +15 -0
- package/dist/{index.js.map → user.cjs.map} +3 -3
- package/dist/{index.d.ts → user.d.ts} +12 -0
- package/dist/user.js +15 -0
- package/dist/{index.esm.js.map → user.js.map} +3 -3
- package/package.json +9 -6
- package/.codeclimate.yml +0 -8
- package/.eslintignore +0 -2
- package/.eslintrc +0 -11
- package/.prettierrc.json +0 -1
- package/@types/amedia-frontend-rpc.d.ts +0 -11
- package/@types/browserid.d.ts +0 -4
- package/build.js +0 -28
- package/checkVersion.cjs +0 -12
- package/cloudbuild.json +0 -29
- package/cop.json +0 -8
- package/dist/actions/authentication.d.ts +0 -4
- package/dist/actions/authentication.spec.d.ts +0 -1
- package/dist/actions/authorization.d.ts +0 -14
- package/dist/actions/authorization.spec.d.ts +0 -4
- package/dist/actions/preferences.d.ts +0 -9
- package/dist/actions/preferences.spec.d.ts +0 -1
- package/dist/actions/storage.d.ts +0 -7
- package/dist/actions/userData.d.ts +0 -2
- package/dist/actions/userData.spec.d.ts +0 -1
- package/dist/autologin/index.d.ts +0 -7
- package/dist/autologin/index.spec.d.ts +0 -1
- package/dist/autologin/thirdPartyCookie.d.ts +0 -3
- package/dist/autologin/thirdPartyCookie.spec.d.ts +0 -1
- package/dist/cache/engines/global.d.ts +0 -24
- package/dist/cache/engines/global.spec.d.ts +0 -1
- package/dist/cache/index.d.ts +0 -11
- package/dist/cache/index.spec.d.ts +0 -1
- package/dist/cache/types.d.ts +0 -11
- package/dist/clients/europa.d.ts +0 -24
- package/dist/clients/europa.spec.d.ts +0 -1
- package/dist/clients/jupiter.d.ts +0 -30
- package/dist/clients/jupiter.spec.d.ts +0 -1
- package/dist/clients/mercury.d.ts +0 -3
- package/dist/clients/mercury.spec.d.ts +0 -1
- package/dist/clients/mercuryLog.d.ts +0 -8
- package/dist/clients/mercuryLog.spec.d.ts +0 -1
- package/dist/clients/nebula.d.ts +0 -2
- package/dist/clients/nebula.spec.d.ts +0 -1
- package/dist/clients/pluto.d.ts +0 -4
- package/dist/clients/pluto.spec.d.ts +0 -1
- package/dist/eventHandlers/activation.d.ts +0 -15
- package/dist/eventHandlers/authentication.d.ts +0 -17
- package/dist/eventHandlers/eventMap.d.ts +0 -29
- package/dist/eventHandlers/state.d.ts +0 -1
- package/dist/eventHandlers/state.spec.d.ts +0 -1
- package/dist/eventHandlers/storage.d.ts +0 -1
- package/dist/eventHandlers/storage.spec.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.spec.d.ts +0 -1
- package/dist/exceptions/PaywallError.d.ts +0 -8
- package/dist/index-authentcation.spec.d.ts +0 -1
- package/dist/index-storage.spec.d.ts +0 -1
- package/dist/index-subscription.spec.d.ts +0 -1
- package/dist/index.esm.js +0 -15
- package/dist/index.js +0 -15
- package/dist/janitor/index.d.ts +0 -10
- package/dist/janitor/index.spec.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
- package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
- package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
- package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
- package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
- package/dist/lib/UserDataRequest.d.ts +0 -22
- package/dist/lib/UserDataRequest.spec.d.ts +0 -1
- package/dist/lib/cookie.d.ts +0 -9
- package/dist/lib/rpc.d.ts +0 -6
- package/dist/store/index.d.ts +0 -23
- package/dist/store/index.spec.d.ts +0 -1
- package/dist/types.d.ts +0 -22
- package/dist/utils/customEventWrapper.d.ts +0 -19
- package/dist/utils/debouncer.d.ts +0 -26
- package/dist/utils/debouncer.spec.d.ts +0 -1
- package/dist/utils/deepEquals.d.ts +0 -1
- package/dist/utils/expectBootLogs.d.ts +0 -2
- package/dist/utils/getOrUpdate.d.ts +0 -6
- package/dist/utils/getOrUpdate.spec.d.ts +0 -1
- package/dist/utils/localhostCheck.d.ts +0 -1
- package/dist/utils/location.d.ts +0 -3
- package/dist/utils/log.d.ts +0 -9
- package/dist/utils/log.spec.d.ts +0 -1
- package/dist/utils/loginState.d.ts +0 -2
- package/dist/utils/networkError.d.ts +0 -1
- package/dist/utils/queue.d.ts +0 -12
- package/dist/utils/type.d.ts +0 -6
- package/dist/utils/url.d.ts +0 -6
- package/dist/utils/url.spec.d.ts +0 -1
- package/eik.json +0 -10
- package/global.d.ts +0 -1
- package/jest.config.js +0 -7
- package/setupJest.js +0 -1
- package/src/actions/authentication.spec.ts +0 -51
- package/src/actions/authentication.ts +0 -43
- package/src/actions/authorization.spec.ts +0 -369
- package/src/actions/authorization.ts +0 -117
- package/src/actions/preferences.spec.ts +0 -41
- package/src/actions/preferences.ts +0 -41
- package/src/actions/storage.ts +0 -22
- package/src/actions/userData.spec.ts +0 -133
- package/src/actions/userData.ts +0 -68
- package/src/autologin/index.spec.ts +0 -239
- package/src/autologin/index.ts +0 -92
- package/src/autologin/thirdPartyCookie.spec.ts +0 -95
- package/src/autologin/thirdPartyCookie.ts +0 -55
- package/src/cache/engines/global.spec.ts +0 -62
- package/src/cache/engines/global.ts +0 -48
- package/src/cache/index.spec.ts +0 -130
- package/src/cache/index.ts +0 -48
- package/src/cache/types.ts +0 -12
- package/src/clients/europa.spec.ts +0 -209
- package/src/clients/europa.ts +0 -183
- package/src/clients/jupiter.spec.ts +0 -179
- package/src/clients/jupiter.ts +0 -119
- package/src/clients/mercury.spec.ts +0 -59
- package/src/clients/mercury.ts +0 -48
- package/src/clients/mercuryLog.spec.ts +0 -20
- package/src/clients/mercuryLog.ts +0 -67
- package/src/clients/nebula.spec.ts +0 -70
- package/src/clients/nebula.ts +0 -83
- package/src/clients/pluto.spec.ts +0 -165
- package/src/clients/pluto.ts +0 -118
- package/src/eventHandlers/activation.ts +0 -35
- package/src/eventHandlers/authentication.ts +0 -34
- package/src/eventHandlers/eventMap.ts +0 -34
- package/src/eventHandlers/state.spec.ts +0 -48
- package/src/eventHandlers/state.ts +0 -23
- package/src/eventHandlers/storage.spec.ts +0 -114
- package/src/eventHandlers/storage.ts +0 -45
- package/src/eventHandlers/userAttributes.spec.ts +0 -93
- package/src/eventHandlers/userAttributes.ts +0 -31
- package/src/exceptions/PaywallError.ts +0 -16
- package/src/index-authentcation.spec.ts +0 -26
- package/src/index-storage.spec.ts +0 -144
- package/src/index-subscription.spec.ts +0 -213
- package/src/index.ts +0 -59
- package/src/janitor/index.spec.ts +0 -18
- package/src/janitor/index.ts +0 -59
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
- package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
- package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
- package/src/janitor/jobs/daxsubCookie.ts +0 -38
- package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
- package/src/janitor/jobs/removeUserStorage.ts +0 -12
- package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
- package/src/lib/PaywallUnlockRequest.ts +0 -143
- package/src/lib/UserDataRequest.spec.ts +0 -77
- package/src/lib/UserDataRequest.ts +0 -213
- package/src/lib/cookie.ts +0 -121
- package/src/lib/rpc.ts +0 -19
- package/src/store/index.spec.ts +0 -49
- package/src/store/index.ts +0 -64
- package/src/types.ts +0 -22
- package/src/utils/customEventWrapper.ts +0 -58
- package/src/utils/debouncer.spec.ts +0 -252
- package/src/utils/debouncer.ts +0 -123
- package/src/utils/deepEquals.ts +0 -42
- package/src/utils/expectBootLogs.ts +0 -9
- package/src/utils/getOrUpdate.spec.ts +0 -166
- package/src/utils/getOrUpdate.ts +0 -66
- package/src/utils/localhostCheck.ts +0 -9
- package/src/utils/location.ts +0 -31
- package/src/utils/log.spec.ts +0 -148
- package/src/utils/log.ts +0 -124
- package/src/utils/loginState.ts +0 -24
- package/src/utils/networkError.ts +0 -11
- package/src/utils/queue.ts +0 -32
- package/src/utils/type.ts +0 -19
- package/src/utils/url.spec.ts +0 -33
- package/src/utils/url.ts +0 -35
- package/testSetup.js +0 -19
- package/tsconfig.eslint.json +0 -24
- package/tsconfig.json +0 -18
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import fetch from 'jest-fetch-mock';
|
|
2
|
-
import cache from '../cache';
|
|
3
|
-
import store from '../store';
|
|
4
|
-
import { getCurrentSession, removeSession } from './pluto';
|
|
5
|
-
import { Logger, setLogger } from '../utils/log';
|
|
6
|
-
import { UserAttributes } from '../types';
|
|
7
|
-
|
|
8
|
-
beforeEach(async () => {
|
|
9
|
-
await cache.clear();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
const userUUID = '3-1-33-7';
|
|
13
|
-
const userName = 'Earl Hickey';
|
|
14
|
-
const userTrackingKey = 'some tracking key';
|
|
15
|
-
const plutoLoggedInResponse = {
|
|
16
|
-
created_at:
|
|
17
|
-
'Wed Apr 28 2021 16:02:04 GMT+0200 (Central European Summer Time)',
|
|
18
|
-
updated_at:
|
|
19
|
-
'Wed Apr 28 2021 16:02:04 GMT+0200 (Central European Summer Time)',
|
|
20
|
-
user_uuid: userUUID,
|
|
21
|
-
user_name: userName,
|
|
22
|
-
tracking_key: userTrackingKey,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
let log: Logger;
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
log = {
|
|
28
|
-
debug: jest.fn(),
|
|
29
|
-
info: jest.fn(),
|
|
30
|
-
warn: jest.fn(),
|
|
31
|
-
error: jest.fn(),
|
|
32
|
-
};
|
|
33
|
-
setLogger(log);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
function itShouldNotHaveLoggedAnything() {
|
|
37
|
-
it('should not have logged anything', () => {
|
|
38
|
-
expect(log.debug).not.toHaveBeenCalled();
|
|
39
|
-
expect(log.info).not.toHaveBeenCalled();
|
|
40
|
-
expect(log.warn).not.toHaveBeenCalled();
|
|
41
|
-
expect(log.error).not.toHaveBeenCalled();
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
describe('Pluto service', () => {
|
|
46
|
-
describe('getCurrentSession()', () => {
|
|
47
|
-
beforeEach(() => {
|
|
48
|
-
jest.spyOn(store, 'merge');
|
|
49
|
-
});
|
|
50
|
-
let currentSession: UserAttributes;
|
|
51
|
-
function itShouldReturnSessionData() {
|
|
52
|
-
it('should return session data', async () => {
|
|
53
|
-
expect(currentSession).toEqual({
|
|
54
|
-
uuid: userUUID,
|
|
55
|
-
name: userName,
|
|
56
|
-
trackingKey: userTrackingKey,
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
function itShouldReturnNoSessionData() {
|
|
61
|
-
it('should return session data', async () => {
|
|
62
|
-
expect(currentSession).toEqual({
|
|
63
|
-
uuid: null,
|
|
64
|
-
name: null,
|
|
65
|
-
trackingKey: null,
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
describe('when user is logged in ', () => {
|
|
71
|
-
describe('When cache is empty', () => {
|
|
72
|
-
beforeEach(async () => {
|
|
73
|
-
fetch.mockResponseOnce(JSON.stringify(plutoLoggedInResponse));
|
|
74
|
-
currentSession = await getCurrentSession();
|
|
75
|
-
});
|
|
76
|
-
it('should call Pluto', () => {
|
|
77
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
78
|
-
});
|
|
79
|
-
itShouldReturnSessionData();
|
|
80
|
-
});
|
|
81
|
-
describe('When cache is not empty', () => {
|
|
82
|
-
beforeEach(async () => {
|
|
83
|
-
cache.set('getCurrentSession', plutoLoggedInResponse, 60000);
|
|
84
|
-
currentSession = await getCurrentSession();
|
|
85
|
-
});
|
|
86
|
-
it('should not call Pluto', () => {
|
|
87
|
-
expect(fetch).toHaveBeenCalledTimes(0);
|
|
88
|
-
});
|
|
89
|
-
itShouldReturnSessionData();
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
describe('when user is not logged in', () => {
|
|
93
|
-
describe('When cache is empty', () => {
|
|
94
|
-
beforeEach(async () => {
|
|
95
|
-
fetch.mockResponseOnce(() =>
|
|
96
|
-
Promise.resolve({
|
|
97
|
-
init: { status: 403 },
|
|
98
|
-
body: '{"errors": [{"code": "session.not_found"}]}',
|
|
99
|
-
})
|
|
100
|
-
);
|
|
101
|
-
currentSession = await getCurrentSession();
|
|
102
|
-
});
|
|
103
|
-
it('should call Pluto', () => {
|
|
104
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
105
|
-
});
|
|
106
|
-
itShouldNotHaveLoggedAnything();
|
|
107
|
-
itShouldReturnNoSessionData();
|
|
108
|
-
});
|
|
109
|
-
describe('When cache is not empty', () => {
|
|
110
|
-
beforeEach(async () => {
|
|
111
|
-
cache.set('getCurrentSession', {}, 60000);
|
|
112
|
-
currentSession = await getCurrentSession();
|
|
113
|
-
});
|
|
114
|
-
it('should not call Pluto', () => {
|
|
115
|
-
expect(fetch).toHaveBeenCalledTimes(0);
|
|
116
|
-
});
|
|
117
|
-
itShouldNotHaveLoggedAnything();
|
|
118
|
-
itShouldReturnNoSessionData();
|
|
119
|
-
});
|
|
120
|
-
describe('when session is invalid for domain', () => {
|
|
121
|
-
beforeEach(async () => {
|
|
122
|
-
fetch.mockResponseOnce(() =>
|
|
123
|
-
Promise.resolve({
|
|
124
|
-
init: { status: 403 },
|
|
125
|
-
body: '{"errors": [{"code": "session.invalid_for_domain"}]}',
|
|
126
|
-
})
|
|
127
|
-
);
|
|
128
|
-
currentSession = await getCurrentSession();
|
|
129
|
-
});
|
|
130
|
-
it('should have logged a warning', () => {
|
|
131
|
-
expect(log.warn).toHaveBeenCalledWith(
|
|
132
|
-
'Pluto responded with HTTP/403: (session.invalid_for_domain) while fetching current session',
|
|
133
|
-
{
|
|
134
|
-
error: { code: 'session.invalid_for_domain' },
|
|
135
|
-
origin: '@amedia/user',
|
|
136
|
-
response: expect.any(Object),
|
|
137
|
-
}
|
|
138
|
-
);
|
|
139
|
-
});
|
|
140
|
-
});
|
|
141
|
-
describe('when session is no longer valid', () => {
|
|
142
|
-
beforeEach(async () => {
|
|
143
|
-
fetch.mockResponseOnce(() =>
|
|
144
|
-
Promise.resolve({
|
|
145
|
-
init: { status: 404 },
|
|
146
|
-
body: '{"errors":[{"id":"f84ea34e-d9eb-11eb-afe7-000000000000","title":"No currentSession found","detail":"Got currentSession not found error","status":"404","code":"session.not_found"}]}\n',
|
|
147
|
-
})
|
|
148
|
-
);
|
|
149
|
-
currentSession = await getCurrentSession();
|
|
150
|
-
});
|
|
151
|
-
itShouldNotHaveLoggedAnything();
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
describe('removeSession()', () => {
|
|
157
|
-
it('should call Plut session delete', () => {
|
|
158
|
-
removeSession('www.rb.no');
|
|
159
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
160
|
-
'/api/pluto/v1/sessions/current?publication_domain=www.rb.no',
|
|
161
|
-
{ method: 'DELETE' }
|
|
162
|
-
);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
package/src/clients/pluto.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { Partial, Record, Static, String, Array } from 'runtypes';
|
|
2
|
-
import log from '../utils/log';
|
|
3
|
-
import getOrUpdate from '../utils/getOrUpdate';
|
|
4
|
-
import store from '../store';
|
|
5
|
-
import { UserAttributes } from '../types';
|
|
6
|
-
import { isAbortedNetworkError } from '../utils/networkError';
|
|
7
|
-
|
|
8
|
-
const ServiceSessionSchema = Partial({
|
|
9
|
-
user_uuid: String,
|
|
10
|
-
user_name: String,
|
|
11
|
-
tracking_key: String,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
const mapSessionToUserAttributes = (
|
|
15
|
-
session: Static<typeof ServiceSessionSchema>
|
|
16
|
-
): UserAttributes => ({
|
|
17
|
-
uuid: session.user_uuid || null,
|
|
18
|
-
name: session.user_name || null,
|
|
19
|
-
trackingKey: session.tracking_key || null,
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
const PlutoErrorSchema = Record({
|
|
23
|
-
code: String,
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const PlutoErrorsSchema = Record({
|
|
27
|
-
errors: Array(PlutoErrorSchema),
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
const IGNORED_CODES = [
|
|
31
|
-
'session.not_found',
|
|
32
|
-
'session.not_specified',
|
|
33
|
-
'user.deactivated',
|
|
34
|
-
];
|
|
35
|
-
const WARNING_CODE = ['session.invalid_for_domain', 'user.not_found'];
|
|
36
|
-
|
|
37
|
-
export const resolveBadPlutoResponse = async (response: Response) => {
|
|
38
|
-
let error: Static<typeof PlutoErrorSchema>;
|
|
39
|
-
const responseClone = response.clone(); // Clone it so we can log pure response if JSON parse fails
|
|
40
|
-
try {
|
|
41
|
-
[error] = PlutoErrorsSchema.check(await response.json()).errors;
|
|
42
|
-
} catch (e) {
|
|
43
|
-
if (isAbortedNetworkError(e?.message)) {
|
|
44
|
-
return Promise.resolve();
|
|
45
|
-
}
|
|
46
|
-
log.warn('Unable to parse error body from pluto', {
|
|
47
|
-
error: e,
|
|
48
|
-
response: responseClone,
|
|
49
|
-
});
|
|
50
|
-
return Promise.reject(e);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (IGNORED_CODES.includes(error?.code)) {
|
|
54
|
-
return Promise.resolve();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (WARNING_CODE.includes(error?.code)) {
|
|
58
|
-
log.warn(
|
|
59
|
-
`Pluto responded with HTTP/${response.status}: (${error.code}) while fetching current session`,
|
|
60
|
-
{ error, response }
|
|
61
|
-
);
|
|
62
|
-
return Promise.resolve();
|
|
63
|
-
}
|
|
64
|
-
log.error(
|
|
65
|
-
`Pluto responded with HTTP/${response.status}: ${response.statusText}) when fetching current session`,
|
|
66
|
-
{ error, response }
|
|
67
|
-
);
|
|
68
|
-
return Promise.reject();
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
const fetchCurrentSession = () =>
|
|
72
|
-
fetch(`/api/pluto/v1/sessions/current`)
|
|
73
|
-
.then((response) => {
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
return resolveBadPlutoResponse(response).then(() => ({}));
|
|
76
|
-
}
|
|
77
|
-
return response.json();
|
|
78
|
-
})
|
|
79
|
-
.then((json) => {
|
|
80
|
-
const nodata = { ttl: -1, data: json };
|
|
81
|
-
|
|
82
|
-
// We are probably logged out in this case.
|
|
83
|
-
if (Object.keys(json).length === 0) {
|
|
84
|
-
return nodata;
|
|
85
|
-
}
|
|
86
|
-
try {
|
|
87
|
-
return {
|
|
88
|
-
ttl: 10000,
|
|
89
|
-
data: ServiceSessionSchema.check(json),
|
|
90
|
-
};
|
|
91
|
-
} catch (error) {
|
|
92
|
-
log.error(error, { json });
|
|
93
|
-
return nodata;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
export const getCurrentSession = async (): Promise<UserAttributes> =>
|
|
98
|
-
getOrUpdate(
|
|
99
|
-
'getCurrentSession',
|
|
100
|
-
ServiceSessionSchema,
|
|
101
|
-
fetchCurrentSession
|
|
102
|
-
).then((currentSession) => mapSessionToUserAttributes(currentSession));
|
|
103
|
-
|
|
104
|
-
export const removeSession = async (domain: string) =>
|
|
105
|
-
fetch(`/api/pluto/v1/sessions/current?publication_domain=${domain}`, {
|
|
106
|
-
method: 'DELETE',
|
|
107
|
-
}).then((res) => {
|
|
108
|
-
if (!res.ok) {
|
|
109
|
-
if (res.status === 403 || res.status === 404) {
|
|
110
|
-
// No session to log out. Ignoring
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
throw new Error(
|
|
114
|
-
`Error, could not perform deleteRequest. Status: ${res.status} Text: ${res.statusText}`
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
store.merge('state', { isLoggedIn: false });
|
|
118
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
|
-
import { activate } from '../actions/authorization';
|
|
3
|
-
|
|
4
|
-
export enum ActivationEvents {
|
|
5
|
-
REQUEST_ACTIVATION = 'amedia-user.activation.request-activation',
|
|
6
|
-
ON_ACTIVATION_COMPLETE = 'amedia-user.activation.on-activation-complete',
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
declare global {
|
|
10
|
-
interface WindowEventMap {
|
|
11
|
-
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
12
|
-
accessFeatures: string[];
|
|
13
|
-
}>;
|
|
14
|
-
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
15
|
-
accessFeatures: string[];
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const listenForActivationRequests = () => {
|
|
21
|
-
listenWithExclusivity(ActivationEvents.REQUEST_ACTIVATION, (evt) => {
|
|
22
|
-
activate(evt.detail.accessFeatures).then((accessFeatures) => {
|
|
23
|
-
dispatch(
|
|
24
|
-
new CustomEvent(ActivationEvents.ON_ACTIVATION_COMPLETE, {
|
|
25
|
-
detail: { accessFeatures },
|
|
26
|
-
})
|
|
27
|
-
);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
if (!globalThis.amediaUserTestMode) {
|
|
34
|
-
listenForActivationRequests();
|
|
35
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
|
-
import { login, logout } from '../actions/authentication';
|
|
3
|
-
|
|
4
|
-
export enum AuthenticationEvents {
|
|
5
|
-
REQUEST_LOGIN = 'amedia-user.authentication.request-login',
|
|
6
|
-
REQUEST_LOGOUT = 'amedia-user.authentication.request-logout',
|
|
7
|
-
|
|
8
|
-
ON_LOGGED_OUT = 'amedia-user.authentication.on-logged-out',
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
interface WindowEventMap {
|
|
13
|
-
[AuthenticationEvents.REQUEST_LOGIN]: CustomEvent<{
|
|
14
|
-
requestedUrl: string;
|
|
15
|
-
}>;
|
|
16
|
-
[AuthenticationEvents.REQUEST_LOGOUT]: CustomEvent<{
|
|
17
|
-
onLoggedOut: () => void;
|
|
18
|
-
}>;
|
|
19
|
-
[AuthenticationEvents.ON_LOGGED_OUT]: CustomEvent;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const listenForAuthenticationRequests = () => {
|
|
24
|
-
listenWithExclusivity(AuthenticationEvents.REQUEST_LOGIN, (evt) => {
|
|
25
|
-
login(evt.detail.requestedUrl);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
listenWithExclusivity(AuthenticationEvents.REQUEST_LOGOUT, () => {
|
|
29
|
-
logout().finally(() => {
|
|
30
|
-
dispatch(new CustomEvent(AuthenticationEvents.ON_LOGGED_OUT));
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
listenForAuthenticationRequests();
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
SAVE_STORAGE = 'amedia-user.save-storage-namespaces',
|
|
8
|
-
UPDATE_STORAGE = 'amedia-user.update-storage-namespaces',
|
|
9
|
-
ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
|
|
10
|
-
ON_STATE_UPDATED = 'amedia-user.on-state-updated',
|
|
11
|
-
ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
declare global {
|
|
15
|
-
interface WindowEventMap {
|
|
16
|
-
[AmediaUserEvents.REQUEST_STATE]: CustomEvent;
|
|
17
|
-
[AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<
|
|
18
|
-
Array<keyof UserAttributes>
|
|
19
|
-
>;
|
|
20
|
-
[AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
|
|
21
|
-
[AmediaUserEvents.SAVE_STORAGE]: CustomEvent<{
|
|
22
|
-
[namespace: string]: object;
|
|
23
|
-
}>;
|
|
24
|
-
[AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
|
|
25
|
-
[namespace: string]: object;
|
|
26
|
-
}>;
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
[AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
29
|
-
[AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
|
|
30
|
-
[AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
|
|
31
|
-
[index: string]: unknown;
|
|
32
|
-
}>;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
|
|
2
|
-
import store from '../store';
|
|
3
|
-
import { AmediaUserEvents } from './eventMap';
|
|
4
|
-
import { listenForStateRequests } from './state';
|
|
5
|
-
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
store.clear();
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
describe('listenForUserAttributeRequests', () => {
|
|
11
|
-
let currentState: any;
|
|
12
|
-
beforeEach(() => {
|
|
13
|
-
listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
|
|
14
|
-
currentState = evt.detail;
|
|
15
|
-
});
|
|
16
|
-
listenForStateRequests();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
describe('when empty initial state', () => {
|
|
20
|
-
beforeEach(() => {
|
|
21
|
-
dispatch(new CustomEvent(AmediaUserEvents.REQUEST_STATE));
|
|
22
|
-
});
|
|
23
|
-
it('should not return empty state', () => {
|
|
24
|
-
expect(currentState).toBeUndefined();
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
describe('with initial state', () => {
|
|
29
|
-
beforeEach(() => {
|
|
30
|
-
store.set('state', { isLoggedIn: false });
|
|
31
|
-
dispatch(new CustomEvent(AmediaUserEvents.REQUEST_STATE));
|
|
32
|
-
});
|
|
33
|
-
it('should return expected state', () => {
|
|
34
|
-
expect(currentState).toEqual({ isLoggedIn: false });
|
|
35
|
-
});
|
|
36
|
-
describe('when state changes', () => {
|
|
37
|
-
beforeEach(() => {
|
|
38
|
-
store.merge('state', { emergencyMode: ['aid'] });
|
|
39
|
-
});
|
|
40
|
-
it('should have updated state', () => {
|
|
41
|
-
expect(currentState).toEqual({
|
|
42
|
-
isLoggedIn: false,
|
|
43
|
-
emergencyMode: ['aid'],
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
});
|
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
|
|
2
|
-
import * as userData from '../actions/userData';
|
|
3
|
-
import * as storage from '../actions/storage';
|
|
4
|
-
import store from '../store';
|
|
5
|
-
import { listenForStorageRequests } from './storage';
|
|
6
|
-
import { AmediaUserEvents } from './eventMap';
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
store.clear();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
describe('listenForStorageRequests', () => {
|
|
13
|
-
let currentStorage: any;
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
|
|
16
|
-
currentStorage = evt.detail;
|
|
17
|
-
});
|
|
18
|
-
listenForStorageRequests();
|
|
19
|
-
jest
|
|
20
|
-
.spyOn(userData, 'requestAttributes')
|
|
21
|
-
.mockReturnValue(Promise.resolve());
|
|
22
|
-
jest.spyOn(storage, 'requestStorage').mockReturnValue();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
describe('AmediaUserEvents.REQUEST_STORAGE_NAMESPACES received', () => {
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
store.set('state', { isLoggedIn: true });
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe('no previous namespaces', () => {
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
dispatch(
|
|
33
|
-
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
34
|
-
detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
|
|
35
|
-
})
|
|
36
|
-
);
|
|
37
|
-
});
|
|
38
|
-
it('calls requestAttributes with requested attributes', () => {
|
|
39
|
-
expect(storage.requestStorage).toHaveBeenCalledWith([
|
|
40
|
-
'privacy_preferences',
|
|
41
|
-
'euucp',
|
|
42
|
-
'favorite_teams',
|
|
43
|
-
]);
|
|
44
|
-
});
|
|
45
|
-
it('does not receive a storage event', () => {
|
|
46
|
-
expect(currentStorage).toBeUndefined();
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
describe('with some namespaces', () => {
|
|
51
|
-
beforeEach(() => {
|
|
52
|
-
store.set('storage', {
|
|
53
|
-
favorite_teams: { stabaek: true, sf: true },
|
|
54
|
-
});
|
|
55
|
-
dispatch(
|
|
56
|
-
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
57
|
-
detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
|
|
58
|
-
})
|
|
59
|
-
);
|
|
60
|
-
});
|
|
61
|
-
it('calls requestStorage with only namespaces we did not already have', () => {
|
|
62
|
-
expect(storage.requestStorage).toHaveBeenCalledWith([
|
|
63
|
-
'privacy_preferences',
|
|
64
|
-
'euucp',
|
|
65
|
-
]);
|
|
66
|
-
});
|
|
67
|
-
it('receives initial event with previous namespaces', () => {
|
|
68
|
-
expect(currentStorage).toEqual({
|
|
69
|
-
favorite_teams: { stabaek: true, sf: true },
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
describe('with previous state and all attributes', () => {
|
|
75
|
-
beforeEach(() => {
|
|
76
|
-
store.set('storage', {
|
|
77
|
-
favorite_teams: { stabaek: true, sf: true },
|
|
78
|
-
privacy_preferences: { ads: true, tracking: true },
|
|
79
|
-
euucp: { what_is: 'this' },
|
|
80
|
-
});
|
|
81
|
-
dispatch(
|
|
82
|
-
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
83
|
-
detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
|
|
84
|
-
})
|
|
85
|
-
);
|
|
86
|
-
});
|
|
87
|
-
it('does not call requestAttributes', () => {
|
|
88
|
-
expect(storage.requestStorage).not.toHaveBeenCalled();
|
|
89
|
-
});
|
|
90
|
-
it('receives initial event with all namespaces', () => {
|
|
91
|
-
expect(currentStorage).toEqual({
|
|
92
|
-
favorite_teams: { stabaek: true, sf: true },
|
|
93
|
-
privacy_preferences: { ads: true, tracking: true },
|
|
94
|
-
euucp: { what_is: 'this' },
|
|
95
|
-
});
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('when is not logged in', () => {
|
|
100
|
-
beforeEach(() => {
|
|
101
|
-
store.set('state', { isLoggedIn: false });
|
|
102
|
-
dispatch(
|
|
103
|
-
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
104
|
-
detail: ['privacy_preferences', 'euucp', 'favorite_teams'],
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('does not call requestAttributes', () => {
|
|
110
|
-
expect(storage.requestStorage).not.toHaveBeenCalled();
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
});
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import store from '../store';
|
|
2
|
-
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
|
-
import { AmediaUserEvents } from './eventMap';
|
|
4
|
-
import { requestStorage, saveStorage, updateStorage } from '../actions/storage';
|
|
5
|
-
import { whenIsLoggedIn } from '../utils/loginState';
|
|
6
|
-
|
|
7
|
-
export const listenForStorageRequests = () => {
|
|
8
|
-
store.on('storage', (storage) => {
|
|
9
|
-
dispatch(
|
|
10
|
-
new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, { detail: storage })
|
|
11
|
-
);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
|
|
15
|
-
whenIsLoggedIn().then(() => {
|
|
16
|
-
const requestedNamespaces = evt.detail;
|
|
17
|
-
const currentStorage = store.get('storage') || {};
|
|
18
|
-
if (Object.keys(currentStorage).length > 0) {
|
|
19
|
-
dispatch(
|
|
20
|
-
new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, {
|
|
21
|
-
detail: currentStorage,
|
|
22
|
-
})
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
const missingNamespaces = requestedNamespaces.filter(
|
|
26
|
-
(namespace) => currentStorage[namespace] === undefined
|
|
27
|
-
);
|
|
28
|
-
if (missingNamespaces.length > 0) {
|
|
29
|
-
requestStorage(missingNamespaces);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
listenWithExclusivity(AmediaUserEvents.UPDATE_STORAGE, (evt) => {
|
|
35
|
-
whenIsLoggedIn().then(() => {
|
|
36
|
-
updateStorage(evt.detail);
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
listenWithExclusivity(AmediaUserEvents.SAVE_STORAGE, (evt) => {
|
|
41
|
-
whenIsLoggedIn().then(() => {
|
|
42
|
-
saveStorage(evt.detail);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
};
|