+ config()->set('services.ldap.user_filter', '(&(username=${user}))');
+ $this->mockUser->name = 'barryldapuser';
+ $expectedFilter = '(&(username=\62\61\72\72\79\6c\64\61\70\75\73\65\72))';
+
+ $this->commonLdapMocks(1, 1, 1, 1, 1);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')
+ ->once()
+ ->with($this->resourceId, config('services.ldap.base_dn'), $expectedFilter, \Mockery::type('array'))
+ ->andReturn(['count' => 0, 0 => []]);
+
+ $resp = $this->mockUserLogin();
+ $resp->assertRedirect('/login');
+ }
+
+ public function test_initial_incorrect_credentials()
+ {
+ $this->commonLdapMocks(1, 1, 1, 0, 1);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)
+ ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
+ ->andReturn(['count' => 1, 0 => [
+ 'uid' => [$this->mockUser->name],
+ 'cn' => [$this->mockUser->name],
+ 'dn' => 'dc=test' . config('services.ldap.base_dn'),
+ ]]);
+ $this->mockLdap->shouldReceive('bind')->times(2)->andReturn(true, false);
+
+ $resp = $this->mockUserLogin();
+ $resp->assertRedirect('/login');
+ $this->followRedirects($resp)->assertSee('These credentials do not match our records.');
+ $this->assertDatabaseMissing('users', ['external_auth_id' => $this->mockUser->name]);
+ }
+
+ public function test_login_not_found_username()
+ {
+ $this->commonLdapMocks(1, 1, 1, 1, 1);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')->times(1)
+ ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
+ ->andReturn(['count' => 0]);
+
+ $resp = $this->mockUserLogin();
+ $resp->assertRedirect('/login');
+ $this->followRedirects($resp)->assertSee('These credentials do not match our records.');
+ $this->assertDatabaseMissing('users', ['external_auth_id' => $this->mockUser->name]);
+ }
+
+ public function test_create_user_form()
+ {
+ $userForm = $this->asAdmin()->get('/settings/users/create');
+ $userForm->assertDontSee('Password');
+
+ $save = $this->post('/settings/users/create', [
+ 'name' => $this->mockUser->name,
+ 'email' => $this->mockUser->email,
+ ]);
+ $save->assertSessionHasErrors(['external_auth_id' => 'The external auth id field is required.']);
+
+ $save = $this->post('/settings/users/create', [
+ 'name' => $this->mockUser->name,
+ 'email' => $this->mockUser->email,
+ 'external_auth_id' => $this->mockUser->name,
+ ]);
+ $save->assertRedirect('/settings/users');
+ $this->assertDatabaseHas('users', ['email' => $this->mockUser->email, 'external_auth_id' => $this->mockUser->name, 'email_confirmed' => true]);
+ }
+
+ public function test_user_edit_form()
+ {
+ $editUser = $this->users->viewer();
+ $editPage = $this->asAdmin()->get("/settings/users/{$editUser->id}");
+ $editPage->assertSee('Edit User');
+ $editPage->assertDontSee('Password');
+
+ $update = $this->put("/settings/users/{$editUser->id}", [
+ 'name' => $editUser->name,
+ 'email' => $editUser->email,
+ 'external_auth_id' => 'test_auth_id',
+ ]);
+ $update->assertRedirect('/settings/users');
+ $this->assertDatabaseHas('users', ['email' => $editUser->email, 'external_auth_id' => 'test_auth_id']);
+ }
+
+ public function test_registration_disabled()
+ {
+ $resp = $this->followingRedirects()->get('/register');
+ $this->withHtml($resp)->assertElementContains('#content', 'Log In');
+ }
+
+ public function test_non_admins_cannot_change_auth_id()
+ {
+ $testUser = $this->users->viewer();
+ $this->actingAs($testUser)
+ ->get('/settings/users/' . $testUser->id)
+ ->assertDontSee('External Authentication');
+ }
+
+ public function test_login_maps_roles_and_retains_existing_roles()
+ {
+ $roleToReceive = Role::factory()->create(['display_name' => 'LdapTester']);
+ $roleToReceive2 = Role::factory()->create(['display_name' => 'LdapTester Second']);
+ $existingRole = Role::factory()->create(['display_name' => 'ldaptester-existing']);
+ $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save();
+ $this->mockUser->attachRole($existingRole);
+
+ app('config')->set([
+ 'services.ldap.user_to_groups' => true,
+ 'services.ldap.group_attribute' => 'memberOf',
+ 'services.ldap.remove_from_groups' => false,
+ ]);
+
+ $this->commonLdapMocks(1, 1, 4, 5, 2, 2, 2);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2)
+ ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
+ ->andReturn(['count' => 1, 0 => [
+ 'uid' => [$this->mockUser->name],
+ 'cn' => [$this->mockUser->name],
+ 'dn' => 'dc=test' . config('services.ldap.base_dn'),
+ 'mail' => [$this->mockUser->email],
+ 'memberof' => [
+ 'count' => 2,
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ 1 => 'cn=ldaptester-second,ou=groups,dc=example,dc=com',
+ ],
+ ]]);
+
+ $this->mockUserLogin()->assertRedirect('/');
+
+ $user = User::where('email', $this->mockUser->email)->first();
+ $this->assertDatabaseHas('role_user', [
+ 'user_id' => $user->id,
+ 'role_id' => $roleToReceive->id,
+ ]);
+ $this->assertDatabaseHas('role_user', [
+ 'user_id' => $user->id,
+ 'role_id' => $roleToReceive2->id,
+ ]);
+ $this->assertDatabaseHas('role_user', [
+ 'user_id' => $user->id,
+ 'role_id' => $existingRole->id,
+ ]);
+ }
+
+ public function test_login_maps_roles_and_removes_old_roles_if_set()
+ {
+ $roleToReceive = Role::factory()->create(['display_name' => 'LdapTester']);
+ $existingRole = Role::factory()->create(['display_name' => 'ldaptester-existing']);
+ $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save();
+ $this->mockUser->attachRole($existingRole);
+
+ app('config')->set([
+ 'services.ldap.user_to_groups' => true,
+ 'services.ldap.group_attribute' => 'memberOf',
+ 'services.ldap.remove_from_groups' => true,
+ ]);
+
+ $this->commonLdapMocks(1, 1, 3, 4, 2, 1, 1);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2)
+ ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
+ ->andReturn(['count' => 1, 0 => [
+ 'uid' => [$this->mockUser->name],
+ 'cn' => [$this->mockUser->name],
+ 'dn' => 'dc=test' . config('services.ldap.base_dn'),
+ 'mail' => [$this->mockUser->email],
+ 'memberof' => [
+ 'count' => 1,
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ ],
+ ]]);
+
+ $this->mockUserLogin()->assertRedirect('/');
+
+ $user = User::query()->where('email', $this->mockUser->email)->first();
+ $this->assertDatabaseHas('role_user', [
+ 'user_id' => $user->id,
+ 'role_id' => $roleToReceive->id,
+ ]);
+ $this->assertDatabaseMissing('role_user', [
+ 'user_id' => $user->id,
+ 'role_id' => $existingRole->id,
+ ]);
+ }
+
+ public function test_dump_user_groups_shows_group_related_details_as_json()
+ {
+ app('config')->set([
+ 'services.ldap.user_to_groups' => true,
+ 'services.ldap.group_attribute' => 'memberOf',
+ 'services.ldap.remove_from_groups' => true,
+ 'services.ldap.dump_user_groups' => true,
+ ]);
+
+ $userResp = ['count' => 1, 0 => [
+ 'uid' => [$this->mockUser->name],
+ 'cn' => [$this->mockUser->name],
+ 'dn' => 'dc=test,' . config('services.ldap.base_dn'),
+ 'mail' => [$this->mockUser->email],
+ ]];
+ $this->commonLdapMocks(1, 1, 4, 5, 2, 2, 0);
+ $this->mockLdap->shouldReceive('searchAndGetEntries')->times(2)
+ ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array'))
+ ->andReturn($userResp, ['count' => 1,
+ 0 => [
+ 'dn' => 'dc=test,' . config('services.ldap.base_dn'),
+ 'memberof' => [
+ 'count' => 1,
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ ],
+ ],
+ ]);
+
+ $this->mockLdap->shouldReceive('read')->times(2);
+ $this->mockLdap->shouldReceive('getEntries')->times(2)
+ ->andReturn([
+ 'count' => 1,
+ 0 => [
+ 'dn' => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ 'memberof' => [
+ 'count' => 1,
+ 0 => 'cn=monsters,ou=groups,dc=example,dc=com',
+ ],
+ ],
+ ], ['count' => 0]);
+
+ $resp = $this->mockUserLogin();
+ $resp->assertJson([
+ 'details_from_ldap' => [
+ 'dn' => 'dc=test,' . config('services.ldap.base_dn'),
+ 'memberof' => [
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ 'count' => 1,
+ ],
+ ],
+ 'parsed_direct_user_groups' => [
+ 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ ],
+ 'parsed_recursive_user_groups' => [
+ 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ 'cn=monsters,ou=groups,dc=example,dc=com',
+ ],
+ 'parsed_resulting_group_names' => [
+ 'ldaptester',
+ 'monsters',
+ ],
+ ]);
+ }
+
+ public function test_recursive_group_search_queries_via_full_dn()
+ {
+ app('config')->set([
+ 'services.ldap.user_to_groups' => true,
+ 'services.ldap.group_attribute' => 'memberOf',
+ ]);
+
+ $userResp = ['count' => 1, 0 => [
+ 'uid' => [$this->mockUser->name],
+ 'cn' => [$this->mockUser->name],
+ 'dn' => 'dc=test,' . config('services.ldap.base_dn'),
+ 'mail' => [$this->mockUser->email],
+ ]];
+ $groupResp = ['count' => 1,
+ 0 => [
+ 'dn' => 'dc=test,' . config('services.ldap.base_dn'),
+ 'memberof' => [
+ 'count' => 1,
+ 0 => 'cn=ldaptester,ou=groups,dc=example,dc=com',
+ ],
+ ],
+ ];
+
+ $this->commonLdapMocks(1, 1, 3, 4, 2, 1);
+
+ $escapedName = ldap_escape($this->mockUser->name);