start project

This commit is contained in:
2026-05-15 16:33:34 +03:30
parent 553238b0fb
commit d756661b45
50 changed files with 3523 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\RateLimiter;
use Laravel\Fortify\Features;
use Tests\TestCase;
class AuthenticationTest extends TestCase
{
use RefreshDatabase;
public function test_login_screen_can_be_rendered()
{
$response = $this->get(route('login'));
$response->assertOk();
}
public function test_users_can_authenticate_using_the_login_screen()
{
$user = User::factory()->create();
$response = $this->post(route('login.store'), [
'email' => $user->email,
'password' => 'password',
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
}
public function test_users_with_two_factor_enabled_are_redirected_to_two_factor_challenge()
{
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$user = User::factory()->withTwoFactor()->create();
$response = $this->post(route('login'), [
'email' => $user->email,
'password' => 'password',
]);
$response->assertRedirect(route('two-factor.login'));
$response->assertSessionHas('login.id', $user->id);
$this->assertGuest();
}
public function test_users_can_not_authenticate_with_invalid_password()
{
$user = User::factory()->create();
$this->post(route('login.store'), [
'email' => $user->email,
'password' => 'wrong-password',
]);
$this->assertGuest();
}
public function test_users_can_logout()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('logout'));
$response->assertRedirect(route('home'));
$this->assertGuest();
}
public function test_users_are_rate_limited()
{
$user = User::factory()->create();
RateLimiter::increment(md5('login'.implode('|', [$user->email, '127.0.0.1'])), amount: 5);
$response = $this->post(route('login.store'), [
'email' => $user->email,
'password' => 'wrong-password',
]);
$response->assertTooManyRequests();
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Laravel\Fortify\Features;
use Tests\TestCase;
class EmailVerificationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::emailVerification());
}
public function test_email_verification_screen_can_be_rendered()
{
$user = User::factory()->unverified()->create();
$response = $this->actingAs($user)->get(route('verification.notice'));
$response->assertOk();
}
public function test_email_can_be_verified()
{
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)],
);
$response = $this->actingAs($user)->get($verificationUrl);
Event::assertDispatched(Verified::class);
$this->assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
}
public function test_email_is_not_verified_with_invalid_hash()
{
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')],
);
$this->actingAs($user)->get($verificationUrl);
Event::assertNotDispatched(Verified::class);
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
public function test_email_is_not_verified_with_invalid_user_id(): void
{
$user = User::factory()->unverified()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => 123, 'hash' => sha1($user->email)],
);
$this->actingAs($user)->get($verificationUrl);
Event::assertNotDispatched(Verified::class);
$this->assertFalse($user->fresh()->hasVerifiedEmail());
}
public function test_verified_user_is_redirected_to_dashboard_from_verification_prompt(): void
{
$user = User::factory()->create();
Event::fake();
$response = $this->actingAs($user)->get(route('verification.notice'));
Event::assertNotDispatched(Verified::class);
$response->assertRedirect(route('dashboard', absolute: false));
}
public function test_already_verified_user_visiting_verification_link_is_redirected_without_firing_event_again(): void
{
$user = User::factory()->create();
Event::fake();
$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)],
);
$this->actingAs($user)->get($verificationUrl)
->assertRedirect(route('dashboard', absolute: false).'?verified=1');
Event::assertNotDispatched(Verified::class);
$this->assertTrue($user->fresh()->hasVerifiedEmail());
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class PasswordConfirmationTest extends TestCase
{
use RefreshDatabase;
public function test_confirm_password_screen_can_be_rendered()
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get(route('password.confirm'));
$response->assertOk();
$response->assertInertia(fn (Assert $page) => $page
->component('auth/confirm-password'),
);
}
public function test_password_confirmation_requires_authentication()
{
$response = $this->get(route('password.confirm'));
$response->assertRedirect(route('login'));
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Laravel\Fortify\Features;
use Tests\TestCase;
class PasswordResetTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::resetPasswords());
}
public function test_reset_password_link_screen_can_be_rendered()
{
$response = $this->get(route('password.request'));
$response->assertOk();
}
public function test_reset_password_link_can_be_requested()
{
Notification::fake();
$user = User::factory()->create();
$this->post(route('password.email'), ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class);
}
public function test_reset_password_screen_can_be_rendered()
{
Notification::fake();
$user = User::factory()->create();
$this->post(route('password.email'), ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get(route('password.reset', $notification->token));
$response->assertOk();
return true;
});
}
public function test_password_can_be_reset_with_valid_token()
{
Notification::fake();
$user = User::factory()->create();
$this->post(route('password.email'), ['email' => $user->email]);
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = $this->post(route('password.update'), [
'token' => $notification->token,
'email' => $user->email,
'password' => 'password',
'password_confirmation' => 'password',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('login'));
return true;
});
}
public function test_password_cannot_be_reset_with_invalid_token(): void
{
$user = User::factory()->create();
$response = $this->post(route('password.update'), [
'token' => 'invalid-token',
'email' => $user->email,
'password' => 'newpassword123',
'password_confirmation' => 'newpassword123',
]);
$response->assertSessionHasErrors('email');
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Tests\Feature\Auth;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Fortify\Features;
use Tests\TestCase;
class RegistrationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::registration());
}
public function test_registration_screen_can_be_rendered()
{
$response = $this->get(route('register'));
$response->assertOk();
}
public function test_new_users_can_register()
{
$response = $this->post(route('register.store'), [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
$this->assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Laravel\Fortify\Features;
use Tests\TestCase;
class TwoFactorChallengeTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
}
public function test_two_factor_challenge_redirects_to_login_when_not_authenticated(): void
{
$response = $this->get(route('two-factor.login'));
$response->assertRedirect(route('login'));
}
public function test_two_factor_challenge_can_be_rendered(): void
{
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$user = User::factory()->withTwoFactor()->create();
$this->post(route('login'), [
'email' => $user->email,
'password' => 'password',
]);
$this->get(route('two-factor.login'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('auth/two-factor-challenge'),
);
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace Tests\Feature\Auth;
use App\Models\User;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Laravel\Fortify\Features;
use Tests\TestCase;
class VerificationNotificationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::emailVerification());
}
public function test_sends_verification_notification(): void
{
Notification::fake();
$user = User::factory()->unverified()->create();
$this->actingAs($user)
->post(route('verification.send'))
->assertRedirect(route('home'));
Notification::assertSentTo($user, VerifyEmail::class);
}
public function test_does_not_send_verification_notification_if_email_is_verified(): void
{
Notification::fake();
$user = User::factory()->create();
$this->actingAs($user)
->post(route('verification.send'))
->assertRedirect(route('dashboard', absolute: false));
Notification::assertNothingSent();
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Tests\Feature;
use App\Models\Category;
use App\Models\Channel;
use App\Models\StreamKey;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CreatorChannelTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_create_one_channel_and_receives_one_time_stream_key(): void
{
$user = User::factory()->create();
$category = Category::factory()->create();
$response = $this->actingAs($user)->post(route('creator.channel.store'), [
'display_name' => 'Nyone Live',
'slug' => 'nyone-live',
'description' => 'Live builds and demos.',
'category_id' => $category->id,
]);
$response
->assertRedirect(route('dashboard', absolute: false))
->assertSessionHas('plain_stream_key');
$this->assertDatabaseHas(Channel::class, [
'user_id' => $user->id,
'slug' => 'nyone-live',
'display_name' => 'Nyone Live',
'category_id' => $category->id,
]);
$this->assertDatabaseCount(StreamKey::class, 1);
}
public function test_user_cannot_create_more_than_one_channel(): void
{
$user = User::factory()->create();
Channel::factory()->for($user)->create();
$response = $this->actingAs($user)->post(route('creator.channel.store'), [
'display_name' => 'Second Channel',
'slug' => 'second-channel',
]);
$response->assertUnprocessable();
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class DashboardTest extends TestCase
{
use RefreshDatabase;
public function test_guests_are_redirected_to_the_login_page()
{
$response = $this->get(route('dashboard'));
$response->assertRedirect(route('login'));
}
public function test_authenticated_users_can_visit_the_dashboard()
{
$user = User::factory()->create();
$this->actingAs($user);
$response = $this->get(route('dashboard'));
$response->assertOk();
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
public function test_returns_a_successful_response()
{
$response = $this->get(route('home'));
$response->assertOk();
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Tests\Feature;
use App\Models\Broadcast;
use App\Models\Channel;
use App\Services\Streaming\StreamKeyManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class MediaMtxIntegrationTest extends TestCase
{
use RefreshDatabase;
public function test_mediamtx_publish_auth_accepts_valid_stream_key(): void
{
$channel = Channel::factory()->create(['slug' => 'valid-channel']);
$plainTextKey = app(StreamKeyManager::class)->rotate($channel);
$response = $this->post('/internal/mediamtx/auth?secret=local-dev-secret', [
'action' => 'publish',
'protocol' => 'rtmp',
'path' => 'valid-channel',
'token' => $plainTextKey,
]);
$response->assertNoContent();
}
public function test_mediamtx_publish_auth_rejects_invalid_key(): void
{
Channel::factory()->create(['slug' => 'valid-channel']);
$response = $this->post('/internal/mediamtx/auth?secret=local-dev-secret', [
'action' => 'publish',
'protocol' => 'rtmp',
'path' => 'valid-channel',
'token' => 'wrong-key',
]);
$response->assertForbidden();
}
public function test_mediamtx_ready_and_not_ready_hooks_update_broadcast_state(): void
{
$channel = Channel::factory()->create(['slug' => 'demo']);
$broadcast = Broadcast::factory()->for($channel)->create([
'title' => 'Demo stream',
'recording_enabled' => true,
]);
$this->post('/internal/mediamtx/ready?secret=local-dev-secret', [
'path' => 'demo',
'source_type' => 'rtmp',
'source_id' => 'publisher-1',
])->assertNoContent();
$broadcast->refresh();
$channel->refresh();
$this->assertSame(Broadcast::STATUS_LIVE, $broadcast->status);
$this->assertTrue($channel->is_live);
$this->assertSame($broadcast->id, $channel->live_broadcast_id);
$this->assertSame('http://localhost:8888/demo/index.m3u8', $broadcast->hls_url);
$this->post('/internal/mediamtx/not-ready?secret=local-dev-secret', [
'path' => 'demo',
])->assertNoContent();
$broadcast->refresh();
$channel->refresh();
$this->assertSame(Broadcast::STATUS_ENDED, $broadcast->status);
$this->assertFalse($channel->is_live);
$this->assertNull($channel->live_broadcast_id);
}
public function test_recording_complete_creates_vod_when_recording_is_enabled(): void
{
$channel = Channel::factory()->create(['slug' => 'demo']);
Broadcast::factory()->for($channel)->live()->create([
'title' => 'Recorded stream',
'recording_enabled' => true,
'started_at' => now(),
]);
$this->post('/internal/mediamtx/recording-complete?secret=local-dev-secret', [
'path' => 'demo',
'segment_path' => storage_path('app/private/recordings/demo/segment.ts'),
'duration' => '12.4s',
])->assertNoContent();
$this->assertDatabaseHas('vods', [
'channel_id' => $channel->id,
'title' => 'Recorded stream',
'duration_seconds' => 12,
]);
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProfileUpdateTest extends TestCase
{
use RefreshDatabase;
public function test_profile_page_is_displayed()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->get(route('profile.edit'));
$response->assertOk();
}
public function test_profile_information_can_be_updated()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->patch(route('profile.update'), [
'name' => 'Test User',
'email' => 'test@example.com',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('profile.edit'));
$user->refresh();
$this->assertSame('Test User', $user->name);
$this->assertSame('test@example.com', $user->email);
$this->assertNull($user->email_verified_at);
}
public function test_email_verification_status_is_unchanged_when_the_email_address_is_unchanged()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->patch(route('profile.update'), [
'name' => 'Test User',
'email' => $user->email,
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('profile.edit'));
$this->assertNotNull($user->refresh()->email_verified_at);
}
public function test_user_can_delete_their_account()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->delete(route('profile.destroy'), [
'password' => 'password',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('home'));
$this->assertGuest();
$this->assertNull($user->fresh());
}
public function test_correct_password_must_be_provided_to_delete_account()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->from(route('profile.edit'))
->delete(route('profile.destroy'), [
'password' => 'wrong-password',
]);
$response
->assertSessionHasErrors('password')
->assertRedirect(route('profile.edit'));
$this->assertNotNull($user->fresh());
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Tests\Feature\Settings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Inertia\Testing\AssertableInertia as Assert;
use Laravel\Fortify\Features;
use Tests\TestCase;
class SecurityTest extends TestCase
{
use RefreshDatabase;
public function test_security_page_is_displayed()
{
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$user = User::factory()->create();
$this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('security.edit'))
->assertInertia(fn (Assert $page) => $page
->component('settings/security')
->where('canManageTwoFactor', true)
->where('twoFactorEnabled', false),
);
}
public function test_security_page_requires_password_confirmation_when_enabled()
{
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
$user = User::factory()->create();
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
$response = $this->actingAs($user)
->get(route('security.edit'));
$response->assertRedirect(route('password.confirm'));
}
public function test_security_page_does_not_require_password_confirmation_when_disabled()
{
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
$user = User::factory()->create();
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => false,
]);
$this->actingAs($user)
->get(route('security.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('settings/security'),
);
}
public function test_security_page_renders_without_two_factor_when_feature_is_disabled()
{
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
config(['fortify.features' => []]);
$user = User::factory()->create();
$this->actingAs($user)
->get(route('security.edit'))
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('settings/security')
->where('canManageTwoFactor', false)
->missing('twoFactorEnabled')
->missing('requiresConfirmation'),
);
}
public function test_password_can_be_updated()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->from(route('security.edit'))
->put(route('user-password.update'), [
'current_password' => 'password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('security.edit'));
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
}
public function test_correct_password_must_be_provided_to_update_password()
{
$user = User::factory()->create();
$response = $this
->actingAs($user)
->from(route('security.edit'))
->put(route('user-password.update'), [
'current_password' => 'wrong-password',
'password' => 'new-password',
'password_confirmation' => 'new-password',
]);
$response
->assertSessionHasErrors('current_password')
->assertRedirect(route('security.edit'));
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\Feature;
use App\Models\Broadcast;
use App\Models\Channel;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ViewerInteractionTest extends TestCase
{
use RefreshDatabase;
public function test_authenticated_user_can_follow_channel(): void
{
$viewer = User::factory()->create();
$channel = Channel::factory()->create(['slug' => 'demo']);
$this->actingAs($viewer)
->post(route('channels.follow', $channel))
->assertRedirect();
$this->assertDatabaseHas('follows', [
'channel_id' => $channel->id,
'user_id' => $viewer->id,
]);
}
public function test_authenticated_user_can_chat_while_channel_is_live(): void
{
$viewer = User::factory()->create();
$channel = Channel::factory()->create(['slug' => 'demo', 'is_live' => true]);
$broadcast = Broadcast::factory()->for($channel)->live()->create();
$channel->forceFill(['live_broadcast_id' => $broadcast->id])->save();
$this->actingAs($viewer)
->post(route('channels.chat.store', $channel), [
'body' => 'Hello chat',
])
->assertRedirect();
$this->assertDatabaseHas('chat_messages', [
'channel_id' => $channel->id,
'broadcast_id' => $broadcast->id,
'user_id' => $viewer->id,
'body' => 'Hello chat',
]);
}
public function test_anonymous_users_can_watch_channel_page_but_not_chat(): void
{
$channel = Channel::factory()->create(['slug' => 'demo']);
$this->get(route('channels.show', $channel))->assertOk();
$this->post(route('channels.chat.store', $channel), [
'body' => 'No auth',
])->assertRedirect(route('login'));
}
}