如何在 Laravel 中使用 tymon/jwt-auth 时测试注销?
How to test logout while using tymon/jwt-auth in Laravel?
我正在尝试使用 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
通过这个包测试注销的正确方法是什么?请帮忙
根据 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');
}
请随时post 关于此问题的另一个答案(如果有的话)。非常感谢。
重写 TestCase
中的方法 be()
以在使用 actingAs()
又名 be()
方法时设置授权 header
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}");
}
}
我正在尝试使用 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
通过这个包测试注销的正确方法是什么?请帮忙
根据 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');
}
请随时post 关于此问题的另一个答案(如果有的话)。非常感谢。
重写 TestCase
中的方法 be()
以在使用 actingAs()
又名 be()
方法时设置授权 header
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}");
}
}