Need to add flash when the issue isn't mismatching passwords on the Register User action.

This commit is contained in:
Dave Smith-Hayes 2024-06-15 22:21:09 -04:00
parent e0a6ccbad4
commit f0c91e5aaa
3 changed files with 36 additions and 31 deletions

View File

@ -1,29 +0,0 @@
<?php
namespace Slovocast\Infrastructure\Middleware;
use Slovocast\Infrastructure\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
class VerifyPasswordMiddleware extends Middleware
{
public function invoke(
Request $request,
Response $response,
callable $next
): Response {
// get user Data
$requestData = $request->getParsedBody();
if ($requestData['password'] !== $requestData['checked_password']) {
$this->session
->getFlash()
->add('error', "Passwords do not match.");
return $response->withStatus(400);
}
return $response;
}
}

View File

@ -8,7 +8,7 @@ use Slovocast\Controller\User\{
RegisterUserPage, RegisterUserPage,
RegisterUserAction RegisterUserAction
}; };
use Slovocast\Infrastructure\Middleware\VerifyPasswordMiddleware; use Slovocast\Middleware\VerifyPasswordMiddleware;
class Routes class Routes
{ {
@ -23,6 +23,6 @@ class Routes
{ {
$app->get('/users/register', RegisterUserPage::class); $app->get('/users/register', RegisterUserPage::class);
$app->post('/users/register', RegisterUserAction::class) $app->post('/users/register', RegisterUserAction::class)
->add(new VerifyPasswordMiddleware()); ->add(VerifyPasswordMiddleware::class);
} }
} }

View File

@ -31,6 +31,18 @@ class RegisterUserActionTest extends TestCase
]); ]);
} }
protected function createNewUserRequestWithMismatchedPasswords(): Request
{
$user = $this->getUser();
return $this->createRequest('POST', '/users/register')
->withParsedBody([
'email' => $user->getEmail(),
'name' => $user->getName(),
'password' => $user->getPassword(),
'checked_password' => 'no-math'
]);
}
public function testSuccessfulRegistration(): void public function testSuccessfulRegistration(): void
{ {
$user = $this->getUser(); $user = $this->getUser();
@ -77,4 +89,26 @@ class RegisterUserActionTest extends TestCase
$this->assertNotEmpty($errorMessages); $this->assertNotEmpty($errorMessages);
$this->assertEquals('Unable to register user.', $errorMessages[0]); $this->assertEquals('Unable to register user.', $errorMessages[0]);
} }
public function testMismatchPassword(): void
{
$user = $this->getUser();
$app = $this->getAppInstance();
/** @var $container \DI\Cotnainer */
$container = $app->getContainer();
$userRepository = $this->prophesize(UserRepositoryInterface::class);
$userRepository->save($user)->willReturn(true);
$container->set(
UserRepositoryInterface::class,
$userRepository->reveal()
);
$request = $this->createNewUserRequestWithMismatchedPasswords();
$response = $app->handle($request);
$this->assertEquals(400, $response->getStatusCode());
}
} }