@@ -6,10 +6,16 @@ const API_URL = 'https://tenant.auth0.com';
6
6
const ClientsManager = require ( `../../src/management/ClientsManager` ) ;
7
7
const { ArgumentError } = require ( 'rest-facade' ) ;
8
8
9
+ /**
10
+ * @type {ClientsManager }
11
+ */
12
+
13
+ let clients ;
14
+
9
15
describe ( 'ClientsManager' , ( ) => {
10
16
before ( function ( ) {
11
17
this . token = 'TOKEN' ;
12
- this . clients = new ClientsManager ( {
18
+ clients = new ClientsManager ( {
13
19
headers : {
14
20
authorization : `Bearer ${ this . token } ` ,
15
21
} ,
@@ -25,8 +31,8 @@ describe('ClientsManager', () => {
25
31
const methods = [ 'getAll' , 'get' , 'create' , 'update' , 'delete' ] ;
26
32
27
33
methods . forEach ( ( method ) => {
28
- it ( `should have a ${ method } method` , function ( ) {
29
- expect ( this . clients [ method ] ) . to . exist . to . be . an . instanceOf ( Function ) ;
34
+ it ( `should have a ${ method } method` , ( ) => {
35
+ expect ( clients [ method ] ) . to . exist . to . be . an . instanceOf ( Function ) ;
30
36
} ) ;
31
37
} ) ;
32
38
} ) ;
@@ -56,34 +62,34 @@ describe('ClientsManager', () => {
56
62
this . request = nock ( API_URL ) . get ( '/clients' ) . reply ( 200 ) ;
57
63
} ) ;
58
64
59
- it ( 'should accept a callback' , function ( done ) {
60
- this . clients . getAll ( ( ) => {
65
+ it ( 'should accept a callback' , ( done ) => {
66
+ clients . getAll ( ( ) => {
61
67
done ( ) ;
62
68
} ) ;
63
69
} ) ;
64
70
65
- it ( 'should return a promise if no callback is given' , function ( done ) {
66
- this . clients . getAll ( ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
71
+ it ( 'should return a promise if no callback is given' , ( done ) => {
72
+ clients . getAll ( ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
67
73
} ) ;
68
74
69
- it ( 'should pass any errors to the promise catch handler' , function ( done ) {
75
+ it ( 'should pass any errors to the promise catch handler' , ( done ) => {
70
76
nock . cleanAll ( ) ;
71
77
72
78
nock ( API_URL ) . get ( '/clients' ) . reply ( 500 ) ;
73
79
74
- this . clients . getAll ( ) . catch ( ( err ) => {
80
+ clients . getAll ( ) . catch ( ( err ) => {
75
81
expect ( err ) . to . exist ;
76
82
done ( ) ;
77
83
} ) ;
78
84
} ) ;
79
85
80
- it ( 'should pass the body of the response to the "then" handler' , function ( done ) {
86
+ it ( 'should pass the body of the response to the "then" handler' , ( done ) => {
81
87
nock . cleanAll ( ) ;
82
88
83
89
const data = [ { test : true } ] ;
84
90
nock ( API_URL ) . get ( '/clients' ) . reply ( 200 , data ) ;
85
91
86
- this . clients . getAll ( ) . then ( ( clients ) => {
92
+ clients . getAll ( ) . then ( ( clients ) => {
87
93
expect ( clients ) . to . be . an . instanceOf ( Array ) ;
88
94
89
95
expect ( clients . length ) . to . equal ( data . length ) ;
@@ -97,7 +103,7 @@ describe('ClientsManager', () => {
97
103
it ( 'should perform a GET request to /api/v2/clients' , function ( done ) {
98
104
const { request } = this ;
99
105
100
- this . clients . getAll ( ) . then ( ( ) => {
106
+ clients . getAll ( ) . then ( ( ) => {
101
107
expect ( request . isDone ( ) ) . to . be . true ;
102
108
done ( ) ;
103
109
} ) ;
@@ -111,13 +117,13 @@ describe('ClientsManager', () => {
111
117
. matchHeader ( 'Authorization' , `Bearer ${ this . token } ` )
112
118
. reply ( 200 ) ;
113
119
114
- this . clients . getAll ( ) . then ( ( ) => {
120
+ clients . getAll ( ) . then ( ( ) => {
115
121
expect ( request . isDone ( ) ) . to . be . true ;
116
122
done ( ) ;
117
123
} ) ;
118
124
} ) ;
119
125
120
- it ( 'should pass the parameters in the query-string' , function ( done ) {
126
+ it ( 'should pass the parameters in the query-string' , ( done ) => {
121
127
nock . cleanAll ( ) ;
122
128
123
129
const request = nock ( API_URL )
@@ -128,7 +134,7 @@ describe('ClientsManager', () => {
128
134
} )
129
135
. reply ( 200 ) ;
130
136
131
- this . clients . getAll ( { include_fields : true , fields : 'test' } ) . then ( ( ) => {
137
+ clients . getAll ( { include_fields : true , fields : 'test' } ) . then ( ( ) => {
132
138
expect ( request . isDone ( ) ) . to . be . true ;
133
139
done ( ) ;
134
140
} ) ;
@@ -142,18 +148,18 @@ describe('ClientsManager', () => {
142
148
this . request = nock ( API_URL ) . post ( '/clients' ) . reply ( 201 , data ) ;
143
149
} ) ;
144
150
145
- it ( 'should accept a callback' , function ( done ) {
146
- this . clients . create ( data , done . bind ( null , null ) ) ;
151
+ it ( 'should accept a callback' , ( done ) => {
152
+ clients . create ( data , done . bind ( null , null ) ) ;
147
153
} ) ;
148
154
149
- it ( 'should return a promise if no callback is given' , function ( done ) {
150
- this . clients . create ( data ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
155
+ it ( 'should return a promise if no callback is given' , ( done ) => {
156
+ clients . create ( data ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
151
157
} ) ;
152
158
153
159
it ( 'should perform a POST request to /api/v2/clients' , function ( done ) {
154
160
const { request } = this ;
155
161
156
- this . clients . create ( data ) . then ( ( ) => {
162
+ clients . create ( data ) . then ( ( ) => {
157
163
expect ( request . isDone ( ) ) . to . be . true ;
158
164
159
165
done ( ) ;
@@ -168,19 +174,19 @@ describe('ClientsManager', () => {
168
174
. matchHeader ( 'Authorization' , `Bearer ${ this . token } ` )
169
175
. reply ( 201 , data ) ;
170
176
171
- this . clients . create ( data ) . then ( ( ) => {
177
+ clients . create ( data ) . then ( ( ) => {
172
178
expect ( request . isDone ( ) ) . to . be . true ;
173
179
174
180
done ( ) ;
175
181
} ) ;
176
182
} ) ;
177
183
178
- it ( 'should include the new client data in the request body' , function ( done ) {
184
+ it ( 'should include the new client data in the request body' , ( done ) => {
179
185
nock . cleanAll ( ) ;
180
186
181
187
const request = nock ( API_URL ) . post ( '/clients' , data ) . reply ( 201 , data ) ;
182
188
183
- this . clients . create ( data ) . then ( ( ) => {
189
+ clients . create ( data ) . then ( ( ) => {
184
190
expect ( request . isDone ( ) ) . to . be . true ;
185
191
186
192
done ( ) ;
@@ -202,20 +208,17 @@ describe('ClientsManager', () => {
202
208
it ( 'should accept a callback' , function ( done ) {
203
209
const params = { id : this . data . id } ;
204
210
205
- this . clients . get ( params , done . bind ( null , null ) ) ;
211
+ clients . get ( params , done . bind ( null , null ) ) ;
206
212
} ) ;
207
213
208
214
it ( 'should return a promise if no callback is given' , function ( done ) {
209
- this . clients
210
- . get ( { id : this . data . id } )
211
- . then ( done . bind ( null , null ) )
212
- . catch ( done . bind ( null , null ) ) ;
215
+ clients . get ( { id : this . data . id } ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
213
216
} ) ;
214
217
215
218
it ( 'should perform a POST request to /api/v2/clients/5' , function ( done ) {
216
219
const { request } = this ;
217
220
218
- this . clients . get ( { client_id : this . data . id } ) . then ( ( ) => {
221
+ clients . get ( { client_id : this . data . id } ) . then ( ( ) => {
219
222
expect ( request . isDone ( ) ) . to . be . true ;
220
223
221
224
done ( ) ;
@@ -230,21 +233,18 @@ describe('ClientsManager', () => {
230
233
this . request = nock ( API_URL ) . patch ( `/clients/${ this . data . id } ` ) . reply ( 200 , this . data ) ;
231
234
} ) ;
232
235
233
- it ( 'should accept a callback' , function ( done ) {
234
- this . clients . update ( { client_id : 5 } , { } , done . bind ( null , null ) ) ;
236
+ it ( 'should accept a callback' , ( done ) => {
237
+ clients . update ( { client_id : 5 } , { } , done . bind ( null , null ) ) ;
235
238
} ) ;
236
239
237
- it ( 'should return a promise if no callback is given' , function ( done ) {
238
- this . clients
239
- . update ( { client_id : 5 } , { } )
240
- . then ( done . bind ( null , null ) )
241
- . catch ( done . bind ( null , null ) ) ;
240
+ it ( 'should return a promise if no callback is given' , ( done ) => {
241
+ clients . update ( { client_id : 5 } , { } ) . then ( done . bind ( null , null ) ) . catch ( done . bind ( null , null ) ) ;
242
242
} ) ;
243
243
244
244
it ( 'should perform a PATCH request to /api/v2/clients/5' , function ( done ) {
245
245
const { request } = this ;
246
246
247
- this . clients . update ( { client_id : 5 } , { } ) . then ( ( ) => {
247
+ clients . update ( { client_id : 5 } , { } ) . then ( ( ) => {
248
248
expect ( request . isDone ( ) ) . to . be . true ;
249
249
250
250
done ( ) ;
@@ -256,7 +256,7 @@ describe('ClientsManager', () => {
256
256
257
257
const request = nock ( API_URL ) . patch ( `/clients/${ this . data . id } ` , this . data ) . reply ( 200 ) ;
258
258
259
- this . clients . update ( { client_id : 5 } , this . data ) . then ( ( ) => {
259
+ clients . update ( { client_id : 5 } , this . data ) . then ( ( ) => {
260
260
expect ( request . isDone ( ) ) . to . be . true ;
261
261
262
262
done ( ) ;
@@ -271,22 +271,81 @@ describe('ClientsManager', () => {
271
271
this . request = nock ( API_URL ) . delete ( `/clients/${ id } ` ) . reply ( 200 ) ;
272
272
} ) ;
273
273
274
- it ( 'should accept a callback' , function ( done ) {
275
- this . clients . delete ( { client_id : id } , done . bind ( null , null ) ) ;
274
+ it ( 'should accept a callback' , ( done ) => {
275
+ clients . delete ( { client_id : id } , done . bind ( null , null ) ) ;
276
276
} ) ;
277
277
278
- it ( 'should return a promise when no callback is given' , function ( done ) {
279
- this . clients . delete ( { client_id : id } ) . then ( done . bind ( null , null ) ) ;
278
+ it ( 'should return a promise when no callback is given' , ( done ) => {
279
+ clients . delete ( { client_id : id } ) . then ( done . bind ( null , null ) ) ;
280
280
} ) ;
281
281
282
282
it ( `should perform a DELETE request to /clients/${ id } ` , function ( done ) {
283
283
const { request } = this ;
284
284
285
- this . clients . delete ( { client_id : id } ) . then ( ( ) => {
285
+ clients . delete ( { client_id : id } ) . then ( ( ) => {
286
286
expect ( request . isDone ( ) ) . to . be . true ;
287
287
288
288
done ( ) ;
289
289
} ) ;
290
290
} ) ;
291
291
} ) ;
292
+
293
+ describe ( '#rotateSecret' , ( ) => {
294
+ const client_id = 5 ;
295
+
296
+ beforeEach ( function ( ) {
297
+ this . request = nock ( API_URL ) . post ( `/clients/${ client_id } /rotate-secret` ) . reply ( 200 ) ;
298
+ } ) ;
299
+
300
+ it ( 'should accept a callback' , ( done ) => {
301
+ clients . rotateClientSecret ( { client_id } , done . bind ( null , null ) ) ;
302
+ } ) ;
303
+
304
+ it ( 'should return a promise if no callback is given' , ( done ) => {
305
+ clients
306
+ . rotateClientSecret ( { client_id } , { } )
307
+ . then ( done . bind ( null , null ) )
308
+ . catch ( done . bind ( null , null ) ) ;
309
+ } ) ;
310
+
311
+ it ( 'should perform a POST request to /api/v2/clients/5/rotate-secret' , function ( done ) {
312
+ const { request } = this ;
313
+
314
+ clients . rotateClientSecret ( { client_id } ) . then ( ( ) => {
315
+ expect ( request . isDone ( ) ) . to . be . true ;
316
+
317
+ done ( ) ;
318
+ } ) ;
319
+ } ) ;
320
+
321
+ it ( 'should return an error when client_id is not sent' , ( ) => {
322
+ expect ( ( ) => {
323
+ clients . rotateClientSecret ( { } ) ;
324
+ } ) . to . throw ( ArgumentError , 'The client_id cannot be null or undefined' ) ;
325
+ } ) ;
326
+
327
+ it ( 'should include the new data in the body of the request' , ( done ) => {
328
+ nock . cleanAll ( ) ;
329
+
330
+ const request = nock ( API_URL ) . post ( `/clients/${ client_id } /rotate-secret` ) . reply ( 200 ) ;
331
+
332
+ clients . rotateClientSecret ( { client_id } ) . then ( ( ) => {
333
+ expect ( request . isDone ( ) ) . to . be . true ;
334
+
335
+ done ( ) ;
336
+ } ) ;
337
+ } ) ;
338
+
339
+ it ( 'should pass any errors to the promise catch handler' , ( done ) => {
340
+ nock . cleanAll ( ) ;
341
+
342
+ nock ( API_URL ) . post ( `/clients/${ client_id } /rotate-secret` ) . reply ( 500 ) ;
343
+
344
+ clients . rotateClientSecret ( { client_id } ) . catch ( ( err ) => {
345
+ expect ( err ) . to . exist ;
346
+
347
+ done ( ) ;
348
+ } ) ;
349
+ } ) ;
350
+ } ) ;
292
351
} ) ;
0 commit comments