|
10 | 10 | use Stackkit\LaravelGoogleCloudScheduler\Command; |
11 | 11 | use Stackkit\LaravelGoogleCloudScheduler\OpenIdVerificator; |
12 | 12 | use Stackkit\LaravelGoogleCloudScheduler\TaskHandler; |
| 13 | +use Throwable; |
13 | 14 |
|
14 | 15 | class TaskHandlerTest extends TestCase |
15 | 16 | { |
@@ -43,6 +44,7 @@ public function it_executes_the_incoming_command() |
43 | 44 | { |
44 | 45 | $this->fakeCommand->shouldReceive('capture')->andReturn('env'); |
45 | 46 | $this->openId->shouldReceive('guardAgainstInvalidOpenIdToken')->andReturnNull(); |
| 47 | + $this->openId->shouldReceive('decodeToken')->andReturnNull(); |
46 | 48 |
|
47 | 49 | $output = $this->taskHandler->handle(); |
48 | 50 |
|
@@ -72,7 +74,61 @@ public function it_requires_a_jwt_signed_by_google() |
72 | 74 | $this->request->headers->add(['Authorization' => 'Bearer ' . $dummyJwt]); |
73 | 75 |
|
74 | 76 | $this->expectException(CloudSchedulerException::class); |
75 | | - $this->expectExceptionMessage('Unauthorized'); |
| 77 | + $this->expectExceptionMessage('Could not decode token'); |
| 78 | + |
| 79 | + $this->taskHandler->handle(); |
| 80 | + } |
| 81 | + |
| 82 | + /** @test */ |
| 83 | + public function the_issue_identifier_should_be_google() |
| 84 | + { |
| 85 | + $this->expectExceptionMessage('The given OpenID token is not valid'); |
| 86 | + |
| 87 | + $this->openId->shouldReceive('decodeToken')->andReturn((object) [ |
| 88 | + 'iss' => 'accounts.not-google.com', |
| 89 | + ]); |
| 90 | + |
| 91 | + $this->taskHandler->handle(); |
| 92 | + } |
| 93 | + |
| 94 | + /** @test */ |
| 95 | + public function the_token_must_not_be_expired() |
| 96 | + { |
| 97 | + $this->expectExceptionMessage('The given OpenID token has expired'); |
| 98 | + |
| 99 | + $this->openId->shouldReceive('decodeToken')->andReturn((object) [ |
| 100 | + 'iss' => 'accounts.google.com', |
| 101 | + 'exp' => time() - 10, |
| 102 | + ]); |
| 103 | + |
| 104 | + $this->taskHandler->handle(); |
| 105 | + } |
| 106 | + |
| 107 | + /** @test */ |
| 108 | + public function the_aud_claim_must_be_the_same_as_the_app_id() |
| 109 | + { |
| 110 | + config()->set('laravel-google-cloud-scheduler.app_url', 'my-application.com'); |
| 111 | + $this->fakeCommand->shouldReceive('capture')->andReturn('env'); |
| 112 | + $this->openId->shouldReceive('decodeToken')->andReturn((object) [ |
| 113 | + 'iss' => 'accounts.google.com', |
| 114 | + 'exp' => time() + 10, |
| 115 | + 'aud' => 'my-application.com', |
| 116 | + ])->byDefault(); |
| 117 | + |
| 118 | + try { |
| 119 | + $this->taskHandler->handle(); |
| 120 | + } catch (Throwable $e) { |
| 121 | + $this->fail('The command should not have thrown an exception'); |
| 122 | + } |
| 123 | + |
| 124 | + $this->openId->shouldReceive('decodeToken')->andReturn((object) [ |
| 125 | + 'iss' => 'accounts.google.com', |
| 126 | + 'exp' => time() + 10, |
| 127 | + 'aud' => 'my-other-application.com', |
| 128 | + ]); |
| 129 | + |
| 130 | + $this->expectException(CloudSchedulerException::class); |
| 131 | + $this->expectExceptionMessage('The given OpenID token is not valid'); |
76 | 132 |
|
77 | 133 | $this->taskHandler->handle(); |
78 | 134 | } |
|
0 commit comments