【问题标题】:Error trying to test laravel/passport auth cicle尝试测试 laravel/passport auth cicle 时出错
【发布时间】:2019-04-26 12:21:02
【问题描述】:

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

我使用的是 Laravel 5.8 版本,我也安装了 Horizo​​n 和 Telescope。我的 PHP 版本是 7.3.4。

我的测试

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);
    }

我的 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】:

    嗯,错误似乎是在测试环境时 Guzzle 本身的问题。这是因为 Guzzle 设置为默认环境。

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

    【讨论】:

      猜你喜欢
      • 2015-03-02
      • 2014-11-10
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2019-05-09
      • 2021-02-04
      • 2022-10-03
      相关资源
      最近更新 更多