Skip to content

Commit

Permalink
Merge branch 'main' into PPF-520/missing-client-scope-for-Entry-API-i…
Browse files Browse the repository at this point in the history
…ntegrations
  • Loading branch information
grubolsch authored Jun 19, 2024
2 parents 3b2087b + 2b988a3 commit 9322c15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 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
10 changes: 4 additions & 6 deletions app/Keycloak/Listeners/BlockClients.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace App\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Keycloak\Client\ApiClient;
use App\Keycloak\Exception\KeyCloakApiFailed;
use App\Keycloak\Repositories\KeycloakClientRepository;
Expand All @@ -19,24 +19,22 @@ final class BlockClients implements ShouldQueue
use Queueable;

public function __construct(
private readonly IntegrationRepository $integrationRepository,
private readonly KeycloakClientRepository $keycloakClientRepository,
private readonly ApiClient $client,
private readonly LoggerInterface $logger
) {
}

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);

foreach ($keycloakClients as $keycloakClient) {
try {
$this->client->blockClient($keycloakClient);

$this->logger->info('Keycloak client blocked', [
'integration_id' => $integration->id->toString(),
'integration_id' => $integrationBlocked->id->toString(),
'client_id' => $keycloakClient->id->toString(),
'environment' => $keycloakClient->environment->value,
]);
Expand All @@ -46,7 +44,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
41 changes: 20 additions & 21 deletions tests/Keycloak/Listeners/BlockClientsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
namespace Tests\Keycloak\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Integration;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\Domain\Integrations\Events\IntegrationDeleted;
use App\Keycloak\Client;
use App\Keycloak\Client\ApiClient;
use App\Keycloak\Listeners\BlockClients;
Expand All @@ -23,39 +22,32 @@ 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;
private LoggerInterface&MockObject $logger;

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

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

$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())
->method('getById')
->with($this->integration->id)
->willReturn($this->integration);
$integrationId = Uuid::fromString(self::INTEGRATION_ID);

$clients = [];
foreach ($this->givenAllRealms()
as $realm) {
$client = new Client(Uuid::uuid4(), $this->integration->id, Uuid::uuid4()->toString(), self::SECRET, $realm->environment);
foreach ($this->givenAllRealms() as $realm) {
$client = new Client(Uuid::uuid4(), $integrationId, Uuid::uuid4()->toString(), self::SECRET, $realm->environment);

$clients[$client->id->toString()] = $client;
}
Expand All @@ -73,22 +65,29 @@ public function test_block_clients_when_integration_is_blocked(): void
$this->assertArrayHasKey('integration_id', $options);
$this->assertArrayHasKey('environment', $options);

$this->assertEquals($this->integration->id->toString(), $options['integration_id']);
$this->assertEquals(self::INTEGRATION_ID, $options['integration_id']);
});

$keycloakClientRepository = $this->createMock(KeycloakClientRepository::class);
$keycloakClientRepository->expects($this->once())
->method('getByIntegrationId')
->with($this->integration->id)
->with($integrationId)
->willReturn($clients);

$createClients = new BlockClients(
$integrationRepository,
$keycloakClientRepository,
$this->apiClient,
$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 9322c15

Please sign in to comment.