Exception when trying to test RefreshToken
- PHP Version: 7.2
- Laravel Version: 6.0
- Laravel Passport Version: 7.2
Hi Everyone! In last days i'm been dealing with some difficults to make tests for an API that uses Laravel Passport. The trouble is when i do one of my feature tests, i have to test if i can login an user that i created before, after log this test user, i need to refresh his token, i tried to do this directly on the endpoint of my API (Using JSON tests API), but it doesn't works, when i give a "dd" on response i got "422" status code, without log, but if i try to send a request directly to OAuth route in passport (oauth/token), it returns me a exception at log, just like that:
(All the stack trace will be attached in this issue because have many lines) [2019-09-27 16:11:11] local.ERROR: Client authentication failed {"exception":"[object] (League\OAuth2\Server\Exception\OAuthServerException(code: 4): Client authentication failed at /home/vagrant/code/keeva/passport/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:138) [StackTrace]
Some Class that i using
use App\Entities\User;
use App\Services\PassportClientManager;
use App\Services\PassportSessionManager;
use GuzzleHttp\Client;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Laravel\Passport\Client as PassportClient;
use Laravel\Passport\ClientRepository as PassportClientRepository;
use Laravel\Passport\Passport;
use Tests\TestCase;
use Mockery;
This is my method:
/**
* @test Should create user and login without errors
*
* @return void
*/
public function should_login_user_and_refresh_token_without_errors()
{
/**
* @internal Setuping user
*/
Passport::actingAs($user = User::create([
'name' => 'TestUser',
'email' => '[email protected]',
'password' => bcrypt('12345678')
]));
$redirect = env('PASSPORT_REDIRECT_URL');
$client = (new PassportClientRepository)->create($user->id, $user->name, $redirect, false, true);
$credentials = json_encode(['email' => '[email protected]', 'password' => '12345678']);
/**
* @internal Authenticating user previously created
*/
$responseForAuthentication = (new Client)->post(env('APP_URL').'api/auth/login', [
'http_errors' => false,
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'body' => $credentials
]);
$statusCode = $responseForAuthentication->getStatusCode();
$this->assertTrue($statusCode === 200);
$responseForAuthenticationAsArray = (array)json_decode($responseForAuthentication->getBody()->getContents());
$refreshToken = $responseForAuthenticationAsArray['refresh_token'];
/**
* @internal Request body to AuthController endpoint
*/
// $refreshRequestBody = json_encode([
// 'refresh_token' => $refreshToken,
// 'client_id' => $client->id
// ]);
/**
* @internal Using HTTP TESTS to AuthController endpoint
*/
// $responseFromRefreshing = $this->json('POST', 'api/auth/refresh', [
// "headers" => [
// "Secret" => $client->secret
// ],
// "body" => $refreshRequestBody
// ]);
/**
* @internal Using Guzzle to Passport Implementation (OAUTH directly)
*/
$responseFromRefreshing = (new Client)->post(env('APP_URL').'oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => (string) $refreshToken,
'client_id' => (int) $client['id'],
'client_secret' => (string) $client['secret'],
'scope' => '',
],
])->getBody()->getContents();
$this->assertArrayHasKey('access_token', $responseForAuthenticationAsArray);
dd($responseFromRefreshing);
}
I don't know if it's something that I didn't do or forget, or if it's really a problem. I've researched a lot in the last few days but couldn't find a solution, the closest one I found similar to this was https://stackoverflow.com/questions/55867542/error-trying-to-test-laravel-passport-auth-cicle
I'm not using env.testing
This post come from a Issue that i openned on GitHub (https://github.com/laravel/passport/issues/1088 ), the same was closed, i dont know if i did correctly, then, i find to check it out here on Laracasts
However I appreciate it!
Best regards!
Please or to participate in this conversation.