Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Gateway Institution Configuration projection row if not exists #407

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}