@amedia/user 0.5.1 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/preferences.d.ts +3 -0
- package/dist/actions/preferences.spec.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -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/package.json +2 -1
- package/src/actions/preferences.spec.ts +55 -0
- package/src/actions/preferences.ts +67 -0
- package/src/index.ts +1 -0
- package/src/services/europa.spec.ts +94 -0
- package/src/services/europa.ts +126 -0
- package/src/services/jupiter.spec.ts +117 -113
- package/src/services/jupiter.ts +0 -1
- package/src/services/mercury.spec.ts +41 -1
- package/src/services/mercury.ts +1 -0
- package/src/services/nebula.spec.ts +71 -0
- package/src/services/nebula.ts +51 -0
- package/src/utils/queue.ts +0 -2
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Record, Partial } from 'runtypes';
|
|
2
|
+
export declare const EuropaSchema: Record<{
|
|
3
|
+
namespaces: Partial<{}, false>;
|
|
4
|
+
}, false>;
|
|
5
|
+
export declare const clearLocalCache: () => Promise<void>;
|
|
6
|
+
export declare const updateNamespace: (namespace: string, data: Object) => Promise<void | {}>;
|
|
7
|
+
export declare const getNamespace: (namespace: string, options?: {
|
|
8
|
+
ignoreCache: boolean;
|
|
9
|
+
}) => Promise<any>;
|
|
10
|
+
export declare const saveNamespace: (namespace: string, data: Object) => Promise<any>;
|
|
11
|
+
export declare const getAllNamespaces: () => Promise<{}>;
|
|
12
|
+
export declare const deleteNamespaceKey: (namespace: string, key: string) => Promise<any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Static, Partial, Dictionary, Unknown, String, Number, Boolean } from 'runtypes';
|
|
2
|
+
declare const InfoSchema: import("runtypes").Intersect<[Dictionary<Unknown, string>, Partial<{
|
|
3
|
+
ad_segments: String;
|
|
4
|
+
ad_segments_user: String;
|
|
5
|
+
age: Number;
|
|
6
|
+
age_user: Number;
|
|
7
|
+
birth_year: String;
|
|
8
|
+
birth_year_user: String;
|
|
9
|
+
gender: String;
|
|
10
|
+
gender_user: String;
|
|
11
|
+
grunnkrets: String;
|
|
12
|
+
grunnkrets_user: String;
|
|
13
|
+
postcode: String;
|
|
14
|
+
postcode_user: String;
|
|
15
|
+
shared_account: Boolean;
|
|
16
|
+
shared_account_user: Boolean;
|
|
17
|
+
user_grouping_number: String;
|
|
18
|
+
user_grouping_number_user: String;
|
|
19
|
+
}, false>]>;
|
|
20
|
+
export declare type Info = Static<typeof InfoSchema>;
|
|
21
|
+
export declare const getInfo: (userKey: string, publicationDomain: string, browserId: string) => Promise<Info>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Client lib for working with aID user and associated data",
|
|
5
5
|
"repository": "https://github.com/amedia/amedia-user-js",
|
|
6
6
|
"author": "Amedia Utvikling (ID og data)",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"@amedia/frontend-rpc": "0.0.12",
|
|
15
15
|
"dexie": "3.0.3",
|
|
16
16
|
"lodash": "4.17.21",
|
|
17
|
+
"regenerator-runtime": "0.13.7",
|
|
17
18
|
"runtypes": "6.2.1"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import queue from '../utils/queue';
|
|
4
|
+
import privacyPreferences from './preferences';
|
|
5
|
+
|
|
6
|
+
fetch.enableMocks();
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
fetch.resetMocks();
|
|
10
|
+
cache.clear();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const europaResponse = {
|
|
14
|
+
namespaces: {
|
|
15
|
+
privacy_preferences: {
|
|
16
|
+
allow_research_usage: 'true',
|
|
17
|
+
connect_with_surveys: true,
|
|
18
|
+
personalzied_ads: 'false',
|
|
19
|
+
personalized_content: false,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe('privacyPreferences', () => {
|
|
25
|
+
it('should return preferences', async () => {
|
|
26
|
+
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
27
|
+
await privacyPreferences();
|
|
28
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('simultanious call should return same preferences', async () => {
|
|
32
|
+
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
33
|
+
|
|
34
|
+
jest.spyOn(queue, 'add');
|
|
35
|
+
privacyPreferences();
|
|
36
|
+
await privacyPreferences();
|
|
37
|
+
expect(queue.add).toHaveBeenCalledTimes(2);
|
|
38
|
+
expect(fetch).toHaveBeenCalledTimes(1);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should use cache if available', async () => {
|
|
42
|
+
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
43
|
+
|
|
44
|
+
jest.spyOn(cache, 'get').mockReturnValue(
|
|
45
|
+
Promise.resolve({
|
|
46
|
+
allowResearchUsage: true,
|
|
47
|
+
connectWithSurveys: true,
|
|
48
|
+
personalziedAds: false,
|
|
49
|
+
personalizedContent: false,
|
|
50
|
+
})
|
|
51
|
+
);
|
|
52
|
+
await Promise.all([privacyPreferences(), privacyPreferences()]);
|
|
53
|
+
expect(fetch).toHaveBeenCalledTimes(0);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Record, Boolean, Static } from 'runtypes';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import { getNamespace } from '../services/europa';
|
|
4
|
+
import log from '../utils/log';
|
|
5
|
+
import queue from '../utils/queue';
|
|
6
|
+
|
|
7
|
+
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
8
|
+
const PRIVACY_PREFERENCES_QUEUE = 'privacy_preferences_queue';
|
|
9
|
+
|
|
10
|
+
const PrivacyPreferencesSchema = Record({
|
|
11
|
+
allowResearchUsage: Boolean,
|
|
12
|
+
connectWithSurveys: Boolean,
|
|
13
|
+
personalizedAds: Boolean,
|
|
14
|
+
personalizedContent: Boolean,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
type PrivacyPreferences = Static<typeof PrivacyPreferencesSchema>;
|
|
18
|
+
|
|
19
|
+
const toBoolean = (val: string | boolean) => val === 'true' || val === true;
|
|
20
|
+
|
|
21
|
+
let isCallingPrivacyPreferences: boolean;
|
|
22
|
+
export const setIsCallingPrivacyPreferences = (value: boolean) =>
|
|
23
|
+
(isCallingPrivacyPreferences = value);
|
|
24
|
+
|
|
25
|
+
const privacyPreferences = () => {
|
|
26
|
+
return new Promise(async (resolve, reject) => {
|
|
27
|
+
queue.add(PRIVACY_PREFERENCES_QUEUE, { resolve, reject });
|
|
28
|
+
if (queue.peekQueueSize(PRIVACY_PREFERENCES_QUEUE) > 1) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const cachedData = await cache.get(
|
|
33
|
+
'privacyPreferences',
|
|
34
|
+
PrivacyPreferencesSchema
|
|
35
|
+
);
|
|
36
|
+
if (cachedData) {
|
|
37
|
+
queue.getAll(PRIVACY_PREFERENCES_QUEUE).forEach((promise) => {
|
|
38
|
+
promise.resolve(cachedData);
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let europaData: any;
|
|
44
|
+
try {
|
|
45
|
+
europaData = await getNamespace('privacy_preferences');
|
|
46
|
+
} catch (error) {
|
|
47
|
+
log.error(`Could not fetch europaData`, error);
|
|
48
|
+
queue.getAll(PRIVACY_PREFERENCES_QUEUE).forEach((promise) => {
|
|
49
|
+
promise.reject();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const mappedPrivacy: PrivacyPreferences = {
|
|
54
|
+
allowResearchUsage: toBoolean(europaData.allow_research_usage),
|
|
55
|
+
connectWithSurveys: toBoolean(europaData.connect_with_surveys),
|
|
56
|
+
personalizedAds: toBoolean(europaData.personalized_ads),
|
|
57
|
+
personalizedContent: toBoolean(europaData.personalized_content),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
cache.set('privacyPreferences', mappedPrivacy, Date.now() + CACHE_DURATION);
|
|
61
|
+
queue.getAll(PRIVACY_PREFERENCES_QUEUE).forEach((promise) => {
|
|
62
|
+
promise.resolve(mappedPrivacy);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export default privacyPreferences;
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import fetch from 'jest-fetch-mock';
|
|
2
|
+
import cache from '../cache';
|
|
3
|
+
import {
|
|
4
|
+
updateNamespace,
|
|
5
|
+
EuropaSchema,
|
|
6
|
+
clearLocalCache,
|
|
7
|
+
saveNamespace,
|
|
8
|
+
deleteNamespaceKey,
|
|
9
|
+
getNamespace,
|
|
10
|
+
getAllNamespaces,
|
|
11
|
+
} from './europa';
|
|
12
|
+
|
|
13
|
+
fetch.enableMocks();
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
fetch.resetMocks();
|
|
17
|
+
cache.clear();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('Europa', () => {
|
|
21
|
+
it('saveNamespace should set namespace and return the new value', async () => {
|
|
22
|
+
const userInfo = { gender: 'U', post_code: 'U' };
|
|
23
|
+
fetch.mockOnce(JSON.stringify(userInfo)); // data to save 'PUT'
|
|
24
|
+
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: userInfo } })); // data retrieved 'GET'
|
|
25
|
+
|
|
26
|
+
const namespaceData = await saveNamespace('user_info', userInfo);
|
|
27
|
+
expect(namespaceData).toMatchObject(userInfo);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('updateNamespace should update namespace', async () => {
|
|
31
|
+
const oldUserInfo = { gender: 'F', post_code: 'U' };
|
|
32
|
+
const newUserInfo = { gender: 'M', post_code: '2150' };
|
|
33
|
+
|
|
34
|
+
fetch.mockOnce(JSON.stringify({})); // initial response // 'PUT'
|
|
35
|
+
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: oldUserInfo } })); // old 'GET'
|
|
36
|
+
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: newUserInfo } })); // updated 'GET'
|
|
37
|
+
|
|
38
|
+
const old = await saveNamespace('user_info', oldUserInfo);
|
|
39
|
+
expect(fetch).toHaveBeenCalledTimes(2);
|
|
40
|
+
expect(old).toMatchObject(oldUserInfo);
|
|
41
|
+
|
|
42
|
+
const updated = await updateNamespace('user_info', newUserInfo).then(
|
|
43
|
+
async () => await cache.get('europa', EuropaSchema)
|
|
44
|
+
);
|
|
45
|
+
expect(updated).toMatchObject({ namespaces: { user_info: newUserInfo } });
|
|
46
|
+
expect(fetch).toHaveBeenCalledTimes(3);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('clearLocalCache should remove Europa cache', async () => {
|
|
50
|
+
fetch.mockResponse(JSON.stringify({ namespaces: { euucp: { x: 'x' } } }));
|
|
51
|
+
|
|
52
|
+
await saveNamespace('euucp', { x: 'x' });
|
|
53
|
+
const x = await cache.get('europa', EuropaSchema);
|
|
54
|
+
expect(x).toMatchObject({ namespaces: { euucp: { x: 'x' } } }); // expect something in cache
|
|
55
|
+
|
|
56
|
+
await clearLocalCache();
|
|
57
|
+
expect(await cache.get('europa', EuropaSchema)).toBeUndefined; // expect cache to be empty
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('deleteNamespaceKey should remove key from europa', async () => {
|
|
61
|
+
fetch.mockOnce(JSON.stringify({})); // 'GET'
|
|
62
|
+
fetch.mockOnce(
|
|
63
|
+
JSON.stringify({ namespaces: { euucp: { x: 'x', y: 'y' } } })
|
|
64
|
+
); // 'PUT'
|
|
65
|
+
fetch.mockOnce(JSON.stringify({}), { status: 200 }); // 'DELETE'
|
|
66
|
+
fetch.mockOnce(JSON.stringify({ namespaces: { euucp: { y: 'y' } } })); // 'PUT'
|
|
67
|
+
|
|
68
|
+
await saveNamespace('euucp', { x: 'x' });
|
|
69
|
+
const beforeDelete = await cache.get('europa', EuropaSchema);
|
|
70
|
+
expect(beforeDelete).toMatchObject({
|
|
71
|
+
namespaces: { euucp: { x: 'x', y: 'y' } },
|
|
72
|
+
}); // expect x and y
|
|
73
|
+
|
|
74
|
+
await deleteNamespaceKey('euucp', 'x');
|
|
75
|
+
const afterDelete = await cache.get('europa', EuropaSchema);
|
|
76
|
+
expect(afterDelete).toMatchObject({ namespaces: { euucp: { y: 'y' } } }); // expect only y
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('getAllNamespaces should return all europa namespaces', async () => {
|
|
80
|
+
const namespaceData = {
|
|
81
|
+
euucp: { x: 'x' },
|
|
82
|
+
user_info: { gender: 'M', post_code: 'U' },
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
fetch.mockResponse(JSON.stringify({ namespaces: namespaceData }));
|
|
86
|
+
const retrieved = await getAllNamespaces();
|
|
87
|
+
expect(retrieved).toMatchObject(namespaceData);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('getNamespace should return europa namespace', async () => {
|
|
91
|
+
fetch.mockResponse(JSON.stringify({ namespaces: { euucp: { x: 'x' } } }));
|
|
92
|
+
expect(await getNamespace('euucp')).toMatchObject({ x: 'x' });
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { Record, Partial } from 'runtypes';
|
|
2
|
+
|
|
3
|
+
import cache from '../cache';
|
|
4
|
+
import log from '../utils/log';
|
|
5
|
+
|
|
6
|
+
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
7
|
+
|
|
8
|
+
export const EuropaSchema = Record({
|
|
9
|
+
namespaces: Partial({}),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const clearLocalCache = () => cache.remove('europa');
|
|
13
|
+
|
|
14
|
+
const doFetch = (url: string, method?: string, body?: Object) =>
|
|
15
|
+
fetch(url, {
|
|
16
|
+
method: method && method,
|
|
17
|
+
body: body && JSON.stringify(body),
|
|
18
|
+
headers: {
|
|
19
|
+
'Content-Type': 'application/json',
|
|
20
|
+
Accept: 'application/json',
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const updateNamespaceInCache = async (namespace: string, data: Object) => {
|
|
25
|
+
const cachedData = await cache.get('europa', EuropaSchema);
|
|
26
|
+
const updatedData = cachedData
|
|
27
|
+
? {
|
|
28
|
+
...cachedData,
|
|
29
|
+
namespaces: { ...cachedData.namespaces, [namespace]: data },
|
|
30
|
+
}
|
|
31
|
+
: { namespaces: { [namespace]: data } };
|
|
32
|
+
|
|
33
|
+
return cache.set('europa', updatedData, Date.now() + CACHE_DURATION);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const updateNamespace = async (namespace: string, data: Object) => {
|
|
37
|
+
const res = await doFetch(
|
|
38
|
+
`//www.aid.no/api/europa/v1/me/${namespace}`,
|
|
39
|
+
'PATCH',
|
|
40
|
+
data
|
|
41
|
+
);
|
|
42
|
+
if (!res.ok) {
|
|
43
|
+
log.error(
|
|
44
|
+
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
45
|
+
res
|
|
46
|
+
);
|
|
47
|
+
return {};
|
|
48
|
+
}
|
|
49
|
+
return await updateNamespaceInCache(namespace, data);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const getNamespace = async (
|
|
53
|
+
namespace: string,
|
|
54
|
+
options: { ignoreCache: boolean } = { ignoreCache: false }
|
|
55
|
+
) => {
|
|
56
|
+
if (!options.ignoreCache) {
|
|
57
|
+
const cacheData = await cache.get('europa', EuropaSchema);
|
|
58
|
+
if (cacheData && cacheData.namespaces[namespace]) {
|
|
59
|
+
return cacheData.namespaces[namespace];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return doFetch(`//www.aid.no/api/europa/v1/me/${namespace}`, 'GET')
|
|
64
|
+
.then((res) => {
|
|
65
|
+
if (!res.ok) {
|
|
66
|
+
log.error(
|
|
67
|
+
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
68
|
+
res
|
|
69
|
+
);
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
return res.json();
|
|
73
|
+
})
|
|
74
|
+
.then((data) => {
|
|
75
|
+
const validData = EuropaSchema.check(data);
|
|
76
|
+
updateNamespaceInCache(namespace, validData.namespaces[namespace]);
|
|
77
|
+
return validData.namespaces[namespace] || {};
|
|
78
|
+
})
|
|
79
|
+
.catch((error) => {
|
|
80
|
+
log.error(`Could not get namespace: ${namespace}. Error:${error}`);
|
|
81
|
+
return {};
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const saveNamespace = (namespace: string, data: Object) =>
|
|
86
|
+
doFetch(`//www.aid.no/api/europa/v1/me/${namespace}`, 'PUT', data)
|
|
87
|
+
.then((res) => {
|
|
88
|
+
if (!res.ok) {
|
|
89
|
+
log.error(
|
|
90
|
+
`Could not get save namespace, namespace:${namespace} status: ${res.status}, statusText: ${res.statusText}`,
|
|
91
|
+
res
|
|
92
|
+
);
|
|
93
|
+
return {};
|
|
94
|
+
}
|
|
95
|
+
})
|
|
96
|
+
.then(() => getNamespace(namespace, { ignoreCache: true })); // updates cache
|
|
97
|
+
|
|
98
|
+
export const getAllNamespaces = () =>
|
|
99
|
+
doFetch(`//www.aid.no/api/europa/v1/me`)
|
|
100
|
+
.then((res) => {
|
|
101
|
+
if (!res.ok) {
|
|
102
|
+
log.error(
|
|
103
|
+
`Could not get all namespaces, status: ${res.status}, statusText: ${res.statusText}`,
|
|
104
|
+
res
|
|
105
|
+
);
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
return res.json();
|
|
109
|
+
})
|
|
110
|
+
.then((data) => {
|
|
111
|
+
const europaValidData = EuropaSchema.check(data);
|
|
112
|
+
cache.set('europa', europaValidData, Date.now() + CACHE_DURATION);
|
|
113
|
+
return europaValidData.namespaces;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
export const deleteNamespaceKey = (namespace: string, key: string) =>
|
|
117
|
+
doFetch(`//www.aid.no/api/europa/v1/me/${namespace}/${key}`, 'DELETE')
|
|
118
|
+
.then((res) => {
|
|
119
|
+
if (!res.ok) {
|
|
120
|
+
log.error(
|
|
121
|
+
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
122
|
+
res
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
})
|
|
126
|
+
.then(() => getNamespace(namespace, { ignoreCache: true })); // updates cache
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable no-console */
|
|
2
1
|
import fetch from 'jest-fetch-mock';
|
|
3
2
|
import log from '../utils/log';
|
|
4
3
|
import {
|
|
@@ -15,136 +14,141 @@ beforeEach(() => {
|
|
|
15
14
|
fetch.resetMocks();
|
|
16
15
|
});
|
|
17
16
|
|
|
18
|
-
describe('
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
+
});
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
await expect(
|
|
52
|
+
checkForAccess(publicationDomain, accessFeatures)
|
|
53
|
+
).rejects.toThrow('Request not ok (400: Bad Request).');
|
|
48
54
|
});
|
|
49
55
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
});
|
|
53
66
|
});
|
|
54
67
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
log.error = jest.fn();
|
|
68
|
+
describe('POST /get_vstoken', () => {
|
|
69
|
+
const publicationDomain = 'www.rb.no';
|
|
58
70
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
});
|
|
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
|
+
};
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
it('should return vstoken', async () => {
|
|
71
|
-
const response: GetVstoken = {
|
|
72
|
-
status: 'success',
|
|
73
|
-
cookie: 'ofofb:16187:amedia_staff,newspaper,plussalt:cc2c2c:rb:jupiter',
|
|
74
|
-
access_features: ['newspaper', 'plussalt'],
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
fetch.mockResponseOnce(JSON.stringify(response));
|
|
78
|
-
|
|
79
|
-
await expect(getVstoken(publicationDomain)).resolves.toEqual(response);
|
|
80
|
-
expect(fetch).toHaveBeenCalledWith(
|
|
81
|
-
`/api/jupiter/v1/get_vstoken/${publicationDomain}`,
|
|
82
|
-
{
|
|
83
|
-
method: 'post',
|
|
84
|
-
}
|
|
85
|
-
);
|
|
86
|
-
});
|
|
78
|
+
fetch.mockResponseOnce(JSON.stringify(response));
|
|
79
|
+
|
|
80
|
+
await expect(getVstoken(publicationDomain)).resolves.toEqual(response);
|
|
87
81
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
82
|
+
expect(fetch).toHaveBeenCalledWith(
|
|
83
|
+
`/api/jupiter/v1/get_vstoken/${publicationDomain}`,
|
|
84
|
+
{
|
|
85
|
+
method: 'post',
|
|
86
|
+
}
|
|
87
|
+
);
|
|
91
88
|
});
|
|
92
89
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
90
|
+
it('should deny access for user not logged in', async () => {
|
|
91
|
+
fetch.mockResponseOnce(JSON.stringify({ status: 'no_session_found' }), {
|
|
92
|
+
status: 400,
|
|
93
|
+
});
|
|
97
94
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
await expect(getVstoken(publicationDomain)).rejects.toThrow(
|
|
96
|
+
'Request not ok (400: Bad Request).'
|
|
97
|
+
);
|
|
98
|
+
});
|
|
101
99
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
});
|
|
106
|
-
});
|
|
100
|
+
it('should fail on runtime validation', async () => {
|
|
101
|
+
fetch.mockResponseOnce(JSON.stringify({ access_features: 'foo' }));
|
|
102
|
+
log.error = jest.fn();
|
|
107
103
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const response: GetNonUserAccess = {
|
|
113
|
-
status: 'success',
|
|
114
|
-
vstoken_cookie: 'd82ab3842a:2619693230:::rb:jupiter',
|
|
115
|
-
uuid: '',
|
|
116
|
-
access_features: [],
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
fetch.mockResponseOnce(JSON.stringify(response));
|
|
120
|
-
|
|
121
|
-
await expect(getNonUserAccess(publicationDomain)).resolves.toEqual(
|
|
122
|
-
response
|
|
123
|
-
);
|
|
124
|
-
expect(
|
|
125
|
-
fetch
|
|
126
|
-
).toHaveBeenCalledWith(
|
|
127
|
-
`/api/jupiter/v1/non_user_access/${publicationDomain}`,
|
|
128
|
-
{ method: 'post' }
|
|
129
|
-
);
|
|
104
|
+
await expect(getVstoken(publicationDomain)).rejects.toThrow(
|
|
105
|
+
'Expected { status: "success"; cookie: string; access_features: string[]; }, but was incompatible'
|
|
106
|
+
);
|
|
107
|
+
});
|
|
130
108
|
});
|
|
131
109
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
+
);
|
|
135
133
|
});
|
|
136
134
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
+
});
|
|
141
144
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
+
it('should fail on runtime validation', async () => {
|
|
146
|
+
fetch.mockResponseOnce(JSON.stringify({ vstoken_cookie: 3 }));
|
|
147
|
+
log.error = jest.fn();
|
|
145
148
|
|
|
146
|
-
|
|
147
|
-
|
|
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
|
+
});
|
|
149
153
|
});
|
|
150
154
|
});
|
package/src/services/jupiter.ts
CHANGED