@amedia/user 0.14.8 → 0.14.9
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 +1 -0
- package/dist/user.d.ts +5 -2
- package/package.json +1 -1
- package/src/actions/authorization.spec.ts +12 -4
- package/src/actions/authorization.ts +5 -2
- package/src/clients/mercury.spec.ts +15 -13
- package/src/clients/mercury.ts +3 -3
- package/src/eventHandlers/activation.ts +6 -1
- package/src/lib/PaywallUnlockRequest.spec.ts +31 -2
- package/src/lib/PaywallUnlockRequest.ts +11 -1
package/README.md
CHANGED
|
@@ -151,6 +151,7 @@ To unlock the paywall, you first need to create a new `PaywallUnlockRequest`. On
|
|
|
151
151
|
to `requestedUrl`. (Provides more flexibility than `reloadOnAccess()`)
|
|
152
152
|
- `withAccessFeatures(accessFeatures: string[])`: Specify what access features will give access to the current content.
|
|
153
153
|
The default is [`newspaper`].
|
|
154
|
+
- `withPublicationDomain(domain: string)`: Specify which site we to attempt activation against. The default is the current domain. This is mostly useful for direktesport.no which often knows what domain the user came from.
|
|
154
155
|
|
|
155
156
|
More details about the access model can be found here: [Access model](https://amedia.slite.com/app/channels/hOlqx3Miez/notes/nhG2Ok39BN)
|
|
156
157
|
|
package/dist/user.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare module '@amedia/user/actions/authorization' {
|
|
|
16
16
|
accessFeatures: string[];
|
|
17
17
|
};
|
|
18
18
|
export const getAccess: (accessFeatures?: string[]) => Promise<Pick<UserAttributes, 'access'>>;
|
|
19
|
-
export const activate: (accessFeatures: string[]) => Promise<string[]>;
|
|
19
|
+
export const activate: (accessFeatures: string[], publicationDomain: string) => Promise<string[]>;
|
|
20
20
|
export const hasAccess: (currentAccessFeatures: string[], requestedAccessFeatures: string[]) => boolean;
|
|
21
21
|
export const setUserAccessCookie: (publicationDomain: string) => Promise<SetCookieResponse>;
|
|
22
22
|
export const setNonUserAccessCookie: (publicationDomain: string) => Promise<SetCookieResponse>;
|
|
@@ -240,7 +240,7 @@ declare module '@amedia/user/clients/jupiter.spec' {
|
|
|
240
240
|
declare module '@amedia/user/clients/mercury' {
|
|
241
241
|
export class UserDeactivatedError extends Error {
|
|
242
242
|
}
|
|
243
|
-
export const activateSubscription: (accessFeatures: string[]) => Promise<string[]>;
|
|
243
|
+
export const activateSubscription: (accessFeatures: string[], publicationDomain: string) => Promise<string[]>;
|
|
244
244
|
|
|
245
245
|
}
|
|
246
246
|
declare module '@amedia/user/clients/mercury.spec' {
|
|
@@ -291,6 +291,7 @@ declare module '@amedia/user/eventHandlers/activation' {
|
|
|
291
291
|
interface WindowEventMap {
|
|
292
292
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
293
293
|
accessFeatures: string[];
|
|
294
|
+
publicationDomain?: string;
|
|
294
295
|
}>;
|
|
295
296
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
296
297
|
accessFeatures: string[];
|
|
@@ -486,6 +487,8 @@ declare module '@amedia/user/lib/PaywallUnlockRequest' {
|
|
|
486
487
|
private requestedAccessFeatures;
|
|
487
488
|
private reload;
|
|
488
489
|
private requestedUrl;
|
|
490
|
+
private publicationDomain;
|
|
491
|
+
withPublicationDomain(publicationDomain: string): this;
|
|
489
492
|
withAccessFeatures(accessFeatures: string[]): this;
|
|
490
493
|
reloadOnAccess(): this;
|
|
491
494
|
redirectOnAccess(requestedUrl: string): this;
|
package/package.json
CHANGED
|
@@ -49,8 +49,13 @@ describe('activate', () => {
|
|
|
49
49
|
.mockResolvedValue(['whatever']);
|
|
50
50
|
});
|
|
51
51
|
it('Returns whatever mercury returns', async () => {
|
|
52
|
-
expect(await activate(['newspaper'])).toEqual([
|
|
53
|
-
|
|
52
|
+
expect(await activate(['newspaper'], 'www.telen.no')).toEqual([
|
|
53
|
+
'whatever',
|
|
54
|
+
]);
|
|
55
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
56
|
+
['newspaper'],
|
|
57
|
+
'www.telen.no'
|
|
58
|
+
);
|
|
54
59
|
});
|
|
55
60
|
});
|
|
56
61
|
|
|
@@ -62,8 +67,11 @@ describe('activate', () => {
|
|
|
62
67
|
jest.spyOn(authentication, 'logout').mockResolvedValue(undefined);
|
|
63
68
|
});
|
|
64
69
|
it('Returns whatever mercury returns', async () => {
|
|
65
|
-
expect(await activate([])).toEqual([]);
|
|
66
|
-
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
70
|
+
expect(await activate([], 'www.telen.no')).toEqual([]);
|
|
71
|
+
expect(mercury.activateSubscription).toHaveBeenCalledWith(
|
|
72
|
+
[],
|
|
73
|
+
'www.telen.no'
|
|
74
|
+
);
|
|
67
75
|
expect(authentication.logout).toHaveBeenCalled();
|
|
68
76
|
});
|
|
69
77
|
});
|
|
@@ -16,9 +16,12 @@ export const getAccess = (
|
|
|
16
16
|
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
17
17
|
checkForAccess(getCurrentPublicationDomain(), accessFeatures);
|
|
18
18
|
|
|
19
|
-
export const activate = async (
|
|
19
|
+
export const activate = async (
|
|
20
|
+
accessFeatures: string[],
|
|
21
|
+
publicationDomain: string
|
|
22
|
+
): Promise<string[]> => {
|
|
20
23
|
try {
|
|
21
|
-
return await activateSubscription(accessFeatures);
|
|
24
|
+
return await activateSubscription(accessFeatures, publicationDomain);
|
|
22
25
|
} catch (error) {
|
|
23
26
|
if (error instanceof UserDeactivatedError) {
|
|
24
27
|
await logout();
|
|
@@ -6,16 +6,18 @@ describe('activateSubscription()', () => {
|
|
|
6
6
|
fetch.mockResponse(JSON.stringify({ status: 'user_deactivated' }), {
|
|
7
7
|
status: 401,
|
|
8
8
|
});
|
|
9
|
-
await expect(
|
|
10
|
-
|
|
11
|
-
);
|
|
9
|
+
await expect(
|
|
10
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
11
|
+
).rejects.toThrow(UserDeactivatedError);
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
it('should return empty list if user not found', async () => {
|
|
15
15
|
fetch.mockResponse(JSON.stringify({ status: 'no_session_found' }), {
|
|
16
16
|
status: 401,
|
|
17
17
|
});
|
|
18
|
-
await expect(
|
|
18
|
+
await expect(
|
|
19
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
20
|
+
).resolves.toEqual([]);
|
|
19
21
|
});
|
|
20
22
|
|
|
21
23
|
it('should return list if access features even if requested access is not "granted"', async () => {
|
|
@@ -26,9 +28,9 @@ describe('activateSubscription()', () => {
|
|
|
26
28
|
access_features: ['newspaper'],
|
|
27
29
|
})
|
|
28
30
|
);
|
|
29
|
-
await expect(
|
|
30
|
-
'newspaper',
|
|
31
|
-
]);
|
|
31
|
+
await expect(
|
|
32
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
33
|
+
).resolves.toEqual(['newspaper']);
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
it('should return list of access features even if status is not "success"', async () => {
|
|
@@ -39,9 +41,9 @@ describe('activateSubscription()', () => {
|
|
|
39
41
|
access_features: ['newspaper'],
|
|
40
42
|
})
|
|
41
43
|
);
|
|
42
|
-
await expect(
|
|
43
|
-
'newspaper',
|
|
44
|
-
]);
|
|
44
|
+
await expect(
|
|
45
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
46
|
+
).resolves.toEqual(['newspaper']);
|
|
45
47
|
});
|
|
46
48
|
|
|
47
49
|
it('should return ["newspaper"] if status is "success" and access is "granted"', async () => {
|
|
@@ -52,8 +54,8 @@ describe('activateSubscription()', () => {
|
|
|
52
54
|
access_features: ['newspaper'],
|
|
53
55
|
})
|
|
54
56
|
);
|
|
55
|
-
await expect(
|
|
56
|
-
'newspaper',
|
|
57
|
-
]);
|
|
57
|
+
await expect(
|
|
58
|
+
activateSubscription(['newspaper'], 'www.telen.no')
|
|
59
|
+
).resolves.toEqual(['newspaper']);
|
|
58
60
|
});
|
|
59
61
|
});
|
package/src/clients/mercury.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import log from '../utils/log';
|
|
2
|
-
import { getCurrentPublicationDomain } from '../utils/url';
|
|
3
2
|
|
|
4
3
|
export class UserDeactivatedError extends Error {}
|
|
5
4
|
|
|
@@ -10,10 +9,11 @@ const EXPECTED_ERRORS = [
|
|
|
10
9
|
];
|
|
11
10
|
|
|
12
11
|
export const activateSubscription = async (
|
|
13
|
-
accessFeatures: string[]
|
|
12
|
+
accessFeatures: string[],
|
|
13
|
+
publicationDomain: string
|
|
14
14
|
): Promise<string[]> =>
|
|
15
15
|
fetch(
|
|
16
|
-
`/api/mercury/v2/access/activate?publication_wwwdomain=${
|
|
16
|
+
`/api/mercury/v2/access/activate?publication_wwwdomain=${publicationDomain}&access_features=${accessFeatures.join(
|
|
17
17
|
','
|
|
18
18
|
)}`,
|
|
19
19
|
{
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
2
2
|
import { activate } from '../actions/authorization';
|
|
3
3
|
import getGlobalThis from '../utils/getGlobalThis';
|
|
4
|
+
import { getCurrentPublicationDomain } from '../utils/url';
|
|
4
5
|
|
|
5
6
|
export enum ActivationEvents {
|
|
6
7
|
REQUEST_ACTIVATION = 'amedia-user.activation.request-activation',
|
|
@@ -11,6 +12,7 @@ declare global {
|
|
|
11
12
|
interface WindowEventMap {
|
|
12
13
|
[ActivationEvents.REQUEST_ACTIVATION]: CustomEvent<{
|
|
13
14
|
accessFeatures: string[];
|
|
15
|
+
publicationDomain?: string;
|
|
14
16
|
}>;
|
|
15
17
|
[ActivationEvents.ON_ACTIVATION_COMPLETE]: CustomEvent<{
|
|
16
18
|
accessFeatures: string[];
|
|
@@ -20,7 +22,10 @@ declare global {
|
|
|
20
22
|
|
|
21
23
|
export const listenForActivationRequests = () => {
|
|
22
24
|
listenWithExclusivity(ActivationEvents.REQUEST_ACTIVATION, (evt) => {
|
|
23
|
-
activate(
|
|
25
|
+
activate(
|
|
26
|
+
evt.detail.accessFeatures,
|
|
27
|
+
evt.detail.publicationDomain || getCurrentPublicationDomain()
|
|
28
|
+
).then((accessFeatures) => {
|
|
24
29
|
dispatch(
|
|
25
30
|
new CustomEvent(ActivationEvents.ON_ACTIVATION_COMPLETE, {
|
|
26
31
|
detail: { accessFeatures },
|
|
@@ -41,9 +41,15 @@ function itShouldNotAttemptActivation() {
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
function itShouldAttemptActivationWithAccessFeatures(
|
|
44
|
+
function itShouldAttemptActivationWithAccessFeatures(
|
|
45
|
+
accessFeatures: string[],
|
|
46
|
+
publicationDomain: string = 'localhost'
|
|
47
|
+
) {
|
|
45
48
|
it('should attempt activation with access features', () => {
|
|
46
|
-
expect(authorization.activate).toHaveBeenCalledWith(
|
|
49
|
+
expect(authorization.activate).toHaveBeenCalledWith(
|
|
50
|
+
accessFeatures,
|
|
51
|
+
publicationDomain
|
|
52
|
+
);
|
|
47
53
|
});
|
|
48
54
|
}
|
|
49
55
|
|
|
@@ -257,6 +263,29 @@ describe('PaywallUnlocker', () => {
|
|
|
257
263
|
itShouldAttemptActivationWithAccessFeatures(['newspaper']);
|
|
258
264
|
itShouldNotRedirect();
|
|
259
265
|
});
|
|
266
|
+
|
|
267
|
+
describe('with specific publicationDomain', () => {
|
|
268
|
+
beforeEach(async () => {
|
|
269
|
+
unlockResponse = await new PaywallUnlockRequest()
|
|
270
|
+
.withPublicationDomain('www.telen.no')
|
|
271
|
+
.tryUnlock();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should give access with access features', () => {
|
|
275
|
+
expect(unlockResponse).toEqual({
|
|
276
|
+
isLoggedIn: true,
|
|
277
|
+
hasAccess: true,
|
|
278
|
+
accessFeatures: ['newspaper'],
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
itShouldAttemptActivationWithAccessFeatures(
|
|
283
|
+
['newspaper'],
|
|
284
|
+
'www.telen.no'
|
|
285
|
+
);
|
|
286
|
+
itShouldNotReload();
|
|
287
|
+
itShouldNotRedirect();
|
|
288
|
+
});
|
|
260
289
|
});
|
|
261
290
|
|
|
262
291
|
describe('and activation does not give access', () => {
|
|
@@ -50,6 +50,13 @@ class PaywallUnlockRequest {
|
|
|
50
50
|
|
|
51
51
|
private requestedUrl: string | undefined;
|
|
52
52
|
|
|
53
|
+
private publicationDomain: string = getCurrentPublicationDomain();
|
|
54
|
+
|
|
55
|
+
public withPublicationDomain(publicationDomain: string): this {
|
|
56
|
+
this.publicationDomain = publicationDomain;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
53
60
|
public withAccessFeatures(accessFeatures: string[]): this {
|
|
54
61
|
this.requestedAccessFeatures = accessFeatures;
|
|
55
62
|
return this;
|
|
@@ -158,7 +165,10 @@ class PaywallUnlockRequest {
|
|
|
158
165
|
}
|
|
159
166
|
|
|
160
167
|
private async activateAccess(requestedAccessFeatures: AccessFeatures) {
|
|
161
|
-
const accessFeatures = await activate(
|
|
168
|
+
const accessFeatures = await activate(
|
|
169
|
+
this.requestedAccessFeatures,
|
|
170
|
+
this.publicationDomain
|
|
171
|
+
);
|
|
162
172
|
return {
|
|
163
173
|
denied: !hasAccess(accessFeatures, requestedAccessFeatures),
|
|
164
174
|
accessFeatures,
|