|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Shield\Mailgun\Test\Unit; |
| 4 | + |
| 5 | +use Shield\Mailgun\Mailgun; |
| 6 | +use Shield\Testing\TestCase; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use PHPUnit\Framework\Assert; |
| 9 | +use Illuminate\Support\Carbon; |
| 10 | +use Shield\Shield\Contracts\Service; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class ServiceTest |
| 14 | + * |
| 15 | + * @package \Shield\Mailgun\Test\Unit |
| 16 | + */ |
| 17 | +class ServiceTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Shield\Mailgun\Mailgun |
| 21 | + */ |
| 22 | + protected $service; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->service = new Mailgun; |
| 29 | + } |
| 30 | + |
| 31 | + /** @test */ |
| 32 | + public function it_is_a_service() |
| 33 | + { |
| 34 | + Assert::assertInstanceOf(Service::class, new Mailgun); |
| 35 | + } |
| 36 | + |
| 37 | + /** @test */ |
| 38 | + public function it_can_verify_a_valid_request() |
| 39 | + { |
| 40 | + $token = 'raNd0mk3y'; |
| 41 | + $this->app['config']['shield.services.mailgun.options.token'] = $token; |
| 42 | + |
| 43 | + // Build the signature for the request payload |
| 44 | + $timestamp = Carbon::now()->timestamp; |
| 45 | + $signature = $this->buildSignature($timestamp, $token); |
| 46 | + |
| 47 | + $request = $this->request(json_encode([ |
| 48 | + 'timestamp' => $timestamp, |
| 49 | + 'token' => $token, |
| 50 | + 'signature' => $signature, |
| 51 | + ])); |
| 52 | + |
| 53 | + $headers = [ |
| 54 | + 'Content-Type' => 'application/json' |
| 55 | + ]; |
| 56 | + |
| 57 | + $request->headers->add($headers); |
| 58 | + |
| 59 | + Assert::assertTrue($this->service->verify($request, $this->getConfig())); |
| 60 | + } |
| 61 | + |
| 62 | + /** @test */ |
| 63 | + public function it_will_not_verify_an_old_request() |
| 64 | + { |
| 65 | + $token = 'raNd0mk3y'; |
| 66 | + $tolerance = 60; |
| 67 | + |
| 68 | + $this->app['config']['shield.services.mailgun.options.token'] = $token; |
| 69 | + $this->app['config']['shield.services.mailgun.options.tolerance'] = $tolerance; |
| 70 | + |
| 71 | + // Build the signature for the request payload |
| 72 | + $timestamp = Carbon::now()->subSeconds($tolerance + 1)->timestamp; |
| 73 | + $signature = $this->buildSignature($timestamp, $token); |
| 74 | + |
| 75 | + $request = $this->request(json_encode([ |
| 76 | + 'timestamp' => $timestamp, |
| 77 | + 'token' => $token, |
| 78 | + 'signature' => $signature, |
| 79 | + ])); |
| 80 | + |
| 81 | + $headers = [ |
| 82 | + 'Content-Type' => 'application/json' |
| 83 | + ]; |
| 84 | + |
| 85 | + $request->headers->add($headers); |
| 86 | + |
| 87 | + Assert::assertFalse($this->service->verify($request, $this->getConfig())); |
| 88 | + } |
| 89 | + |
| 90 | + /** @test */ |
| 91 | + public function it_will_not_verify_a_bad_request() |
| 92 | + { |
| 93 | + $token = 'good'; |
| 94 | + $this->app['config']['shield.services.mailgun.options.token'] = $token; |
| 95 | + |
| 96 | + // Build the signature for the request payload |
| 97 | + $timestamp = Carbon::now()->timestamp; |
| 98 | + $signature = $this->buildSignature($timestamp, $token); |
| 99 | + |
| 100 | + $request = $this->request(json_encode([ |
| 101 | + 'timestamp' => $timestamp, |
| 102 | + 'token' => 'bad', |
| 103 | + 'signature' => $signature, |
| 104 | + ])); |
| 105 | + |
| 106 | + $headers = [ |
| 107 | + 'Content-Type' => 'application/json' |
| 108 | + ]; |
| 109 | + |
| 110 | + $request->headers->add($headers); |
| 111 | + |
| 112 | + Assert::assertFalse($this->service->verify($request, $this->getConfig())); |
| 113 | + } |
| 114 | + |
| 115 | + /** @test */ |
| 116 | + public function it_requires_a_post_request() |
| 117 | + { |
| 118 | + // Set up valid data |
| 119 | + $token = 'raNd0mk3y'; |
| 120 | + $this->app['config']['shield.services.mailgun.options.token'] = $token; |
| 121 | + |
| 122 | + // Build the signature for the request payload |
| 123 | + $timestamp = Carbon::now()->timestamp; |
| 124 | + $signature = $this->buildSignature($timestamp, $token); |
| 125 | + |
| 126 | + $requestBody = json_encode([ |
| 127 | + 'timestamp' => $timestamp, |
| 128 | + 'token' => $token, |
| 129 | + 'signature' => $signature, |
| 130 | + ]); |
| 131 | + |
| 132 | + $examples = ['GET', 'PUT', 'PATCH', 'DELETE', 'POST']; |
| 133 | + |
| 134 | + foreach ($examples as $example) { |
| 135 | + |
| 136 | + $request = Request::create('http://example.com', $example, [], [], [], [], $requestBody); |
| 137 | + $request->headers->add([ |
| 138 | + 'Content-Type' => 'application/json' |
| 139 | + ]); |
| 140 | + |
| 141 | + $assertion = $example === 'POST' ? 'assertTrue' : 'assertFalse'; |
| 142 | + |
| 143 | + Assert::$assertion( |
| 144 | + $this->service->verify($request, $this->getConfig()), |
| 145 | + "Expected $example to $assertion, but it did not." |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** @test */ |
| 151 | + public function it_has_correct_headers_required() |
| 152 | + { |
| 153 | + Assert::assertArraySubset([], $this->service->headers()); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get a configuration value for mailgun |
| 158 | + * @param $value An optional value |
| 159 | + * @param $default When a value is requested, a default value |
| 160 | + * |
| 161 | + * @return Collection|string |
| 162 | + */ |
| 163 | + protected function getConfig($value = null, $default = null) |
| 164 | + { |
| 165 | + $config = collect($this->app['config']['shield.services.mailgun.options']); |
| 166 | + |
| 167 | + if ($value === null) { |
| 168 | + return $config; |
| 169 | + } |
| 170 | + |
| 171 | + return $config->get($value, $default); |
| 172 | + } |
| 173 | + |
| 174 | + protected function buildSignature($timestamp, $token) |
| 175 | + { |
| 176 | + return hash_hmac( |
| 177 | + 'sha256', |
| 178 | + sprintf('%s%s', $timestamp, $token), |
| 179 | + $this->getConfig('token') |
| 180 | + ); |
| 181 | + } |
| 182 | +} |
0 commit comments