[英]Laravel running tests as different users
I am using Laravel 5.1 and I am trying to test my controllers. 我正在使用Laravel 5.1,并且正在尝试测试我的控制器。
I have several roles for my users and policies defined for different actions. 我为用户定义了多个角色,并为不同的操作定义了策略。 Firstly, each of the requests needs to be made by an authenticated user, so running a test with no user returns a 401 Unauthorized, as expected.
首先,每个请求都需要由经过身份验证的用户发出,因此,如预期的那样,在没有用户的情况下运行测试会返回401未经授权。
But when I want to test the functionality for authorized users, I still get the 401 Unauthorized status code. 但是,当我想为授权用户测试功能时,我仍然会得到401未经授权状态代码。
It may be worth mentioning that I use basic stateless HTTP authentication on these controllers. 值得一提的是,我在这些控制器上使用了基本的无状态HTTP身份验证。
I have tried the following: 我尝试了以下方法:
public function testViewAllUsersAsAdmin()
{
$user = UserRepositoryTest::createTestAdmin();
Auth::login($user);
$response = $this->call('GET', route('users.index'));
$this->assertEquals($response->getStatusCode(), Response::HTTP_OK);
}
and 和
public function testViewAllUsersAsAdmin()
{
$user = UserRepositoryTest::createTestAdmin();
$response = $this->actingAs($user)
->call('GET', route('users.index'));
$this->assertEquals($response->getStatusCode(), Response::HTTP_OK);
}
and also this (in case there was anything wrong with my new user, which there shouldn't be) 还有这个(以防我的新用户有任何问题,应该不会的)
public function testViewAllUsersAsAdmin()
{
$user = User::find(1);
$response = $this->actingAs($user)
->call('GET', route('users.index'));
$this->assertEquals($response->getStatusCode(), Response::HTTP_OK);
}
but in every case I get a 401 response code so my tests fail. 但是在每种情况下,我都会收到401响应代码,因此我的测试失败。
I can access the routes fine using postman when logging in as a dummy user. 以虚拟用户身份登录时,我可以使用邮递员访问路由。
I am running out of ideas, so any help would be appreciated. 我的想法不多了,因此不胜感激。
You need to add Session::start()
in the setUp
function or in the beginning of the function which user need to log in. 您需要在
setUp
函数或用户需要登录的函数开头添加Session::start()
。
public function setUp()
{
parent::setUp();
Session::start();
}
or 要么
public function testViewAllUsersAsAdmin()
{
Session::start();
$user = UserRepositoryTest::createTestAdmin();
Auth::login($user);
$response = $this->call('GET', route('users.index'));
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
Through some experimentation, I found that the problem lay inside my authentication middleware. 通过一些实验,我发现问题出在我的身份验证中间件之内。 Since I want the API to be stateless, the authentication looks like this:
由于我希望API是无状态的,因此身份验证如下所示:
public function handle($request, Closure $next)
{
return Auth::onceBasic() ?: $next($request);
}
And apparently, it's not possible to authenticate a user the way I was doing it. 显然,不可能像我那样对用户进行身份验证。
My solution was simply to disable the middleware, using the WithoutMiddleware
trait or $this->withoutMiddleware()
at the beginning of each test. 我的解决方案只是在每次测试开始时使用
WithoutMiddleware
特性或$this->withoutMiddleware()
来禁用中间件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.