mirror of
https://github.com/AnimeThemes/animethemes-server.git
synced 2026-07-25 16:24:35 +02:00
184 lines
5.9 KiB
PHP
184 lines
5.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Constants\Config\VideoConstants;
|
|
use App\Constants\FeatureConstants;
|
|
use App\Enums\Auth\SpecialPermission;
|
|
use App\Enums\Http\StreamingMethod;
|
|
use App\Events\Wiki\Video\VideoThrottled;
|
|
use App\Features\AllowVideoStreams;
|
|
use App\Jobs\SendDiscordNotificationJob;
|
|
use App\Models\Auth\User;
|
|
use App\Models\Wiki\Video;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Bus;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Laravel\Pennant\Feature;
|
|
use Laravel\Sanctum\Sanctum;
|
|
|
|
use function Pest\Laravel\get;
|
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
uses(WithFaker::class);
|
|
|
|
test('video streaming not allowed forbidden', function (): void {
|
|
Storage::fake(Config::get(VideoConstants::DEFAULT_DISK_QUALIFIED));
|
|
|
|
Feature::deactivate(AllowVideoStreams::class);
|
|
|
|
$video = Video::factory()->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);
|
|
});
|