When running a unit-test that involves sending multiple requests with different authorization credentials (ie, a single test involving multiple users), the AuthServiceProvider is only invoked for the 1st request. The user information is cached and re-used for all subsequent requests even if the credentials change.
For the example below,
Unit-test:
public function testAction()
{
// User1 performs action
$headers1 = [ 'Authorization' => 'Bearer User1' ];
$this->call( 'GET', '/test', [], [], [],
$this->transformHeadersToServerVars( $headers1 ), null );
$this->seeStatusCode( 200 );
// User2 performs action
$headers2 = [ 'Authorization' => 'Bearer User2' ];
$this->call( 'GET', '/test', [], [], [],
$this->transformHeadersToServerVars( $headers2 ), null );
$this->seeStatusCode( 200 );
}
Controller:
public function test(Request $request)
{
$token = $request->bearerToken();
$userid = $request->user();
if ($token != $userid)
abort(400, 'UserId mismatch');
}
AuthServiceProvider:
public function boot()
{
$this->app['auth']->viaRequest('api', function ($request) {
return $request->bearerToken();
});
}
When logging the results in the Controller:
1st request: Bearer Token 'User1'. UserId 'User1'
2nd request: Bearer Token 'User2'. UserId 'User1'
Tested on Lumen 5.2 and 5.4
I am fairly sure you can only make one $request to your application per test, unless you manually refresh the application between calls.
Would be better to separate each request into its own test.
See something like https://github.com/laravel/framework/issues/9369
Having separate tests adds unnecessary complexity - the developer then needs to deal with sequencing the tests and passing data between the tests.
Consider typical API test cases:
Actually - that should all be separate tests anyway:
(1) add item,
(2) retrieve list of items
(3) remove item,
Anyway - I think this is better for the forums. Or a discussion on laravel/internals if you think the behavior should be changed.
It's best to split this up into separate tests like @laurencei suggested.