mdn-backend / tests /Unit /SignedFileUrlFactoryTest.php
internationalscholarsprogram's picture
feat(backend): replace backend with peter branch + Emergency Care integration
6773345
Raw
History Blame Contribute Delete
3.69 kB
<?php
namespace Tests\Unit;
use App\Domain\Files\SignedFileUrlFactory;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;
class SignedFileUrlFactoryTest extends TestCase
{
public function test_signed_file_url_uses_public_api_base_path_and_single_signature(): void
{
config([
'app.key' => 'test-signing-key',
'app.public_api_base_url' => 'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1',
]);
$expiresAt = Carbon::createFromTimestamp(1782917487);
$payload = app(SignedFileUrlFactory::class)->temporaryFileDownloadUrl('applicant.files.download', 'file-123', $expiresAt);
$url = $payload['url'];
$this->assertStringStartsWith(
'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1/applicant/files/file-123/download?',
$url,
);
$this->assertSame(1, substr_count($url, 'signature='));
$parts = parse_url($url);
parse_str($parts['query'] ?? '', $query);
$unsignedQuery = http_build_query(['expires' => $query['expires']], '', '&', PHP_QUERY_RFC3986);
$expectedSignature = hash_hmac('sha256', '/api/v1/applicant/files/file-123/download?'.$unsignedQuery, 'test-signing-key');
$this->assertSame((string) $expiresAt->getTimestamp(), (string) $query['expires']);
$this->assertSame($expectedSignature, $query['signature']);
}
public function test_signed_file_url_infers_cpanel_subdirectory_from_current_request_when_public_base_is_missing(): void
{
config([
'app.key' => 'test-signing-key',
'app.url' => 'https://clientportaldev.qhtestingserver.com',
'app.public_api_base_url' => null,
'platform.backend_api_base_url' => null,
]);
$this->assertNull(config('platform.backend_api_base_url'));
$request = Request::create(
'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1/review/applications/app-123',
'GET'
);
app()->instance('request', $request);
URL::setRequest($request);
$expiresAt = Carbon::createFromTimestamp(1783156761);
$payload = app(SignedFileUrlFactory::class)->temporaryFileDownloadUrl(
'applicant.files.download',
'019f2c47-e00b-731e-8ff5-121c1b9e06c3',
$expiresAt,
);
$url = $payload['url'];
$this->assertStringStartsWith(
'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1/applicant/files/019f2c47-e00b-731e-8ff5-121c1b9e06c3/download?',
$url,
);
$this->assertStringNotContainsString('https://clientportaldev.qhtestingserver.com/api/v1/applicant/files', $url);
$this->assertSame(1, substr_count($url, 'signature='));
}
public function test_signed_file_stream_url_uses_stream_route(): void
{
config([
'app.key' => 'test-signing-key',
'app.public_api_base_url' => 'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1',
]);
$expiresAt = Carbon::createFromTimestamp(1782917487);
$payload = app(SignedFileUrlFactory::class)->temporaryFileStreamUrl('applicant.files.stream', 'file-123', $expiresAt);
$url = $payload['url'];
$this->assertStringStartsWith(
'https://clientportaldev.qhtestingserver.com/diaspora_nexus_backend/api/v1/applicant/files/file-123/stream?',
$url,
);
$this->assertSame(1, substr_count($url, 'signature='));
}
}