Skip to content

Commit

Permalink
Merge pull request #1129 from cultuurnet/PPF-488/bugfix-activating-in…
Browse files Browse the repository at this point in the history
…tegration-removes-consumer-group

PPF-488 Always send the groups when updating uitidv1
  • Loading branch information
grubolsch authored May 22, 2024
2 parents 05ead5f + 27b9fc4 commit 47f7acf
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 28 deletions.
6 changes: 5 additions & 1 deletion app/UiTiDv1/Jobs/ActivateConsumerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\UiTiDv1\Jobs;

use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\UiTiDv1\Events\ConsumerActivated;
use App\UiTiDv1\Repositories\UiTiDv1ConsumerRepository;
use App\UiTiDv1\UiTiDv1ClusterSDK;
Expand All @@ -21,14 +22,17 @@ final class ActivateConsumerHandler implements ShouldQueue
public function __construct(
private readonly UiTiDv1ClusterSDK $clusterSDK,
private readonly UiTiDv1ConsumerRepository $consumerRepository,
private readonly IntegrationRepository $integrationRepository,
private readonly LoggerInterface $logger,
) {
}

public function handle(ActivateConsumer $event): void
{
try {
$this->clusterSDK->activateConsumers($this->consumerRepository->getById($event->id));
$uiTiDv1Consumer = $this->consumerRepository->getById($event->id);
$integration = $this->integrationRepository->getById($uiTiDv1Consumer->integrationId);
$this->clusterSDK->activateConsumers($integration, $uiTiDv1Consumer);
} catch (ModelNotFoundException|UiTiDv1SDKException $e) {
$this->logger->error(
'Failed to activate UiTiD v1 client: ' . $e->getMessage(),
Expand Down
6 changes: 5 additions & 1 deletion app/UiTiDv1/Jobs/BlockConsumerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\UiTiDv1\Jobs;

use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\UiTiDv1\Events\ConsumerBlocked;
use App\UiTiDv1\Repositories\UiTiDv1ConsumerRepository;
use App\UiTiDv1\UiTiDv1ClusterSDK;
Expand All @@ -21,14 +22,17 @@ final class BlockConsumerHandler implements ShouldQueue
public function __construct(
private readonly UiTiDv1ClusterSDK $clusterSDK,
private readonly UiTiDv1ConsumerRepository $consumerRepository,
private readonly IntegrationRepository $integrationRepository,
private readonly LoggerInterface $logger,
) {
}

public function handle(BlockConsumer $event): void
{
try {
$this->clusterSDK->blockConsumers($this->consumerRepository->getById($event->id));
$uiTiDv1Consumer = $this->consumerRepository->getById($event->id);
$integration = $this->integrationRepository->getById($uiTiDv1Consumer->integrationId);
$this->clusterSDK->blockConsumers($integration, $uiTiDv1Consumer);
} catch (ModelNotFoundException|UiTiDv1SDKException $e) {
$this->logger->error(
'Failed to block UiTiD v1 client: ' . $e->getMessage(),
Expand Down
5 changes: 4 additions & 1 deletion app/UiTiDv1/Listeners/BlockConsumers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\UiTiDv1\Listeners;

use App\Domain\Integrations\Events\IntegrationBlocked;
use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\UiTiDv1\Repositories\UiTiDv1ConsumerRepository;
use App\UiTiDv1\UiTiDv1ClusterSDK;
use Illuminate\Bus\Queueable;
Expand All @@ -19,6 +20,7 @@ final class BlockConsumers implements ShouldQueue
public function __construct(
private readonly UiTiDv1ClusterSDK $clusterSDK,
private readonly UiTiDv1ConsumerRepository $consumerRepository,
private readonly IntegrationRepository $integrationRepository,
private readonly LoggerInterface $logger,
) {
}
Expand All @@ -27,7 +29,8 @@ public function handle(IntegrationBlocked $integrationBlocked): void
{
$consumers = $this->consumerRepository->getByIntegrationId($integrationBlocked->id);

$this->clusterSDK->blockConsumers(...$consumers);
$integration = $this->integrationRepository->getById($integrationBlocked->id);
$this->clusterSDK->blockConsumers($integration, ... $consumers);

$this->logger->info(
'UiTiD v1 consumer(s) blocked',
Expand Down
8 changes: 4 additions & 4 deletions app/UiTiDv1/UiTiDv1ClusterSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public function createConsumerForIntegrationOnEnvironment(
return $this->uitidv1EnvironmentSDKs[$environment->value]->createConsumerForIntegration($integration);
}

public function blockConsumers(UiTiDv1Consumer ...$uiTiDv1Consumers): void
public function blockConsumers(Integration $integration, UiTiDv1Consumer ...$uiTiDv1Consumers): void
{
foreach ($uiTiDv1Consumers as $uiTiDv1Consumer) {
$this->uitidv1EnvironmentSDKs[$uiTiDv1Consumer->environment->value]->blockConsumer($uiTiDv1Consumer);
$this->uitidv1EnvironmentSDKs[$uiTiDv1Consumer->environment->value]->blockConsumer($integration, $uiTiDv1Consumer);
}
}

public function activateConsumers(UiTiDv1Consumer ...$uiTiDv1Consumers): void
public function activateConsumers(Integration $integration, UiTiDv1Consumer ...$uiTiDv1Consumers): void
{
foreach ($uiTiDv1Consumers as $uiTiDv1Consumer) {
$this->uitidv1EnvironmentSDKs[$uiTiDv1Consumer->environment->value]->activateConsumer($uiTiDv1Consumer);
$this->uitidv1EnvironmentSDKs[$uiTiDv1Consumer->environment->value]->activateConsumer($integration, $uiTiDv1Consumer);
}
}

Expand Down
7 changes: 5 additions & 2 deletions app/UiTiDv1/UiTiDv1EnvironmentSDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,25 @@ public function updateConsumerForIntegration(Integration $integration, UiTiDv1Co
{
$formData = [
'name' => $this->consumerName($integration),
'group' => $this->permissionGroupsPerIntegrationType[$integration->type->value] ?? [],
];

$this->sendPostRequest('serviceconsumer/' . $consumer->consumerKey, $formData);
}

public function blockConsumer(UiTiDv1Consumer $consumer): void
public function blockConsumer(Integration $integration, UiTiDv1Consumer $consumer): void
{
$this->sendPostRequest('serviceconsumer/' . $consumer->consumerKey, [
'status' => UiTiDv1ConsumerStatus::Blocked->value,
'group' => $this->permissionGroupsPerIntegrationType[$integration->type->value] ?? [],
]);
}

public function activateConsumer(UiTiDv1Consumer $consumer): void
public function activateConsumer(Integration $integration, UiTiDv1Consumer $consumer): void
{
$this->sendPostRequest('serviceconsumer/' . $consumer->consumerKey, [
'status' => UiTiDv1ConsumerStatus::Active->value,
'group' => $this->permissionGroupsPerIntegrationType[$integration->type->value] ?? [],
]);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/CreateIntegration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Tests;

use App\Domain\Integrations\Integration;
use App\Domain\Integrations\IntegrationPartnerStatus;
use App\Domain\Integrations\IntegrationStatus;
use App\Domain\Integrations\IntegrationType;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

trait CreateIntegration
{
public function givenThereIsAnIntegration(UuidInterface $integrationId, array $options = []): Integration
{
return new Integration(
$integrationId,
$options['type'] ?? IntegrationType::SearchApi,
$options['name'] ?? 'Mock Integration',
$options['description'] ?? 'Mock description',
Uuid::uuid4(),
$options['status'] ?? IntegrationStatus::Draft,
$options['partnerStatus'] ?? IntegrationPartnerStatus::THIRD_PARTY,
);
}
}
27 changes: 21 additions & 6 deletions tests/UiTiDv1/Jobs/ActivateConsumerHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\UiTiDv1\Jobs;

use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\UiTiDv1\Events\ConsumerActivated;
use App\UiTiDv1\Jobs\ActivateConsumer;
use App\UiTiDv1\Jobs\ActivateConsumerHandler;
Expand All @@ -16,6 +17,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use Ramsey\Uuid\Uuid;
use Tests\CreateIntegration;
use Tests\TestCase;
use Tests\UiTiDv1\CreatesMockUiTiDv1ClusterSDK;
use Tests\UiTiDv1\CreatesMockUiTiDv1Consumer;
Expand All @@ -24,11 +26,11 @@ final class ActivateConsumerHandlerTest extends TestCase
{
use CreatesMockUiTiDv1ClusterSDK;
use CreatesMockUiTiDv1Consumer;
use CreateIntegration;

private ClientInterface&MockObject $httpClient;

private UiTiDv1ConsumerRepository&MockObject $clientRepository;

private IntegrationRepository&MockObject $integrationRepository;
private ActivateConsumerHandler $activateClient;

protected function setUp(): void
Expand All @@ -38,21 +40,29 @@ protected function setUp(): void
$this->httpClient = $this->createMock(ClientInterface::class);

$this->clientRepository = $this->createMock(UiTiDv1ConsumerRepository::class);
$this->integrationRepository = $this->createMock(IntegrationRepository::class);

$this->activateClient = new ActivateConsumerHandler(
$this->createMockUiTiDv1ClusterSDK($this->httpClient),
$this->clientRepository,
$this->integrationRepository,
new NullLogger()
);
}

public function test_does_it_sent_an_activate_request(): void
{
$id = Uuid::uuid4();
$uiTiDv1Consumer = $this->createConsumer($id);
$this->clientRepository->expects($this->once())
->method('getById')
->with($id)
->willReturn($this->createConsumer($id));
->willReturn($uiTiDv1Consumer);

$this->integrationRepository->expects($this->once())
->method('getById')
->with($uiTiDv1Consumer->integrationId)
->willReturn($this->givenThereIsAnIntegration($id));

$this->httpClient->expects($this->once())
->method('request')
Expand All @@ -62,7 +72,7 @@ function ($method, $path, $options) {
[
'POST',
'serviceconsumer/consumer-key-1',
'status=ACTIVE',
'status=ACTIVE&group=3',
]
=> new Response(200, [], ''),
default => throw new LogicException('Invalid arguments received'),
Expand All @@ -73,7 +83,6 @@ function ($method, $path, $options) {
$this->activateClient->handle(new ActivateConsumer($id));

Event::assertDispatched(ConsumerActivated::class);

}

public function test_it_does_not_try_to_block_an_invalid_client(): void
Expand All @@ -96,10 +105,16 @@ public function test_it_does_not_try_to_block_an_invalid_client(): void
public function test_it_stops_on_invalid_request(): void
{
$id = Uuid::uuid4();
$uiTiDv1Consumer = $this->createConsumer($id);
$this->clientRepository->expects($this->once())
->method('getById')
->with($id)
->willReturn($this->createConsumer($id));
->willReturn($uiTiDv1Consumer);

$this->integrationRepository->expects($this->once())
->method('getById')
->with($uiTiDv1Consumer->integrationId)
->willReturn($this->givenThereIsAnIntegration($id));

$this->httpClient->expects($this->once())
->method('request')
Expand Down
22 changes: 17 additions & 5 deletions tests/UiTiDv1/Jobs/BlockConsumerHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\UiTiDv1\Jobs;

use App\Domain\Integrations\Repositories\IntegrationRepository;
use App\UiTiDv1\Events\ConsumerBlocked;
use App\UiTiDv1\Jobs\BlockConsumer;
use App\UiTiDv1\Jobs\BlockConsumerHandler;
Expand All @@ -16,6 +17,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\NullLogger;
use Ramsey\Uuid\Uuid;
use Tests\CreateIntegration;
use Tests\TestCase;
use Tests\UiTiDv1\CreatesMockUiTiDv1ClusterSDK;
use Tests\UiTiDv1\CreatesMockUiTiDv1Consumer;
Expand All @@ -24,11 +26,11 @@ final class BlockConsumerHandlerTest extends TestCase
{
use CreatesMockUiTiDv1ClusterSDK;
use CreatesMockUiTiDv1Consumer;
use CreateIntegration;

private ClientInterface&MockObject $httpClient;

private UiTiDv1ConsumerRepository&MockObject $clientRepository;

private IntegrationRepository&MockObject $integrationRepository;
private BlockConsumerHandler $blockClient;


Expand All @@ -37,12 +39,13 @@ protected function setUp(): void
parent::setUp();

$this->httpClient = $this->createMock(ClientInterface::class);

$this->clientRepository = $this->createMock(UiTiDv1ConsumerRepository::class);
$this->integrationRepository = $this->createMock(IntegrationRepository::class);

$this->blockClient = new BlockConsumerHandler(
$this->createMockUiTiDv1ClusterSDK($this->httpClient),
$this->clientRepository,
$this->integrationRepository,
new NullLogger()
);
}
Expand All @@ -55,6 +58,10 @@ public function test_does_it_sent_an_activate_request(): void
->with($id)
->willReturn($this->createConsumer($id));

$this->integrationRepository->expects($this->once())
->method('getById')
->willReturn($this->givenThereIsAnIntegration($id));

$this->httpClient->expects($this->once())
->method('request')
->willReturnCallback(
Expand All @@ -63,7 +70,7 @@ function ($method, $path, $options) {
[
'POST',
'serviceconsumer/consumer-key-1',
'status=BLOCKED',
'status=BLOCKED&group=3',
]
=> new Response(200, [], ''),
default => throw new LogicException('Invalid arguments received'),
Expand Down Expand Up @@ -96,10 +103,15 @@ public function test_it_does_not_try_to_block_an_invalid_client(): void
public function test_it_stops_on_invalid_request(): void
{
$id = Uuid::uuid4();
$uiTiDv1Consumer = $this->createConsumer($id);
$this->clientRepository->expects($this->once())
->method('getById')
->with($id)
->willReturn($this->createConsumer($id));
->willReturn($uiTiDv1Consumer);

$this->integrationRepository->expects($this->once())
->method('getById')
->willReturn($this->givenThereIsAnIntegration($id));

$this->httpClient->expects($this->once())
->method('request')
Expand Down
Loading

0 comments on commit 47f7acf

Please sign in to comment.