Files
bagicms/tests/Feature/AuthControllerTest.php
T
2026-07-23 12:50:03 +07:00

50 lines
1.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;
class AuthControllerTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_login_and_logout_with_sanctum_token(): void
{
$user = User::factory()->create([
'username' => 'tester',
'password' => Hash::make('secret123'),
]);
$loginResponse = $this->postJson('/api/login', [
'username' => 'tester',
'password' => 'secret123',
]);
$loginResponse->assertStatus(200)
->assertJsonPath('message', 'Login successful')
->assertJsonStructure([
'message',
'user' => ['id', 'name', 'username'],
'token',
]);
$this->assertDatabaseHas('personal_access_tokens', [
'tokenable_id' => $user->id,
'tokenable_type' => User::class,
]);
$token = $loginResponse->json('token');
$logoutResponse = $this->withHeader('Authorization', 'Bearer ' . $token)
->postJson('/api/logout');
$logoutResponse->assertStatus(200)
->assertJsonPath('message', 'Logout successful');
$this->assertDatabaseCount('personal_access_tokens', 0);
}
}