createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertForbidden(); }); test('cannot stream soft deleted video', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); $video = Video::factory()->trashed()->createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertNotFound(); }); test('video streaming permitted for bypass', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class, fake()->boolean()); /** @var StreamingMethod $streamingMethod */ $streamingMethod = Arr::random(StreamingMethod::cases()); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, $streamingMethod->value); $video = Video::factory()->createOne(); $user = User::factory()->withPermissions(SpecialPermission::BYPASS_FEATURE_FLAGS->value)->createOne(); Sanctum::actingAs($user); $response = get(route('video.show', ['video' => $video])); $response->assertSuccessful(); }); test('invalid streaming method error', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, fake()->word()); $video = Video::factory()->createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertServerError(); }); test('streamed through response', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, StreamingMethod::RESPONSE->value); $video = Video::factory()->createOne(); $response = get(route('video.show', ['video' => $video])); $this->assertInstanceOf(StreamedResponse::class, $response->baseResponse); }); test('streamed through nginx redirect', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, StreamingMethod::NGINX->value); $video = Video::factory()->createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertHeader('X-Accel-Redirect'); }); test('not throttled', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, Arr::random(StreamingMethod::cases())->value); Config::set(VideoConstants::RATE_LIMITER_QUALIFIED, -1); $video = Video::factory()->createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertHeaderMissing('X-RateLimit-Limit'); $response->assertHeaderMissing('X-RateLimit-Remaining'); }); test('rate limited', function (): void { Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, Arr::random(StreamingMethod::cases())->value); Config::set(VideoConstants::RATE_LIMITER_QUALIFIED, fake()->randomDigitNotNull()); $video = Video::factory()->createOne(); $response = get(route('video.show', ['video' => $video])); $response->assertHeader('X-RateLimit-Limit'); $response->assertHeader('X-RateLimit-Remaining'); }); test('throttled event', function (): void { $limit = fake()->randomDigitNotNull(); Event::fake(); Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, Arr::random(StreamingMethod::cases())->value); Config::set(VideoConstants::RATE_LIMITER_QUALIFIED, $limit); $video = Video::factory()->createOne(); Collection::times($limit + 1, function () use ($video): void { get(route('video.show', ['video' => $video])); }); Event::assertDispatched(VideoThrottled::class); }); test('throttled notification', function (): void { $limit = fake()->randomDigitNotNull(); Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED)); Bus::fake(SendDiscordNotificationJob::class); Feature::activate(FeatureConstants::ALLOW_DISCORD_NOTIFICATIONS); Feature::activate(AllowVideoStreams::class); Config::set(VideoConstants::STREAMING_METHOD_QUALIFIED, Arr::random(StreamingMethod::cases())->value); Config::set(VideoConstants::RATE_LIMITER_QUALIFIED, $limit); Event::fakeExcept(VideoThrottled::class); $video = Video::factory()->createOne(); Collection::times($limit + 1, function () use ($video): void { get(route('video.show', ['video' => $video])); }); Bus::assertDispatched(SendDiscordNotificationJob::class); });