jwt-auth を使った認証テスト #11
Sakamoto0525
started this conversation in
General
Replies: 4 comments 2 replies
-
jwt-auth の 公式ドキュメントを参考にするlogin() あたりが使えそう // Get some user from somewhere
$user = User::first();
// Get the token
$token = auth()->login($user); |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
基本的には継承元である |
Beta Was this translation helpful? Give feedback.
0 replies
-
書いてみた余計な部分は省いてます TestCase.phpabstract class TestCase extends BaseTestCase
{
public function login(User $user): void
{
if (!$token = auth('api')->login($user)) {
throw new Exception('faild to login', 500);
}
$this->withHeaders(['Authorization' => 'Bearer ' . $token]);
}
}UsersControllerTest.php /** @test */
public function ログインしていない時、401レスポンスが返ること(): void
{
$id = factory(User::class)->create()->id;
$res = $this->json('GET', "/api/users/$id");
$res->assertStatus(401);
}
/** @test */
public function ログインしている時、200レスポンスが返ること(): void
{
$user = factory(User::class)->create();
$this->login($user);
$res = $this->json('GET', "/api/users/$user->id");
$res->assertStatus(200);
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
参考資料正直v6なのであんまり役に立たなさそう
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
課題
jwt-authを使う場合、公式がセッション・認証のペルパーとして提供しているactiongAsが使えない。(Laravel v6の場合 ←
公式ドキュメント
Beta Was this translation helpful? Give feedback.
All reactions