|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Dreamonkey\CloudFrontUrlSigner\Tests; |
| 4 | + |
| 5 | +use Aws\CloudFront\CloudFrontClient; |
| 6 | +use DateTime; |
| 7 | +use DateTimeZone; |
| 8 | +use Dreamonkey\CloudFrontUrlSigner\CloudFrontUrlSigner; |
| 9 | + |
| 10 | +class SignatureGenerationTest extends TestCase |
| 11 | +{ |
| 12 | + private $dummyUrl = 'http://myapp.com'; |
| 13 | + private $dummyPrivateKeyPath = 'dummy/path/key.pem'; |
| 14 | + private $dummyKeyPairId = 'dummyKeyPairId'; |
| 15 | + |
| 16 | + private $mockCloudFrontClient; |
| 17 | + |
| 18 | + protected function setUp() |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + $this->mockCloudFrontClient = $this->createMock(CloudFrontClient::class); |
| 22 | + $this->mockCloudFrontClient->method('getSignedUrl')->willReturn('dummysignedurl'); |
| 23 | + } |
| 24 | + |
| 25 | + /** @test */ |
| 26 | + public function it_registered_cloudfront_url_signer_in_the_container() |
| 27 | + { |
| 28 | + config(['cloudfront-url-signer.private_key_path' => $this->dummyPrivateKeyPath]); |
| 29 | + $instance = $this->app['cloudfront-url-signer']; |
| 30 | + |
| 31 | + $this->assertInstanceOf(\Dreamonkey\CloudFrontUrlSigner\CloudFrontUrlSigner::class, $instance); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @test |
| 36 | + * |
| 37 | + * @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidKeyPairId |
| 38 | + */ |
| 39 | + public function it_will_throw_an_exception_for_an_empty_key_pair_id() |
| 40 | + { |
| 41 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 42 | + new CloudFrontUrlSigner($this->mockCloudFrontClient, $this->dummyPrivateKeyPath, ''); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @test |
| 47 | + * |
| 48 | + * @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidPrivateKeyPath |
| 49 | + */ |
| 50 | + public function it_will_throw_an_exception_for_an_empty_private_key_path() |
| 51 | + { |
| 52 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 53 | + new CloudFrontUrlSigner($this->mockCloudFrontClient, '', $this->dummyKeyPairId); |
| 54 | + } |
| 55 | + |
| 56 | + /** @test */ |
| 57 | + public function it_can_sign_a_signed_url_that_expires_at_a_certain_time() |
| 58 | + { |
| 59 | + $expiration = DateTime::createFromFormat('d/m/Y H:i:s', '10/08/2115 18:15:44', |
| 60 | + new DateTimeZone('Europe/Brussels')); |
| 61 | + |
| 62 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 63 | + $urlSigner = (new CloudFrontUrlSigner($this->mockCloudFrontClient, |
| 64 | + $this->dummyPrivateKeyPath, $this->dummyKeyPairId)); |
| 65 | + |
| 66 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 67 | + $signedUrl = $urlSigner->sign($this->dummyUrl, $expiration); |
| 68 | + |
| 69 | + $this->assertTrue(is_string($signedUrl)); |
| 70 | + } |
| 71 | + |
| 72 | + /** @test */ |
| 73 | + public function it_can_sign_a_signed_url_that_expires_after_a_relative_amount_of_days() |
| 74 | + { |
| 75 | + $expiration = 30; |
| 76 | + |
| 77 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 78 | + $urlSigner = (new CloudFrontUrlSigner($this->mockCloudFrontClient, |
| 79 | + $this->dummyPrivateKeyPath, $this->dummyKeyPairId)); |
| 80 | + |
| 81 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 82 | + $signedUrl = $urlSigner->sign($this->dummyUrl, $expiration); |
| 83 | + |
| 84 | + $this->assertTrue(is_string($signedUrl)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @test |
| 89 | + * |
| 90 | + * @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration |
| 91 | + */ |
| 92 | + public function it_does_not_allow_expiration_in_the_past_when_integer_is_given() |
| 93 | + { |
| 94 | + $expiration = -5; |
| 95 | + |
| 96 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 97 | + $urlSigner = (new CloudFrontUrlSigner($this->mockCloudFrontClient, |
| 98 | + $this->dummyPrivateKeyPath, $this->dummyKeyPairId)); |
| 99 | + |
| 100 | + $urlSigner->sign($this->dummyUrl, $expiration); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @test |
| 105 | + * |
| 106 | + * @expectedException \Dreamonkey\CloudFrontUrlSigner\Exceptions\InvalidExpiration |
| 107 | + */ |
| 108 | + public function it_does_not_allow_expiration_in_the_past_when_datetime_is_given() |
| 109 | + { |
| 110 | + $expiration = DateTime::createFromFormat('d/m/Y H:i:s', '10/08/2005 18:15:44'); |
| 111 | + |
| 112 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 113 | + $urlSigner = (new CloudFrontUrlSigner($this->mockCloudFrontClient, |
| 114 | + $this->dummyPrivateKeyPath, $this->dummyKeyPairId)); |
| 115 | + |
| 116 | + $urlSigner->sign($this->dummyUrl, $expiration); |
| 117 | + } |
| 118 | +} |
0 commit comments