diff --git a/app/UiTiDv1/Jobs/ActivateConsumerHandler.php b/app/UiTiDv1/Jobs/ActivateConsumerHandler.php index a515a327d..58ef15852 100644 --- a/app/UiTiDv1/Jobs/ActivateConsumerHandler.php +++ b/app/UiTiDv1/Jobs/ActivateConsumerHandler.php @@ -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; @@ -21,6 +22,7 @@ final class ActivateConsumerHandler implements ShouldQueue public function __construct( private readonly UiTiDv1ClusterSDK $clusterSDK, private readonly UiTiDv1ConsumerRepository $consumerRepository, + private readonly IntegrationRepository $integrationRepository, private readonly LoggerInterface $logger, ) { } @@ -28,7 +30,9 @@ public function __construct( 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(), diff --git a/app/UiTiDv1/Jobs/BlockConsumerHandler.php b/app/UiTiDv1/Jobs/BlockConsumerHandler.php index 484047b2d..d8fc620ad 100644 --- a/app/UiTiDv1/Jobs/BlockConsumerHandler.php +++ b/app/UiTiDv1/Jobs/BlockConsumerHandler.php @@ -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; @@ -21,6 +22,7 @@ final class BlockConsumerHandler implements ShouldQueue public function __construct( private readonly UiTiDv1ClusterSDK $clusterSDK, private readonly UiTiDv1ConsumerRepository $consumerRepository, + private readonly IntegrationRepository $integrationRepository, private readonly LoggerInterface $logger, ) { } @@ -28,7 +30,9 @@ public function __construct( 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(), diff --git a/app/UiTiDv1/Listeners/BlockConsumers.php b/app/UiTiDv1/Listeners/BlockConsumers.php index 0f3701804..8442b1b9d 100644 --- a/app/UiTiDv1/Listeners/BlockConsumers.php +++ b/app/UiTiDv1/Listeners/BlockConsumers.php @@ -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; @@ -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, ) { } @@ -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', diff --git a/app/UiTiDv1/UiTiDv1ClusterSDK.php b/app/UiTiDv1/UiTiDv1ClusterSDK.php index df36854b6..6ddccc0dd 100644 --- a/app/UiTiDv1/UiTiDv1ClusterSDK.php +++ b/app/UiTiDv1/UiTiDv1ClusterSDK.php @@ -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); } } diff --git a/app/UiTiDv1/UiTiDv1EnvironmentSDK.php b/app/UiTiDv1/UiTiDv1EnvironmentSDK.php index 349f69005..45460755b 100644 --- a/app/UiTiDv1/UiTiDv1EnvironmentSDK.php +++ b/app/UiTiDv1/UiTiDv1EnvironmentSDK.php @@ -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] ?? [], ]); } diff --git a/tests/CreateIntegration.php b/tests/CreateIntegration.php new file mode 100644 index 000000000..f2e3ba82a --- /dev/null +++ b/tests/CreateIntegration.php @@ -0,0 +1,28 @@ +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() ); } @@ -49,10 +53,16 @@ protected function setUp(): void 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') @@ -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'), @@ -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 @@ -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') diff --git a/tests/UiTiDv1/Jobs/BlockConsumerHandlerTest.php b/tests/UiTiDv1/Jobs/BlockConsumerHandlerTest.php index cc4a6aa09..c7daf1c63 100644 --- a/tests/UiTiDv1/Jobs/BlockConsumerHandlerTest.php +++ b/tests/UiTiDv1/Jobs/BlockConsumerHandlerTest.php @@ -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; @@ -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; @@ -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; @@ -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() ); } @@ -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( @@ -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'), @@ -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') diff --git a/tests/UiTiDv1/Listeners/BlockConsumersTest.php b/tests/UiTiDv1/Listeners/BlockConsumersTest.php index 5a9828dca..a216ef729 100644 --- a/tests/UiTiDv1/Listeners/BlockConsumersTest.php +++ b/tests/UiTiDv1/Listeners/BlockConsumersTest.php @@ -5,6 +5,7 @@ namespace Tests\UiTiDv1\Listeners; use App\Domain\Integrations\Events\IntegrationBlocked; +use App\Domain\Integrations\Repositories\IntegrationRepository; use App\UiTiDv1\Listeners\BlockConsumers; use App\UiTiDv1\Repositories\UiTiDv1ConsumerRepository; use App\UiTiDv1\UiTiDv1Consumer; @@ -15,26 +16,29 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; use Ramsey\Uuid\Uuid; +use Tests\CreateIntegration; use Tests\UiTiDv1\CreatesMockUiTiDv1ClusterSDK; final class BlockConsumersTest extends TestCase { use CreatesMockUiTiDv1ClusterSDK; + use CreateIntegration; private ClientInterface&MockObject $httpClient; - private UiTiDv1ConsumerRepository&MockObject $consumerRepository; - + private IntegrationRepository&MockObject $integrationRepository; private BlockConsumers $blockConsumers; protected function setUp(): void { $this->httpClient = $this->createMock(ClientInterface::class); $this->consumerRepository = $this->createMock(UiTiDv1ConsumerRepository::class); + $this->integrationRepository = $this->createMock(IntegrationRepository::class); $this->blockConsumers = new BlockConsumers( $this->createMockUiTiDv1ClusterSDK($this->httpClient), $this->consumerRepository, + $this->integrationRepository, $this->createMock(LoggerInterface::class) ); } @@ -78,6 +82,10 @@ public function test_it_blocks_consumers(): void ->with($integrationId) ->willReturn($consumers); + $this->integrationRepository->expects($this->once()) + ->method('getById') + ->willReturn($this->givenThereIsAnIntegration($integrationId)); + $this->httpClient->expects($this->exactly(3)) ->method('request') ->willReturnCallback( @@ -89,7 +97,7 @@ public function test_it_blocks_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'status=BLOCKED', + 'body' => 'status=BLOCKED&group=3', ], ], [ @@ -98,7 +106,7 @@ public function test_it_blocks_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'status=BLOCKED', + 'body' => 'status=BLOCKED&group=9', ], ], [ @@ -107,7 +115,7 @@ public function test_it_blocks_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'status=BLOCKED', + 'body' => 'status=BLOCKED&group=15', ], ] => new Response(200, [], ''), default => throw new \LogicException('Invalid arguments received'), diff --git a/tests/UiTiDv1/Listeners/UpdateConsumersTest.php b/tests/UiTiDv1/Listeners/UpdateConsumersTest.php index d74c2c76a..dea40ea3c 100644 --- a/tests/UiTiDv1/Listeners/UpdateConsumersTest.php +++ b/tests/UiTiDv1/Listeners/UpdateConsumersTest.php @@ -113,7 +113,7 @@ public function test_it_updates_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29', + 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29&group=1&group=2', ], ], [ @@ -122,7 +122,7 @@ public function test_it_updates_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29', + 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29&group=7&group=8', ], ], [ @@ -131,7 +131,7 @@ public function test_it_updates_consumers(): void [ 'http_errors' => false, 'headers' => ['content-type' => 'application/x-www-form-urlencoded'], - 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29', + 'body' => 'name=Mock%20Integration%20%28via%20publiq%20platform%29&group=13&group=14', ], ] => new Response(200, [], ''), default => throw new \LogicException('Invalid arguments received'),