I have Auth proxy on my OAuth2. With code like this below:
public function retrieveToken($username, $password, $scopes = ''){
// ...
$client = new Client();
$result = $client->post(url('/oauth/token'),[
'form_params' => [
'grant_type' => 'password',
'client_id' => "CLIENT_ID",
'client_secret' => "SECRET",
'username' => $username,
'password' => $password,
'scope' => $scopes
],
'timeout' => 5,
"http_errors" => true,
]);
// ...
}
I would like to test this method. In this case I create user within database transaction. And call my auth endpoint which use auth proxy to redirect request to OAuth2.
use DatabaseTransactions;
// ...
public function login(){
$user = factory(User::class)->create();
$response = $this->post('/login',[
"username" => $user->email,
"password" => "secret",
]);
$response
->assertSuccessful();
}
But everytime I receive 401 error as my user is not visible for OAuth2. For user existing in database it's working, but within transaction it doesn't.
Is there any solution for this issue? Or any idea why it's working like that?
Laravel has a helper for testing authentication named actingAs
:
You may also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the actingAs method:
$this->actingAs($user, 'api')
Documentation here on testing APIs and authentication.