22
33namespace Dreamonkey \CloudFrontUrlSigner \Tests ;
44
5- use Aws \CloudFront \CloudFrontClient ;
65use DateTime ;
76use DateTimeZone ;
8- use Dreamonkey \CloudFrontUrlSigner \CloudFrontUrlSigner ;
7+ use League \Uri \Components \Query ;
8+ use League \Uri \Http ;
99
1010class SignatureGenerationTest extends TestCase
1111{
12- private $ dummyPrivateKeyPath = 'dummy/path/ key.pem ' ;
12+ private $ dummyPrivateKeyPath = 'tests/dummy- key.pem ' ;
1313 private $ dummyKeyPairId = 'dummyKeyPairId ' ;
1414 private $ dummyUrl = 'http://myapp.com ' ;
1515
16- private $ mockCloudFrontClient ;
17-
1816 protected function setUp ()
1917 {
2018 parent ::setUp ();
21- $ this ->mockCloudFrontClient = $ this ->createMock (CloudFrontClient::class);
22- $ this ->mockCloudFrontClient ->method ('getSignedUrl ' )->willReturn ('dummysignedurl ' );
19+
20+ config (['cloudfront-url-signer.key_pair_id ' => $ this ->dummyKeyPairId ]);
21+ config (['cloudfront-url-signer.private_key_path ' => $ this ->dummyPrivateKeyPath ]);
2322 }
2423
2524 /** @test */
2625 public function it_registered_cloudfront_url_signer_in_the_container ()
2726 {
28- config (['cloudfront-url-signer.key_pair_id ' => $ this ->dummyKeyPairId ]);
2927 $ instance = $ this ->app ['cloudfront-url-signer ' ];
3028
3129 $ this ->assertInstanceOf (\Dreamonkey \CloudFrontUrlSigner \CloudFrontUrlSigner::class, $ instance );
@@ -38,8 +36,10 @@ public function it_registered_cloudfront_url_signer_in_the_container()
3836 */
3937 public function it_will_throw_an_exception_for_an_empty_key_pair_id ()
4038 {
39+ config (['cloudfront-url-signer.key_pair_id ' => '' ]);
40+
4141 /** @noinspection PhpUnhandledExceptionInspection */
42- new CloudFrontUrlSigner ($ this ->mockCloudFrontClient , $ this -> dummyPrivateKeyPath , '' );
42+ sign ($ this ->dummyUrl );
4343 }
4444
4545 /**
@@ -49,24 +49,22 @@ public function it_will_throw_an_exception_for_an_empty_key_pair_id()
4949 */
5050 public function it_will_throw_an_exception_for_an_empty_private_key_path ()
5151 {
52+ config (['cloudfront-url-signer.private_key_path ' => '' ]);
53+
5254 /** @noinspection PhpUnhandledExceptionInspection */
53- new CloudFrontUrlSigner ($ this ->mockCloudFrontClient , '' , $ this -> dummyKeyPairId );
55+ sign ($ this ->dummyUrl );
5456 }
5557
5658 /** @test */
5759 public function it_can_sign_an_url_that_expires_at_a_certain_time ()
5860 {
59- $ expiration = DateTime::createFromFormat ('d/m/Y H:i:s ' , '10/08/2115 18:15:44 ' ,
61+ $ expiration = DateTime::createFromFormat ('d/m/Y H:i:s ' , '10/08/2025 18:15:44 ' ,
6062 new DateTimeZone ('Europe/Brussels ' ));
6163
6264 /** @noinspection PhpUnhandledExceptionInspection */
63- $ urlSigner = (new CloudFrontUrlSigner ($ this ->mockCloudFrontClient ,
64- $ this ->dummyPrivateKeyPath , $ this ->dummyKeyPairId ));
65-
66- /** @noinspection PhpUnhandledExceptionInspection */
67- $ signedUrl = $ urlSigner ->sign ($ this ->dummyUrl , $ expiration );
65+ $ signedUrl = sign ($ this ->dummyUrl , $ expiration );
6866
69- $ this ->assertTrue ( is_string ($ signedUrl ));
67+ $ this ->assertEquals ( $ expiration -> getTimestamp (), $ this -> getSignedUrlExpirationTimestamp ($ signedUrl ));
7068 }
7169
7270 /** @test */
@@ -75,13 +73,9 @@ public function it_can_sign_an_url_that_expires_after_a_relative_amount_of_days(
7573 $ expiration = 30 ;
7674
7775 /** @noinspection PhpUnhandledExceptionInspection */
78- $ urlSigner = (new CloudFrontUrlSigner ($ this ->mockCloudFrontClient ,
79- $ this ->dummyPrivateKeyPath , $ this ->dummyKeyPairId ));
76+ $ signedUrl = sign ($ this ->dummyUrl , $ expiration );
8077
81- /** @noinspection PhpUnhandledExceptionInspection */
82- $ signedUrl = $ urlSigner ->sign ($ this ->dummyUrl , $ expiration );
83-
84- $ this ->assertTrue (is_string ($ signedUrl ));
78+ $ this ->assertLessThanOrEqual (60 , (new DateTime ())->modify ($ expiration . ' days ' )->getTimestamp () - $ this ->getSignedUrlExpirationTimestamp ($ signedUrl ));
8579 }
8680
8781 /**
@@ -93,11 +87,7 @@ public function it_does_not_allow_expiration_in_the_past_when_integer_is_given()
9387 {
9488 $ expiration = -5 ;
9589
96- /** @noinspection PhpUnhandledExceptionInspection */
97- $ urlSigner = (new CloudFrontUrlSigner ($ this ->mockCloudFrontClient ,
98- $ this ->dummyPrivateKeyPath , $ this ->dummyKeyPairId ));
99-
100- $ urlSigner ->sign ($ this ->dummyUrl , $ expiration );
90+ sign ($ this ->dummyUrl , $ expiration );
10191 }
10292
10393 /**
@@ -109,10 +99,15 @@ public function it_does_not_allow_expiration_in_the_past_when_datetime_is_given(
10999 {
110100 $ expiration = DateTime::createFromFormat ('d/m/Y H:i:s ' , '10/08/2005 18:15:44 ' );
111101
112- /** @noinspection PhpUnhandledExceptionInspection */
113- $ urlSigner = (new CloudFrontUrlSigner ($ this ->mockCloudFrontClient ,
114- $ this ->dummyPrivateKeyPath , $ this ->dummyKeyPairId ));
102+ sign ($ this ->dummyUrl , $ expiration );
103+ }
115104
116- $ urlSigner ->sign ($ this ->dummyUrl , $ expiration );
105+ /**
106+ * @param string $signedUrl
107+ * @return int
108+ */
109+ private function getSignedUrlExpirationTimestamp (string $ signedUrl ): int
110+ {
111+ return (int )(new Query (Http::createFromString ($ signedUrl )->getQuery ()))->getParam ('Expires ' );
117112 }
118113}
0 commit comments