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' ) ;
3
5
4
6
Given ( 'I log in as {string}:{string}' , async function ( username , password ) {
5
7
try {
@@ -15,11 +17,44 @@ Given('I log in as {string}:{string}', async function (username, password) {
15
17
}
16
18
} ) ;
17
19
18
-
19
20
When ( 'I logout' , async function ( ) {
20
21
await this . kuzzle . auth . logout ( ) ;
21
22
} ) ;
22
23
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
+ } ) ;
23
58
24
59
Then ( / ^ t h e J W T i s ( i n ) ? v a l i d $ / , async function ( not ) {
25
60
const response = await this . kuzzle . auth . checkToken ( this . jwt ) ;
@@ -32,3 +67,10 @@ Then(/^the JWT is (in)?valid$/, async function (not) {
32
67
}
33
68
} ) ;
34
69
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