Protect recordings per user, seed a demo login on container start, and bind-mount the app with Vite HMR for local Compose development.
110 lines
3.3 KiB
PHP
110 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Recording;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class AuthenticationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_guests_are_redirected_from_recordings_to_login(): void
|
|
{
|
|
$this->get(route('recordings.index'))
|
|
->assertRedirect(route('login'));
|
|
}
|
|
|
|
public function test_login_page_is_shown(): void
|
|
{
|
|
$this->get(route('login'))
|
|
->assertOk()
|
|
->assertSee('Log in');
|
|
}
|
|
|
|
public function test_register_page_is_shown(): void
|
|
{
|
|
$this->get(route('register'))
|
|
->assertOk()
|
|
->assertSee('Create an account');
|
|
}
|
|
|
|
public function test_users_can_register_and_are_authenticated(): void
|
|
{
|
|
$response = $this->post(route('register.store'), [
|
|
'name' => 'Ada Lovelace',
|
|
'email' => 'ada@example.com',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
]);
|
|
|
|
$response->assertRedirect(route('recordings.index'));
|
|
$this->assertAuthenticated();
|
|
$this->assertDatabaseHas('users', ['email' => 'ada@example.com']);
|
|
}
|
|
|
|
public function test_users_can_log_in(): void
|
|
{
|
|
$user = User::factory()->create([
|
|
'email' => 'ben@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response = $this->post(route('login.store'), [
|
|
'email' => 'ben@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
$response->assertRedirect(route('recordings.index'));
|
|
$this->assertAuthenticatedAs($user);
|
|
}
|
|
|
|
public function test_first_registered_user_claims_orphaned_recordings(): void
|
|
{
|
|
Recording::query()->create([
|
|
'title' => 'Orphan',
|
|
'original_filename' => 'orphan.mp3',
|
|
'file_path' => 'recordings/orphan.mp3',
|
|
'file_size_bytes' => 10,
|
|
'transcription_status' => 'pending',
|
|
'user_id' => null,
|
|
]);
|
|
|
|
$this->post(route('register.store'), [
|
|
'name' => 'First User',
|
|
'email' => 'first@example.com',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
])->assertRedirect(route('recordings.index'));
|
|
|
|
$user = User::query()->where('email', 'first@example.com')->first();
|
|
$this->assertNotNull($user);
|
|
$this->assertSame($user->id, Recording::query()->first()->user_id);
|
|
}
|
|
|
|
public function test_second_registered_user_does_not_claim_orphans(): void
|
|
{
|
|
User::factory()->create();
|
|
|
|
Recording::query()->create([
|
|
'title' => 'Still orphan',
|
|
'original_filename' => 'still.mp3',
|
|
'file_path' => 'recordings/still.mp3',
|
|
'file_size_bytes' => 10,
|
|
'transcription_status' => 'pending',
|
|
'user_id' => null,
|
|
]);
|
|
|
|
$this->post(route('register.store'), [
|
|
'name' => 'Second User',
|
|
'email' => 'second@example.com',
|
|
'password' => 'password',
|
|
'password_confirmation' => 'password',
|
|
])->assertRedirect(route('recordings.index'));
|
|
|
|
$this->assertNull(Recording::query()->where('title', 'Still orphan')->value('user_id'));
|
|
}
|
|
}
|