尝试测试laravel / passport auth圈时出错

问题描述 投票:0回答:1

在尝试测试我的应用程序的auth流程时,我发现了Laravel / Passport的一些问题......更奇怪的是它只发生在我测试环境时。期望200 http代码,但收到500。

我正在使用Laravel 5.8版本,我也安装了Horizo​​n和Telescope。我的PHP版本是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);
            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);
    }

这是产生的错误:

在终端上

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]
testing laravel-passport laravel-5.8
1个回答
0
投票

好吧,在测试环境时,错误似乎与Guzzle本身有关。这是因为Guzzle设置为默认环境。

然后,我按照@lawrencedawson给出的答案解决它,你可以看到here

© www.soinside.com 2019 - 2024. All rights reserved.