0

I am attempting to add Pest and unit tests to a Nova Admin/Laravel application.

I want to test that a page exists, but I don't want to have to deal with whether or not the user is authenticated as an admin to view the page. I am trying to test that a page exists, regardless of whether the user is logged in, has a specific role, has a specific permission etc.

Each time I run a test that relies on a route, the page is redirected to the login page. How can I disable the redirect and bypass all role/permission/authentication-related settings to simply test the content of the page?


class ExampleTest extends TestCase
{

    public function test_it_should_have_a_login_page()
    {
        $response = $this->get('/login');
        $response->assertStatus(200);  //<-- this passes
    }

    
    public function test_it_should_have_a_routing_resources_page()
    {

        $response = $this
                ->withoutMiddleware(\App\Http\Middleware\Authenticate::class)
                ->withoutMiddleware(\App\Http\Middleware\RedirectIfAuthenticated::class)
                ->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class)
                ->get('/resources/routing');
        $response->assertStatus(200);  //<-- this fails with status of 302: the page has redirected to /login
    }
}


An additional, unsuccessful attempt:

 public function test_it_should_have_a_routing_resources_page()
    {
        $user = User::factory()->create();
        $role = NovaRole::create(['name' => 'admin', 'updated_at' => now(), 'created_at' => now(), 'slug' => 'admin']);
        $user->assignRole($role->name);
        $permissions = config('nova-permissions.permissions');
        foreach ($permissions as $permission) {
            $user->hasPermissionTo($permission);
        }

        $response = $this
                ->actingAs($user)
                ->withoutMiddleware(\App\Http\Middleware\Authenticate::class)
                ->withoutMiddleware(\App\Http\Middleware\RedirectIfAuthenticated::class)
                ->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class)
                ->get('/resources/routing');
        $response->assertStatus(200); //<-- now this fails and returns 403, forbidden
    }
Asa LeHolland
  • 372
  • 4
  • 12

1 Answers1

0

Aha!! I was trying to bypass the wrong middleware. What really helped solve it was going into nova.php and commenting out each entry in 'api_middleware' until I found the piece of middleware that was blocking the test calls.

Working code is below:


public function test_it_should_have_a_routing_resources_page()
    {
        $user = User::factory()->create();
        $permissions = config('nova-permissions.permissions');
        foreach ($permissions as $permission) {
            $user->hasPermissionTo($permission);
        }

        $response = $this
                ->actingAs($user)
                ->withoutMiddleware(Laravel\Nova\Http\Middleware\Authenticate::class) //<-- this was the issue
                ->get('/resources/routing');
        $response->assertStatus(200);
    }
Asa LeHolland
  • 372
  • 4
  • 12