2025-12-25 06:33:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2025-12-25 06:33:21 +01:00
|
|
|
use App\Models\User;
|
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
|
|
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
describe('Registration', function (): void {
|
|
|
|
|
it('registers a new user successfully', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$response = $this->postJson('/api/v1/register', [
|
|
|
|
|
'name' => 'Test User',
|
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
|
'password' => 'password123',
|
|
|
|
|
'password_confirmation' => 'password123',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(201)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => [
|
|
|
|
|
'user' => ['id', 'name', 'email'],
|
|
|
|
|
'token',
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'User registered successfully',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
|
|
|
'email' => 'test@example.com',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails registration with invalid data', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$response = $this->postJson('/api/v1/register', [
|
|
|
|
|
'name' => '',
|
|
|
|
|
'email' => 'invalid-email',
|
|
|
|
|
'password' => 'short',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(422);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails registration with duplicate email', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
User::factory()->create(['email' => 'existing@example.com']);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/register', [
|
|
|
|
|
'name' => 'Test User',
|
|
|
|
|
'email' => 'existing@example.com',
|
|
|
|
|
'password' => 'password123',
|
|
|
|
|
'password_confirmation' => 'password123',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(422);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
describe('Login', function (): void {
|
|
|
|
|
it('logs in with valid credentials', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$user = User::factory()->create([
|
|
|
|
|
'password' => bcrypt('password123'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/login', [
|
|
|
|
|
'email' => $user->email,
|
|
|
|
|
'password' => 'password123',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => [
|
|
|
|
|
'user' => ['id', 'name', 'email'],
|
|
|
|
|
'token',
|
|
|
|
|
],
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Login successful',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails login with invalid credentials', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$user = User::factory()->create([
|
|
|
|
|
'password' => bcrypt('password123'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response = $this->postJson('/api/v1/login', [
|
|
|
|
|
'email' => $user->email,
|
|
|
|
|
'password' => 'wrongpassword',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(401)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => false,
|
|
|
|
|
'message' => 'Invalid credentials',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails login with non-existent user', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$response = $this->postJson('/api/v1/login', [
|
|
|
|
|
'email' => 'nonexistent@example.com',
|
|
|
|
|
'password' => 'password123',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(401);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
describe('Logout', function (): void {
|
|
|
|
|
it('logs out authenticated user', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$token = $user->createToken('test-token')->plainTextToken;
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
$response = $this->withHeader('Authorization', 'Bearer '.$token)
|
2025-12-25 06:33:21 +01:00
|
|
|
->postJson('/api/v1/logout');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'message' => 'Logged out successfully',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails logout without authentication', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$response = $this->postJson('/api/v1/logout');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(401);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
describe('Me', function (): void {
|
|
|
|
|
it('returns authenticated user data', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$user = User::factory()->create();
|
|
|
|
|
$token = $user->createToken('test-token')->plainTextToken;
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
$response = $this->withHeader('Authorization', 'Bearer '.$token)
|
2025-12-25 06:33:21 +01:00
|
|
|
->getJson('/api/v1/me');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(200)
|
|
|
|
|
->assertJsonStructure([
|
|
|
|
|
'success',
|
|
|
|
|
'message',
|
|
|
|
|
'data' => ['id', 'name', 'email'],
|
|
|
|
|
])
|
|
|
|
|
->assertJson([
|
|
|
|
|
'success' => true,
|
|
|
|
|
'data' => [
|
|
|
|
|
'id' => $user->id,
|
|
|
|
|
'email' => $user->email,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 20:58:32 +01:00
|
|
|
it('fails without authentication', function (): void {
|
2025-12-25 06:33:21 +01:00
|
|
|
$response = $this->getJson('/api/v1/me');
|
|
|
|
|
|
|
|
|
|
$response->assertStatus(401);
|
|
|
|
|
});
|
|
|
|
|
});
|