Skip to content

Commit

Permalink
Deleting integration should block keycloak clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Koen Eelen committed Jun 18, 2024
1 parent 969fdb4 commit 7b55f02
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/Keycloak/KeycloakServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Events\IntegrationCreated;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Domain\Integrations\Events\IntegrationUnblocked;
use App\Domain\Integrations\Events\IntegrationUpdated;
use App\Domain\Integrations\Events\IntegrationUrlCreated;
Expand Down Expand Up @@ -74,6 +75,7 @@ private function bootstrapEventHandling(): void
Event::listen(IntegrationUpdated::class, [UpdateClients::class, 'handle']);
Event::listen(IntegrationBlocked::class, [BlockClients::class, 'handle']);
Event::listen(IntegrationUnblocked::class, [UnblockClients::class, 'handle']);
Event::listen(IntegrationDeleted::class, [BlockClients::class, 'handle']);

Event::listen(MissingClientsDetected::class, [CreateClients::class, 'handleCreatingMissingClients']);

Expand Down
5 changes: 3 additions & 2 deletions app/Keycloak/Listeners/BlockClients.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Keycloak\Client\ApiClient;
use App\Keycloak\Exception\KeyCloakApiFailed;
Expand All @@ -26,7 +27,7 @@ public function __construct(
) {
}

public function handle(IntegrationBlocked $integrationBlocked): void
public function handle(IntegrationBlocked|IntegrationDeleted $integrationBlocked): void
{
$integration = $this->integrationRepository->getById($integrationBlocked->id);
$keycloakClients = $this->keycloakClientRepository->getByIntegrationId($integrationBlocked->id);
Expand All @@ -46,7 +47,7 @@ public function handle(IntegrationBlocked $integrationBlocked): void
}
}

public function failed(IntegrationBlocked $integrationBlocked, Throwable $throwable): void
public function failed(IntegrationBlocked|IntegrationDeleted $integrationBlocked, Throwable $throwable): void
{
$this->logger->error('Failed to block Keycloak client(s)', [
'integration_id' => $integrationBlocked->id->toString(),
Expand Down
21 changes: 16 additions & 5 deletions tests/Keycloak/Listeners/BlockClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Domain\Integrations\Integration;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Keycloak\Client;
Expand All @@ -23,11 +24,10 @@ final class BlockClientsTest extends TestCase
{
use CreatesIntegration;
use KeycloakHttpClientFactory;


use RealmFactory;

private const SECRET = 'my-secret';
private const INTEGRATION_ID = '3f2c8aa3-6d5d-4a72-ba41-ab26bc8e591d';

private Integration $integration;
private ApiClient&MockObject $apiClient;
Expand All @@ -38,13 +38,16 @@ protected function setUp(): void
parent::setUp();

// This is a search API integration
$this->integration = $this->givenThereIsAnIntegration(Uuid::uuid4());
$this->integration = $this->givenThereIsAnIntegration(Uuid::fromString(self::INTEGRATION_ID));

$this->apiClient = $this->createMock(ApiClient::class);
$this->logger = $this->createMock(LoggerInterface::class);
}

public function test_block_clients_when_integration_is_blocked(): void
/**
*@dataProvider differentWaysToBlockClients
*/
public function test_block_clients_when_integration_is_blocked_or_deleted(IntegrationBlocked|IntegrationDeleted $event): void
{
$integrationRepository = $this->createMock(IntegrationRepository::class);
$integrationRepository->expects($this->once())
Expand Down Expand Up @@ -89,6 +92,14 @@ public function test_block_clients_when_integration_is_blocked(): void
$this->logger
);

$createClients->handle(new IntegrationBlocked($this->integration->id));
$createClients->handle($event);
}

public static function differentWaysToBlockClients() : array
{
return [
[new IntegrationBlocked(Uuid::fromString(self::INTEGRATION_ID))],
[new IntegrationDeleted(Uuid::fromString(self::INTEGRATION_ID))],
];
}
}

0 comments on commit 7b55f02

Please sign in to comment.