【问题标题】:How to test logout while using tymon/jwt-auth in Laravel?如何在 Laravel 中使用 tymon/jwt-auth 测试注销?
【发布时间】:2019-09-28 17:42:18
【问题描述】:

我正在尝试使用 tymon/jwt-auth 包对我的 api 进行注销测试。这里我定义了 api 路由、控制器和单元测试。

api.php:

Route::group(['middleware' => 'api', 'prefix' => 'auth'], function ($router) {
    Route::post('login', 'AuthController@login');
    Route::post('logout', 'AuthController@logout');
    Route::post('refresh', 'AuthController@refresh');
    Route::post('me', 'AuthController@me');

    Route::post('me/profile', 'AuthController@profile');
});

AuthController.php:

/**
 * Log the user out (Invalidate the token).
 *
 * @return \Illuminate\Http\JsonResponse
 */
public function logout()
{
    auth()->logout();

    return response()->json(['message' => 'Successfully logged out']);
}

tests/Unit/AuthenticationTest.php:

/**
 * Test if user can login trough internal api.
 *
 * @return void
 */
public function testLogin()
{
    $response = $this->post('api/auth/login', [
        'email' => 'admin@xscriptconnect.com',
        'password' => 'password'
    ]);

    $response->assertStatus(200)
        ->assertJsonStructure(['access_token', 'token_type', 'expires_in']);

    $this->assertAuthenticated('api');
}

/**
 * Test if user can logout trough internal api.
 *
 * @return void
 */
public function testLogout()
{
    $user = User::first();
    $user = $this->actingAs($user, 'api');

    $user->post('api/auth/logout')
        ->assertStatus(200)
        ->assertJsonStructure(['message']);

    $this->assertUnauthenticatedAs($user, 'api');
}

登录测试工作正常,但是当它开始注销测试时,断言失败。它向我显示了这个错误:

There was 1 failure:

1) Tests\Unit\AuthenticationTest::testLogout
Expected status code 200 but received 500.
Failed asserting that false is true.

当我使用这种方法测试它时:

public function testLogout()
{
    $user = User::first();
    $this->actingAs($user, 'api');

    $response = auth()->logout();
    $response->assertStatus(200);
    $response->assertJsonStructure(['message']);
}

我收到了这个错误:

There was 1 error:

1) Tests\Unit\AuthenticationTest::testLogout
Tymon\JWTAuth\Exceptions\JWTException: Token could not be parsed from the request

通过此软件包测试注销的正确方法是什么?请帮忙。

【问题讨论】:

  • 需要通过前端注销用户,删除token即可。

标签: laravel unit-testing jwt-auth


【解决方案1】:

根据它的github页面中的this comment,我找到了解决这个问题的方法。我像这样更改了我的脚本,它可以工作。

/**
 * Test if user can logout trough internal api.
 *
 * @return void
 */
public function testLogout()
{
    $user = User::first();
    $token = \JWTAuth::fromUser($user);

    $this->post('api/auth/logout?token=' . $token)
        ->assertStatus(200)
        ->assertJsonStructure(['message']);

    $this->assertGuest('api');
}

如果有的话,请随时发布有关此问题的另一个答案。非常感谢。

【讨论】:

  • 这个 api 是为我的 react native 应用程序构建的,我认为我需要对其进行测试,因为应用程序内部会有一个注销按钮,并且 TTL 将是生命周期,以保持用户登录直到无论如何按下注销按钮。只是为了确保..你怎么看?这个测试真的没必要吗?
  • 工作正常,但\JWTAuth::fromUser($user); 必须替换为\Tymon\JWTAuth\Facades\JWTAuth::fromUser($user);
【解决方案2】:

在使用 actingAs() aka be() 方法时,覆盖 TestCase 中的方法 be() 以设置授权标头

use Illuminate\Contracts\Auth\Authenticatable as UserContract;

abstract class TestCase extends BaseTestCase
{
    public function be(UserContract $user, $driver = null)
    {
        $token = auth()->fromUser($user);

        return parent::be($user, $driver)->withHeader('Authorization', "Bearer {$token}");
    }
}

【讨论】:

    猜你喜欢
    • 2018-06-11
    • 2020-05-11
    • 2017-06-10
    • 2019-07-16
    • 2019-05-22
    • 2019-09-13
    • 1970-01-01
    • 2018-10-21
    • 2019-04-29
    相关资源
    最近更新 更多