Skip to content

Commit 772c1fd

Browse files
authored
Merge pull request #1418 from guardian/ph-20250823-0100-prelude
[sign-in gate] split test file
2 parents 0f9adb1 + 3cfb33c commit 772c1fd

12 files changed

+340
-345
lines changed

src/server/signin-gate/libPure.ts

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export const isValidSection = (sectionId: string): boolean => {
204204
return !invalidSections.includes(sectionId);
205205
};
206206

207-
export const isValidTagIdCollection = (tagIds: string[]): boolean => {
207+
export const isValidTagIds = (tagIds: string[]): boolean => {
208208
const invalidTagIds = ['info/newsletter-sign-up'];
209209
// Check that no tagId is in the invalidTagIds list.
210210
return !tagIds.some((tagId: string): boolean => invalidTagIds.includes(tagId));
@@ -355,7 +355,7 @@ export const decideGuGateTypeNonConsentedIreland = (
355355
};
356356

357357
export const getTreatmentsRequestPayloadToGateType = (
358-
getTreatmentsRequestPayload: GetTreatmentsRequestPayload,
358+
payload: GetTreatmentsRequestPayload,
359359
): GateType => {
360360
// This function is a pure function (without any side effects) which gets the body
361361
// of a '/auxia/get-treatments' request and returns the correct GateType.
@@ -378,18 +378,15 @@ export const getTreatmentsRequestPayloadToGateType = (
378378
// body.shouldServeDismissible take priority over the fact that body.showDefaultGate
379379
// could possibly have value 'mandatory'
380380

381-
if (
382-
getTreatmentsRequestPayload.showDefaultGate !== undefined &&
383-
getTreatmentsRequestPayload.shouldServeDismissible
384-
) {
381+
if (payload.showDefaultGate !== undefined && payload.shouldServeDismissible) {
385382
return 'GuDismissible';
386383
}
387384

388385
// --------------------------------------------------------------
389386
// The attribute showDefaultGate overrides any other behavior
390387

391-
if (getTreatmentsRequestPayload.showDefaultGate) {
392-
if (getTreatmentsRequestPayload.showDefaultGate == 'mandatory') {
388+
if (payload.showDefaultGate) {
389+
if (payload.showDefaultGate == 'mandatory') {
393390
return 'GuMandatory';
394391
} else {
395392
return 'GuDismissible';
@@ -403,10 +400,10 @@ export const getTreatmentsRequestPayloadToGateType = (
403400
// might be decommissioned in the future.
404401

405402
if (
406-
!isValidContentType(getTreatmentsRequestPayload.contentType) ||
407-
!isValidSection(getTreatmentsRequestPayload.sectionId) ||
408-
!isValidTagIdCollection(getTreatmentsRequestPayload.tagIds) ||
409-
!articleIdentifierIsAllowed(getTreatmentsRequestPayload.articleIdentifier)
403+
!isValidContentType(payload.contentType) ||
404+
!isValidSection(payload.sectionId) ||
405+
!isValidTagIds(payload.tagIds) ||
406+
!articleIdentifierIsAllowed(payload.articleIdentifier)
410407
) {
411408
return 'None';
412409
}
@@ -417,13 +414,13 @@ export const getTreatmentsRequestPayloadToGateType = (
417414
// traffic (consented or not consented) to Auxia. (For privacy vigilantes reading this,
418415
// Auxia is not going to process non consented traffic for targetting.)
419416

420-
if (getTreatmentsRequestPayload.countryCode === 'IE') {
421-
if (mvtIdIsAuxiaAudienceShare(getTreatmentsRequestPayload.mvtId)) {
417+
if (payload.countryCode === 'IE') {
418+
if (mvtIdIsAuxiaAudienceShare(payload.mvtId)) {
422419
return 'AuxiaAPI';
423420
} else {
424421
return decideGuGateTypeNonConsentedIreland(
425-
getTreatmentsRequestPayload.dailyArticleCount,
426-
getTreatmentsRequestPayload.gateDisplayCount,
422+
payload.dailyArticleCount,
423+
payload.gateDisplayCount,
427424
);
428425
}
429426
}
@@ -440,9 +437,9 @@ export const getTreatmentsRequestPayloadToGateType = (
440437
// That split used to be done client side, but it's now been moved to SDC and is driven by
441438
// `mvtIdIsAuxiaAudienceShare`.
442439

443-
if (!mvtIdIsAuxiaAudienceShare(getTreatmentsRequestPayload.mvtId)) {
444-
if (getTreatmentsRequestPayload.should_show_legacy_gate_tmp) {
445-
return decideGateTypeNoneOrDismissible(getTreatmentsRequestPayload.gateDismissCount);
440+
if (!mvtIdIsAuxiaAudienceShare(payload.mvtId)) {
441+
if (payload.should_show_legacy_gate_tmp) {
442+
return decideGateTypeNoneOrDismissible(payload.gateDismissCount);
446443
} else {
447444
return 'None';
448445
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { articleIdentifierIsAllowed } from '../libPure';
2+
3+
it('articleIdentifierIsAllowed', () => {
4+
expect(
5+
articleIdentifierIsAllowed(
6+
'www.theguardian.com/money/2017/mar/10/ministers-to-criminalise-use-of-ticket-tout-harvesting-software',
7+
),
8+
).toBe(true);
9+
expect(articleIdentifierIsAllowed('www.theguardian.com/tips')).toBe(false);
10+
expect(articleIdentifierIsAllowed('www.theguardian.com/tips#test')).toBe(false);
11+
expect(articleIdentifierIsAllowed('www.theguardian.com/tips/test')).toBe(false);
12+
});
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { buildGetTreatmentsRequestPayload } from '../libPure';
2+
3+
describe('buildGetTreatmentsRequestPayload', () => {
4+
it('should return return the right payload', () => {
5+
const projectId = 'projectId';
6+
const browserId = 'browserId';
7+
const isSupporter = true;
8+
const dailyArticleCount = 21;
9+
const articleIdentifier = 'articleIdentifier';
10+
const editionId = 'UK';
11+
const countryCode = 'GB';
12+
const hasConsented = true;
13+
const shouldServeDismissible = false;
14+
15+
const expectedAnswer = {
16+
projectId,
17+
userId: browserId,
18+
contextualAttributes: [
19+
{
20+
key: 'is_supporter',
21+
boolValue: isSupporter,
22+
},
23+
{
24+
key: 'daily_article_count',
25+
integerValue: dailyArticleCount,
26+
},
27+
{
28+
key: 'article_identifier',
29+
stringValue: articleIdentifier,
30+
},
31+
{
32+
key: 'edition',
33+
stringValue: editionId,
34+
},
35+
{
36+
key: 'country_key',
37+
stringValue: countryCode,
38+
},
39+
{
40+
key: 'has_consented',
41+
boolValue: hasConsented,
42+
},
43+
{
44+
key: 'should_not_serve_mandatory',
45+
boolValue: shouldServeDismissible,
46+
},
47+
],
48+
surfaces: [
49+
{
50+
surface: 'ARTICLE_PAGE',
51+
maximumTreatmentCount: 1,
52+
},
53+
],
54+
languageCode: 'en-GB',
55+
};
56+
57+
const returnedAnswer = buildGetTreatmentsRequestPayload(
58+
projectId,
59+
browserId,
60+
isSupporter,
61+
dailyArticleCount,
62+
articleIdentifier,
63+
editionId,
64+
countryCode,
65+
hasConsented,
66+
shouldServeDismissible,
67+
);
68+
expect(returnedAnswer).toStrictEqual(expectedAnswer);
69+
});
70+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import {
2+
buildGuUserTreatmentsEnvelop,
3+
guDismissibleUserTreatment,
4+
guMandatoryUserTreatment,
5+
} from '../libPure';
6+
7+
describe('buildGuUserTreatmentsEnvelop', () => {
8+
const dismissibleGuGateTreatment = guDismissibleUserTreatment();
9+
const mandatoryGuGateTreatment = guMandatoryUserTreatment();
10+
11+
it('should not return gate data if the number of gate dismissal is more than 5 (low gate display count)', () => {
12+
const gateDismissCount = 6;
13+
const gateDisplayCount = 0; // dismissible gate
14+
const expectAnswer = {
15+
responseId: '',
16+
userTreatments: [],
17+
};
18+
expect(
19+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
20+
).toStrictEqual(expectAnswer);
21+
expect(
22+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'FR'),
23+
).toStrictEqual(expectAnswer);
24+
});
25+
26+
it('should not return gate data if the number of gate dismissal is more than 5 (high gate display count)', () => {
27+
const gateDismissCount = 6;
28+
const gateDisplayCount = 10; // mandatory gate
29+
const expectAnswer = {
30+
responseId: '',
31+
userTreatments: [],
32+
};
33+
expect(
34+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
35+
).toStrictEqual(expectAnswer);
36+
expect(
37+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'FR'),
38+
).toStrictEqual(expectAnswer);
39+
});
40+
41+
it('[in Ireland] should return a dismissible gate if gateDisplayCount is in {0, 1, 2}', () => {
42+
const gateDismissCount = 2; // low number allowing for a gate
43+
const gateDisplayCount = 0; // dismissible gate
44+
const expectAnswer = {
45+
responseId: '',
46+
userTreatments: [dismissibleGuGateTreatment],
47+
};
48+
expect(
49+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
50+
).toStrictEqual(expectAnswer);
51+
});
52+
53+
it('[in Ireland] should return a dismissible gate if gateDisplayCount is in {0, 1, 2}', () => {
54+
const gateDismissCount = 2; // low number allowing for a gate
55+
const gateDisplayCount = 2; // dismissible gate
56+
const expectAnswer = {
57+
responseId: '',
58+
userTreatments: [dismissibleGuGateTreatment],
59+
};
60+
expect(
61+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
62+
).toStrictEqual(expectAnswer);
63+
});
64+
65+
it('[in Ireland] should return a mandatory gate if gateDisplayCount is >= 3', () => {
66+
const gateDismissCount = 2; // low number allowing for a gate
67+
const gateDisplayCount = 3; // mandatory gate
68+
const expectAnswer = {
69+
responseId: '',
70+
userTreatments: [mandatoryGuGateTreatment],
71+
};
72+
expect(
73+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
74+
).toStrictEqual(expectAnswer);
75+
});
76+
77+
it('[Ireland] should return a mandatory gate if gateDisplayCount is >= 3', () => {
78+
const gateDismissCount = 2; // low number allowing for a gate
79+
const gateDisplayCount = 6; // mandatory gate
80+
const expectAnswer = {
81+
responseId: '',
82+
userTreatments: [mandatoryGuGateTreatment],
83+
};
84+
expect(
85+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'IE'),
86+
).toStrictEqual(expectAnswer);
87+
});
88+
89+
it('[outside Ireland] should return a dismissible gate for any gateDisplayCount', () => {
90+
const gateDismissCount = 2; // low number allowing for a gate
91+
const gateDisplayCount = 0; // mandatory gate
92+
const expectAnswer = {
93+
responseId: '',
94+
userTreatments: [dismissibleGuGateTreatment],
95+
};
96+
expect(
97+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'FR'),
98+
).toStrictEqual(expectAnswer);
99+
});
100+
101+
it('[outside Ireland] should return a dismissible gate for any gateDisplayCount', () => {
102+
const gateDismissCount = 2; // low number allowing for a gate
103+
const gateDisplayCount = 6; // mandatory gate
104+
const expectAnswer = {
105+
responseId: '',
106+
userTreatments: [dismissibleGuGateTreatment],
107+
};
108+
expect(
109+
buildGuUserTreatmentsEnvelop(gateDismissCount, gateDisplayCount, 'FR'),
110+
).toStrictEqual(expectAnswer);
111+
});
112+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { buildLogTreatmentInteractionRequestPayload } from '../libPure';
2+
3+
describe('buildLogTreatmentInteractionRequestPayload', () => {
4+
it('', () => {
5+
const expectedAnswer = {
6+
projectId: 'projectId',
7+
userId: 'browserId',
8+
treatmentTrackingId: 'treatmentTrackingId',
9+
treatmentId: 'treatmentId',
10+
surface: 'surface',
11+
interactionType: 'interactionType',
12+
interactionTimeMicros: 123456789,
13+
actionName: 'actionName',
14+
};
15+
expect(
16+
buildLogTreatmentInteractionRequestPayload(
17+
'projectId',
18+
'browserId',
19+
'treatmentTrackingId',
20+
'treatmentId',
21+
'surface',
22+
'interactionType',
23+
123456789,
24+
'actionName',
25+
),
26+
).toStrictEqual(expectedAnswer);
27+
});
28+
});

0 commit comments

Comments
 (0)