we use 'prefix' and for authenticity we use 'middleware' if you know what i mean
How to test a redirect for ressource route
Hello.
I'm currently implementing testing in a pretty big application.
I'm still at the start and i would like some advice on a problem :
How do you test the redirection for a group of routes.
For example, in the app, only admins can navigate to the user CRUD. The routes for the controller are always prefixed with "/admin/users/".
Do i need to test every route in the controller to be sure a user gets redirected each time ?
And then i have roles on the admin, and they are redirected too on some routes. Do i also test every routes for them ?
Thanks.
Personally, I would be as explicit as possible in tests. Specify every route and role. You can use PHPUnit's data providers to loop through multiple datasets.
The easiest way would be to test the route's response. Laravel's test suite provides you with a TestResponse instance after using the built-in get(), post(), etc. methods. The TestResponse class has a method called assertRedirect(), so a simple test could look like this:
/**
* @dataProvider routesDataProvider
*/
public function testAdminRoutesRequireAdminRole($route, $method) {
// Either don't authenticate or authenticate with a user that has insufficient permissions
$this->call($method, $route)
->assertRedirect('/access-denied');
}
public function routesDataProvider() {
return [
['/admin/page', 'GET'],
];
}
Please or to participate in this conversation.