How do i unit test the auth filter in Laravel 4.1?

半城伤御伤魂 提交于 2020-01-03 08:49:10

问题


I want to write a unit test that should check if an unauthenticated user can view the user list (which he shouldnt be able to).

My routes

Route::group(array('prefix' => 'admin'), function() {
    Route::get('login', function() {
        return View::make('auth.login');
    });
    Route::post('login', function() {
        Auth::attempt( array('email' => Input::get('email'), 'password' => Input::get('password')) );
        return Redirect::intended('admin');
    });
    Route::get('logout', 'AuthController@logout');

    Route::group(array('before' => 'auth'), function() {
        Route::get('/', function() {
            return Redirect::to('admin/users');
        });
        Route::resource('users', 'UsersController');
    });
});

My test

public function testUnauthenticatedUserIndexAccess() {
    $response = $this->call('GET', 'admin/users');

    $this->assertRedirectedTo('admin/login');
}

My filter

Route::filter('auth', function() {
    if (Auth::guest()) return Redirect::guest('admin/login');
});

Result

Failed asserting that Illuminate\Http\Response Object (...) is an instance of class "Illuminate\Http\RedirectResponse".

If i log the $response from the test, it shows the full user list like if an admin was logged in during testing.

If i browse to admin/users using a browser without logging in I'm redirected to login like i should, so the auth filter is indeed working.

Questions

  1. Is there something in Laravel that logs in the first user during testing for you by default? Or is Auth::guest() always false by default during testing?
  2. If so, how do i become "logged out" during unit testing? I tried $this->be(null) but got an error saying the object passed must implement UserInterface.

回答1:


Laravel filters are automatically disabled for unit tests; you need to enable them for a specific test case.

Route::enableFilters();

Or this if you're not keen on using the static facades

$this->app['router']->enableFilters();

Take a look in the unit testing documentation.

Your test should now return the correct object type allowing your test to pass.



来源:https://stackoverflow.com/questions/22580882/how-do-i-unit-test-the-auth-filter-in-laravel-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!