|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Shield\Stripe\Test\Unit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Assert; |
| 6 | +use Shield\Shield\Contracts\Service; |
| 7 | +use Shield\Stripe\Stripe; |
| 8 | +use Shield\Testing\TestCase; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class ServiceTest |
| 12 | + * |
| 13 | + * @package \Shield\Stripe\Test |
| 14 | + */ |
| 15 | +class ServiceTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var Stripe |
| 19 | + */ |
| 20 | + private $service; |
| 21 | + |
| 22 | + public function setUp() |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->service = new Stripe; |
| 27 | + } |
| 28 | + |
| 29 | + /** @test */ |
| 30 | + public function it_is_a_service() |
| 31 | + { |
| 32 | + Assert::assertInstanceOf(Service::class, new Stripe); |
| 33 | + } |
| 34 | + |
| 35 | + /** @test */ |
| 36 | + public function it_can_verify_a_valid_request() |
| 37 | + { |
| 38 | + $token = 'raNd0mk3y'; |
| 39 | + |
| 40 | + $this->app['config']['shield.services.stripe.options.secret'] = $token; |
| 41 | + |
| 42 | + $content = json_encode(['data' => 'sample content']); |
| 43 | + |
| 44 | + $request = $this->request($content); |
| 45 | + |
| 46 | + $time = time(); |
| 47 | + |
| 48 | + $signature = $time . '.' . $content; |
| 49 | + |
| 50 | + $headers = [ |
| 51 | + 'Stripe-Signature' => 't=' . $time . ',v1=' . hash_hmac('sha256', $signature, $token) |
| 52 | + ]; |
| 53 | + |
| 54 | + $request->headers->add($headers); |
| 55 | + |
| 56 | + Assert::assertTrue($this->service->verify($request, collect($this->app['config']['shield.services.stripe.options']))); |
| 57 | + } |
| 58 | + |
| 59 | + /** @test */ |
| 60 | + public function it_will_not_verify_a_bad_request() |
| 61 | + { |
| 62 | + $this->app['config']['shield.services.stripe.options.secret'] = 'good'; |
| 63 | + |
| 64 | + $content = json_encode(['data' => 'sample content']); |
| 65 | + |
| 66 | + $request = $this->request($content); |
| 67 | + |
| 68 | + $time = time(); |
| 69 | + |
| 70 | + $signature = $time . '.' . $content; |
| 71 | + |
| 72 | + $headers = [ |
| 73 | + 'Stripe-Signature' => 't=' . $time . ',v1=' . hash_hmac('sha256', $signature, 'bad') |
| 74 | + ]; |
| 75 | + |
| 76 | + $request->headers->add($headers); |
| 77 | + |
| 78 | + Assert::assertFalse($this->service->verify($request, collect($this->app['config']['shield.services.stripe.options']))); |
| 79 | + } |
| 80 | + |
| 81 | + /** @test */ |
| 82 | + public function it_will_fail_if_timestamp_is_over_tolerance() |
| 83 | + { |
| 84 | + $token = 'raNd0mk3y'; |
| 85 | + |
| 86 | + $this->app['config']['shield.services.stripe.options.secret'] = $token; |
| 87 | + $this->app['config']['shield.services.stripe.options.tolerance'] = 60 * 5; |
| 88 | + |
| 89 | + $content = 'sample content'; |
| 90 | + |
| 91 | + $request = $this->request($content); |
| 92 | + |
| 93 | + $time = time() - 61 * 5; // 5 seconds over |
| 94 | + |
| 95 | + $signature = $time . '.' . $content; |
| 96 | + |
| 97 | + $headers = [ |
| 98 | + 'Stripe-Signature' => 't=' . $time . ',v1=' . hash_hmac('sha256', $signature, $token) |
| 99 | + ]; |
| 100 | + |
| 101 | + $request->headers->add($headers); |
| 102 | + |
| 103 | + Assert::assertFalse($this->service->verify($request, collect($this->app['config']['shield.services.stripe.options']))); |
| 104 | + } |
| 105 | + |
| 106 | + /** @test */ |
| 107 | + public function it_has_correct_headers_required() |
| 108 | + { |
| 109 | + Assert::assertArraySubset(['Stripe-Signature'], $this->service->headers()); |
| 110 | + } |
| 111 | +} |
0 commit comments