diff --git a/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php b/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php deleted file mode 100644 index cdc999f..0000000 --- a/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php +++ /dev/null @@ -1,29 +0,0 @@ -getParsedBody(); - - if ($requestData['password'] !== $requestData['checked_password']) { - $this->session - ->getFlash() - ->add('error', "Passwords do not match."); - return $response->withStatus(400); - } - - return $response; - } -} diff --git a/app/src/Routes.php b/app/src/Routes.php index ab3a6fd..6e1b6c7 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -8,7 +8,7 @@ use Slovocast\Controller\User\{ RegisterUserPage, RegisterUserAction }; -use Slovocast\Infrastructure\Middleware\VerifyPasswordMiddleware; +use Slovocast\Middleware\VerifyPasswordMiddleware; class Routes { @@ -23,6 +23,6 @@ class Routes { $app->get('/users/register', RegisterUserPage::class); $app->post('/users/register', RegisterUserAction::class) - ->add(new VerifyPasswordMiddleware()); + ->add(VerifyPasswordMiddleware::class); } } diff --git a/app/tests/Controller/User/RegisterUserActionTest.php b/app/tests/Controller/User/RegisterUserActionTest.php index 31dfc33..18c5db3 100644 --- a/app/tests/Controller/User/RegisterUserActionTest.php +++ b/app/tests/Controller/User/RegisterUserActionTest.php @@ -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 { $user = $this->getUser(); @@ -77,4 +89,26 @@ class RegisterUserActionTest extends TestCase $this->assertNotEmpty($errorMessages); $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()); + } }