Files
iro2-back-lecture/tests/Feature/FileLinkWebTest.php
kosipov b36cf07635
Some checks failed
Deploy back2 / deploy (push) Failing after 46s
add autodeploy and add basic auth for file uploader
2026-04-02 19:42:04 +03:00

63 lines
1.9 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
class FileLinkWebTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Config::set('file_links.upload_basic_auth_users', [
['username' => 'uploader1', 'password' => 'password1'],
['username' => 'uploader2', 'password' => 'password2'],
]);
}
private function authHeader(string $username = 'uploader1', string $password = 'password1'): array
{
return [
'Authorization' => 'Basic ' . base64_encode($username . ':' . $password),
];
}
public function test_files_page_requires_basic_auth(): void
{
$response = $this->get('/files');
$response->assertUnauthorized();
$response->assertHeader('WWW-Authenticate', 'Basic realm="File Upload"');
}
public function test_files_page_shows_disabled_state(): void
{
Config::set('file_links.enabled', false);
$response = $this->withHeaders($this->authHeader())->get('/files');
$response->assertOk();
$response->assertSee('Функция отключена конфигом');
}
public function test_it_uploads_file_from_web_form_and_returns_temporary_link(): void
{
Storage::fake('local');
Config::set('file_links.enabled', true);
Config::set('file_links.disk', 'local');
Config::set('file_links.directory', 'temporary-links');
$response = $this->withHeaders($this->authHeader())->post('/files', [
'file' => UploadedFile::fake()->create('web-sample.txt', 1),
]);
$response->assertRedirect('/files');
$response->assertSessionHas('file_link');
$response->assertSessionHas('file_expires_at');
}
}