Skip to content

Commit

Permalink
Merge pull request #1346 from SURFnet/feature/1343-fix-contractual-base
Browse files Browse the repository at this point in the history
Fix: Contractual base gets overwritten in manage
  • Loading branch information
johanib authored Jan 13, 2025
2 parents 555cba0 + e548d94 commit 435f5ad
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Surfnet\ServiceProviderDashboard\Domain\Entity\Constants;
use Surfnet\ServiceProviderDashboard\Domain\Entity\ManageEntity;
use Surfnet\ServiceProviderDashboard\Domain\Repository\PublishEntityRepository;
use Surfnet\ServiceProviderDashboard\Domain\Service\ContractualBaseService;
use Surfnet\ServiceProviderDashboard\Infrastructure\HttpClient\Exceptions\RuntimeException\PublishMetadataException;
use Symfony\Component\HttpFoundation\RequestStack;
use function array_key_exists;
Expand All @@ -34,6 +35,7 @@ class PublishEntityTestCommandHandler implements CommandHandler
{
public function __construct(
private readonly PublishEntityRepository $publishClient,
private readonly ContractualBaseService $contractualBaseHelper,
private readonly EntityServiceInterface $entityService,
private readonly LoggerInterface $logger,
private readonly RequestStack $requestStack,
Expand All @@ -50,6 +52,7 @@ public function handle(PublishEntityTestCommand $command): void
if ($entity->isManageEntity()) {
// The entity as it is now known in Manage
$pristineEntity = $this->entityService->getPristineManageEntityById($entity->getId(), $entity->getEnvironment());
$this->contractualBaseHelper->writeContractualBaseForTestEntity($entity, $pristineEntity);
}
try {
$this->logger->info(
Expand All @@ -58,7 +61,6 @@ public function handle(PublishEntityTestCommand $command): void
$entity->getMetaData()->getNameEn()
)
);

$publishResponse = $this->publishClient->publish(
$entity,
$pristineEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,20 @@ public function writeContractualBase(ManageEntity $entity): void
// 4. Set the coin value on the entity
$entity->getMetaData()?->getCoin()->setContractualBase($contractualBase);
}

public function writeContractualBaseForTestEntity(ManageEntity $entity, ?ManageEntity $originalEntity): void
{
if ($entity->getEnvironment() !== Constants::ENVIRONMENT_TEST) {
return;
}

if ($originalEntity === null) {
return;
}

$originalContractualBase = $originalEntity->getMetaData()?->getCoin()->getContractualBase();
if ($originalContractualBase !== null) {
$entity->getMetaData()?->getCoin()->setContractualBase($originalContractualBase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

use Surfnet\ServiceProviderDashboard\Application\Service\EntityServiceInterface;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Contact;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Entity\Coin;
use Surfnet\ServiceProviderDashboard\Domain\Entity\Entity\MetaData;
use Surfnet\ServiceProviderDashboard\Domain\Service\ContractualBaseService;
use Surfnet\ServiceProviderDashboard\Infrastructure\Manage\Client\PublishEntityClient;
use Surfnet\ServiceProviderDashboard\Infrastructure\Manage\Client\QueryClient;
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\Mock;
Expand Down Expand Up @@ -56,6 +58,7 @@ public function setUp(): void

$this->commandHandler = new PublishEntityTestCommandHandler(
$this->client,
new ContractualBaseService(),
$this->entityService,
$this->logger,
$this->requestStack
Expand All @@ -67,14 +70,25 @@ public function setUp(): void
public function test_it_can_publish_to_manage()
{
$manageEntity = m::mock(ManageEntity::class);
$metaData = m::mock(MetaData::class);
$coin = m::mock(Coin::class);

$manageEntity
->shouldReceive('getMetaData->getNameEn')
->shouldReceive('getMetaData')
->andReturn($metaData);

$metaData
->shouldReceive('getNameEn')
->andReturn('Test Entity Name')
->shouldReceive('geMetaData->getManageId')
->shouldReceive('getProtocol->geProtocol')
->shouldReceive('setIdpAllowAll')
->shouldReceive('setIdpWhitelistRaw')
->andReturn(Constants::TYPE_OPENID_CONNECT_TNG);
->shouldReceive('getManageId')
->shouldReceive('getProtocol')
->andReturn(Constants::TYPE_OPENID_CONNECT_TNG)
->shouldReceive('getCoin')
->andReturn($coin);

$coin
->shouldReceive('getContractualBase')
->andReturn('some_contractual_base_value');

$this->logger
->shouldReceive('info')
Expand All @@ -97,8 +111,7 @@ public function test_it_can_publish_to_manage()
->andReturn('uuid');
$manageEntity->shouldReceive('setId');
$manageEntity
->shouldReceive('getEnvironment')
->andReturn('test');
->shouldReceive('getEnvironment');

$this->entityService
->shouldReceive('getPristineManageEntityById')
Expand All @@ -117,14 +130,29 @@ public function test_it_can_publish_to_manage()
public function test_it_handles_failing_publish()
{
$manageEntity = m::mock(ManageEntity::class);
$metaData = m::mock(MetaData::class);
$coin = m::mock(Coin::class);

$manageEntity
->shouldReceive('getMetaData->getNameEn')
->shouldReceive('getMetaData')
->andReturn($metaData);

$metaData
->shouldReceive('getNameEn')
->andReturn('Test Entity Name')
->shouldReceive('geMetaData->getManageId')
->shouldReceive('getProtocol->geProtocol')
->shouldReceive('setIdpAllowAll')
->shouldReceive('setIdpWhitelistRaw')
->andReturn(Constants::TYPE_OPENID_CONNECT_TNG);
->shouldReceive('getManageId')
->shouldReceive('getProtocol')
->andReturn(Constants::TYPE_OPENID_CONNECT_TNG)
->shouldReceive('getCoin')
->andReturn($coin);

$coin
->shouldReceive('getContractualBase')
->andReturn('some_contractual_base_value');

$coin
->shouldReceive('setContractualBase')
->with('some_contractual_base_value');

$manageEntity
->shouldReceive('getAllowedIdentityProviders->getAllowedIdentityProviders')
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/Domain/Service/ContractualBaseServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,77 @@ public function testWriteContractualBase(
$this->assertEquals($expectedContractualBase, $entity->getMetaData()->getCoin()->getContractualBase());
}

public function testContractualBaseForTest(): void
{
$entity = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);
$this->assertNull($entity->getMetaData()->getCoin()->getContractualBase());

$pristine = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE,
);
$pristine->getMetaData()->getCoin()->setContractualBase(Constants::CONTRACTUAL_BASE_IX);

$this->service->writeContractualBaseForTestEntity($entity, $pristine);

$this->assertSame(Constants::CONTRACTUAL_BASE_IX, $entity->getMetaData()->getCoin()->getContractualBase());
}

public function testWriteContractualBaseForTestEntityWithNonTestEnvironment(): void
{
$entity = $this->createMockEntity(
Constants::ENVIRONMENT_PRODUCTION,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);
$pristine = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);
$pristine->getMetaData()->getCoin()->setContractualBase(Constants::CONTRACTUAL_BASE_IX);

$this->service->writeContractualBaseForTestEntity($entity, $pristine);

$this->assertNull($entity->getMetaData()->getCoin()->getContractualBase());
}

public function testWriteContractualBaseForTestEntityWithNullPristineEntity(): void
{
$entity = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);

$this->service->writeContractualBaseForTestEntity($entity, null);

$this->assertNull($entity->getMetaData()->getCoin()->getContractualBase());
}

public function testWriteContractualBaseForTestEntityWithNullOriginalContractualBase(): void
{
$entity = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);
$pristine = $this->createMockEntity(
Constants::ENVIRONMENT_TEST,
Constants::TYPE_SAML,
Constants::SERVICE_TYPE_INSTITUTE
);

$this->service->writeContractualBaseForTestEntity($entity, $pristine);

$this->assertNull($entity->getMetaData()->getCoin()->getContractualBase());
}

public function contractualBaseDataProvider(): array
{
return [
Expand Down

0 comments on commit 435f5ad

Please sign in to comment.