@amedia/user 0.5.2 → 0.6.0
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 +2 -2
- package/cloudbuild.json +29 -0
- package/cop.json +8 -0
- package/dist/actions/authentication.d.ts +1 -1
- package/dist/actions/preferences.d.ts +9 -0
- package/dist/actions/preferences.spec.d.ts +1 -0
- package/dist/cache/index.d.ts +3 -2
- package/dist/clients/europa.d.ts +12 -0
- package/dist/clients/europa.spec.d.ts +1 -0
- package/dist/clients/jupiter.d.ts +29 -0
- package/dist/clients/jupiter.spec.d.ts +1 -0
- package/dist/clients/mercury.d.ts +18 -0
- package/dist/clients/mercury.spec.d.ts +4 -0
- package/dist/clients/nebula.d.ts +22 -0
- package/dist/clients/nebula.spec.d.ts +1 -0
- package/dist/clients/pluto.d.ts +8 -0
- package/dist/clients/pluto.spec.d.ts +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/lib/rpc.d.ts +6 -1
- package/dist/services/europa.d.ts +12 -0
- package/dist/services/europa.spec.d.ts +1 -0
- package/dist/services/nebula.d.ts +22 -0
- package/dist/services/nebula.spec.d.ts +1 -0
- package/dist/store/store.d.ts +13 -0
- package/dist/store/store.spec.d.ts +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/utils/getOrUpdate.d.ts +3 -0
- package/package.json +4 -3
- package/src/actions/authentication.ts +2 -2
- package/src/actions/authorization.ts +4 -7
- package/src/actions/preferences.spec.ts +54 -0
- package/src/actions/preferences.ts +69 -0
- package/src/actions/userData.ts +2 -0
- package/src/cache/index.spec.ts +7 -2
- package/src/cache/index.ts +4 -3
- package/src/clients/europa.spec.ts +93 -0
- package/src/clients/europa.ts +126 -0
- package/src/clients/jupiter.spec.ts +154 -0
- package/src/{services → clients}/jupiter.ts +0 -1
- package/src/clients/mercury.spec.ts +70 -0
- package/src/clients/nebula.spec.ts +71 -0
- package/src/clients/nebula.ts +51 -0
- package/src/index-authentcation.spec.ts +2 -2
- package/src/index-userdata.spec.ts +11 -11
- package/src/index.ts +4 -3
- package/src/lib/rpc.ts +9 -3
- package/src/store/store.spec.ts +34 -0
- package/src/store/store.ts +46 -0
- package/src/types.ts +8 -0
- package/src/utils/getOrUpdate.ts +41 -0
- package/src/utils/log.ts +2 -1
- package/src/utils/queue.ts +0 -2
- package/src/services/jupiter.spec.ts +0 -150
- package/src/services/mercury.spec.ts +0 -30
- /package/src/{services → clients}/mercury.ts +0 -0
- /package/src/{services → clients}/pluto.spec.ts +0 -0
- /package/src/{services → clients}/pluto.ts +0 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import log from '../utils/log';
|
|
3
|
+
import {
|
|
4
|
+
checkForAccess,
|
|
5
|
+
getVstoken,
|
|
6
|
+
AccessFeatures,
|
|
7
|
+
CheckAccessForPaper,
|
|
8
|
+
GetVstoken,
|
|
9
|
+
GetNonUserAccess,
|
|
10
|
+
getNonUserAccess,
|
|
11
|
+
} from './jupiter';
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
fetch.resetMocks();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('jupiter service', () => {
|
|
18
|
+
describe('GET /check_access_for_paper', () => {
|
|
19
|
+
const publicationDomain = 'www.rb.no';
|
|
20
|
+
const accessFeatures: AccessFeatures = ['newspaper'];
|
|
21
|
+
|
|
22
|
+
it('should check access for paper', async () => {
|
|
23
|
+
const response: CheckAccessForPaper = {
|
|
24
|
+
status: 'success',
|
|
25
|
+
access: 'granted',
|
|
26
|
+
publication: {
|
|
27
|
+
domain: 'www.rb.no',
|
|
28
|
+
id: 95,
|
|
29
|
+
},
|
|
30
|
+
access_features: ['amedia_staff', 'newspaper'],
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
34
|
+
|
|
35
|
+
await expect(
|
|
36
|
+
checkForAccess(publicationDomain, accessFeatures)
|
|
37
|
+
).resolves.toEqual(response);
|
|
38
|
+
|
|
39
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
40
|
+
`/api/jupiter/v1/check_access_for_paper/${publicationDomain}/?require=any&access_features=${accessFeatures.join(
|
|
41
|
+
','
|
|
42
|
+
)}`
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should deny access for user not logged in', async () => {
|
|
47
|
+
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
48
|
+
status: 400,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
await expect(
|
|
52
|
+
checkForAccess(publicationDomain, accessFeatures)
|
|
53
|
+
).rejects.toThrow('Request not ok (400: Bad Request).');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should fail on runtime validation', async () => {
|
|
57
|
+
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
58
|
+
log.error = jest.fn();
|
|
59
|
+
|
|
60
|
+
await expect(
|
|
61
|
+
checkForAccess(publicationDomain, accessFeatures)
|
|
62
|
+
).rejects.toThrow(
|
|
63
|
+
'Expected { status: "success"; access: "granted" | "denied"; publication: { id: number; domain: string; }; access_features: string[]; }, but was incompatible'
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('POST /get_vstoken', () => {
|
|
69
|
+
const publicationDomain = 'www.rb.no';
|
|
70
|
+
|
|
71
|
+
it('should return vstoken', async () => {
|
|
72
|
+
const response: GetVstoken = {
|
|
73
|
+
status: 'success',
|
|
74
|
+
cookie: 'ofofb:16187:amedia_staff,newspaper,plussalt:cc2c2c:rb:jupiter',
|
|
75
|
+
access_features: ['newspaper', 'plussalt'],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
79
|
+
|
|
80
|
+
await expect(getVstoken(publicationDomain)).resolves.toEqual(response);
|
|
81
|
+
|
|
82
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
83
|
+
`/api/jupiter/v1/get_vstoken/${publicationDomain}`,
|
|
84
|
+
{
|
|
85
|
+
method: 'post',
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should deny access for user not logged in', async () => {
|
|
91
|
+
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
92
|
+
status: 400,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
await expect(getVstoken(publicationDomain)).rejects.toThrow(
|
|
96
|
+
'Request not ok (400: Bad Request).'
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should fail on runtime validation', async () => {
|
|
101
|
+
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
102
|
+
log.error = jest.fn();
|
|
103
|
+
|
|
104
|
+
await expect(getVstoken(publicationDomain)).rejects.toThrow(
|
|
105
|
+
'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('POST /non_user_access', () => {
|
|
111
|
+
const publicationDomain = 'www.rb.no';
|
|
112
|
+
|
|
113
|
+
it('should return none user access', async () => {
|
|
114
|
+
const response: GetNonUserAccess = {
|
|
115
|
+
status: 'success',
|
|
116
|
+
vstoken_cookie: 'd82ab3842a:2619693230:::rb:jupiter',
|
|
117
|
+
uuid: '',
|
|
118
|
+
access_features: [],
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
122
|
+
|
|
123
|
+
await expect(getNonUserAccess(publicationDomain)).resolves.toEqual(
|
|
124
|
+
response
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
expect(
|
|
128
|
+
fetch
|
|
129
|
+
).toHaveBeenCalledWith(
|
|
130
|
+
`/api/jupiter/v1/non_user_access/${publicationDomain}`,
|
|
131
|
+
{ method: 'post' }
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should deny access for user not logged in', async () => {
|
|
136
|
+
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
137
|
+
status: 400,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await expect(getNonUserAccess(publicationDomain)).rejects.toThrow(
|
|
141
|
+
'Request not ok (400: Bad Request).'
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should fail on runtime validation', async () => {
|
|
146
|
+
fetch.mockResponseOnce(JSON.stringify({ vstoken_cookie: 3 }));
|
|
147
|
+
log.error = jest.fn();
|
|
148
|
+
|
|
149
|
+
await expect(getNonUserAccess(publicationDomain)).rejects.toThrow(
|
|
150
|
+
'Expected { status: "success"; vstoken_cookie: string; uuid: string; access_features: string[]; }, but was incompatible'
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fetch from 'jest-fetch-mock';
|
|
6
|
+
import backendLog, {
|
|
7
|
+
activateSubscription,
|
|
8
|
+
UserDeactivatedError,
|
|
9
|
+
} from './mercury';
|
|
10
|
+
|
|
11
|
+
fetch.enableMocks();
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
fetch.resetMocks();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('Mercury backendlog', () => {
|
|
18
|
+
it('should include level in url', () => {
|
|
19
|
+
backendLog.error('jest message', {});
|
|
20
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
21
|
+
expect.stringContaining('/error'),
|
|
22
|
+
expect.anything()
|
|
23
|
+
);
|
|
24
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should include additional context as query parameters', () => {
|
|
28
|
+
backendLog.error('jest message', { referrer: 'https://www.rb.no/' });
|
|
29
|
+
const formData = fetch.mock.calls[0][1]?.body as FormData;
|
|
30
|
+
|
|
31
|
+
expect(formData?.get('referrer')).toEqual('https://www.rb.no/');
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('activateSubscription()', () => {
|
|
36
|
+
it('should throw error if user is deactivated', () => {
|
|
37
|
+
fetch.mockResponse(JSON.stringify({ status: 'user_deactivated' }), {
|
|
38
|
+
status: 401,
|
|
39
|
+
});
|
|
40
|
+
return expect(activateSubscription('newspaper')).rejects.toThrow(
|
|
41
|
+
UserDeactivatedError
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should return false if user not found', () => {
|
|
46
|
+
fetch.mockResponse(JSON.stringify({ status: 'whatever' }), {
|
|
47
|
+
status: 401,
|
|
48
|
+
});
|
|
49
|
+
return expect(activateSubscription('newspaper')).resolves.toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should return false if access is not "granted"', () => {
|
|
53
|
+
fetch.mockResponse(JSON.stringify({ status: 'success', access: 'denied' }));
|
|
54
|
+
return expect(activateSubscription('newspaper')).resolves.toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should return false if status is not "success"', () => {
|
|
58
|
+
fetch.mockResponse(
|
|
59
|
+
JSON.stringify({ status: 'unknown', access: 'granted' })
|
|
60
|
+
);
|
|
61
|
+
return expect(activateSubscription('newspaper')).resolves.toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should return true if status is "success" and access is "granted"', () => {
|
|
65
|
+
fetch.mockResponse(
|
|
66
|
+
JSON.stringify({ status: 'success', access: 'granted' })
|
|
67
|
+
);
|
|
68
|
+
return expect(activateSubscription('newspaper')).resolves.toBe(true);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import log from '../utils/log';
|
|
3
|
+
import { Info, getInfo } from './nebula';
|
|
4
|
+
|
|
5
|
+
describe('nebula service', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
fetch.resetMocks();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe('GET /info', () => {
|
|
11
|
+
const userKey = '9ea9680f-c810-4693-a981-ca8e732a8a8f';
|
|
12
|
+
const publicationDomain = 'www.rb.no';
|
|
13
|
+
const browserId = 'fc0bce06-560b-48e4-92f2-840e67178516';
|
|
14
|
+
|
|
15
|
+
it('should return user information', async () => {
|
|
16
|
+
const response: Info = {
|
|
17
|
+
ad_segments: '1022776316,1037513379,1043845105',
|
|
18
|
+
ad_segments_user: '1022776316,1037513379,1043845105',
|
|
19
|
+
age: 28,
|
|
20
|
+
age_user: 28,
|
|
21
|
+
birth_year: '1992',
|
|
22
|
+
birth_year_user: '1992',
|
|
23
|
+
gender: 'M',
|
|
24
|
+
gender_user: 'M',
|
|
25
|
+
grunnkrets: '19030312',
|
|
26
|
+
grunnkrets_user: '19030312',
|
|
27
|
+
postcode: '0150',
|
|
28
|
+
postcode_user: '0150',
|
|
29
|
+
shared_account: false,
|
|
30
|
+
shared_account_user: false,
|
|
31
|
+
user_grouping_number: '34',
|
|
32
|
+
user_grouping_number_user: '34',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
36
|
+
|
|
37
|
+
await expect(
|
|
38
|
+
getInfo(userKey, publicationDomain, browserId)
|
|
39
|
+
).resolves.toEqual(response);
|
|
40
|
+
|
|
41
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
42
|
+
`https://services.gcloud.api.no/api/nebula/v1/info?user_key=${userKey}&domain=${publicationDomain}&browser_id=${browserId}`
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should fail on runtime validation if gender is boolean', async () => {
|
|
47
|
+
fetch.mockResponseOnce(JSON.stringify({ gender: true }));
|
|
48
|
+
log.error = jest.fn();
|
|
49
|
+
|
|
50
|
+
await expect(
|
|
51
|
+
getInfo(userKey, publicationDomain, browserId)
|
|
52
|
+
).rejects.toThrow(
|
|
53
|
+
'Expected { ad_segments?: string; ad_segments_user?: string; age?: number; age_user?: number; birth_year?: string; birth_year_user?: string; gender?: string; gender_user?: string; grunnkrets?: string; grunnkrets_user?: string; postcode?: string; postcode_user?: string; shared_account?: boolean; shared_account_user?: boolean; user_grouping_number?: string; user_grouping_number_user?: string; }, but was incompatible'
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('should pass on runtime validation if unknown property is given', async () => {
|
|
58
|
+
const response: Info = {
|
|
59
|
+
fullname: 'ra. dean baskaran',
|
|
60
|
+
age: 28,
|
|
61
|
+
shared_account: false,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
65
|
+
|
|
66
|
+
await expect(
|
|
67
|
+
getInfo(userKey, publicationDomain, browserId)
|
|
68
|
+
).resolves.toEqual(response);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Static,
|
|
3
|
+
Partial,
|
|
4
|
+
Dictionary,
|
|
5
|
+
Unknown,
|
|
6
|
+
String,
|
|
7
|
+
Number,
|
|
8
|
+
Boolean,
|
|
9
|
+
} from 'runtypes';
|
|
10
|
+
import log from '../utils/log';
|
|
11
|
+
|
|
12
|
+
const InfoSchema = Dictionary(Unknown, String).And(
|
|
13
|
+
Partial({
|
|
14
|
+
ad_segments: String,
|
|
15
|
+
ad_segments_user: String,
|
|
16
|
+
age: Number,
|
|
17
|
+
age_user: Number,
|
|
18
|
+
birth_year: String,
|
|
19
|
+
birth_year_user: String,
|
|
20
|
+
gender: String,
|
|
21
|
+
gender_user: String,
|
|
22
|
+
grunnkrets: String,
|
|
23
|
+
grunnkrets_user: String,
|
|
24
|
+
postcode: String,
|
|
25
|
+
postcode_user: String,
|
|
26
|
+
shared_account: Boolean,
|
|
27
|
+
shared_account_user: Boolean,
|
|
28
|
+
user_grouping_number: String,
|
|
29
|
+
user_grouping_number_user: String,
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
export type Info = Static<typeof InfoSchema>;
|
|
34
|
+
|
|
35
|
+
export const getInfo = (
|
|
36
|
+
userKey: string,
|
|
37
|
+
publicationDomain: string,
|
|
38
|
+
browserId: string
|
|
39
|
+
): Promise<Info> =>
|
|
40
|
+
fetch(
|
|
41
|
+
`https://services.gcloud.api.no/api/nebula/v1/info?user_key=${userKey}&domain=${publicationDomain}&browser_id=${browserId}`
|
|
42
|
+
)
|
|
43
|
+
.then((response) => response.json())
|
|
44
|
+
.then((json) => {
|
|
45
|
+
try {
|
|
46
|
+
return InfoSchema.check(json);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
log.error('Invalid schema', error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { goToLoginPage, logout } from './index';
|
|
6
6
|
import { setRootElement } from './utils/customEventWrapper';
|
|
7
7
|
import * as authentication from './actions/authentication';
|
|
8
8
|
import { listenForAuthenticationRequests } from './eventHandlers/authentication';
|
|
@@ -31,7 +31,7 @@ describe('Public endpoint - authentication', () => {
|
|
|
31
31
|
|
|
32
32
|
it('should login', () => {
|
|
33
33
|
jest.spyOn(authentication, 'login').mockImplementation();
|
|
34
|
-
|
|
34
|
+
goToLoginPage('https://www.avisnavn.no');
|
|
35
35
|
expect(authentication.login).toHaveBeenCalledWith(
|
|
36
36
|
'https://www.avisnavn.no'
|
|
37
37
|
);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { subscribeToUserAttributes } from './index';
|
|
6
6
|
import * as userData from './actions/userData';
|
|
7
7
|
import { setRootElement } from './utils/customEventWrapper';
|
|
8
8
|
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
@@ -30,7 +30,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
const onUserUpdate = jest.fn();
|
|
33
|
-
|
|
33
|
+
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
34
34
|
|
|
35
35
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
36
36
|
name: 'Donald Duck',
|
|
@@ -47,9 +47,9 @@ describe('Public endpoint - userdata', () => {
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
const onUserUpdate1 = jest.fn();
|
|
50
|
-
|
|
50
|
+
subscribeToUserAttributes(['name'], onUserUpdate1);
|
|
51
51
|
const onUserUpdate2 = jest.fn();
|
|
52
|
-
|
|
52
|
+
subscribeToUserAttributes(['name'], onUserUpdate2);
|
|
53
53
|
|
|
54
54
|
expect(onUserUpdate1).toHaveBeenCalledTimes(1);
|
|
55
55
|
expect(onUserUpdate1).toHaveBeenCalledWith({
|
|
@@ -77,10 +77,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
const firstOnUserUpdate = jest.fn();
|
|
80
|
-
|
|
80
|
+
subscribeToUserAttributes(['name'], firstOnUserUpdate);
|
|
81
81
|
|
|
82
82
|
const secondOnUserUpdate = jest.fn();
|
|
83
|
-
|
|
83
|
+
subscribeToUserAttributes(['name'], secondOnUserUpdate);
|
|
84
84
|
|
|
85
85
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(2);
|
|
86
86
|
expect(firstOnUserUpdate).nthCalledWith(1, {});
|
|
@@ -105,7 +105,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
const onUserUpdate = jest.fn();
|
|
108
|
-
|
|
108
|
+
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
109
109
|
|
|
110
110
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
111
111
|
name: 'Donald Duck',
|
|
@@ -124,7 +124,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
124
124
|
});
|
|
125
125
|
|
|
126
126
|
const onUserUpdate = jest.fn();
|
|
127
|
-
|
|
127
|
+
subscribeToUserAttributes(['name', 'email'], onUserUpdate);
|
|
128
128
|
|
|
129
129
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
130
130
|
name: 'Donald Duck',
|
|
@@ -143,7 +143,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
143
143
|
});
|
|
144
144
|
|
|
145
145
|
const onUserUpdate = jest.fn();
|
|
146
|
-
expect(() =>
|
|
146
|
+
expect(() => subscribeToUserAttributes([], onUserUpdate)).toThrowError(
|
|
147
147
|
new Error('You must subscribe to at least one attribute')
|
|
148
148
|
);
|
|
149
149
|
});
|
|
@@ -159,10 +159,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
159
159
|
});
|
|
160
160
|
|
|
161
161
|
const firstOnUserUpdate = jest.fn();
|
|
162
|
-
|
|
162
|
+
subscribeToUserAttributes(['name', 'email'], firstOnUserUpdate);
|
|
163
163
|
|
|
164
164
|
const secondOnUserUpdate = jest.fn();
|
|
165
|
-
|
|
165
|
+
subscribeToUserAttributes(['email'], secondOnUserUpdate);
|
|
166
166
|
|
|
167
167
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
168
168
|
expect(firstOnUserUpdate).nthCalledWith(1, {
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import 'regenerator-runtime';
|
|
2
2
|
import _ from 'lodash';
|
|
3
|
-
import { AuthenticationEvents } from './eventHandlers/authentication';
|
|
4
3
|
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
5
4
|
import { ActivationEvents } from './eventHandlers/activation';
|
|
6
5
|
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
@@ -11,7 +10,9 @@ import * as authentication from './actions/authentication';
|
|
|
11
10
|
* Enables you to subscribe to the user attributes you care about and
|
|
12
11
|
* get notified when something changes
|
|
13
12
|
*/
|
|
14
|
-
export const
|
|
13
|
+
export const subscribeToUserAttributes = <
|
|
14
|
+
T extends Array<keyof UserAttributes>
|
|
15
|
+
>(
|
|
15
16
|
attributes: T,
|
|
16
17
|
onUserUpdated: (attributes: Pick<UserAttributes, T[number]>) => void,
|
|
17
18
|
onStateUpdated?: (attributes: State) => void
|
|
@@ -56,7 +57,7 @@ export const subscribeToUserUpdates = <T extends Array<keyof UserAttributes>>(
|
|
|
56
57
|
* to the requestedUrl if supplied or the current url before redirecting away if not.
|
|
57
58
|
* @param requestedUrl
|
|
58
59
|
*/
|
|
59
|
-
export const
|
|
60
|
+
export const goToLoginPage = (requestedUrl?: string) =>
|
|
60
61
|
authentication.login(requestedUrl || window.location.href);
|
|
61
62
|
export const logout = () => authentication.logout();
|
|
62
63
|
export const tryActivateSubscriptionAccess = (
|
package/src/lib/rpc.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { RPCclient } from '@amedia/frontend-rpc';
|
|
2
2
|
|
|
3
|
-
let rpcSessionClient
|
|
4
|
-
|
|
3
|
+
let rpcSessionClient: {
|
|
4
|
+
addMethod: (methodName: string) => void;
|
|
5
|
+
userAuthenticated: (boolean) => void;
|
|
6
|
+
};
|
|
7
|
+
export const getRPCClient = (): Pick<
|
|
8
|
+
typeof rpcSessionClient,
|
|
9
|
+
'userAuthenticated'
|
|
10
|
+
> => {
|
|
5
11
|
if (rpcSessionClient) {
|
|
6
12
|
return rpcSessionClient;
|
|
7
13
|
}
|
|
8
14
|
rpcSessionClient = new RPCclient({
|
|
9
15
|
version: '1.0.0',
|
|
10
16
|
component: `userdata-session`,
|
|
17
|
+
methods: ['userAuthenticated'],
|
|
11
18
|
});
|
|
12
19
|
|
|
13
|
-
rpcSessionClient.addMethod('userAuthenticated');
|
|
14
20
|
return rpcSessionClient;
|
|
15
21
|
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import store from './store';
|
|
2
|
+
|
|
3
|
+
beforeEach(() => {
|
|
4
|
+
store.clear();
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
describe('State', () => {
|
|
8
|
+
it('should set in state', () => {
|
|
9
|
+
store.set('userAttributes', { access: ['newspaper'] });
|
|
10
|
+
expect(store.get('userAttributes')).toEqual({ access: ['newspaper'] });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('should emit on change', () => {
|
|
14
|
+
const listenerSpy = jest.fn();
|
|
15
|
+
store.on('userAttributes', listenerSpy);
|
|
16
|
+
expect(listenerSpy).not.toHaveBeenCalled();
|
|
17
|
+
store.set('userAttributes', { name: 'Donald' });
|
|
18
|
+
expect(listenerSpy).toHaveBeenCalledWith({ name: 'Donald' });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should emit to multiple relevant', () => {
|
|
22
|
+
const listenerSpy1 = jest.fn();
|
|
23
|
+
const listenerSpy2 = jest.fn();
|
|
24
|
+
const listenerSpy3 = jest.fn();
|
|
25
|
+
store.on('userAttributes', listenerSpy1);
|
|
26
|
+
store.on('userAttributes', listenerSpy2);
|
|
27
|
+
store.on('state', listenerSpy3);
|
|
28
|
+
|
|
29
|
+
store.set('userAttributes', { name: 'Donald' });
|
|
30
|
+
expect(listenerSpy1).toHaveBeenCalledWith({ name: 'Donald' });
|
|
31
|
+
expect(listenerSpy2).toHaveBeenCalledWith({ name: 'Donald' });
|
|
32
|
+
expect(listenerSpy3).not.toHaveBeenCalled();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { State, UserAttributes } from '../types';
|
|
2
|
+
|
|
3
|
+
type StoreDataMap = {
|
|
4
|
+
userAttributes: UserAttributes;
|
|
5
|
+
state: State;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type Listener<T> = (data: T) => void;
|
|
9
|
+
type Listeners<T = StoreDataMap> = {
|
|
10
|
+
[key in keyof T]?: Array<Listener<T[key]>>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const store: { [index in keyof StoreDataMap]?: StoreDataMap[index] } = {};
|
|
14
|
+
const listeners: Listeners = {};
|
|
15
|
+
|
|
16
|
+
const emit = (key: keyof StoreDataMap): void => {
|
|
17
|
+
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const on = (
|
|
21
|
+
key: keyof StoreDataMap,
|
|
22
|
+
listener: Listener<StoreDataMap[typeof key]>
|
|
23
|
+
) => {
|
|
24
|
+
if (!listeners[key]) {
|
|
25
|
+
listeners[key] = [];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// @ts-ignore todo How can this possibly be undefined?
|
|
29
|
+
listeners[key].push(listener);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
|
|
33
|
+
// @ts-ignore todo Not sure why this type fails.
|
|
34
|
+
store[key] = { ...store[key], ...value };
|
|
35
|
+
emit(key);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const get = (key: keyof StoreDataMap): StoreDataMap[typeof key] | undefined =>
|
|
39
|
+
store[key];
|
|
40
|
+
|
|
41
|
+
const clear = () => {
|
|
42
|
+
store.state = undefined;
|
|
43
|
+
store.userAttributes = undefined;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export default { set, get, on, clear };
|
package/src/types.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type UserAttributes = {
|
|
|
5
5
|
trackingKey?: string;
|
|
6
6
|
extraData?: [];
|
|
7
7
|
access?: string[];
|
|
8
|
+
privacyPreferences?: PrivacyPreferences;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
11
|
export type State = {
|
|
@@ -15,3 +16,10 @@ export type State = {
|
|
|
15
16
|
export type AnonymousAttributes = {
|
|
16
17
|
age: number;
|
|
17
18
|
};
|
|
19
|
+
|
|
20
|
+
export type PrivacyPreferences = {
|
|
21
|
+
allowResearchUsage: Boolean;
|
|
22
|
+
connectWithSurveys: Boolean;
|
|
23
|
+
personalizedAds: Boolean;
|
|
24
|
+
personalizedContent: Boolean;
|
|
25
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Runtype, Static } from 'runtypes';
|
|
2
|
+
import queue from './queue';
|
|
3
|
+
import cache from '../cache';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_TTL_10_SECONDS = 10000;
|
|
6
|
+
const cacheTTLMap: { [index: string]: number } = {};
|
|
7
|
+
|
|
8
|
+
const getOrUpdate = <T extends Runtype>(
|
|
9
|
+
key: string,
|
|
10
|
+
schema: T,
|
|
11
|
+
action: () => Promise<Static<T>>
|
|
12
|
+
): Promise<Static<T>> =>
|
|
13
|
+
new Promise((resolve, reject) => {
|
|
14
|
+
queue.add(key, { resolve, reject });
|
|
15
|
+
if (queue.peekQueueSize(key) > 1) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
cache.get(key, schema).then((cacheResponse) => {
|
|
20
|
+
if (cacheResponse) {
|
|
21
|
+
queue.getAll(key).forEach((promise) => {
|
|
22
|
+
promise.resolve(cacheResponse);
|
|
23
|
+
});
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
action()
|
|
27
|
+
.then((res) => {
|
|
28
|
+
cache.set(key, res, cacheTTLMap[key] || DEFAULT_TTL_10_SECONDS);
|
|
29
|
+
queue.getAll(key).forEach((promise) => {
|
|
30
|
+
promise.resolve(res);
|
|
31
|
+
});
|
|
32
|
+
})
|
|
33
|
+
.catch(() => {
|
|
34
|
+
queue.getAll(key).forEach((promise) => {
|
|
35
|
+
promise.reject();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export default getOrUpdate;
|
package/src/utils/log.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import mercury from '../
|
|
1
|
+
import mercury from '../clients/mercury';
|
|
2
2
|
|
|
3
3
|
export type Logger = {
|
|
4
4
|
debug: (message: string | any, ...extra: any[]) => void;
|
|
@@ -13,6 +13,7 @@ export const setLogger = (engine: Logger) => {
|
|
|
13
13
|
logger = engine;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
16
17
|
const getLogger = () => logger || mercury;
|
|
17
18
|
|
|
18
19
|
/* export default {
|