vendredi 26 avril 2019

Error trying to test laravel/passport auth cicle

I'm issuing some problems with Laravel/Passport while trying to test the auth flow of my application... The stranger thing is that it only happens when I'm on testing env. The expects the 200 http code but instead receive a 500.

I'm using the Laravel 5.8 version, I'm also have installed Horizon and Telescope. My PHP version is 7.3.4.

My test

public function testLogin()
    {
        $user = factory(\App\Models\User::class)->create();

        $response = $this->json('post', 'v1/login', [
            'email' => $user->email,
            'password' => 'secret',
        ]);

        $response->assertStatus(Response::HTTP_OK);
    }

My AuthController

public function login(AuthRequest $request)
    {
        try {
            $credentials = $request->only(['email', 'password']);
            throw_if(!auth()->attempt($credentials), UnauthorizedAccess::class, __('auth.auth_required'));

            $accessToken = $this->handleAccessToken($credentials);
            dd($accessToken);
            return response()->json($accessToken);
        } catch (UnauthorizedAccess $ex) {
            return response()->json([
                'success' => false,
                'message' => $ex->getMessage()
            ], Response::HTTP_UNAUTHORIZED);
        }
    }

private function handleAccessToken(array $credentials)
    {
        $user = User::where(['email' => $credentials['email']])->first();
        $http = new Client();

        $response = $http->post(url('oauth/token'), [
            'form_params' => [
                'grant_type'    => 'password',
                'client_id'     => $user->client->id,
                'client_secret' => $user->client->secret,
                'username'      => $credentials['email'],
                'password'      => $credentials['password'],
                'scope'         => '*',
            ],
        ]);

        return json_decode((string) $response->getBody(), true);
    }


This is the erro that is produced:

on terminal

1) Tests\Feature\AuthTest::testLogin
Expected status code 200 but received 500.
Failed asserting that false is true.


laravel.log

[2019-04-26 09:02:53] local.ERROR: Client authentication failed {"exception":"[object] (League\\OAuth2\\Server\\Exception\\OAuthServerException(code: 4): Client authentication failed at /home/***/workspace/***/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:138)
[stacktrace]

[2019-04-26 09:02:53] testing.ERROR: Client error: `POST http://***/oauth/token` resulted in a `401 Unauthorized` response:
{"error":"invalid_client","error_description":"Client authentication failed","message":"Client authentication failed"}
 {"userId":"52b19d0d-e0c5-4924-84ae-d07700c672df","exception":"[object] (GuzzleHttp\\Exception\\ClientException(code: 401): Client error: `POST http://***/oauth/token` resulted in a `401 Unauthorized` response:
{\"error\":\"invalid_client\",\"error_description\":\"Client authentication failed\",\"message\":\"Client authentication failed\"}
 at /home/***/workspace/***/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113)
[stacktrace]

Aucun commentaire:

Enregistrer un commentaire