@@ -11,6 +11,7 @@ import { telemetry } from '../../../../telemetry.js';
11
11
import { pid } from '../../../../utils/pid.js' ;
12
12
import { session } from '../../../../utils/session.js' ;
13
13
import { sinonUtil } from '../../../../utils/sinonUtil.js' ;
14
+ import { spo } from '../../../../utils/spo.js' ;
14
15
import commands from '../../commands.js' ;
15
16
import command from './user-remove.js' ;
16
17
import { settingsNames } from '../../../../settingsNames.js' ;
@@ -88,6 +89,21 @@ describe(commands.USER_REMOVE, () => {
88
89
"serviceProvisioningErrors" : [ ]
89
90
} ]
90
91
} ;
92
+ const userResponse = {
93
+ "Id" : 10 ,
94
+ "IsHiddenInUI" : false ,
95
+ "LoginName" : validLoginName ,
96
+ "Title" : "John Doe" ,
97
+ "PrincipalType" : 1 ,
98
+ "Email" : validEmail ,
99
+ "Expiration" : "" ,
100
+ "IsEmailAuthenticationGuestUser" : false ,
101
+ "IsShareByEmailGuestUser" : false ,
102
+ "IsSiteAdmin" : false ,
103
+ "UserId" : { "NameId" : "10010001b0c19a2" , "NameIdIssuer" : "urn:federation:microsoftonline" } ,
104
+ "UserPrincipalName" : validUserName
105
+ } ;
106
+
91
107
let log : any [ ] ;
92
108
let requests : any [ ] ;
93
109
let logger : Logger ;
@@ -129,6 +145,7 @@ describe(commands.USER_REMOVE, () => {
129
145
request . post ,
130
146
request . get ,
131
147
cli . promptForConfirmation ,
148
+ spo . getUserByEmail ,
132
149
cli . getSettingWithDefaultValue
133
150
] ) ;
134
151
} ) ;
@@ -212,7 +229,7 @@ describe(commands.USER_REMOVE, () => {
212
229
const actual = await command . validate ( { options : { webUrl : validWebUrl , email : 'invalid' } } , commandInfo ) ;
213
230
assert . notStrictEqual ( actual , true ) ;
214
231
} ) ;
215
-
232
+
216
233
it ( 'passes validation url is valid and id is passed' , async ( ) => {
217
234
const actual = await command . validate ( { options : { webUrl : validWebUrl , id : 1 } } , commandInfo ) ;
218
235
assert . strictEqual ( actual , true ) ;
@@ -271,22 +288,22 @@ describe(commands.USER_REMOVE, () => {
271
288
it ( 'removes user by id successfully without prompting with confirmation argument' , async ( ) => {
272
289
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
273
290
requests . push ( opts ) ;
274
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
291
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
275
292
return true ;
276
293
}
277
294
throw 'Invalid request' ;
278
295
} ) ;
279
296
280
297
await command . action ( logger , {
281
298
options : {
282
- webUrl : "https://contoso.sharepoint.com/subsite" ,
299
+ webUrl : validWebUrl ,
283
300
id : 10 ,
284
301
force : true
285
302
}
286
303
} ) ;
287
304
let correctRequestIssued = false ;
288
305
requests . forEach ( r => {
289
- if ( r . url . indexOf ( ` _api/web/siteusers/removebyid(10)`) > - 1 &&
306
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` &&
290
307
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
291
308
correctRequestIssued = true ;
292
309
}
@@ -297,22 +314,22 @@ describe(commands.USER_REMOVE, () => {
297
314
it ( 'removes user by login name successfully without prompting with confirmation argument' , async ( ) => {
298
315
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
299
316
requests . push ( opts ) ;
300
- if ( opts . url === "https://contoso.sharepoint.com/subsite/ _api/web/siteusers/removeByLoginName('i%3A0%23.f%7Cmembership%7Cparker%40tenant.onmicrosoft.com')" ) {
317
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removeByLoginName('i%3A0%23.f%7Cmembership%7Cparker%40tenant.onmicrosoft.com')` ) {
301
318
return true ;
302
319
}
303
320
throw 'Invalid request' ;
304
321
} ) ;
305
322
306
323
await command . action ( logger , {
307
324
options : {
308
- webUrl : "https://contoso.sharepoint.com/subsite" ,
325
+ webUrl : validWebUrl ,
309
326
loginName :
"i:0#.f|membership|[email protected] " ,
310
327
force : true
311
328
}
312
329
} ) ;
313
330
let correctRequestIssued = false ;
314
331
requests . forEach ( r => {
315
- if ( r . url . indexOf ( ` _api/web/siteusers/removeByLoginName('i%3A0%23.f%7Cmembership%7Cparker%40tenant.onmicrosoft.com')`) > - 1 &&
332
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removeByLoginName('i%3A0%23.f%7Cmembership%7Cparker%40tenant.onmicrosoft.com')` &&
316
333
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
317
334
correctRequestIssued = true ;
318
335
}
@@ -323,7 +340,7 @@ describe(commands.USER_REMOVE, () => {
323
340
it ( 'removes user by id successfully from web when prompt confirmed' , async ( ) => {
324
341
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
325
342
requests . push ( opts ) ;
326
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
343
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
327
344
return true ;
328
345
}
329
346
throw 'Invalid request' ;
@@ -333,13 +350,13 @@ describe(commands.USER_REMOVE, () => {
333
350
sinon . stub ( cli , 'promptForConfirmation' ) . resolves ( true ) ;
334
351
await command . action ( logger , {
335
352
options : {
336
- webUrl : "https://contoso.sharepoint.com/subsite" ,
353
+ webUrl : validWebUrl ,
337
354
id : 10
338
355
}
339
356
} ) ;
340
357
let correctRequestIssued = false ;
341
358
requests . forEach ( r => {
342
- if ( r . url . indexOf ( ` _api/web/siteusers/removebyid(10)`) > - 1 &&
359
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` &&
343
360
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
344
361
correctRequestIssued = true ;
345
362
}
@@ -350,7 +367,7 @@ describe(commands.USER_REMOVE, () => {
350
367
it ( 'removes user by login name successfully from web when prompt confirmed' , async ( ) => {
351
368
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
352
369
requests . push ( opts ) ;
353
- if ( ( opts . url as string ) . indexOf ( ` _api/web/siteusers/removeByLoginName` ) > - 1 ) {
370
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removeByLoginName(' ${ formatting . encodeQueryParameter ( validLoginName ) } ')` ) {
354
371
return true ;
355
372
}
356
373
throw 'Invalid request' ;
@@ -366,7 +383,7 @@ describe(commands.USER_REMOVE, () => {
366
383
} ) ;
367
384
let correctRequestIssued = false ;
368
385
requests . forEach ( r => {
369
- if ( r . url . indexOf ( ` _api/web/siteusers/removeByLoginName` ) > - 1 &&
386
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removeByLoginName(' ${ formatting . encodeQueryParameter ( validLoginName ) } ')` &&
370
387
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
371
388
correctRequestIssued = true ;
372
389
}
@@ -377,7 +394,7 @@ describe(commands.USER_REMOVE, () => {
377
394
it ( 'removes user from web successfully without prompting with confirmation argument (verbose)' , async ( ) => {
378
395
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
379
396
requests . push ( opts ) ;
380
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
397
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
381
398
return true ;
382
399
}
383
400
throw 'Invalid request' ;
@@ -386,14 +403,14 @@ describe(commands.USER_REMOVE, () => {
386
403
await command . action ( logger , {
387
404
options : {
388
405
verbose : true ,
389
- webUrl : "https://contoso.sharepoint.com/subsite" ,
406
+ webUrl : validWebUrl ,
390
407
id : 10 ,
391
408
force : true
392
409
}
393
410
} ) ;
394
411
let correctRequestIssued = false ;
395
412
requests . forEach ( r => {
396
- if ( r . url . indexOf ( ` _api/web/siteusers/removebyid(10)`) > - 1 &&
413
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` &&
397
414
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
398
415
correctRequestIssued = true ;
399
416
}
@@ -404,7 +421,7 @@ describe(commands.USER_REMOVE, () => {
404
421
it ( 'removes user from web successfully without prompting with confirmation argument (debug)' , async ( ) => {
405
422
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
406
423
requests . push ( opts ) ;
407
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
424
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
408
425
return true ;
409
426
}
410
427
throw 'Invalid request' ;
@@ -413,51 +430,31 @@ describe(commands.USER_REMOVE, () => {
413
430
await command . action ( logger , {
414
431
options : {
415
432
debug : true ,
416
- webUrl : "https://contoso.sharepoint.com/subsite" ,
433
+ webUrl : validWebUrl ,
417
434
id : 10 ,
418
435
force : true
419
436
}
420
437
} ) ;
421
438
let correctRequestIssued = false ;
422
439
requests . forEach ( r => {
423
- if ( r . url . indexOf ( ` _api/web/siteusers/removebyid(10)`) > - 1 &&
440
+ if ( r . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` &&
424
441
r . headers [ 'accept' ] === 'application/json;odata=nometadata' ) {
425
442
correctRequestIssued = true ;
426
443
}
427
444
} ) ;
428
445
assert ( correctRequestIssued ) ;
429
446
} ) ;
430
447
431
- it ( 'removes email by username successfully without prompting with confirmation argument' , async ( ) => {
448
+ it ( 'removes user by email successfully without prompting with confirmation argument' , async ( ) => {
432
449
let removeRequestIssued = false ;
433
-
434
- sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
435
- if ( ( opts . url as string ) . indexOf ( `_api/web/siteusers/GetByEmail('${ formatting . encodeQueryParameter ( validEmail ) } ')` ) > - 1 ) {
436
- return {
437
- "Id" : 10 ,
438
- "IsHiddenInUI" : false ,
439
- "LoginName" : validLoginName ,
440
- "Title" : "John Doe" ,
441
- "PrincipalType" : 1 ,
442
- "Email" : validEmail ,
443
- "Expiration" : "" ,
444
- "IsEmailAuthenticationGuestUser" : false ,
445
- "IsShareByEmailGuestUser" : false ,
446
- "IsSiteAdmin" : false ,
447
- "UserId" : { "NameId" : "10010001b0c19a2" , "NameIdIssuer" : "urn:federation:microsoftonline" } ,
448
- "UserPrincipalName" : validUserName
449
- } ;
450
- }
451
- throw 'Invalid request' ;
452
- } ) ;
453
-
450
+ sinon . stub ( spo , 'getUserByEmail' ) . resolves ( userResponse ) ;
454
451
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
455
452
requests . push ( opts ) ;
456
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
453
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
457
454
removeRequestIssued = true ;
458
455
return Promise . resolve ( ) ;
459
456
}
460
- throw ' Invalid request' ;
457
+ throw ` Invalid request` ;
461
458
} ) ;
462
459
463
460
await command . action ( logger , {
@@ -474,33 +471,20 @@ describe(commands.USER_REMOVE, () => {
474
471
it ( 'removes user by username successfully without prompting with confirmation argument' , async ( ) => {
475
472
sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
476
473
requests . push ( opts ) ;
477
- if ( ( opts . url as string ) . indexOf ( ` _api/web/siteusers?$filter=UserPrincipalName eq ('${ formatting . encodeQueryParameter ( validUserName ) } ')`) > - 1 ) {
474
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers?$filter=UserPrincipalName eq ('${ formatting . encodeQueryParameter ( validUserName ) } ')`) {
478
475
return {
479
- "value" : [ {
480
- "Id" : 10 ,
481
- "IsHiddenInUI" : false ,
482
- "LoginName" : validLoginName ,
483
- "Title" : "John Doe" ,
484
- "PrincipalType" : 1 ,
485
- "Email" : validEmail ,
486
- "Expiration" : "" ,
487
- "IsEmailAuthenticationGuestUser" : false ,
488
- "IsShareByEmailGuestUser" : false ,
489
- "IsSiteAdmin" : false ,
490
- "UserId" : { "NameId" : "10010001b0c19a2" , "NameIdIssuer" : "urn:federation:microsoftonline" } ,
491
- "UserPrincipalName" : validUserName
492
- } ]
476
+ "value" : [ userResponse ]
493
477
} ;
494
478
}
495
- throw ' Invalid request' ;
479
+ throw ` Invalid request` ;
496
480
} ) ;
497
481
498
482
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
499
483
requests . push ( opts ) ;
500
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
484
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
501
485
return true ;
502
486
}
503
- throw ' Invalid request' ;
487
+ throw ` Invalid request` ;
504
488
} ) ;
505
489
506
490
await command . action ( logger , {
@@ -525,7 +509,7 @@ describe(commands.USER_REMOVE, () => {
525
509
526
510
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
527
511
requests . push ( opts ) ;
528
- if ( ( opts . url as string ) . indexOf ( `${ validWebUrl } /_api/web/siteusers/removeByLoginName('c:0o.c|federateddirectoryclaimprovider|${ validEntraGroupId } ')` ) > - 1 ) {
512
+ if ( opts . url === `${ validWebUrl } /_api/web/siteusers/removeByLoginName('c:0o.c|federateddirectoryclaimprovider|${ validEntraGroupId } ')` ) {
529
513
return true ;
530
514
}
531
515
throw 'Invalid request' ;
@@ -598,7 +582,7 @@ describe(commands.USER_REMOVE, () => {
598
582
it ( 'handles error when removing user using from web' , async ( ) => {
599
583
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
600
584
requests . push ( opts ) ;
601
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
585
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
602
586
throw 'An error has occurred' ;
603
587
}
604
588
throw 'Invalid request' ;
@@ -616,15 +600,15 @@ describe(commands.USER_REMOVE, () => {
616
600
it ( 'handles generic error when user not found when username is passed without prompting with confirmation argument' , async ( ) => {
617
601
sinon . stub ( request , 'get' ) . callsFake ( async ( opts ) => {
618
602
requests . push ( opts ) ;
619
- if ( ( opts . url as string ) . indexOf ( ` _api/web/siteusers?$filter=UserPrincipalName eq ('${ formatting . encodeQueryParameter ( validUserName ) } ')`) > - 1 ) {
603
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers?$filter=UserPrincipalName eq ('${ formatting . encodeQueryParameter ( validUserName ) } ')`) {
620
604
return { "value" : [ ] } ;
621
605
}
622
606
throw 'Invalid request' ;
623
607
} ) ;
624
608
625
609
sinon . stub ( request , 'post' ) . callsFake ( async ( opts ) => {
626
610
requests . push ( opts ) ;
627
- if ( ( opts . url as string ) . indexOf ( ' _api/web/siteusers/removebyid(10)' ) > - 1 ) {
611
+ if ( opts . url === ` ${ validWebUrl } / _api/web/siteusers/removebyid(10)` ) {
628
612
return Promise . resolve ( ) ;
629
613
}
630
614
throw 'Invalid request' ;
0 commit comments