Skip to content

Commit 1b2f808

Browse files
authored
test(api): factor out server calls in token.route acceptance tests
by using a _getPostFormOptions helper function.
1 parent 9584b22 commit 1b2f808

File tree

1 file changed

+75
-117
lines changed

1 file changed

+75
-117
lines changed

api/tests/identity-access-management/acceptance/application/token.route.test.js

+75-117
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
3232
});
3333

3434
it('returns a 200 with an access token and a refresh token when authentication is ok', async function () {
35-
// given / when
36-
const response = await server.inject({
37-
method: 'POST',
35+
// given
36+
const options = _getPostFormOptions({
3837
url: '/api/token',
39-
headers: {
40-
'content-type': 'application/x-www-form-urlencoded',
41-
'x-forwarded-proto': 'https',
42-
'x-forwarded-host': 'orga.pix.fr',
43-
},
44-
payload: querystring.stringify({
45-
grant_type: 'password',
46-
username: userEmailAddress,
47-
password: userPassword,
48-
}),
38+
dataToPost: { grant_type: 'password', username: userEmailAddress, password: userPassword },
39+
applicationName: 'orga',
4940
});
5041

42+
// when
43+
const response = await server.inject(options);
44+
5145
// then
5246
const result = response.result;
5347
expect(response.statusCode).to.equal(200);
@@ -72,22 +66,15 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
7266

7367
await databaseBuilder.commit();
7468

75-
// when
76-
const response = await server.inject({
77-
method: 'POST',
69+
const options = _getPostFormOptions({
7870
url: '/api/token',
79-
headers: {
80-
'content-type': 'application/x-www-form-urlencoded',
81-
'x-forwarded-proto': 'https',
82-
'x-forwarded-host': 'orga.pix.fr',
83-
},
84-
payload: querystring.stringify({
85-
grant_type: 'password',
86-
username: 'beth.rave1212',
87-
password: userPassword,
88-
}),
71+
dataToPost: { grant_type: 'password', username: 'beth.rave1212', password: userPassword },
72+
applicationName: 'orga',
8973
});
9074

75+
// when
76+
const response = await server.inject(options);
77+
9178
// then
9279
expect(response.statusCode).to.equal(401);
9380
expect(response.result.errors[0].title).equal('PasswordShouldChange');
@@ -97,36 +84,29 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
9784
context('when user needs to refresh his access token', function () {
9885
it('returns a 200 with a new access token', async function () {
9986
// given
100-
const { result: accessTokenResult } = await server.inject({
101-
method: 'POST',
87+
const optionsForAccessToken = _getPostFormOptions({
10288
url: '/api/token',
103-
headers: {
104-
'content-type': 'application/x-www-form-urlencoded',
105-
'x-forwarded-proto': 'https',
106-
'x-forwarded-host': 'orga.pix.fr',
107-
},
108-
payload: querystring.stringify({
89+
dataToPost: {
10990
grant_type: 'password',
11091
username: userEmailAddress,
11192
password: userPassword,
112-
}),
93+
},
94+
applicationName: 'orga',
11395
});
96+
const { result: accessTokenResult } = await server.inject(optionsForAccessToken);
11497

115-
// when
116-
const response = await server.inject({
117-
method: 'POST',
98+
const options = _getPostFormOptions({
11899
url: '/api/token',
119-
headers: {
120-
'content-type': 'application/x-www-form-urlencoded',
121-
'x-forwarded-proto': 'https',
122-
'x-forwarded-host': 'orga.pix.fr',
123-
},
124-
payload: querystring.stringify({
100+
dataToPost: {
125101
grant_type: 'refresh_token',
126102
refresh_token: accessTokenResult.refresh_token,
127-
}),
103+
},
104+
applicationName: 'orga',
128105
});
129106

107+
// when
108+
const response = await server.inject(options);
109+
130110
// then
131111
const result = response.result;
132112
expect(response.statusCode).to.equal(200);
@@ -144,7 +124,7 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
144124
context('when requestedApplication is admin', function () {
145125
context('when admin member has allowed role but has been disabled', function () {
146126
it('returns http code 403', async function () {
147-
//given
127+
// given
148128
const user = databaseBuilder.factory.buildUser.withRawPassword({
149129
150130
rawPassword: userPassword,
@@ -157,7 +137,11 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
157137
});
158138
await databaseBuilder.commit();
159139

160-
const options = _getServerOptions({ username: user.email, password: userPassword, applicationName: 'admin' });
140+
const options = _getPostFormOptions({
141+
url: '/api/token',
142+
dataToPost: { grant_type: 'password', username: user.email, password: userPassword },
143+
applicationName: 'admin',
144+
});
161145

162146
// when
163147
const response = await server.inject(options);
@@ -170,21 +154,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
170154

171155
context('when application is Pix Certif', function () {
172156
it('returns http code 200 with accessToken when authentication is ok', async function () {
173-
//given
157+
// given
174158
databaseBuilder.factory.buildCertificationCenter({ id: 345 });
175159
databaseBuilder.factory.buildSession({ id: 121, certificationCenterId: 345 });
176160
const candidate = databaseBuilder.factory.buildCertificationCandidate({ sessionId: 121 });
177161
databaseBuilder.factory.buildCoreSubscription({ certificationCandidateId: candidate.id });
178162
databaseBuilder.factory.buildSupervisorAccess({ userId, sessionId: 121 });
179163
await databaseBuilder.commit();
180164

181-
const options = _getServerOptions({
182-
username: userEmailAddress,
183-
password: userPassword,
165+
const options = _getPostFormOptions({
166+
url: '/api/token',
167+
dataToPost: { grant_type: 'password', username: userEmailAddress, password: userPassword },
184168
applicationName: 'certif',
185169
});
186170

187-
await databaseBuilder.commit();
188171
// when
189172
const response = await server.inject(options);
190173

@@ -214,9 +197,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
214197
databaseBuilder.factory.buildUserLogin({ userId, failureCount: 9 });
215198
await databaseBuilder.commit();
216199

217-
const options = _getServerOptions({
218-
username: '[email protected]',
219-
password: 'wrongPassword',
200+
const options = _getPostFormOptions({
201+
url: '/api/token',
202+
dataToPost: { grant_type: 'password', username: '[email protected]', password: 'wrongPassword' },
220203
applicationName: 'app',
221204
});
222205

@@ -246,9 +229,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
246229
});
247230
await databaseBuilder.commit();
248231

249-
const options = _getServerOptions({
250-
username: '[email protected]',
251-
password: userPassword,
232+
const options = _getPostFormOptions({
233+
url: '/api/token',
234+
dataToPost: { grant_type: 'password', username: '[email protected]', password: userPassword },
252235
applicationName: 'app',
253236
});
254237

@@ -275,9 +258,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
275258
});
276259
await databaseBuilder.commit();
277260

278-
const options = _getServerOptions({
279-
username: '[email protected]',
280-
password: userPassword,
261+
const options = _getPostFormOptions({
262+
url: '/api/token',
263+
dataToPost: { grant_type: 'password', username: '[email protected]', password: userPassword },
281264
applicationName: 'app',
282265
});
283266

@@ -306,23 +289,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
306289
});
307290
await databaseBuilder.commit();
308291

309-
// when
310-
const response = await server.inject({
311-
method: 'POST',
292+
const options = _getPostFormOptions({
312293
url: '/api/token',
313-
headers: {
314-
'content-type': 'application/x-www-form-urlencoded',
315-
cookie: `locale=${localeFromCookie}`,
316-
'x-forwarded-proto': 'https',
317-
'x-forwarded-host': 'app.pix.fr',
318-
},
319-
payload: querystring.stringify({
320-
grant_type: 'password',
321-
username: userWithoutLocale.email,
322-
password: userPassword,
323-
}),
294+
dataToPost: { grant_type: 'password', username: userWithoutLocale.email, password: userPassword },
295+
applicationName: 'app',
296+
localeFromCookie,
324297
});
325298

299+
// when
300+
const response = await server.inject(options);
301+
326302
// then
327303
expect(response.statusCode).to.equal(200);
328304
const user = await knex('users').where({ id: userWithoutLocale.id }).first();
@@ -343,23 +319,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
343319
});
344320
await databaseBuilder.commit();
345321

346-
// when
347-
const response = await server.inject({
348-
method: 'POST',
322+
const options = _getPostFormOptions({
349323
url: '/api/token',
350-
headers: {
351-
'content-type': 'application/x-www-form-urlencoded',
352-
cookie: `locale=${localeFromCookie}`,
353-
'x-forwarded-proto': 'https',
354-
'x-forwarded-host': 'app.pix.fr',
355-
},
356-
payload: querystring.stringify({
324+
dataToPost: {
357325
grant_type: 'password',
358326
username: userWithLocale.email,
359327
password: userPassword,
360-
}),
328+
},
329+
applicationName: 'app',
330+
localeFromCookie,
361331
});
362332

333+
// when
334+
const response = await server.inject(options);
335+
363336
// then
364337
expect(response.statusCode).to.equal(200);
365338
const user = await knex('users').where({ id: userWithLocale.id }).first();
@@ -380,22 +353,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
380353
beforeEach(async function () {
381354
const targetProfile = databaseBuilder.factory.buildTargetProfile({ isSimplifiedAccess: false });
382355
databaseBuilder.factory.buildCampaign({ code: campaignCode, targetProfile });
356+
await databaseBuilder.commit();
383357

384-
options = {
385-
method: 'POST',
358+
options = _getPostFormOptions({
386359
url: '/api/token/anonymous',
387-
headers: {
388-
'content-type': 'application/x-www-form-urlencoded',
389-
'x-forwarded-proto': 'https',
390-
'x-forwarded-host': 'app.pix.fr',
391-
},
392-
payload: querystring.stringify({
360+
dataToPost: {
393361
campaign_code: campaignCode,
394362
lang,
395-
}),
396-
};
397-
398-
await databaseBuilder.commit();
363+
},
364+
applicationName: 'app',
365+
});
399366
});
400367

401368
it('returns an 401', async function () {
@@ -418,22 +385,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
418385
beforeEach(async function () {
419386
const targetProfileId = databaseBuilder.factory.buildTargetProfile({ isSimplifiedAccess: true }).id;
420387
databaseBuilder.factory.buildCampaign({ code: simplifiedAccessCampaignCode, targetProfileId });
388+
await databaseBuilder.commit();
421389

422-
options = {
423-
method: 'POST',
390+
options = _getPostFormOptions({
424391
url: '/api/token/anonymous',
425-
headers: {
426-
'content-type': 'application/x-www-form-urlencoded',
427-
'x-forwarded-proto': 'https',
428-
'x-forwarded-host': 'app.pix.fr',
429-
},
430-
payload: querystring.stringify({
392+
dataToPost: {
431393
campaign_code: simplifiedAccessCampaignCode,
432394
lang,
433-
}),
434-
};
435-
436-
await databaseBuilder.commit();
395+
},
396+
applicationName: 'app',
397+
});
437398
});
438399

439400
it('returns a 200 with accessToken', async function () {
@@ -540,19 +501,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
540501
});
541502
});
542503

543-
function _getServerOptions({ username, password, applicationName }) {
504+
function _getPostFormOptions({ url, dataToPost, applicationName, localeFromCookie }) {
544505
return {
545506
method: 'POST',
546-
url: '/api/token',
507+
url,
547508
headers: {
548509
'content-type': 'application/x-www-form-urlencoded',
549510
'x-forwarded-proto': 'https',
550511
'x-forwarded-host': `${applicationName}.pix.fr`,
512+
...(localeFromCookie && { cookie: `locale=${localeFromCookie}` }),
551513
},
552-
payload: querystring.stringify({
553-
grant_type: 'password',
554-
username,
555-
password,
556-
}),
514+
payload: querystring.stringify(dataToPost),
557515
};
558516
}

0 commit comments

Comments
 (0)