File size: 3,689 Bytes
6773345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?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='));
    }
}