@amedia/user 0.5.2 → 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.
@@ -3,7 +3,10 @@
3
3
  */
4
4
 
5
5
  import fetch from 'jest-fetch-mock';
6
- import backendLog from './mercury';
6
+ import backendLog, {
7
+ activateSubscription,
8
+ UserDeactivatedError,
9
+ } from './mercury';
7
10
 
8
11
  fetch.enableMocks();
9
12
 
@@ -28,3 +31,40 @@ describe('Mercury backendlog', () => {
28
31
  expect(formData?.get('referrer')).toEqual('https://www.rb.no/');
29
32
  });
30
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
+ });
@@ -66,6 +66,7 @@ export const activateSubscription = async (
66
66
  }
67
67
  return false;
68
68
  }
69
+ console.log(data);
69
70
  return data?.status === 'success' && data?.access === 'granted';
70
71
  });
71
72
 
@@ -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
+ });
@@ -1,5 +1,3 @@
1
- import { Function } from 'runtypes';
2
-
3
1
  const queue: {
4
2
  [key: string]: {
5
3
  resolve: (PromiseLike) => void;