|
1 | 1 | <?php namespace Tests; |
| 2 | +use BookStack\Role; |
| 3 | +use BookStack\Services\Ldap; |
2 | 4 | use BookStack\User; |
| 5 | +use Mockery\MockInterface; |
3 | 6 |
|
4 | 7 | class LdapTest extends BrowserKitTest |
5 | 8 | { |
6 | 9 |
|
| 10 | + /** |
| 11 | + * @var MockInterface |
| 12 | + */ |
7 | 13 | protected $mockLdap; |
| 14 | + |
8 | 15 | protected $mockUser; |
9 | 16 | protected $resourceId = 'resource-test'; |
10 | 17 |
|
11 | 18 | public function setUp() |
12 | 19 | { |
13 | 20 | parent::setUp(); |
14 | 21 | if (!defined('LDAP_OPT_REFERRALS')) define('LDAP_OPT_REFERRALS', 1); |
15 | | - app('config')->set(['auth.method' => 'ldap', 'services.ldap.base_dn' => 'dc=ldap,dc=local', 'auth.providers.users.driver' => 'ldap']); |
16 | | - $this->mockLdap = \Mockery::mock(\BookStack\Services\Ldap::class); |
17 | | - $this->app['BookStack\Services\Ldap'] = $this->mockLdap; |
| 22 | + app('config')->set([ |
| 23 | + 'auth.method' => 'ldap', |
| 24 | + 'services.ldap.base_dn' => 'dc=ldap,dc=local', |
| 25 | + 'services.ldap.email_attribute' => 'mail', |
| 26 | + 'services.ldap.user_to_groups' => false, |
| 27 | + 'auth.providers.users.driver' => 'ldap', |
| 28 | + ]); |
| 29 | + $this->mockLdap = \Mockery::mock(Ldap::class); |
| 30 | + $this->app[Ldap::class] = $this->mockLdap; |
18 | 31 | $this->mockUser = factory(User::class)->make(); |
19 | 32 | } |
20 | 33 |
|
@@ -133,4 +146,104 @@ public function test_non_admins_cannot_change_auth_id() |
133 | 146 | ->dontSee('External Authentication'); |
134 | 147 | } |
135 | 148 |
|
| 149 | + public function test_login_maps_roles_and_retains_existsing_roles() |
| 150 | + { |
| 151 | + $roleToRecieve = factory(Role::class)->create(['name' => 'ldaptester']); |
| 152 | + $roleToRecieve2 = factory(Role::class)->create(['name' => 'ldaptester-second']); |
| 153 | + $existingRole = factory(Role::class)->create(['name' => 'ldaptester-existing']); |
| 154 | + $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save(); |
| 155 | + $this->mockUser->attachRole($existingRole); |
| 156 | + |
| 157 | + app('config')->set([ |
| 158 | + 'services.ldap.user_to_groups' => true, |
| 159 | + 'services.ldap.group_attribute' => 'memberOf', |
| 160 | + 'services.ldap.remove_from_groups' => false, |
| 161 | + ]); |
| 162 | + $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId); |
| 163 | + $this->mockLdap->shouldReceive('setVersion')->times(2); |
| 164 | + $this->mockLdap->shouldReceive('setOption')->times(5); |
| 165 | + $this->mockLdap->shouldReceive('searchAndGetEntries')->times(5) |
| 166 | + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) |
| 167 | + ->andReturn(['count' => 1, 0 => [ |
| 168 | + 'uid' => [$this->mockUser->name], |
| 169 | + 'cn' => [$this->mockUser->name], |
| 170 | + 'dn' => ['dc=test' . config('services.ldap.base_dn')], |
| 171 | + 'mail' => [$this->mockUser->email], |
| 172 | + 'memberof' => [ |
| 173 | + 'count' => 2, |
| 174 | + 0 => "cn=ldaptester,ou=groups,dc=example,dc=com", |
| 175 | + 1 => "cn=ldaptester-second,ou=groups,dc=example,dc=com", |
| 176 | + ] |
| 177 | + ]]); |
| 178 | + $this->mockLdap->shouldReceive('bind')->times(6)->andReturn(true); |
| 179 | + |
| 180 | + $this->visit('/login') |
| 181 | + ->see('Username') |
| 182 | + ->type($this->mockUser->name, '#username') |
| 183 | + ->type($this->mockUser->password, '#password') |
| 184 | + ->press('Log In') |
| 185 | + ->seePageIs('/'); |
| 186 | + |
| 187 | + $user = User::where('email', $this->mockUser->email)->first(); |
| 188 | + $this->seeInDatabase('role_user', [ |
| 189 | + 'user_id' => $user->id, |
| 190 | + 'role_id' => $roleToRecieve->id |
| 191 | + ]); |
| 192 | + $this->seeInDatabase('role_user', [ |
| 193 | + 'user_id' => $user->id, |
| 194 | + 'role_id' => $roleToRecieve2->id |
| 195 | + ]); |
| 196 | + $this->seeInDatabase('role_user', [ |
| 197 | + 'user_id' => $user->id, |
| 198 | + 'role_id' => $existingRole->id |
| 199 | + ]); |
| 200 | + } |
| 201 | + |
| 202 | + public function test_login_maps_roles_and_removes_old_roles_if_set() |
| 203 | + { |
| 204 | + $roleToRecieve = factory(Role::class)->create(['name' => 'ldaptester']); |
| 205 | + $existingRole = factory(Role::class)->create(['name' => 'ldaptester-existing']); |
| 206 | + $this->mockUser->forceFill(['external_auth_id' => $this->mockUser->name])->save(); |
| 207 | + $this->mockUser->attachRole($existingRole); |
| 208 | + |
| 209 | + app('config')->set([ |
| 210 | + 'services.ldap.user_to_groups' => true, |
| 211 | + 'services.ldap.group_attribute' => 'memberOf', |
| 212 | + 'services.ldap.remove_from_groups' => true, |
| 213 | + ]); |
| 214 | + $this->mockLdap->shouldReceive('connect')->times(2)->andReturn($this->resourceId); |
| 215 | + $this->mockLdap->shouldReceive('setVersion')->times(2); |
| 216 | + $this->mockLdap->shouldReceive('setOption')->times(4); |
| 217 | + $this->mockLdap->shouldReceive('searchAndGetEntries')->times(4) |
| 218 | + ->with($this->resourceId, config('services.ldap.base_dn'), \Mockery::type('string'), \Mockery::type('array')) |
| 219 | + ->andReturn(['count' => 1, 0 => [ |
| 220 | + 'uid' => [$this->mockUser->name], |
| 221 | + 'cn' => [$this->mockUser->name], |
| 222 | + 'dn' => ['dc=test' . config('services.ldap.base_dn')], |
| 223 | + 'mail' => [$this->mockUser->email], |
| 224 | + 'memberof' => [ |
| 225 | + 'count' => 1, |
| 226 | + 0 => "cn=ldaptester,ou=groups,dc=example,dc=com", |
| 227 | + ] |
| 228 | + ]]); |
| 229 | + $this->mockLdap->shouldReceive('bind')->times(5)->andReturn(true); |
| 230 | + |
| 231 | + $this->visit('/login') |
| 232 | + ->see('Username') |
| 233 | + ->type($this->mockUser->name, '#username') |
| 234 | + ->type($this->mockUser->password, '#password') |
| 235 | + ->press('Log In') |
| 236 | + ->seePageIs('/'); |
| 237 | + |
| 238 | + $user = User::where('email', $this->mockUser->email)->first(); |
| 239 | + $this->seeInDatabase('role_user', [ |
| 240 | + 'user_id' => $user->id, |
| 241 | + 'role_id' => $roleToRecieve->id |
| 242 | + ]); |
| 243 | + $this->dontSeeInDatabase('role_user', [ |
| 244 | + 'user_id' => $user->id, |
| 245 | + 'role_id' => $existingRole->id |
| 246 | + ]); |
| 247 | + } |
| 248 | + |
136 | 249 | } |
0 commit comments