2016-04-19 20 views
3

のためのLDAP Laravelの認証を嘲笑します。 /app/providers/AuthServiceProvider.phpでは、どのように私は</p> <p>認証は、箱から出して正常に動作しますLDAPに対してユーザーを認証するために<code>Ldap-connector</code><a href="https://github.com/SaschaDens/ldap-connector" rel="nofollow">package</a>を使用して、私のLaravelプロジェクトでユニットテスト

私は、たとえば、LDAPユーザアクセスを管理するために定義されたポリシーを持っている:コントローラで

public function boot(GateContract $gate) 
{ 
    $this->registerPolicies($gate); 

    $gate->define('rewards', function ($user) { 
     return ($user->getAdLDAP()->inGroup('Admin') || $user->getAdLDAP()->inGroup('Marketing')) ? true : false; 
    }); 
    // other policies 
    // ... 
} 

を私は次のようにログインしているユーザーのためのポリシーをご確認ください:

class RewardController extends Controller { 
    public function __construct($handler = null) { 
     $this->authorize('rewards'); 
    } 
    // other methods 
    // ... 
} 

すべて正常に動作し、ログインしたユーザーがMarketingまたはAdminグループを持たない場合、コントローラーは403例外をスローします。 LDAP認証ドライバが実装されているので、今

、私はLDAP認証を模擬し、試験にコントローラへのアクセスを提供する必要が私のphpunitのテストのために、それは方法ですが、そうでない政策は私にThis action is unauthorized.エラーがスローされます

私は、ユーザーとは思いませんモデルApp/Userが使用されていると私はちょうどそれが私は自分自身

Iで解決策を見つけることができたhere

答えて

1

からLaravelドキュメントから$this->actingAs($user)を行っていることができないLDAP接続パッケージを切り替えますAdldap2/Adldap2-Laravel

に私がexisting unit testsを使用してAdminグループのユーザーに自分の認証を作成しました:

<?php 

use Adldap\Auth\Guard; 
use Adldap\Connections\Manager; 
use Adldap\Connections\Provider; 
use Adldap\Contracts\Connections\ConnectionInterface; 
use Adldap\Laravel\Facades\Adldap; 
use Adldap\Models\User; 
use Adldap\Query\Builder; 
use Adldap\Schemas\Schema; 
use Adldap\Search\Factory; 
use Illuminate\Support\Facades\Auth; 
use Adldap\Models\Group; 

class TestCase extends Illuminate\Foundation\Testing\TestCase 
{ 
    public function pass_auth() { 

     $mockedProvider = $this->mock(Provider::class); 
     $mockedBuilder = $this->mock(Builder::class); 
     $mockedSearch = $this->mock(Factory::class); 
     $mockedAuth = $this->mock(Guard::class); 
     $mockedConnection = $this->mock(ConnectionInterface::class); 

     $mockedConnection->shouldReceive('isBound')->once()->andReturn(true); 

     $mockedBuilder->shouldReceive('getSchema')->once()->andReturn(Schema::get()); 
     $mockedBuilder->shouldReceive('getConnection')->once()->andReturn($mockedConnection); 

     $adUser = (new User([], $mockedBuilder))->setRawAttributes([ 
      'samaccountname' => ['jdoe'], 
      'mail'   => ['[email protected]'], 
      'cn'    => ['John Doe'], 
     ]); 

     $manager = new Manager(); 
     $manager->add('default', $mockedProvider); 

     Adldap::shouldReceive('getManager')->andReturn($manager); 

     $mockedProvider->shouldReceive('search')->once()->andReturn($mockedSearch); 
     $mockedProvider->shouldReceive('getSchema')->andReturn(Schema::get()); 
     $mockedProvider->shouldReceive('auth')->once()->andReturn($mockedAuth); 

     $mockedSearch->shouldReceive('users')->once()->andReturn($mockedSearch); 
     $mockedSearch->shouldReceive('select')->once()->andReturn($mockedBuilder); 

     $mockedBuilder->shouldReceive('whereEquals')->once()->andReturn($mockedBuilder); 
     $mockedBuilder->shouldReceive('first')->once()->andReturn($adUser); 

     $mockedAuth->shouldReceive('attempt')->once()->andReturn(true); 

     $this->assertTrue(Auth::attempt(['username' => 'jdoe', 'password' => '12345'])); 

     $mockedGroup = $this->mock(Group::class); 
     $mockedGroup->shouldReceive('getName')->once()->andReturn('Admin'); 

     $mockedBuilder->shouldReceive('newInstance')->andReturnSelf(); 
     $mockedBuilder->shouldReceive('newCollection')->andReturn([$mockedGroup]); 

    } 
} 

この部分はuserモック

$mockedGroup = $this->mock(Group::class); 
$mockedGroup->shouldReceive('getName')->once()->andReturn('Admin'); 

$mockedBuilder->shouldReceive('newInstance')->andReturnSelf(); 
$mockedBuilder->shouldReceive('newCollection')->andReturn([$mockedGroup]); 
Adminグループを追加します
関連する問題

 関連する問題