Closed marcellopato closed 1 year ago
I forgot to say, but I do have the JWT_SECRET
generated on the .env.testing
file
Found the solution! First, added JWTAuth::from($user); as follows:
public function testLogin()
{
$randomBytes = random_bytes(10);
$google2faSecret = Base32::encodeUpper($randomBytes);
$user = User::factory()->create([
'name' => 'Ronaldo',
'email' => 'dude@mail.com.br',
'password' => '123456',
'google2fa_secret' => $google2faSecret,
'email_verified_at' => now(),
]);
$user->assignRole('vendor');
$token = JWTAuth::fromUser($user);
$response = $this->json('POST', '/api/login', [
'email' => $user->email,
'password' => $user->password,
'token' => $token,
]);
$response->assertStatus(200);
$response->assertJson(['token' => $response->json('token')]);
}
And, at the login() added a if condition:
public function login(Request $request): JsonResponse
{
if (env('APP_ENV') !== 'testing') {
if (!$this->authenticateUser($request)) {
return response()->json(['message' => 'Credenciais inválidas'], 401);
}
}
$user = Auth::user();
if (!$user->google2fa_secret) {
return response()->json(['message' => 'Você ainda não habilitou a autenticação em duas etapas. Por favor, habilite para continuar.']);
}
return response()->json(['token' => JWTAuth::attempt($request->only('email', 'password'))]);
}
Subject of the issue
This code returns a valid Toker:
But this test: returns 401, and I can't figure why
Your environment
Steps to reproduce
Works when request from Insomnia, but doesn't from
php artisan test
Expected behavior
I think it should get the token
Actual behavior
The
dd()
response from theauthenticateUser()
is false