@@ -76,14 +76,21 @@ protected function mockUserLogin(?string $email = null): TestResponse
76
76
/**
77
77
* Set LDAP method mocks for things we commonly call without altering.
78
78
*/
79
- protected function commonLdapMocks (int $ connects = 1 , int $ versions = 1 , int $ options = 2 , int $ binds = 4 , int $ escapes = 2 , int $ explodes = 0 )
79
+ protected function commonLdapMocks (int $ connects = 1 , int $ versions = 1 , int $ options = 2 , int $ binds = 4 , int $ escapes = 2 , int $ explodes = 0 , int $ groups = 0 )
80
80
{
81
81
$ this ->mockLdap ->shouldReceive ('connect ' )->times ($ connects )->andReturn ($ this ->resourceId );
82
82
$ this ->mockLdap ->shouldReceive ('setVersion ' )->times ($ versions );
83
83
$ this ->mockLdap ->shouldReceive ('setOption ' )->times ($ options );
84
84
$ this ->mockLdap ->shouldReceive ('bind ' )->times ($ binds )->andReturn (true );
85
85
$ this ->mockEscapes ($ escapes );
86
86
$ this ->mockExplodes ($ explodes );
87
+ $ this ->mockGroupLookups ($ groups );
88
+ }
89
+
90
+ protected function mockGroupLookups (int $ times = 1 ): void
91
+ {
92
+ $ this ->mockLdap ->shouldReceive ('read ' )->times ($ times )->andReturn (['count ' => 0 ]);
93
+ $ this ->mockLdap ->shouldReceive ('getEntries ' )->times ($ times )->andReturn (['count ' => 0 ]);
87
94
}
88
95
89
96
public function test_login ()
@@ -307,8 +314,8 @@ public function test_login_maps_roles_and_retains_existing_roles()
307
314
'services.ldap.remove_from_groups ' => false ,
308
315
]);
309
316
310
- $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 4 , 6 );
311
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (4 )
317
+ $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 2 , 2 , 2 );
318
+ $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (2 )
312
319
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), \Mockery::type ('string ' ), \Mockery::type ('array ' ))
313
320
->andReturn (['count ' => 1 , 0 => [
314
321
'uid ' => [$ this ->mockUser ->name ],
@@ -352,8 +359,8 @@ public function test_login_maps_roles_and_removes_old_roles_if_set()
352
359
'services.ldap.remove_from_groups ' => true ,
353
360
]);
354
361
355
- $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 3 , 2 );
356
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (3 )
362
+ $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 2 , 1 , 1 );
363
+ $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (2 )
357
364
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), \Mockery::type ('string ' ), \Mockery::type ('array ' ))
358
365
->andReturn (['count ' => 1 , 0 => [
359
366
'uid ' => [$ this ->mockUser ->name ],
@@ -394,22 +401,26 @@ public function test_dump_user_groups_shows_group_related_details_as_json()
394
401
'dn ' => 'dc=test, ' . config ('services.ldap.base_dn ' ),
395
402
'mail ' => [$ this ->mockUser ->email ],
396
403
]];
397
- $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 4 , 2 );
398
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (4 )
404
+ $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 2 , 2 , 0 );
405
+ $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (2 )
399
406
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), \Mockery::type ('string ' ), \Mockery::type ('array ' ))
400
407
->andReturn ($ userResp , ['count ' => 1 ,
401
- 0 => [
402
- 'dn ' => 'dc=test, ' . config ('services.ldap.base_dn ' ),
408
+ 0 => [
409
+ 'dn ' => 'dc=test, ' . config ('services.ldap.base_dn ' ),
403
410
'memberof ' => [
404
411
'count ' => 1 ,
405
- 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com ' ,
412
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com ' ,
406
413
],
407
414
],
408
- ], [
415
+ ]);
416
+
417
+ $ this ->mockLdap ->shouldReceive ('read ' )->times (2 );
418
+ $ this ->mockLdap ->shouldReceive ('getEntries ' )->times (2 )
419
+ ->andReturn ([
409
420
'count ' => 1 ,
410
- 0 => [
411
- 'dn ' => 'cn=ldaptester,ou=groups,dc=example,dc=com ' ,
412
- 'memberof ' => [
421
+ 0 => [
422
+ 'dn ' => 'cn=ldaptester,ou=groups,dc=example,dc=com ' ,
423
+ 'memberof ' => [
413
424
'count ' => 1 ,
414
425
0 => 'cn=monsters,ou=groups,dc=example,dc=com ' ,
415
426
],
@@ -426,9 +437,13 @@ public function test_dump_user_groups_shows_group_related_details_as_json()
426
437
],
427
438
],
428
439
'parsed_direct_user_groups ' => [
429
- 'ldaptester ' ,
440
+ 'cn= ldaptester,ou=groups,dc=example,dc=com ' ,
430
441
],
431
442
'parsed_recursive_user_groups ' => [
443
+ 'cn=ldaptester,ou=groups,dc=example,dc=com ' ,
444
+ 'cn=monsters,ou=groups,dc=example,dc=com ' ,
445
+ ],
446
+ 'parsed_resulting_group_names ' => [
432
447
'ldaptester ' ,
433
448
'monsters ' ,
434
449
],
@@ -458,15 +473,18 @@ public function test_recursive_group_search_queries_via_full_dn()
458
473
],
459
474
];
460
475
461
- $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 3 , 1 );
476
+ $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 2 , 1 );
462
477
463
478
$ escapedName = ldap_escape ($ this ->mockUser ->name );
464
479
$ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->twice ()
465
480
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), "(&(uid= {$ escapedName })) " , \Mockery::type ('array ' ))
466
481
->andReturn ($ userResp , $ groupResp );
467
482
468
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (1 )
469
- ->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), $ groupResp [0 ]['dn ' ], ['memberof ' ])
483
+ $ this ->mockLdap ->shouldReceive ('read ' )->times (1 )
484
+ ->with ($ this ->resourceId , 'cn=ldaptester,ou=groups,dc=example,dc=com ' , '(objectClass=*) ' , ['memberof ' ])
485
+ ->andReturn (['count ' => 0 ]);
486
+ $ this ->mockLdap ->shouldReceive ('getEntries ' )->times (1 )
487
+ ->with ($ this ->resourceId , ['count ' => 0 ])
470
488
->andReturn (['count ' => 0 ]);
471
489
472
490
$ resp = $ this ->mockUserLogin ();
@@ -491,8 +509,8 @@ public function test_login_maps_roles_using_external_auth_ids_if_set()
491
509
'services.ldap.remove_from_groups ' => true ,
492
510
]);
493
511
494
- $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 3 , 2 );
495
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (3 )
512
+ $ this ->commonLdapMocks (1 , 1 , 3 , 4 , 2 , 1 , 1 );
513
+ $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (2 )
496
514
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), \Mockery::type ('string ' ), \Mockery::type ('array ' ))
497
515
->andReturn (['count ' => 1 , 0 => [
498
516
'uid ' => [$ this ->mockUser ->name ],
@@ -532,8 +550,8 @@ public function test_login_group_mapping_does_not_conflict_with_default_role()
532
550
'services.ldap.remove_from_groups ' => true ,
533
551
]);
534
552
535
- $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 4 , 6 );
536
- $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (4 )
553
+ $ this ->commonLdapMocks (1 , 1 , 4 , 5 , 2 , 2 , 2 );
554
+ $ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )->times (2 )
537
555
->with ($ this ->resourceId , config ('services.ldap.base_dn ' ), \Mockery::type ('string ' ), \Mockery::type ('array ' ))
538
556
->andReturn (['count ' => 1 , 0 => [
539
557
'uid ' => [$ this ->mockUser ->name ],
@@ -772,9 +790,9 @@ public function test_login_with_email_confirmation_required_maps_groups_but_show
772
790
'services.ldap.remove_from_groups ' => true ,
773
791
]);
774
792
775
- $ this ->commonLdapMocks (1 , 1 , 6 , 8 , 6 , 4 );
793
+ $ this ->commonLdapMocks (1 , 1 , 6 , 8 , 4 , 2 , 2 );
776
794
$ this ->mockLdap ->shouldReceive ('searchAndGetEntries ' )
777
- ->times (6 )
795
+ ->times (4 )
778
796
->andReturn (['count ' => 1 , 0 => [
779
797
'uid ' => [$ user ->name ],
780
798
'cn ' => [$ user ->name ],
0 commit comments