@amedia/user 0.12.34 → 0.13.1
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/README.md +54 -2
- package/dist/actions/authorization.spec.d.ts +4 -0
- package/dist/clients/mercury.d.ts +4 -12
- package/dist/index.d.ts +2 -12
- package/dist/index.js +1 -1
- package/dist/lib/UserDataRequest.spec.d.ts +1 -0
- package/dist/utils/log.d.ts +5 -10
- package/dist/utils/log.spec.d.ts +1 -0
- package/eik.json +1 -1
- package/package.json +1 -1
- package/src/actions/authorization.spec.ts +236 -0
- package/src/actions/authorization.ts +3 -1
- package/src/actions/userData.ts +1 -1
- package/src/autologin/thirdPartyCookie.ts +1 -2
- package/src/cache/index.spec.ts +21 -6
- package/src/cache/index.ts +2 -2
- package/src/clients/europa.ts +1 -2
- package/src/clients/jupiter.ts +4 -4
- package/src/clients/mercury.ts +10 -12
- package/src/clients/nebula.ts +2 -2
- package/src/clients/pluto.spec.ts +45 -1
- package/src/clients/pluto.ts +45 -11
- package/src/index.ts +2 -8
- package/src/lib/UserDataRequest.spec.ts +62 -0
- package/src/utils/log.spec.ts +153 -0
- package/src/utils/log.ts +66 -17
- package/amedia-user-js.iml +0 -9
- package/src/utils/logValidationError.ts +0 -22
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
hasAccess,
|
|
7
|
+
setAccessCookies,
|
|
8
|
+
setNonUserAccessCookie,
|
|
9
|
+
setUserAccessCookie,
|
|
10
|
+
} from './authorization';
|
|
11
|
+
import * as jupiter from '../clients/jupiter';
|
|
12
|
+
import * as cookie from '../lib/cookie';
|
|
13
|
+
import { setRootElement } from '../utils/customEventWrapper';
|
|
14
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
15
|
+
|
|
16
|
+
const publicationDomain = 'www.avisnavn.no';
|
|
17
|
+
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
setRootElement(new Document());
|
|
20
|
+
restoreAllMocks();
|
|
21
|
+
jest.spyOn(cookie, 'setCookie');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe('hasAccess', () => {
|
|
25
|
+
let access = false;
|
|
26
|
+
function itReturnsTrue() {
|
|
27
|
+
it('returns true', () => {
|
|
28
|
+
expect(access).toBeTruthy();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function itReturnsFalse() {
|
|
32
|
+
it('returns false', () => {
|
|
33
|
+
expect(access).not.toBeTruthy();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
describe('with no access requested', () => {
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
access = hasAccess(['newspaper'], []);
|
|
39
|
+
});
|
|
40
|
+
itReturnsTrue();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('with access requested without access', () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
access = hasAccess([], ['newspaper']);
|
|
46
|
+
});
|
|
47
|
+
itReturnsFalse();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('with access to some requested access feature', () => {
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
access = hasAccess(['newspaper'], ['all', 'plussalt', 'newspaper']);
|
|
53
|
+
});
|
|
54
|
+
itReturnsTrue();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('with access to other access features than requested requested access feature', () => {
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
access = hasAccess(['pluss', 'sport'], ['all', 'plussalt', 'newspaper']);
|
|
60
|
+
});
|
|
61
|
+
itReturnsFalse();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
let cookieResponse;
|
|
66
|
+
|
|
67
|
+
function itReturnsCookieResponse(success, accessFeatures) {
|
|
68
|
+
it(`returns cookie response with success = ${success}`, () => {
|
|
69
|
+
expect(cookieResponse.success).toEqual(success);
|
|
70
|
+
});
|
|
71
|
+
it(`returns cookie response with accessFeatures = ${accessFeatures}`, () => {
|
|
72
|
+
expect(cookieResponse.accessFeatures).toEqual(accessFeatures);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function itSetsNoCookies() {
|
|
77
|
+
it('does not set any cookies', () => {
|
|
78
|
+
expect(cookie.setCookie).not.toHaveBeenCalled();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function itDoesNotSetVstokenCookie() {
|
|
83
|
+
it('does not set VSTOKEN cookie', () => {
|
|
84
|
+
expect(cookie.setCookie).not.toHaveBeenCalledWith('VSTOKEN');
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function itSetsVstokenCookie() {
|
|
89
|
+
it('sets VSTOKEN cookie', () => {
|
|
90
|
+
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
91
|
+
'VSTOKEN',
|
|
92
|
+
'COOKIE_VALUE',
|
|
93
|
+
365,
|
|
94
|
+
'/'
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function itDoesNotSetVstoken2Cookie() {
|
|
100
|
+
it('does not set VSTOKEN2 cookie', () => {
|
|
101
|
+
expect(cookie.setCookie).not.toHaveBeenCalledWith('VSTOKEN2');
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function itSetsVstoken2Cookie() {
|
|
106
|
+
it('sets VSTOKEN2 cookie', () => {
|
|
107
|
+
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
108
|
+
'VSTOKEN2',
|
|
109
|
+
'COOKIE2_VALUE',
|
|
110
|
+
1,
|
|
111
|
+
'/'
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function mockGetUserAccess(status, accessFeatures) {
|
|
117
|
+
beforeEach(async () => {
|
|
118
|
+
jest.spyOn(jupiter, 'getUserAccess').mockResolvedValue({
|
|
119
|
+
status,
|
|
120
|
+
cookie: 'COOKIE_VALUE',
|
|
121
|
+
access_features: accessFeatures,
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function getUserAccessSucceeds(accessFeatures) {
|
|
127
|
+
mockGetUserAccess('success', accessFeatures);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function getUserAccessFails(accessFeatures) {
|
|
131
|
+
mockGetUserAccess('failure', accessFeatures);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function mockGetNonUserAccessSucceeds(status, accessFeatures) {
|
|
135
|
+
beforeEach(async () => {
|
|
136
|
+
jest.spyOn(jupiter, 'getNonUserAccess').mockResolvedValue({
|
|
137
|
+
status,
|
|
138
|
+
uuid: 'mycompany',
|
|
139
|
+
vstoken_cookie: 'COOKIE2_VALUE',
|
|
140
|
+
access_features: accessFeatures,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function getNonUserAccessSucceeds(accessFeatures) {
|
|
146
|
+
mockGetNonUserAccessSucceeds('success', accessFeatures);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function getNonUserAccessFails(accessFeatures) {
|
|
150
|
+
mockGetNonUserAccessSucceeds('failure', accessFeatures);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
describe('setUserAccessCookie', () => {
|
|
154
|
+
describe('when getUserAccess is successful', () => {
|
|
155
|
+
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
156
|
+
beforeEach(async () => {
|
|
157
|
+
cookieResponse = await setUserAccessCookie(publicationDomain);
|
|
158
|
+
});
|
|
159
|
+
itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
|
|
160
|
+
itSetsVstokenCookie();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('when getUserAccess is not successful', () => {
|
|
164
|
+
getUserAccessFails(['newspaper', 'plussalt']);
|
|
165
|
+
beforeEach(async () => {
|
|
166
|
+
cookieResponse = await setUserAccessCookie(publicationDomain);
|
|
167
|
+
});
|
|
168
|
+
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
169
|
+
itSetsNoCookies();
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe('setNonUserAccessCookie', () => {
|
|
174
|
+
describe('when getNonUserAccess is successful', () => {
|
|
175
|
+
getNonUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
176
|
+
beforeEach(async () => {
|
|
177
|
+
cookieResponse = await setNonUserAccessCookie(publicationDomain);
|
|
178
|
+
});
|
|
179
|
+
itReturnsCookieResponse(true, ['newspaper', 'plussalt']);
|
|
180
|
+
itSetsVstoken2Cookie();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
describe('when getNonUserAccess is not successful', () => {
|
|
184
|
+
getNonUserAccessFails(['newspaper', 'plussalt']);
|
|
185
|
+
beforeEach(async () => {
|
|
186
|
+
cookieResponse = await setNonUserAccessCookie(publicationDomain);
|
|
187
|
+
});
|
|
188
|
+
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
189
|
+
itSetsNoCookies();
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('setAccessCookies', () => {
|
|
194
|
+
describe('when both VSTOKEN and VSTOKEN2 requests succeed', () => {
|
|
195
|
+
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
196
|
+
getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
|
|
197
|
+
beforeEach(async () => {
|
|
198
|
+
cookieResponse = await setAccessCookies(publicationDomain);
|
|
199
|
+
});
|
|
200
|
+
itReturnsCookieResponse(true, ['newspaper', 'pluss', 'plussalt', 'sport']);
|
|
201
|
+
itSetsVstokenCookie();
|
|
202
|
+
itSetsVstoken2Cookie();
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
describe('when VSTOKEN fails and VSTOKEN2 succeeds', () => {
|
|
206
|
+
getUserAccessFails(['newspaper']);
|
|
207
|
+
getNonUserAccessSucceeds(['sport', 'pluss', 'newspaper']);
|
|
208
|
+
beforeEach(async () => {
|
|
209
|
+
cookieResponse = await setAccessCookies(publicationDomain);
|
|
210
|
+
});
|
|
211
|
+
itReturnsCookieResponse(false, ['newspaper', 'pluss', 'sport']);
|
|
212
|
+
itDoesNotSetVstokenCookie();
|
|
213
|
+
itSetsVstoken2Cookie();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('when VSTOKEN succeeds and VSTOKEN2 fails', () => {
|
|
217
|
+
getUserAccessSucceeds(['newspaper', 'plussalt']);
|
|
218
|
+
getNonUserAccessFails(['newspaper']);
|
|
219
|
+
beforeEach(async () => {
|
|
220
|
+
cookieResponse = await setAccessCookies(publicationDomain);
|
|
221
|
+
});
|
|
222
|
+
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
223
|
+
itSetsVstokenCookie();
|
|
224
|
+
itDoesNotSetVstoken2Cookie();
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('when both VSTOKEN and VSTOKEN2 fails', () => {
|
|
228
|
+
getUserAccessFails(['newspaper', 'plussalt']);
|
|
229
|
+
getNonUserAccessFails(['newspaper']);
|
|
230
|
+
beforeEach(async () => {
|
|
231
|
+
cookieResponse = await setAccessCookies(publicationDomain);
|
|
232
|
+
});
|
|
233
|
+
itReturnsCookieResponse(false, ['newspaper', 'plussalt']);
|
|
234
|
+
itSetsNoCookies();
|
|
235
|
+
});
|
|
236
|
+
});
|
|
@@ -32,6 +32,7 @@ export const hasAccess = (
|
|
|
32
32
|
currentAccessFeatures: string[],
|
|
33
33
|
requestedAccessFeatures: string[]
|
|
34
34
|
): boolean =>
|
|
35
|
+
requestedAccessFeatures.length < 1 ||
|
|
35
36
|
requestedAccessFeatures.filter((requestedAccessFeature) =>
|
|
36
37
|
currentAccessFeatures.includes(requestedAccessFeature)
|
|
37
38
|
).length > 0;
|
|
@@ -99,7 +100,8 @@ export const setAccessCookies = async (
|
|
|
99
100
|
success: accumulatedResult.success && result.success,
|
|
100
101
|
accessFeatures: accumulatedResult.accessFeatures
|
|
101
102
|
.concat(result.accessFeatures)
|
|
102
|
-
.filter((v, i, a) => a.indexOf(v) === i)
|
|
103
|
+
.filter((v, i, a) => a.indexOf(v) === i) // Remove duplicates by keeping only first copy
|
|
104
|
+
.sort(), // We want to keep result deterministic for easy testing
|
|
103
105
|
}),
|
|
104
106
|
{
|
|
105
107
|
success: true,
|
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.warn(
|
|
56
|
+
log.warn(`User attribute call failed (${service.toString()})`, error);
|
|
57
57
|
})
|
|
58
58
|
);
|
|
59
59
|
});
|
|
@@ -2,7 +2,6 @@ import { Boolean, Record, Static, String } from 'runtypes';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import log from '../utils/log';
|
|
4
4
|
import { getAidDomain } from '../utils/url';
|
|
5
|
-
import logValidationError from '../utils/logValidationError';
|
|
6
5
|
|
|
7
6
|
export const CACHE_KEY = 'haveTriedThirdPartyLoginCheck';
|
|
8
7
|
export const CACHE_TTL = 60 * 60 * 1000; // One hour
|
|
@@ -39,7 +38,7 @@ const getAidSession = () =>
|
|
|
39
38
|
try {
|
|
40
39
|
return ServiceSessionSchema.check(data);
|
|
41
40
|
} catch (error) {
|
|
42
|
-
|
|
41
|
+
log.error(error);
|
|
43
42
|
return null;
|
|
44
43
|
}
|
|
45
44
|
});
|
package/src/cache/index.spec.ts
CHANGED
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
import { Record, String } from 'runtypes';
|
|
6
6
|
import * as MockDate from 'mockdate';
|
|
7
7
|
import cache, { setCacheEngine } from '.';
|
|
8
|
-
import
|
|
8
|
+
import { setLogger } from '../utils/log';
|
|
9
9
|
import restoreAllMocks = jest.restoreAllMocks;
|
|
10
10
|
|
|
11
11
|
const TestDataSchema = Record({
|
|
12
12
|
test: String,
|
|
13
13
|
});
|
|
14
14
|
|
|
15
|
+
let log;
|
|
15
16
|
let mockEngine;
|
|
16
17
|
beforeEach(() => {
|
|
17
18
|
restoreAllMocks();
|
|
@@ -25,7 +26,10 @@ beforeEach(() => {
|
|
|
25
26
|
};
|
|
26
27
|
// @ts-ignore
|
|
27
28
|
setCacheEngine(mockEngine);
|
|
28
|
-
log
|
|
29
|
+
log = {
|
|
30
|
+
error: jest.fn(),
|
|
31
|
+
};
|
|
32
|
+
setLogger(log);
|
|
29
33
|
});
|
|
30
34
|
|
|
31
35
|
describe('Cache', () => {
|
|
@@ -65,11 +69,22 @@ describe('Cache', () => {
|
|
|
65
69
|
expect(mockEngine.get).toHaveBeenCalledWith('foo');
|
|
66
70
|
expect(mockEngine.remove).toHaveBeenCalledWith('foo');
|
|
67
71
|
expect(log.error).toHaveBeenCalledWith(
|
|
68
|
-
'
|
|
72
|
+
'Expected { test: string; }, but was incompatible',
|
|
69
73
|
{
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
error: {
|
|
75
|
+
code: 'CONTENT_INCORRECT',
|
|
76
|
+
details: {
|
|
77
|
+
test: 'Expected string, but was missing',
|
|
78
|
+
},
|
|
79
|
+
message: 'Expected { test: string; }, but was incompatible',
|
|
80
|
+
name: 'ValidationError',
|
|
81
|
+
stack: expect.stringMatching(
|
|
82
|
+
/ValidationError: Expected \{ test: string; \}, but was incompatible\n(^\s+at\s.+$\n){3,}/m
|
|
83
|
+
),
|
|
84
|
+
},
|
|
85
|
+
cacheData: { invalid: 'data' },
|
|
86
|
+
origin: '@amedia/user',
|
|
87
|
+
}
|
|
73
88
|
);
|
|
74
89
|
});
|
|
75
90
|
|
package/src/cache/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Static } from 'runtypes';
|
|
2
2
|
import { RuntypeBase } from 'runtypes/lib/runtype';
|
|
3
|
+
import log from '../utils/log';
|
|
3
4
|
import globalCache from './engines/global';
|
|
4
5
|
import { Engine } from './types';
|
|
5
|
-
import logValidationError from '../utils/logValidationError';
|
|
6
6
|
|
|
7
7
|
let cacheEngine: Engine;
|
|
8
8
|
|
|
@@ -33,7 +33,7 @@ const get = async <T extends RuntypeBase>(
|
|
|
33
33
|
const typedData = validator.check(cacheEntry.data);
|
|
34
34
|
return typedData as T;
|
|
35
35
|
} catch (error) {
|
|
36
|
-
|
|
36
|
+
log.error(error, { cacheData: cacheEntry.data });
|
|
37
37
|
remove(key);
|
|
38
38
|
return Promise.resolve();
|
|
39
39
|
}
|
package/src/clients/europa.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Record, Unknown, Dictionary, String } from 'runtypes';
|
|
|
3
3
|
import cache from '../cache';
|
|
4
4
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
5
5
|
import log from '../utils/log';
|
|
6
|
-
import logValidationError from '../utils/logValidationError';
|
|
7
6
|
|
|
8
7
|
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
9
8
|
|
|
@@ -117,7 +116,7 @@ export const getAllNamespaces = () =>
|
|
|
117
116
|
data: europaValidData,
|
|
118
117
|
};
|
|
119
118
|
} catch (error) {
|
|
120
|
-
|
|
119
|
+
log.error(error, { data });
|
|
121
120
|
throw error;
|
|
122
121
|
}
|
|
123
122
|
}),
|
package/src/clients/jupiter.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from 'runtypes';
|
|
10
10
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
11
11
|
import { UserAttributes } from '../types';
|
|
12
|
-
import
|
|
12
|
+
import log from '../utils/log';
|
|
13
13
|
|
|
14
14
|
export type AccessFeatures = string[];
|
|
15
15
|
|
|
@@ -70,7 +70,7 @@ export const checkForAccess = (
|
|
|
70
70
|
data,
|
|
71
71
|
};
|
|
72
72
|
} catch (error) {
|
|
73
|
-
|
|
73
|
+
log.error(error, { json });
|
|
74
74
|
throw error;
|
|
75
75
|
}
|
|
76
76
|
})
|
|
@@ -90,7 +90,7 @@ export const getUserAccess = (publicationDomain: string): Promise<GetVstoken> =>
|
|
|
90
90
|
try {
|
|
91
91
|
return GetVstokenSchema.check(json);
|
|
92
92
|
} catch (error) {
|
|
93
|
-
|
|
93
|
+
log.error(error, { json });
|
|
94
94
|
throw error;
|
|
95
95
|
}
|
|
96
96
|
});
|
|
@@ -113,7 +113,7 @@ export const getNonUserAccess = (
|
|
|
113
113
|
try {
|
|
114
114
|
return GetNonUserAccessSchema.check(json);
|
|
115
115
|
} catch (error) {
|
|
116
|
-
|
|
116
|
+
log.error(error, { json });
|
|
117
117
|
throw error;
|
|
118
118
|
}
|
|
119
119
|
});
|
package/src/clients/mercury.ts
CHANGED
|
@@ -11,7 +11,6 @@ function postLog(url: string, query: { [index: string]: any }) {
|
|
|
11
11
|
method: 'POST',
|
|
12
12
|
body,
|
|
13
13
|
headers: {
|
|
14
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
15
14
|
Accept: 'application/json',
|
|
16
15
|
},
|
|
17
16
|
};
|
|
@@ -22,28 +21,24 @@ function postLog(url: string, query: { [index: string]: any }) {
|
|
|
22
21
|
});
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
const backendLog = (
|
|
26
|
-
level: string,
|
|
27
|
-
message: string,
|
|
28
|
-
context: { [index: string]: any }
|
|
29
|
-
) => {
|
|
24
|
+
const backendLog = (level: string, message: string, context: Object) => {
|
|
30
25
|
const extendedContext = {
|
|
31
26
|
...context,
|
|
32
27
|
message,
|
|
33
28
|
url: getCurrentUrl(),
|
|
34
29
|
domain: getCurrentHostname(),
|
|
35
30
|
};
|
|
36
|
-
postLog(
|
|
31
|
+
postLog(`/api/mercury/v1/log/${level}`, extendedContext);
|
|
37
32
|
};
|
|
38
33
|
|
|
39
34
|
const log = {
|
|
40
|
-
error: (message: string, context:
|
|
35
|
+
error: (message: string, context: Object) =>
|
|
41
36
|
backendLog('error', message, context),
|
|
42
|
-
warn: (message: string, context:
|
|
37
|
+
warn: (message: string, context: Object) =>
|
|
43
38
|
backendLog('warn', message, context),
|
|
44
|
-
info: (message: string, context:
|
|
39
|
+
info: (message: string, context: Object) =>
|
|
45
40
|
backendLog('info', message, context),
|
|
46
|
-
debug: (message: string, context:
|
|
41
|
+
debug: (message: string, context: Object) =>
|
|
47
42
|
backendLog('debug', message, context),
|
|
48
43
|
};
|
|
49
44
|
|
|
@@ -64,7 +59,10 @@ export const activateSubscription = async (
|
|
|
64
59
|
try {
|
|
65
60
|
data = await response.json();
|
|
66
61
|
} catch (error) {
|
|
67
|
-
log.error(
|
|
62
|
+
log.error(
|
|
63
|
+
'Activate subscription. Response from mercury does not contain parsable json',
|
|
64
|
+
{ error, response, accessFeatures }
|
|
65
|
+
);
|
|
68
66
|
}
|
|
69
67
|
if (!response.ok) {
|
|
70
68
|
if (response.status === 401 && data?.status === 'user_deactivated') {
|
package/src/clients/nebula.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from 'runtypes';
|
|
10
10
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
11
11
|
import { UserAttributes } from '../types';
|
|
12
|
-
import
|
|
12
|
+
import log from '../utils/log';
|
|
13
13
|
|
|
14
14
|
const InfoSchema = Dictionary(Unknown, String).And(
|
|
15
15
|
Partial({
|
|
@@ -61,7 +61,7 @@ const getNebulaData = (
|
|
|
61
61
|
data: InfoSchema.check(json),
|
|
62
62
|
};
|
|
63
63
|
} catch (error) {
|
|
64
|
-
|
|
64
|
+
log.error(error, { json });
|
|
65
65
|
throw error;
|
|
66
66
|
}
|
|
67
67
|
});
|
|
@@ -2,6 +2,7 @@ import fetch from 'jest-fetch-mock';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import store from '../store';
|
|
4
4
|
import { getCurrentSession, removeSession } from './pluto';
|
|
5
|
+
import { setLogger } from '../utils/log';
|
|
5
6
|
import restoreAllMocks = jest.restoreAllMocks;
|
|
6
7
|
|
|
7
8
|
beforeEach(async () => {
|
|
@@ -23,6 +24,26 @@ const plutoLoggedInResponse = {
|
|
|
23
24
|
tracking_key: userTrackingKey,
|
|
24
25
|
};
|
|
25
26
|
|
|
27
|
+
let log;
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
log = {
|
|
30
|
+
debug: jest.fn(),
|
|
31
|
+
info: jest.fn(),
|
|
32
|
+
warn: jest.fn(),
|
|
33
|
+
error: jest.fn(),
|
|
34
|
+
};
|
|
35
|
+
setLogger(log);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
function itShouldNotHaveLoggedAnything() {
|
|
39
|
+
it('should not have logged anything', () => {
|
|
40
|
+
expect(log.debug).not.toHaveBeenCalled();
|
|
41
|
+
expect(log.info).not.toHaveBeenCalled();
|
|
42
|
+
expect(log.warn).not.toHaveBeenCalled();
|
|
43
|
+
expect(log.error).not.toHaveBeenCalled();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
26
47
|
describe('Pluto service', () => {
|
|
27
48
|
describe('getCurrentSession()', () => {
|
|
28
49
|
beforeEach(() => {
|
|
@@ -82,7 +103,7 @@ describe('Pluto service', () => {
|
|
|
82
103
|
fetch.mockResponseOnce(() =>
|
|
83
104
|
Promise.resolve({
|
|
84
105
|
init: { status: 403 },
|
|
85
|
-
body: '{"code": "session.not_found"}',
|
|
106
|
+
body: '{"errors": [{"code": "session.not_found"}]}',
|
|
86
107
|
})
|
|
87
108
|
);
|
|
88
109
|
currentSession = await getCurrentSession();
|
|
@@ -90,6 +111,7 @@ describe('Pluto service', () => {
|
|
|
90
111
|
it('should call Pluto', () => {
|
|
91
112
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
92
113
|
});
|
|
114
|
+
itShouldNotHaveLoggedAnything();
|
|
93
115
|
itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
|
|
94
116
|
});
|
|
95
117
|
describe('When cache is not empty', () => {
|
|
@@ -100,8 +122,30 @@ describe('Pluto service', () => {
|
|
|
100
122
|
it('should not call Pluto', () => {
|
|
101
123
|
expect(fetch).toHaveBeenCalledTimes(0);
|
|
102
124
|
});
|
|
125
|
+
itShouldNotHaveLoggedAnything();
|
|
103
126
|
itShouldReturnNoSessionDataAndSetLoggedInFalseInState();
|
|
104
127
|
});
|
|
128
|
+
describe('when session is invalid for domain', () => {
|
|
129
|
+
beforeEach(async () => {
|
|
130
|
+
fetch.mockResponseOnce(() =>
|
|
131
|
+
Promise.resolve({
|
|
132
|
+
init: { status: 403 },
|
|
133
|
+
body: '{"errors": [{"code": "session.invalid_for_domain"}]}',
|
|
134
|
+
})
|
|
135
|
+
);
|
|
136
|
+
currentSession = await getCurrentSession();
|
|
137
|
+
});
|
|
138
|
+
it('should have logged a warning', () => {
|
|
139
|
+
expect(log.warn).toHaveBeenCalledWith(
|
|
140
|
+
'Pluto responded with HTTP/403: (session.invalid_for_domain) while fetching current session',
|
|
141
|
+
{
|
|
142
|
+
error: { code: 'session.invalid_for_domain' },
|
|
143
|
+
origin: '@amedia/user',
|
|
144
|
+
response: expect.any(Response),
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
105
149
|
});
|
|
106
150
|
});
|
|
107
151
|
|
package/src/clients/pluto.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Partial, Static, String } from 'runtypes';
|
|
1
|
+
import { Partial, Record, Static, String, Array } from 'runtypes';
|
|
2
2
|
import log from '../utils/log';
|
|
3
3
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
4
4
|
import store from '../store';
|
|
5
5
|
import { UserAttributes } from '../types';
|
|
6
|
-
import logValidationError from '../utils/logValidationError';
|
|
7
6
|
|
|
8
7
|
const ServiceSessionSchema = Partial({
|
|
9
8
|
user_uuid: String,
|
|
@@ -19,18 +18,53 @@ const mapSessionToUserAttributes = (
|
|
|
19
18
|
trackingKey: session.tracking_key,
|
|
20
19
|
});
|
|
21
20
|
|
|
21
|
+
const PlutoErrorSchema = Record({
|
|
22
|
+
code: String,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const PlutoErrorsSchema = Record({
|
|
26
|
+
errors: Array(PlutoErrorSchema),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const IGNORED_CODES = [
|
|
30
|
+
'session.not_found',
|
|
31
|
+
'session.not_specified',
|
|
32
|
+
'user.deactivated',
|
|
33
|
+
];
|
|
34
|
+
const WARNING_CODE = ['session.invalid_for_domain', 'user.not_found'];
|
|
35
|
+
|
|
36
|
+
const resolveBadResponse = async (response) => {
|
|
37
|
+
let error: Static<typeof PlutoErrorSchema>;
|
|
38
|
+
try {
|
|
39
|
+
[error] = PlutoErrorsSchema.check(JSON.parse(await response.body)).errors;
|
|
40
|
+
} catch (e) {
|
|
41
|
+
log.error('Unable to parse error body from pluto', { error: e, response });
|
|
42
|
+
return Promise.reject();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (IGNORED_CODES.includes(error?.code)) {
|
|
46
|
+
return Promise.resolve();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (WARNING_CODE.includes(error?.code)) {
|
|
50
|
+
log.warn(
|
|
51
|
+
`Pluto responded with HTTP/${response.status}: (${error.code}) while fetching current session`,
|
|
52
|
+
{ error, response }
|
|
53
|
+
);
|
|
54
|
+
return Promise.resolve();
|
|
55
|
+
}
|
|
56
|
+
log.error(
|
|
57
|
+
`Pluto responded with HTTP/${response.status}: ${response.statusText}) when fetching current session`,
|
|
58
|
+
{ error, response }
|
|
59
|
+
);
|
|
60
|
+
return Promise.reject();
|
|
61
|
+
};
|
|
62
|
+
|
|
22
63
|
const fetchCurrentSession = () =>
|
|
23
64
|
fetch(`/api/pluto/v1/sessions/current`)
|
|
24
65
|
.then((response) => {
|
|
25
66
|
if (!response.ok) {
|
|
26
|
-
|
|
27
|
-
return {};
|
|
28
|
-
}
|
|
29
|
-
log.error(
|
|
30
|
-
`Unknown error (${response.status}: ${response.statusText}) when fetching current session`,
|
|
31
|
-
{ response }
|
|
32
|
-
);
|
|
33
|
-
return Promise.reject();
|
|
67
|
+
return resolveBadResponse(response).then(() => ({}));
|
|
34
68
|
}
|
|
35
69
|
return response.json();
|
|
36
70
|
})
|
|
@@ -47,7 +81,7 @@ const fetchCurrentSession = () =>
|
|
|
47
81
|
data: ServiceSessionSchema.check(json),
|
|
48
82
|
};
|
|
49
83
|
} catch (error) {
|
|
50
|
-
|
|
84
|
+
log.error(error, { json });
|
|
51
85
|
return nodata;
|
|
52
86
|
}
|
|
53
87
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
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 UserDataRequest from './lib/UserDataRequest';
|
|
5
|
-
import PaywallUnlockRequest from './lib/PaywallUnlockRequest';
|
|
6
4
|
import { AmediaUserEvents } from './eventHandlers/eventMap';
|
|
7
5
|
import { getCurrentUrl } from './utils/url';
|
|
8
6
|
import { runJanitor } from './janitor';
|
|
@@ -11,12 +9,8 @@ import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
|
11
9
|
import { listenForStorageRequests } from './eventHandlers/storage';
|
|
12
10
|
import { hasAccess } from './actions/authorization';
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* get notified when something changes
|
|
17
|
-
*/
|
|
18
|
-
export const user = () => new UserDataRequest();
|
|
19
|
-
export const paywall = () => new PaywallUnlockRequest();
|
|
12
|
+
export { default as UserDataRequest } from './lib/UserDataRequest';
|
|
13
|
+
export { default as PaywallUnlockRequest } from './lib/PaywallUnlockRequest';
|
|
20
14
|
|
|
21
15
|
/**
|
|
22
16
|
* This will, if necessary, trigger a redirect to www.aid.no and return
|