Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marickvantuil committed Mar 15, 2024
1 parent 1b654ea commit 67f470d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 56 deletions.
8 changes: 8 additions & 0 deletions config/cloud-scheduler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

return [
'app_url' => env('CLOUD_SCHEDULER_APP_URL'),
'service_account' => env('CLOUD_SCHEDULER_SERVICE_ACCOUNT'),
'disable_task_handler' => false,
'disable_token_verification' => false,
];
5 changes: 0 additions & 5 deletions config/laravel-google-cloud-scheduler.php

This file was deleted.

6 changes: 5 additions & 1 deletion src/CloudSchedulerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public function boot(Router $router)

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/laravel-google-cloud-scheduler.php', 'laravel-google-cloud-scheduler');
$this->mergeConfigFrom(__DIR__.'/../config/cloud-scheduler.php', 'cloud-scheduler-config');

$this->publishes([
__DIR__.'/../config/cloud-scheduler.php' => config_path('cloud-scheduler.php'),
], 'cloud-scheduler-config');
}

private function registerRoutes(Router $router)
Expand Down
8 changes: 6 additions & 2 deletions src/OpenIdVerificatorConcrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ public function verify(?string $token, array $config): void
throw new CloudSchedulerException('Missing [Authorization] header');
}

(new AccessToken())->verify(
$payload = (new AccessToken())->verify(
$token,
[
'audience' => config('laravel-google-cloud-scheduler.app_url'),
'audience' => config('cloud-scheduler.app_url'),
'throwException' => true,
]
);

if (($payload['email'] ?? '') !== config('cloud-scheduler.service_account')) {
throw new CloudSchedulerException('Invalid service account email');
}
}
}
11 changes: 9 additions & 2 deletions src/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public function __construct(
*/
public function handle()
{
// OpenIdVerificator::verify(request()->bearerToken(), []);
logger('Version without verification.');
if (config('cloud-scheduler.disable_task_handler')) {
abort(404);
}

if (config('cloud-scheduler.disable_token_verification') !== true) {
OpenIdVerificator::verify(request()->bearerToken(), []);
}

set_time_limit(0);

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

private function runCommand($command)
{
Artisan::bootstrap();

if ($this->isScheduledCommand($command)) {
$scheduledCommand = $this->getScheduledCommand($command);

Expand Down
70 changes: 24 additions & 46 deletions tests/TaskHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,42 @@

class TaskHandlerTest extends TestCase
{
private $taskHandler;

private $fakeCommand;

public function setUp(): void
{
parent::setUp();

$this->fakeCommand = Mockery::mock(Command::class)->makePartial();

config()->set('laravel-google-cloud-scheduler.app_url', 'my-application.com');

request()->headers->add(['Authorization' => 'Bearer test']);

$this->taskHandler = new TaskHandler(
$this->fakeCommand,
app(Schedule::class),
Container::getInstance()
);
}

#[Test]
public function it_executes_the_incoming_command()
{
// Arrange
OpenIdVerificator::fake();

$this->fakeCommand->shouldReceive('capture')->andReturn('env');

$output = $this->taskHandler->handle();
// Act
$output = $this->call('POST', '/cloud-scheduler-job', content: 'php artisan env')->content();

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

#[Test]
public function it_requires_a_jwt()
{
$this->fakeCommand->shouldReceive('capture')->andReturn('env');

request()->headers->remove('Authorization');
// Act
$response = $this->call('POST', '/cloud-scheduler-job', content: 'php artisan env');

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

$this->taskHandler->handle();
}

#[Test]
public function it_requires_a_jwt_signed_by_google()
{
$this->fakeCommand->shouldReceive('capture')->andReturn('env');

$this->expectException(UnexpectedValueException::class);

$this->taskHandler->handle();
// Act
$response = $this
->withToken('hey')
->call('POST', '/cloud-scheduler-job', server: ['HTTP_AUTHORIZATION' => 'Bearer 123'], content: 'php artisan env');

// Assert
$this->assertStringContainsString('Wrong number of segments', $response->content());
$response->assertStatus(500);
}

#[Test]
Expand All @@ -78,11 +61,11 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
OpenIdVerificator::fake();
Event::fake();

$this->fakeCommand->shouldReceive('capture')->andReturn('test:command');

cache()->clear();

$this->taskHandler->handle();
$this->assertLoggedLines(0);

$this->call('POST', '/cloud-scheduler-job', content: 'php artisan test:command');

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

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

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

$this->assertLoggedLines(1);

cache()->delete($mutex);

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

$this->assertLoggedLines(2);
}
Expand All @@ -108,11 +91,8 @@ public function it_prevents_overlapping_if_the_command_is_scheduled_without_over
public function it_runs_the_before_and_after_callbacks()
{
OpenIdVerificator::fake();
Event::fake();

$this->fakeCommand->shouldReceive('capture')->andReturn('test:command2');

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

$this->assertLoggedLines(3);
$this->assertLogged('log after');
Expand All @@ -124,10 +104,8 @@ public function it_runs_the_before_and_after_callbacks()
public function it_can_run_the_schedule_run_command()
{
OpenIdVerificator::fake();
Event::fake(TaskOutput::class);
$this->fakeCommand->shouldReceive('capture')->andReturn('schedule:run');

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

$this->assertLoggedLines(5);
$this->assertLogged('TestCommand');
Expand Down

0 comments on commit 67f470d

Please sign in to comment.