Test redirects after login by roles with Laravel

Ariel Mejia

September 15th, 2020 - 1 min read

Common scenario

Users with different roles should be redirected to different dashboards after login

For the sake of simplicity the example is using user model property role as simple string column, but you can add as many complexity as you need

use RefreshDatabase;
/** @test */
public function test_app_admin_can_login_and_go_to_admin_dashboard()
{
    $user = User::factory()->create([
        'role' => 'admin'
    ]);
    
    session()->setPreviousUrl('/login');
    
    $response = $this->post('/login', [
        'email' => $user->email,
        'password' => 'password',
    ]);
    
    $response->assertLocation(route('admin.dashboard')); // admin/dashboard
}

Code by steps

  • First create a user model by factory
  • Then add a session to establish a current url
  • Then make the request to log in with data (the default password in factories is password)
  • finally assert the current location, after login with any route as you need, if it's a named route probably it is better

Thanks for reading!


Ariel Mejia Illustration

Ariel Mejia

Laravel Senior Developer

Engineer with 7+ years of experience working in backend & frontend technologies
Open Source Maintainer of packages for Laravel community.

Stay up with Laravel

Sign up & get tips and tricks

You'll get monthly updates regarding my most recent articles and products.