Skip to content

Commit

Permalink
Redirect /login to /home (#2524)
Browse files Browse the repository at this point in the history
* conditional routing for /login

* amend routes logic and behat tests

* refactoring

* refactor routes file
  • Loading branch information
Lbagg1 authored Feb 13, 2024
1 parent 7763413 commit 3fb97a1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
16 changes: 15 additions & 1 deletion service-front/app/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
$app->route('/home', [
new ConditionalRoutingMiddleware(
$container,
$factory,
$ALLOW_GOV_ONE_LOGIN,
Actor\Handler\AuthenticateOneLoginHandler::class,
Actor\Handler\ActorTriagePageHandler::class
Expand All @@ -95,13 +96,25 @@
$app->get('/activate-account/{token}', Actor\Handler\ActivateAccountHandler::class, 'activate-account');

// User auth
$app->route('/login', Actor\Handler\LoginPageHandler::class, ['GET', 'POST'], 'login');
$app->route('/login', [
new ConditionalRoutingMiddleware(
$container,
$factory,
$ALLOW_GOV_ONE_LOGIN,
function () {
return new \Laminas\Diactoros\Response\RedirectResponse('/home');
},
Actor\Handler\LoginPageHandler::class
)
], ['GET', 'POST'], 'login');

$app->get('/session-expired', Actor\Handler\ActorSessionExpiredHandler::class, 'session-expired');
$app->get('/session-check', Actor\Handler\ActorSessionCheckHandler::class, 'session-check');
$app->get('/session-refresh', Common\Handler\SessionRefreshHandler::class, 'session-refresh');
$app->get('/home/login', [
new ConditionalRoutingMiddleware(
$container,
$factory,
$ALLOW_GOV_ONE_LOGIN,
Actor\Handler\OneLoginCallbackHandler::class,
Mezzio\Handler\NotFoundHandler::class
Expand Down Expand Up @@ -312,6 +325,7 @@
Common\Middleware\Authentication\AuthenticationMiddleware::class,
new ConditionalRoutingMiddleware(
$container,
$factory,
$DELETE_LPA_FEATURE,
Actor\Handler\RemoveLpaHandler::class,
$defaultNotFoundPage
Expand Down
16 changes: 16 additions & 0 deletions service-front/app/features/context/UI/AccountContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ public function iAccessTheLoginForm(): void
$this->ui->assertElementContainsText('button[name=sign-in]', 'Sign in');
}

/**
* @When /^I access the login page$/
*/
public function iAccessTheLoginPage(): void
{
$this->ui->visit('/login');
}

/**
* @When /^I access the use a lasting power of attorney web page$/
*/
Expand Down Expand Up @@ -312,6 +320,14 @@ public function iAmOnTheYourDetailsPage(): void
$this->ui->clickLink('Your details');
}

/**
* @Then /^I am redirected to the one login page$/
*/
public function iAmRedirectedToTheOneLoginPage(): void
{
$this->ui->assertPageAddress('/home');
}

/**
* @Given /^I am signed in$/
*/
Expand Down
5 changes: 5 additions & 0 deletions service-front/app/features/one-login.feature
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@
Given I have logged in to one login in English
When I have an account whose sub matches a local account
Then I see the LPA dashboard with any LPAs that are in the account

@ui @actor @ff:allow_gov_one_login:true
Scenario: I am redirected to the one login page from the login page
When I access the login page
Then I am redirected to the one login page
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Common\Middleware\Routing;

use Mezzio\MiddlewareFactoryInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -13,25 +14,23 @@

class ConditionalRoutingMiddleware implements MiddlewareInterface
{
private ContainerInterface $middlewareContainer;

/**
* @param ContainerInterface $container It is necessary to pass the container so we can resolve the feature flag
* at runtime. Passing just the FeatureEnabled component results in having to
* re-initialise a number of container items to facilitate testing
* - increasing complexity.
* @param string $featureFlagName The name of the feature flag that will be used to determine the
* correct route
* @param string $trueRoute The route taken if the feature flag is true
* @param string $falseRoute The route taken if the feature flag is false Or undefined.
* @param ContainerInterface $container It is necessary to pass the container so we can resolve the feature flag
* @param ContainerInterface $middlewareContainer
* @param MiddlewareFactoryInterface $middlewareFactory
* @param string $featureFlagName The name of the feature flag that will be used to determine the
* correct route
* @param string|callable $trueRoute The route taken if the feature flag is true
* @param string|callable $falseRoute The route taken if the feature flag is false Or undefined.
*/
public function __construct(
ContainerInterface $container,
private ContainerInterface $middlewareContainer,
private MiddlewareFactoryInterface $middlewareFactory,
private string $featureFlagName,
private string $trueRoute,
private string $falseRoute,
private mixed $trueRoute,
private mixed $falseRoute,
) {
$this->middlewareContainer = $container;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand All @@ -44,8 +43,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface

$flagEnabled = $config['feature_flags'][$this->featureFlagName] ?? false;

$middleware = $this->middlewareContainer->get($flagEnabled ? $this->trueRoute : $this->falseRoute);

return $middleware->handle($request, $handler);
$middleware = $this->middlewareFactory->prepare($flagEnabled ? $this->trueRoute : $this->falseRoute);
return $middleware->process($request, $handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@

use Common\Middleware\Routing\ConditionalRoutingMiddleware;
use Interop\Container\ContainerInterface;
use Mezzio\MiddlewareFactoryInterface;
use Monolog\Test\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use UnexpectedValueException;

use function Laminas\Stratigility\middleware;

class ConditionalRoutingMiddlewareTest extends TestCase
{
use ProphecyTrait;

private ObjectProphecy|MiddlewareFactoryInterface $middlewareFactoryProphecy;
private ObjectProphecy|ContainerInterface $containerProphecy;
private ObjectProphecy|ServerRequestInterface $requestInterfaceProphecy;
private ObjectProphecy|RequestHandlerInterface $requestHandlerInterfaceProphecy;

public function setUp(): void
{
$this->middlewareFactoryProphecy = $this->prophesize(MiddlewareFactoryInterface::class);
$this->containerProphecy = $this->prophesize(ContainerInterface::class);
$this->requestInterfaceProphecy = $this->prophesize(ServerRequestInterface::class);
$this->requestHandlerInterfaceProphecy = $this->prophesize(RequestHandlerInterface::class);
Expand All @@ -32,62 +38,66 @@ public function setUp(): void
/** @test */
public function test_when_feature_flag_is_on_true_route_is_called(): void
{
$trueRouteProphecy = $this->prophesize(RequestHandlerInterface::class);
$this->containerProphecy->get('TrueRoute')->shouldBeCalled()->willReturn($trueRouteProphecy);
$this->containerProphecy->get('FalseRoute')->shouldNotBeCalled();
$middlewareProphecy = $this->prophesize(MiddlewareInterface::class);
$middlewareProphecy->process(Argument::cetera())->shouldBeCalled();

$this->containerProphecy->get('config')->willReturn(['feature_flags' => ['Feature_Flag_Name' => true]]);

$this->middlewareFactoryProphecy->prepare('TrueRoute')->shouldBeCalled()->willReturn($middlewareProphecy);
$this->middlewareFactoryProphecy->prepare('FalseRoute')->shouldNotBeCalled();

$sut = new ConditionalRoutingMiddleware(
$this->containerProphecy->reveal(),
$this->middlewareFactoryProphecy->reveal(),
'Feature_Flag_Name',
'TrueRoute',
'FalseRoute'
);

$trueRouteProphecy->handle(Argument::cetera())->shouldBeCalled();

$sut->process($this->requestInterfaceProphecy->reveal(), $this->requestHandlerInterfaceProphecy->reveal());
}

/** @test */
public function test_when_feature_flag_is_off_false_route_is_called(): void
{
$trueRouteProphecy = $this->prophesize(RequestHandlerInterface::class);
$middlewareProphecy = $this->prophesize(MiddlewareInterface::class);
$middlewareProphecy->process(Argument::cetera())->shouldBeCalled();

$this->containerProphecy->get('TrueRoute')->shouldNotBeCalled();
$this->containerProphecy->get('FalseRoute')->shouldBeCalled()->willReturn($trueRouteProphecy);
$this->containerProphecy->get('config')->willReturn(['feature_flags' => ['Feature_Flag_Name' => false]]);

$this->middlewareFactoryProphecy->prepare('TrueRoute')->shouldNotBeCalled();
$this->middlewareFactoryProphecy->prepare('FalseRoute')->shouldBeCalled()->willReturn($middlewareProphecy);

$sut = new ConditionalRoutingMiddleware(
$this->containerProphecy->reveal(),
$this->middlewareFactoryProphecy->reveal(),
'Feature_Flag_Name',
'TrueRoute',
'FalseRoute'
);

$trueRouteProphecy->handle(Argument::cetera())->shouldBeCalled();

$sut->process($this->requestInterfaceProphecy->reveal(), $this->requestHandlerInterfaceProphecy->reveal());
}

/** @test */
public function test_when_feature_flag_is_undefined_false_route_is_called(): void
{
$trueRouteProphecy = $this->prophesize(RequestHandlerInterface::class);
$middlewareProphecy = $this->prophesize(MiddlewareInterface::class);
$middlewareProphecy->process(Argument::cetera())->shouldBeCalled();

$this->containerProphecy->get('TrueRoute')->shouldNotBeCalled();
$this->containerProphecy->get('FalseRoute')->shouldBeCalled()->willReturn($trueRouteProphecy);
$this->containerProphecy->get('config')->willReturn(['feature_flags' => []]);

$this->middlewareFactoryProphecy->prepare('TrueRoute')->shouldNotBeCalled();
$this->middlewareFactoryProphecy->prepare('FalseRoute')->shouldBeCalled()->willReturn($middlewareProphecy);

$sut = new ConditionalRoutingMiddleware(
$this->containerProphecy->reveal(),
$this->middlewareFactoryProphecy->reveal(),
'Feature_Flag_Name',
'TrueRoute',
'FalseRoute'
);

$trueRouteProphecy->handle(Argument::cetera())->shouldBeCalled();

$sut->process($this->requestInterfaceProphecy->reveal(), $this->requestHandlerInterfaceProphecy->reveal());
}

Expand All @@ -98,6 +108,7 @@ public function test_when_feature_flag_is_not_defined_error_raised(): void

$sut = new ConditionalRoutingMiddleware(
$this->containerProphecy->reveal(),
$this->middlewareFactoryProphecy->reveal(),
'Feature_Flag_Name',
'TrueRoute',
'FalseRoute'
Expand Down

0 comments on commit 3fb97a1

Please sign in to comment.