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
}