Skip to content

Commit 67f470d

Browse files
committed
wip
1 parent 1b654ea commit 67f470d

6 files changed

+52
-56
lines changed

config/cloud-scheduler.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'app_url' => env('CLOUD_SCHEDULER_APP_URL'),
5+
'service_account' => env('CLOUD_SCHEDULER_SERVICE_ACCOUNT'),
6+
'disable_task_handler' => false,
7+
'disable_token_verification' => false,
8+
];

config/laravel-google-cloud-scheduler.php

-5
This file was deleted.

src/CloudSchedulerServiceProvider.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public function boot(Router $router)
1515

1616
public function register()
1717
{
18-
$this->mergeConfigFrom(__DIR__.'/../config/laravel-google-cloud-scheduler.php', 'laravel-google-cloud-scheduler');
18+
$this->mergeConfigFrom(__DIR__.'/../config/cloud-scheduler.php', 'cloud-scheduler-config');
19+
20+
$this->publishes([
21+
__DIR__.'/../config/cloud-scheduler.php' => config_path('cloud-scheduler.php'),
22+
], 'cloud-scheduler-config');
1923
}
2024

2125
private function registerRoutes(Router $router)

src/OpenIdVerificatorConcrete.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ public function verify(?string $token, array $config): void
1313
throw new CloudSchedulerException('Missing [Authorization] header');
1414
}
1515

16-
(new AccessToken())->verify(
16+
$payload = (new AccessToken())->verify(
1717
$token,
1818
[
19-
'audience' => config('laravel-google-cloud-scheduler.app_url'),
19+
'audience' => config('cloud-scheduler.app_url'),
2020
'throwException' => true,
2121
]
2222
);
23+
24+
if (($payload['email'] ?? '') !== config('cloud-scheduler.service_account')) {
25+
throw new CloudSchedulerException('Invalid service account email');
26+
}
2327
}
2428
}

src/TaskHandler.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ public function __construct(
2929
*/
3030
public function handle()
3131
{
32-
// OpenIdVerificator::verify(request()->bearerToken(), []);
33-
logger('Version without verification.');
32+
if (config('cloud-scheduler.disable_task_handler')) {
33+
abort(404);
34+
}
35+
36+
if (config('cloud-scheduler.disable_token_verification') !== true) {
37+
OpenIdVerificator::verify(request()->bearerToken(), []);
38+
}
3439

3540
set_time_limit(0);
3641

@@ -41,6 +46,8 @@ public function handle()
4146

4247
private function runCommand($command)
4348
{
49+
Artisan::bootstrap();
50+
4451
if ($this->isScheduledCommand($command)) {
4552
$scheduledCommand = $this->getScheduledCommand($command);
4653

tests/TaskHandlerTest.php

+24-46
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,42 @@
1717

1818
class TaskHandlerTest extends TestCase
1919
{
20-
private $taskHandler;
21-
22-
private $fakeCommand;
23-
24-
public function setUp(): void
25-
{
26-
parent::setUp();
27-
28-
$this->fakeCommand = Mockery::mock(Command::class)->makePartial();
29-
30-
config()->set('laravel-google-cloud-scheduler.app_url', 'my-application.com');
31-
32-
request()->headers->add(['Authorization' => 'Bearer test']);
33-
34-
$this->taskHandler = new TaskHandler(
35-
$this->fakeCommand,
36-
app(Schedule::class),
37-
Container::getInstance()
38-
);
39-
}
40-
4120
#[Test]
4221
public function it_executes_the_incoming_command()
4322
{
23+
// Arrange
4424
OpenIdVerificator::fake();
4525

46-
$this->fakeCommand->shouldReceive('capture')->andReturn('env');
47-
48-
$output = $this->taskHandler->handle();
26+
// Act
27+
$output = $this->call('POST', '/cloud-scheduler-job', content: 'php artisan env')->content();
4928

29+
// Assert
5030
$this->assertStringContainsString('The application environment is [testing]', $output);
5131
}
5232

5333
#[Test]
5434
public function it_requires_a_jwt()
5535
{
56-
$this->fakeCommand->shouldReceive('capture')->andReturn('env');
57-
58-
request()->headers->remove('Authorization');
36+
// Act
37+
$response = $this->call('POST', '/cloud-scheduler-job', content: 'php artisan env');
5938

60-
$this->expectException(CloudSchedulerException::class);
39+
// Assert
40+
$this->assertStringContainsString('Missing [Authorization] header', $response->content());
41+
$response->assertStatus(500);
6142

62-
$this->taskHandler->handle();
6343
}
6444

6545
#[Test]
6646
public function it_requires_a_jwt_signed_by_google()
6747
{
68-
$this->fakeCommand->shouldReceive('capture')->andReturn('env');
69-
70-
$this->expectException(UnexpectedValueException::class);
71-
72-
$this->taskHandler->handle();
48+
// Act
49+
$response = $this
50+
->withToken('hey')
51+
->call('POST', '/cloud-scheduler-job', server: ['HTTP_AUTHORIZATION' => 'Bearer 123'], content: 'php artisan env');
52+
53+
// Assert
54+
$this->assertStringContainsString('Wrong number of segments', $response->content());
55+
$response->assertStatus(500);
7356
}
7457

7558
#[Test]
@@ -78,11 +61,11 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
7861
OpenIdVerificator::fake();
7962
Event::fake();
8063

81-
$this->fakeCommand->shouldReceive('capture')->andReturn('test:command');
82-
8364
cache()->clear();
8465

85-
$this->taskHandler->handle();
66+
$this->assertLoggedLines(0);
67+
68+
$this->call('POST', '/cloud-scheduler-job', content: 'php artisan test:command');
8669

8770
$this->assertLoggedLines(1);
8871
$this->assertLogged('TestCommand');
@@ -93,13 +76,13 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
9376

9477
cache()->add($mutex, true, 60);
9578

96-
$this->taskHandler->handle();
79+
$this->call('POST', '/cloud-scheduler-job', content: 'php artisan test:command');
9780

9881
$this->assertLoggedLines(1);
9982

10083
cache()->delete($mutex);
10184

102-
$this->taskHandler->handle();
85+
$this->call('POST', '/cloud-scheduler-job', content: 'php artisan test:command');
10386

10487
$this->assertLoggedLines(2);
10588
}
@@ -108,11 +91,8 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
10891
public function it_runs_the_before_and_after_callbacks()
10992
{
11093
OpenIdVerificator::fake();
111-
Event::fake();
112-
113-
$this->fakeCommand->shouldReceive('capture')->andReturn('test:command2');
11494

115-
$this->taskHandler->handle();
95+
$this->call('POST', '/cloud-scheduler-job', content: 'php artisan test:command2');
11696

11797
$this->assertLoggedLines(3);
11898
$this->assertLogged('log after');
@@ -124,10 +104,8 @@ public function it_runs_the_before_and_after_callbacks()
124104
public function it_can_run_the_schedule_run_command()
125105
{
126106
OpenIdVerificator::fake();
127-
Event::fake(TaskOutput::class);
128-
$this->fakeCommand->shouldReceive('capture')->andReturn('schedule:run');
129107

130-
$this->taskHandler->handle();
108+
$this->call('POST', '/cloud-scheduler-job', content: 'php artisan schedule:run');
131109

132110
$this->assertLoggedLines(5);
133111
$this->assertLogged('TestCommand');

0 commit comments

Comments
 (0)