1- const  { Given,  When,  Then}  =  require ( 'cucumber' ) ; 
2- const  should  =  require ( 'should' ) ; 
1+ const 
2+   { Given,  When,  Then}  =  require ( 'cucumber' ) , 
3+   should  =  require ( 'should' ) , 
4+   retry  =  require ( 'retry' ) ; 
35
46Given ( 'I log in as {string}:{string}' ,  async  function  ( username ,  password )  { 
57  try  { 
@@ -15,11 +17,44 @@ Given('I log in as {string}:{string}', async function (username, password) {
1517  } 
1618} ) ; 
1719
18- 
1920When ( 'I logout' ,  async  function  ( )  { 
2021  await  this . kuzzle . auth . logout ( ) ; 
2122} ) ; 
2223
24+ When ( 'I refresh the JWT' ,  function  ( cb )  { 
25+   this . previousJwt  =  this . jwt ; 
26+ 
27+   // we have to wait for at least 1s: if we ask Kuzzle to refresh a JWT that 
28+   // has been generated during the same second, then the new JWT will be 
29+   // identical to the one being refreshed 
30+   setTimeout ( async  ( )  =>  { 
31+     const  token  =  await  this . kuzzle . auth . refreshToken ( ) ; 
32+     this . jwt  =  token . jwt ; 
33+     cb ( ) ; 
34+   } ,  1000 ) ; 
35+ } ) ; 
36+ 
37+ Then ( 'the previous JWT is now invalid' ,  function  ( cb )  { 
38+   // prevent false positives, just in case 
39+   should ( this . previousJwt ) . be . a . String ( ) . and . not . empty ( ) ; 
40+   should ( this . previousJwt ) . not . eql ( this . jwt ) ; 
41+ 
42+   const  op  =  retry . operation ( { retries : 10 ,  minTimeout : 500 ,  factor : 1 } ) ; 
43+ 
44+   op . attempt ( ( )  =>  { 
45+     this . kuzzle . auth . checkToken ( this . previousJwt ) 
46+       . then ( response  =>  { 
47+         const  err  =  response . valid  ? new  Error ( 'Unexpected valid token' )  : null ; 
48+ 
49+         if  ( op . retry ( err ) )  { 
50+           return ; 
51+         } 
52+ 
53+         cb ( err ) ; 
54+       } ) 
55+       . catch ( err  =>  cb ( err ) ) ; 
56+   } ) ; 
57+ } ) ; 
2358
2459Then ( / ^ t h e   J W T   i s   ( i n ) ? v a l i d $ / ,  async  function  ( not )  { 
2560  const  response  =  await  this . kuzzle . auth . checkToken ( this . jwt ) ; 
@@ -32,3 +67,10 @@ Then(/^the JWT is (in)?valid$/, async function (not) {
3267  } 
3368} ) ; 
3469
70+ Given ( 'I get my rights' ,  async  function  ( )  { 
71+   this . rights  =  await  this . kuzzle . auth . getMyRights ( ) ; 
72+ } ) ; 
73+ 
74+ Then ( 'I have a vector with {int} rights' ,  function  ( nbRights )  { 
75+   should ( this . rights ) . have . length ( nbRights ) ; 
76+ } ) ; 
0 commit comments