Skip to content

Commit

Permalink
Create GW Inst Config if not present in projection
Browse files Browse the repository at this point in the history
When the Gateway institution configuration is updated because the
sso on 2fa config changed for that institution. The projection would
only be updated if the institution already exists in the projection. If
the projection did not yet have the institution, then the projection was
not updated.

This fix now creates the row if the projection is not yet present.
  • Loading branch information
MKodde committed Aug 15, 2023
1 parent 64cdf02 commit a35e0d5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public function applySsoOn2faOptionChangedEvent(SsoOn2faOptionChangedEvent $even
if ($institutionConfiguration) {
$institutionConfiguration->ssoOn2faEnabled = $event->ssoOn2faOption->isEnabled();
$this->repository->save($institutionConfiguration);
return;
}
// It can happen that the event changed for an institution that already exists, but is not yet projected to
// this projection. In that case we can create it.
if (!$institutionConfiguration) {
$institutionConfiguration = new InstitutionConfiguration(
(string)$event->institution,
$event->ssoOn2faOption->isEnabled()
);
$this->repository->save($institutionConfiguration);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* Copyright 2022 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\GatewayBundle\Tests\Projector;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Rhumsaa\Uuid\Uuid;
use Surfnet\Stepup\Configuration\Event\SsoOn2faOptionChangedEvent;
use Surfnet\Stepup\Configuration\Value\Institution;
use Surfnet\Stepup\Configuration\Value\InstitutionConfigurationId;
use Surfnet\Stepup\Configuration\Value\SsoOn2faOption;
use Surfnet\StepupMiddleware\GatewayBundle\Entity\InstitutionConfiguration;
use Surfnet\StepupMiddleware\GatewayBundle\Projector\InstitutionConfigurationProjector;
use Surfnet\StepupMiddleware\GatewayBundle\Repository\InstitutionConfigurationRepository;

class InstitutionConfigurationProjectorTest extends TestCase
{
use m\Adapter\Phpunit\MockeryPHPUnitIntegration;

private $projector;

private $repository;
protected function setUp(): void
{
$repository = m::mock(InstitutionConfigurationRepository::class);
$projector = new InstitutionConfigurationProjector($repository);
$this->repository = $repository;
$this->projector = $projector;
}

public function test_create_row_when_non_existent()
{
$event = new SsoOn2faOptionChangedEvent(
new InstitutionConfigurationId(Uuid::uuid4()->toString()),
new Institution('institution-a.nl'),
new SsoOn2faOption(true)
);
$this->repository->shouldReceive('findByInstitution')->with('institution-a.nl')->andReturn(null);
$this->repository->shouldReceive('save')->withArgs(function(InstitutionConfiguration $configuration){
return $configuration->institution === 'institution-a.nl' && $configuration->ssoOn2faEnabled === true;
});

$this->projector->applySsoOn2faOptionChangedEvent($event);
}
public function test_updates_existing_row()
{
$event = new SsoOn2faOptionChangedEvent(
new InstitutionConfigurationId(Uuid::uuid4()->toString()),
new Institution('institution-a.nl'),
new SsoOn2faOption(true)
);
$configuration = new InstitutionConfiguration('institution-a.nl', false);

$this->repository->shouldReceive('findByInstitution')->with('institution-a.nl')->andReturn($configuration);
$this->repository->shouldReceive('save')->withArgs(function(InstitutionConfiguration $configuration){
return $configuration->institution === 'institution-a.nl' && $configuration->ssoOn2faEnabled === true;
});

$this->projector->applySsoOn2faOptionChangedEvent($event);
}
}

0 comments on commit a35e0d5

Please sign in to comment.