@@ -32,22 +32,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
32
32
} ) ;
33
33
34
34
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 ( {
38
37
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' ,
49
40
} ) ;
50
41
42
+ // when
43
+ const response = await server . inject ( options ) ;
44
+
51
45
// then
52
46
const result = response . result ;
53
47
expect ( response . statusCode ) . to . equal ( 200 ) ;
@@ -72,22 +66,15 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
72
66
73
67
await databaseBuilder . commit ( ) ;
74
68
75
- // when
76
- const response = await server . inject ( {
77
- method : 'POST' ,
69
+ const options = _getPostFormOptions ( {
78
70
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' ,
89
73
} ) ;
90
74
75
+ // when
76
+ const response = await server . inject ( options ) ;
77
+
91
78
// then
92
79
expect ( response . statusCode ) . to . equal ( 401 ) ;
93
80
expect ( response . result . errors [ 0 ] . title ) . equal ( 'PasswordShouldChange' ) ;
@@ -97,36 +84,29 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
97
84
context ( 'when user needs to refresh his access token' , function ( ) {
98
85
it ( 'returns a 200 with a new access token' , async function ( ) {
99
86
// given
100
- const { result : accessTokenResult } = await server . inject ( {
101
- method : 'POST' ,
87
+ const optionsForAccessToken = _getPostFormOptions ( {
102
88
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 : {
109
90
grant_type : 'password' ,
110
91
username : userEmailAddress ,
111
92
password : userPassword ,
112
- } ) ,
93
+ } ,
94
+ applicationName : 'orga' ,
113
95
} ) ;
96
+ const { result : accessTokenResult } = await server . inject ( optionsForAccessToken ) ;
114
97
115
- // when
116
- const response = await server . inject ( {
117
- method : 'POST' ,
98
+ const options = _getPostFormOptions ( {
118
99
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 : {
125
101
grant_type : 'refresh_token' ,
126
102
refresh_token : accessTokenResult . refresh_token ,
127
- } ) ,
103
+ } ,
104
+ applicationName : 'orga' ,
128
105
} ) ;
129
106
107
+ // when
108
+ const response = await server . inject ( options ) ;
109
+
130
110
// then
131
111
const result = response . result ;
132
112
expect ( response . statusCode ) . to . equal ( 200 ) ;
@@ -144,7 +124,7 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
144
124
context ( 'when requestedApplication is admin' , function ( ) {
145
125
context ( 'when admin member has allowed role but has been disabled' , function ( ) {
146
126
it ( 'returns http code 403' , async function ( ) {
147
- //given
127
+ // given
148
128
const user = databaseBuilder . factory . buildUser . withRawPassword ( {
149
129
150
130
rawPassword : userPassword ,
@@ -157,7 +137,11 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
157
137
} ) ;
158
138
await databaseBuilder . commit ( ) ;
159
139
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
+ } ) ;
161
145
162
146
// when
163
147
const response = await server . inject ( options ) ;
@@ -170,21 +154,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
170
154
171
155
context ( 'when application is Pix Certif' , function ( ) {
172
156
it ( 'returns http code 200 with accessToken when authentication is ok' , async function ( ) {
173
- //given
157
+ // given
174
158
databaseBuilder . factory . buildCertificationCenter ( { id : 345 } ) ;
175
159
databaseBuilder . factory . buildSession ( { id : 121 , certificationCenterId : 345 } ) ;
176
160
const candidate = databaseBuilder . factory . buildCertificationCandidate ( { sessionId : 121 } ) ;
177
161
databaseBuilder . factory . buildCoreSubscription ( { certificationCandidateId : candidate . id } ) ;
178
162
databaseBuilder . factory . buildSupervisorAccess ( { userId, sessionId : 121 } ) ;
179
163
await databaseBuilder . commit ( ) ;
180
164
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 } ,
184
168
applicationName : 'certif' ,
185
169
} ) ;
186
170
187
- await databaseBuilder . commit ( ) ;
188
171
// when
189
172
const response = await server . inject ( options ) ;
190
173
@@ -214,9 +197,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
214
197
databaseBuilder . factory . buildUserLogin ( { userId, failureCount : 9 } ) ;
215
198
await databaseBuilder . commit ( ) ;
216
199
217
- const options = _getServerOptions ( {
218
-
219
- password : 'wrongPassword' ,
200
+ const options = _getPostFormOptions ( {
201
+ url : '/api/token ' ,
202
+ dataToPost : { grant_type : ' password' , username :
'[email protected] ' , password : ' wrongPassword' } ,
220
203
applicationName : 'app' ,
221
204
} ) ;
222
205
@@ -246,9 +229,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
246
229
} ) ;
247
230
await databaseBuilder . commit ( ) ;
248
231
249
- const options = _getServerOptions ( {
250
-
251
- password : userPassword ,
232
+ const options = _getPostFormOptions ( {
233
+ url : '/api/token ' ,
234
+ dataToPost : { grant_type : ' password' , username : '[email protected] ' , password : userPassword } ,
252
235
applicationName : 'app' ,
253
236
} ) ;
254
237
@@ -275,9 +258,9 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
275
258
} ) ;
276
259
await databaseBuilder . commit ( ) ;
277
260
278
- const options = _getServerOptions ( {
279
-
280
- password : userPassword ,
261
+ const options = _getPostFormOptions ( {
262
+ url : '/api/token ' ,
263
+ dataToPost : { grant_type : ' password' , username : '[email protected] ' , password : userPassword } ,
281
264
applicationName : 'app' ,
282
265
} ) ;
283
266
@@ -306,23 +289,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
306
289
} ) ;
307
290
await databaseBuilder . commit ( ) ;
308
291
309
- // when
310
- const response = await server . inject ( {
311
- method : 'POST' ,
292
+ const options = _getPostFormOptions ( {
312
293
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,
324
297
} ) ;
325
298
299
+ // when
300
+ const response = await server . inject ( options ) ;
301
+
326
302
// then
327
303
expect ( response . statusCode ) . to . equal ( 200 ) ;
328
304
const user = await knex ( 'users' ) . where ( { id : userWithoutLocale . id } ) . first ( ) ;
@@ -343,23 +319,20 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
343
319
} ) ;
344
320
await databaseBuilder . commit ( ) ;
345
321
346
- // when
347
- const response = await server . inject ( {
348
- method : 'POST' ,
322
+ const options = _getPostFormOptions ( {
349
323
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 : {
357
325
grant_type : 'password' ,
358
326
username : userWithLocale . email ,
359
327
password : userPassword ,
360
- } ) ,
328
+ } ,
329
+ applicationName : 'app' ,
330
+ localeFromCookie,
361
331
} ) ;
362
332
333
+ // when
334
+ const response = await server . inject ( options ) ;
335
+
363
336
// then
364
337
expect ( response . statusCode ) . to . equal ( 200 ) ;
365
338
const user = await knex ( 'users' ) . where ( { id : userWithLocale . id } ) . first ( ) ;
@@ -380,22 +353,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
380
353
beforeEach ( async function ( ) {
381
354
const targetProfile = databaseBuilder . factory . buildTargetProfile ( { isSimplifiedAccess : false } ) ;
382
355
databaseBuilder . factory . buildCampaign ( { code : campaignCode , targetProfile } ) ;
356
+ await databaseBuilder . commit ( ) ;
383
357
384
- options = {
385
- method : 'POST' ,
358
+ options = _getPostFormOptions ( {
386
359
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 : {
393
361
campaign_code : campaignCode ,
394
362
lang,
395
- } ) ,
396
- } ;
397
-
398
- await databaseBuilder . commit ( ) ;
363
+ } ,
364
+ applicationName : 'app' ,
365
+ } ) ;
399
366
} ) ;
400
367
401
368
it ( 'returns an 401' , async function ( ) {
@@ -418,22 +385,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
418
385
beforeEach ( async function ( ) {
419
386
const targetProfileId = databaseBuilder . factory . buildTargetProfile ( { isSimplifiedAccess : true } ) . id ;
420
387
databaseBuilder . factory . buildCampaign ( { code : simplifiedAccessCampaignCode , targetProfileId } ) ;
388
+ await databaseBuilder . commit ( ) ;
421
389
422
- options = {
423
- method : 'POST' ,
390
+ options = _getPostFormOptions ( {
424
391
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 : {
431
393
campaign_code : simplifiedAccessCampaignCode ,
432
394
lang,
433
- } ) ,
434
- } ;
435
-
436
- await databaseBuilder . commit ( ) ;
395
+ } ,
396
+ applicationName : 'app' ,
397
+ } ) ;
437
398
} ) ;
438
399
439
400
it ( 'returns a 200 with accessToken' , async function ( ) {
@@ -540,19 +501,16 @@ describe('Acceptance | Identity Access Management | Route | Token', function ()
540
501
} ) ;
541
502
} ) ;
542
503
543
- function _getServerOptions ( { username , password , applicationName } ) {
504
+ function _getPostFormOptions ( { url , dataToPost , applicationName, localeFromCookie } ) {
544
505
return {
545
506
method : 'POST' ,
546
- url : '/api/token' ,
507
+ url,
547
508
headers : {
548
509
'content-type' : 'application/x-www-form-urlencoded' ,
549
510
'x-forwarded-proto' : 'https' ,
550
511
'x-forwarded-host' : `${ applicationName } .pix.fr` ,
512
+ ...( localeFromCookie && { cookie : `locale=${ localeFromCookie } ` } ) ,
551
513
} ,
552
- payload : querystring . stringify ( {
553
- grant_type : 'password' ,
554
- username,
555
- password,
556
- } ) ,
514
+ payload : querystring . stringify ( dataToPost ) ,
557
515
} ;
558
516
}
0 commit comments