|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Adldap\Laravel\Tests\Console; |
| 4 | + |
| 5 | +use Mockery as m; |
| 6 | +use Adldap\Query\Builder; |
| 7 | +use Adldap\Laravel\Facades\Resolver; |
| 8 | +use Adldap\Laravel\Tests\Models\User; |
| 9 | +use Adldap\Laravel\Tests\DatabaseTestCase; |
| 10 | +use Adldap\Models\Attributes\AccountControl; |
| 11 | + |
| 12 | +class ImportTest extends DatabaseTestCase |
| 13 | +{ |
| 14 | + public function test_importing_one_user() |
| 15 | + { |
| 16 | + $b = m::mock(Builder::class); |
| 17 | + |
| 18 | + $u = $this->makeLdapUser(); |
| 19 | + |
| 20 | + $b->shouldReceive('findOrFail')->once()->with('jdoe')->andReturn($u); |
| 21 | + |
| 22 | + Resolver::shouldReceive('query')->once()->andReturn($b) |
| 23 | + ->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail') |
| 24 | + ->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email'); |
| 25 | + |
| 26 | + $r = $this->artisan('adldap:import', ['user' => 'jdoe', '--no-interaction']); |
| 27 | + |
| 28 | + $this->assertEquals(0, $r); |
| 29 | + $this-> assertDatabaseHas( 'users', [ 'email' => '[email protected]']); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_importing_multiple_users() |
| 33 | + { |
| 34 | + $b = m::mock(Builder::class); |
| 35 | + |
| 36 | + $users = [ |
| 37 | + $this->makeLdapUser([ |
| 38 | + 'samaccountname' => ['johndoe'], |
| 39 | + 'userprincipalname' => [ '[email protected]'], |
| 40 | + |
| 41 | + 'cn' => ['John Doe'], |
| 42 | + ]), |
| 43 | + $this->makeLdapUser([ |
| 44 | + 'samaccountname' => ['janedoe'], |
| 45 | + 'userprincipalname' => [ '[email protected]'], |
| 46 | + |
| 47 | + 'cn' => ['Jane Doe'], |
| 48 | + ]) |
| 49 | + ]; |
| 50 | + |
| 51 | + $b->shouldReceive('paginate')->once()->andReturn($b) |
| 52 | + ->shouldReceive('getResults')->once()->andReturn($users); |
| 53 | + |
| 54 | + Resolver::shouldReceive('query')->once()->andReturn($b) |
| 55 | + ->shouldReceive('getLdapDiscoveryAttribute')->twice()->andReturn('mail') |
| 56 | + ->shouldReceive('getEloquentUsernameAttribute')->twice()->andReturn('email'); |
| 57 | + |
| 58 | + $r = $this->artisan('adldap:import', ['--no-interaction']); |
| 59 | + |
| 60 | + $this->assertEquals(0, $r); |
| 61 | + $this-> assertDatabaseHas( 'users', [ 'email' => '[email protected]']); |
| 62 | + $this-> assertDatabaseHas( 'users', [ 'email' => '[email protected]']); |
| 63 | + } |
| 64 | + |
| 65 | + public function test_model_will_be_restored_when_ldap_account_is_active() |
| 66 | + { |
| 67 | + $model = User::create([ |
| 68 | + |
| 69 | + 'name' => 'John Doe', |
| 70 | + 'password' => bcrypt('password'), |
| 71 | + ]); |
| 72 | + |
| 73 | + $model->delete(); |
| 74 | + |
| 75 | + $this->assertTrue($model->trashed()); |
| 76 | + |
| 77 | + $user = $this->makeLdapUser(); |
| 78 | + |
| 79 | + $ac = new AccountControl(); |
| 80 | + |
| 81 | + $ac->accountIsNormal(); |
| 82 | + |
| 83 | + $user->setUserAccountControl($ac); |
| 84 | + |
| 85 | + $this->assertTrue($user->isEnabled()); |
| 86 | + |
| 87 | + $b = m::mock(Builder::class); |
| 88 | + |
| 89 | + $b->shouldReceive('paginate')->once()->andReturn($b) |
| 90 | + ->shouldReceive('getResults')->once()->andReturn([$user]); |
| 91 | + |
| 92 | + Resolver::shouldReceive('query')->once()->andReturn($b) |
| 93 | + ->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail') |
| 94 | + ->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email'); |
| 95 | + |
| 96 | + $r = $this->artisan('adldap:import', ['--restore' => true, '--no-interaction' => true]); |
| 97 | + |
| 98 | + $this->assertEquals(0, $r); |
| 99 | + $this->assertFalse($model->fresh()->trashed()); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_model_will_be_soft_deleted_when_ldap_account_is_disabled() |
| 103 | + { |
| 104 | + $model = User::create([ |
| 105 | + |
| 106 | + 'name' => 'John Doe', |
| 107 | + 'password' => bcrypt('password'), |
| 108 | + ]); |
| 109 | + |
| 110 | + $this->assertFalse($model->trashed()); |
| 111 | + |
| 112 | + $user = $this->makeLdapUser(); |
| 113 | + |
| 114 | + $ac = new AccountControl(); |
| 115 | + |
| 116 | + $ac->accountIsDisabled(); |
| 117 | + |
| 118 | + $user->setUserAccountControl($ac); |
| 119 | + |
| 120 | + $this->assertTrue($user->isDisabled()); |
| 121 | + |
| 122 | + $b = m::mock(Builder::class); |
| 123 | + |
| 124 | + $b->shouldReceive('paginate')->once()->andReturn($b) |
| 125 | + ->shouldReceive('getResults')->once()->andReturn([$user]); |
| 126 | + |
| 127 | + Resolver::shouldReceive('query')->once()->andReturn($b) |
| 128 | + ->shouldReceive('getLdapDiscoveryAttribute')->once()->andReturn('mail') |
| 129 | + ->shouldReceive('getEloquentUsernameAttribute')->once()->andReturn('email'); |
| 130 | + |
| 131 | + $r = $this->artisan('adldap:import', ['--delete' => true, '--no-interaction' => true]); |
| 132 | + |
| 133 | + $this->assertEquals(0, $r); |
| 134 | + $this->assertTrue($model->fresh()->trashed()); |
| 135 | + } |
| 136 | + |
| 137 | + public function test_filter_option_applies_to_ldap_query() |
| 138 | + { |
| 139 | + $f = '(samaccountname=jdoe)'; |
| 140 | + |
| 141 | + $b = m::mock(Builder::class); |
| 142 | + |
| 143 | + $b |
| 144 | + ->shouldReceive('rawFilter')->once()->with($f)->andReturn($b) |
| 145 | + ->shouldReceive('paginate')->once()->andReturn($b) |
| 146 | + ->shouldReceive('getResults')->once()->andReturn([]); |
| 147 | + |
| 148 | + Resolver::shouldReceive('query')->once()->andReturn($b); |
| 149 | + |
| 150 | + $r = $this->artisan('adldap:import', ['--filter' => $f, '--no-interaction' => true]); |
| 151 | + |
| 152 | + $this->assertEquals(0, $r); |
| 153 | + } |
| 154 | +} |
0 commit comments