Пользователи с правами администратора могут перейти в раздел /codes сайта.
Обычные пользователи перенаправляются (302) обратно на панель /dashboard и получают сообщение «Извините, вам не разрешено туда», когда они переходят в /qr.
Ручное тестирование проходит успешно, но тестирование laravel завершается неудачей.
Я использую laravel 5.1
Тест для администратора:
Код: Выделить всё
public function testAdminViewCodes()
{
//create an admin user
$user = factory(App\User::class, 'admin')->create();
$this->actingAs($user)
->visit('/codes')
->seePageIs('/codes')
->see('Codes');
}
Код: Выделить всё
public function testNormalViewCodesFail()
{
//create a normal user
$normal_user = factory(App\User::class)->create();
//TODO: Fix this failing test FFS
$this->actingAs($normal_user)
->visit('/qr')
->seePageIs('/dashboard')
->see('Sorry you are not allowed there');
}
Код: Выделить всё
There was 1 failure:
1) AdminTest::testNormalViewQRCodesFail
Did not land on expected page [http://localhost/dashboard].
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/dashboard'
+'http://localhost/codes'
Код: Выделить всё
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'email' => $faker->email,
'password' => bcrypt(str_random(10)),
'remember_token' => str_random(10),
'is_admin' => false,
];
});
$factory->defineAs(App\User::class, 'admin', function ($faker) use ($factory) {
$user = $factory->raw(App\User::class);
return array_merge($user, ['is_admin' => true]);
});
Код: Выделить всё
Подробнее здесь: [url]https://stackoverflow.com/questions/38812534/how-to-test-an-authorization-redirect-with-laravel[/url]
Мобильная версия