1+ <?php
2+
3+ /**
4+ * SPDX-License-Identifier: MIT
5+ * Copyright (c) 2017-2018 Tobias Reich
6+ * Copyright (c) 2018-2025 LycheeOrg.
7+ */
8+
9+ /**
10+ * We don't care for unhandled exceptions in tests.
11+ * It is the nature of a test to throw an exception.
12+ * Without this suppression we had 100+ Linter warning in this file which
13+ * don't help anything.
14+ *
15+ * @noinspection PhpDocMissingThrowsInspection
16+ * @noinspection PhpUnhandledExceptionInspection
17+ */
18+
19+ namespace Tests \Feature_v2 ;
20+
21+ use App \Models \Configs ;
22+ use Illuminate \Support \Facades \URL ;
23+ use Tests \Feature_v2 \Base \BaseApiWithDataTest ;
24+
25+ class SecureImageLinksTest extends BaseApiWithDataTest
26+ {
27+ public function setUp (): void
28+ {
29+ parent ::setUp ();
30+ Configs::set ('temporary_image_link_enabled ' , '1 ' );
31+ Configs::invalidateCache ();
32+ }
33+
34+ public function tearDown (): void
35+ {
36+ Configs::set ('temporary_image_link_enabled ' , '0 ' );
37+ Configs::set ('secure_image_link_enabled ' , '0 ' );
38+ Configs::invalidateCache ();
39+ parent ::tearDown ();
40+ }
41+
42+ public function testTemporaryImage (): void
43+ {
44+ $ response = $ this ->getJsonWithData ('Album ' , ['album_id ' => $ this ->album4 ->id ]);
45+ $ this ->assertOk ($ response );
46+ $ url = $ response ->json ('resource.photos.0.size_variants.medium.url ' );
47+ $ this ->assertStringContainsString ('/image/medium/ ' , $ url );
48+
49+ $ response = $ this ->get ($ url );
50+ $ this ->assertNotFound ($ response );
51+ $ response ->assertSeeText ('File not found ' ); // We mocked the file !
52+
53+ $ unsigned_url = explode ('? ' , $ url )[0 ];
54+ $ response = $ this ->get ($ unsigned_url );
55+ $ this ->assertForbidden ($ response );
56+ }
57+
58+ public function testEncryptedImages (): void
59+ {
60+ Configs::set ('secure_image_link_enabled ' , '1 ' );
61+ Configs::invalidateCache ();
62+
63+ $ response = $ this ->getJsonWithData ('Album ' , ['album_id ' => $ this ->album4 ->id ]);
64+ $ this ->assertOk ($ response );
65+ $ url = $ response ->json ('resource.photos.0.size_variants.medium.url ' );
66+ $ this ->assertStringContainsString ('/image/ ' , $ url );
67+
68+ $ response = $ this ->get ($ url );
69+ $ this ->assertNotFound ($ response );
70+ $ response ->assertSeeText ('File not found ' ); // We mocked the file !
71+
72+ $ unsigned_url = explode ('? ' , $ url )[0 ];
73+ $ response = $ this ->get ($ unsigned_url );
74+ $ this ->assertForbidden ($ response );
75+ }
76+
77+ public function testBrokenEncryption (): void
78+ {
79+ Configs::set ('secure_image_link_enabled ' , '1 ' );
80+ Configs::invalidateCache ();
81+
82+ $ broken_url = URL ::temporarySignedRoute ('image ' , now ()->addSeconds (10 ), ['path ' => 'broken_path ' ]);
83+ $ response = $ this ->get ($ broken_url );
84+ $ this ->assertForbidden ($ response );
85+ $ response ->assertSeeText ('Invalid payload ' );
86+ }
87+ }
0 commit comments