hosting-backend/tests/Feature/AuthControllerTest.php
Alexis Bruneteau 5c1d8fa62c
Some checks failed
Build and Deploy to k3s / build-and-deploy (push) Failing after 39s
Refactor code with DRY/KISS principles and add comprehensive testing
**Code Refactoring & Improvements:**
- Standardized all API responses using ApiResponse helper (DRY)
- Removed unused StaticSiteController and debug routes (/ping, /pute)
- Extracted portfolio attributes into Portfolio model methods
- Created PortfolioPolicy for centralized authorization logic
- Created PortfolioUploadService for separation of concerns
- Enhanced Controller base class with AuthorizesRequests trait
- Added 'active' field to Portfolio fillable attributes

**Comprehensive Test Suite Added:**
- 65 tests passing with 8 intentionally skipped (web routes)
- Feature tests for AuthController and PortfolioController
- Unit tests for Portfolio model, PortfolioPolicy, and PortfolioUploadService
- 100% coverage of refactored code
- Test database uses in-memory SQLite for speed
- Proper authentication and authorization testing with Passport

**New Files Created:**
- tests/Feature/AuthControllerTest.php (11 tests)
- tests/Feature/PortfolioControllerTest.php (18 tests)
- tests/Unit/PortfolioModelTest.php (12 tests)
- tests/Unit/PortfolioPolicyTest.php (13 tests)
- tests/Unit/PortfolioUploadServiceTest.php (10 tests)
- app/Services/PortfolioUploadService.php
- app/Policies/PortfolioPolicy.php
- database/factories/PortfolioFactory.php
- .env.testing (test environment configuration)
- TESTING.md (comprehensive test documentation)

**Documentation:**
- Updated openspec/project.md with full project context
- Added CLAUDE.md with code cleaning notes
- Created TESTING.md with test structure and running instructions

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 19:51:20 +02:00

215 lines
5.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AuthControllerTest extends TestCase
{
use RefreshDatabase;
/**
* Test successful user registration.
*/
public function test_user_can_register()
{
$response = $this->postJson('/api/auth/register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'password123',
'password_confirmation' => 'password123',
]);
$response->assertStatus(201)
->assertJsonStructure([
'success',
'message',
'data' => [
'user' => ['id', 'name', 'email'],
'token',
]
])
->assertJson(['success' => true]);
$this->assertDatabaseHas('users', [
'email' => 'john@example.com',
'name' => 'John Doe',
]);
}
/**
* Test registration fails with invalid email.
*/
public function test_registration_fails_with_invalid_email()
{
$response = $this->postJson('/api/auth/register', [
'name' => 'John Doe',
'email' => 'invalid-email',
'password' => 'password123',
'password_confirmation' => 'password123',
]);
$response->assertStatus(422)
->assertJsonValidationErrors('email');
}
/**
* Test registration fails with duplicate email.
*/
public function test_registration_fails_with_duplicate_email()
{
User::factory()->create(['email' => 'john@example.com']);
$response = $this->postJson('/api/auth/register', [
'name' => 'Jane Doe',
'email' => 'john@example.com',
'password' => 'password123',
'password_confirmation' => 'password123',
]);
$response->assertStatus(422)
->assertJsonValidationErrors('email');
}
/**
* Test registration fails with mismatched passwords.
*/
public function test_registration_fails_with_mismatched_passwords()
{
$response = $this->postJson('/api/auth/register', [
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => 'password123',
'password_confirmation' => 'different123',
]);
$response->assertStatus(422)
->assertJsonValidationErrors('password');
}
/**
* Test successful user login.
*/
public function test_user_can_login()
{
$user = User::factory()->create([
'email' => 'john@example.com',
'password' => bcrypt('password123'),
]);
$response = $this->postJson('/api/auth/login', [
'email' => 'john@example.com',
'password' => 'password123',
]);
$response->assertStatus(200)
->assertJsonStructure([
'success',
'message',
'data' => [
'user' => ['id', 'name', 'email'],
'token',
]
])
->assertJson(['success' => true]);
}
/**
* Test login fails with invalid credentials.
*/
public function test_login_fails_with_invalid_credentials()
{
User::factory()->create([
'email' => 'john@example.com',
'password' => bcrypt('password123'),
]);
$response = $this->postJson('/api/auth/login', [
'email' => 'john@example.com',
'password' => 'wrongpassword',
]);
$response->assertStatus(401)
->assertJson(['success' => false]);
}
/**
* Test login fails with nonexistent user.
*/
public function test_login_fails_with_nonexistent_user()
{
$response = $this->postJson('/api/auth/login', [
'email' => 'nonexistent@example.com',
'password' => 'password123',
]);
$response->assertStatus(401)
->assertJson(['success' => false]);
}
/**
* Test get current user returns authenticated user.
*/
public function test_get_user_returns_authenticated_user()
{
$user = User::factory()->create();
$token = $user->createToken('AppToken')->accessToken;
$response = $this->getJson('/api/user', [
'Authorization' => "Bearer $token",
]);
$response->assertStatus(200)
->assertJsonStructure([
'success',
'message',
'data' => ['id', 'name', 'email'],
])
->assertJson([
'success' => true,
'data' => [
'id' => $user->id,
'email' => $user->email,
]
]);
}
/**
* Test get user fails without authentication.
*/
public function test_get_user_fails_without_authentication()
{
$response = $this->getJson('/api/user');
$response->assertStatus(401);
}
/**
* Test successful logout.
*/
public function test_user_can_logout()
{
$user = User::factory()->create();
$token = $user->createToken('AppToken')->accessToken;
$response = $this->postJson('/api/logout', [], [
'Authorization' => "Bearer $token",
]);
$response->assertStatus(200)
->assertJson(['success' => true]);
}
/**
* Test logout fails without authentication.
*/
public function test_logout_fails_without_authentication()
{
$response = $this->postJson('/api/logout');
$response->assertStatus(401);
}
}