aiblog/tests/Feature/Auth/LoginTest.php
2024-11-17 11:30:01 +08:00

74 lines
No EOL
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class LoginTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_login_with_email(): void
{
$user = User::factory()->create([
'email' => 'test@example.com',
'password' => bcrypt('password'),
]);
$response = $this->postJson('/api/auth/login', [
'login' => 'test@example.com',
'password' => 'password',
]);
$response->assertStatus(200)
->assertJsonStructure([
'token',
'user' => [
'id',
'name',
'email',
'role',
],
]);
}
public function test_user_can_login_with_account(): void
{
$user = User::factory()->create([
'account' => 'testuser',
'password' => bcrypt('password'),
]);
$response = $this->postJson('/api/auth/login', [
'login' => 'testuser',
'password' => 'password',
]);
$response->assertStatus(200)
->assertJsonStructure([
'token',
'user' => [
'id',
'name',
'email',
'role',
],
]);
}
public function test_user_cannot_login_with_incorrect_credentials(): void
{
$user = User::factory()->create();
$response = $this->postJson('/api/auth/login', [
'login' => $user->email,
'password' => 'wrong-password',
]);
$response->assertStatus(422);
}
}