quiniela-sembradores-backend/tests/Feature/Api/AuthTest.php

80 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2026-03-19 18:18:18 -06:00
<?php
namespace Tests\Feature\Api;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_register_and_receive_a_token(): void
{
$response = $this->postJson('/api/auth/register', [
'first_name' => 'Hector',
'last_name' => 'Vio',
'email' => 'hector@example.com',
'phone' => '8112345678',
'password' => 'password123',
'device_name' => 'react-web',
]);
$response
->assertCreated()
->assertJsonStructure([
'message',
'token',
'token_type',
'user' => ['id', 'first_name', 'last_name', 'name', 'email', 'phone'],
]);
$this->assertDatabaseHas('users', [
'first_name' => 'Hector',
'last_name' => 'Vio',
'email' => 'hector@example.com',
'phone' => '8112345678',
]);
}
public function test_user_can_login_and_receive_a_token(): void
{
$user = User::factory()->create([
'email' => 'hector@example.com',
'phone' => '8112345678',
'password' => 'password123',
]);
$response = $this->postJson('/api/auth/login', [
'email' => $user->email,
'password' => 'password123',
'device_name' => 'react-web',
]);
$response
->assertOk()
->assertJsonStructure([
'message',
'token',
'token_type',
'user' => ['id', 'first_name', 'last_name', 'name', 'email', 'phone'],
]);
}
public function test_authenticated_user_can_logout_and_revoke_current_token(): void
{
$user = User::factory()->create();
$token = $user->createToken('react-web');
$response = $this->withToken($token->plainTextToken)
->postJson('/api/auth/logout');
$response->assertOk()->assertJson([
'message' => 'Logout successful.',
]);
$this->assertDatabaseCount('personal_access_tokens', 0);
}
}