7
votes

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

1 Answers

18
votes

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.