Get the form key from the session first, and then send the request to create a user afterwards.

This commit is contained in:
Dave Smith-Hayes 2024-12-02 20:03:50 +00:00
parent f3bbfd7762
commit bf48532ac1

View File

@ -19,7 +19,7 @@ class RegisterUserActionTest extends TestCase
]); ]);
} }
protected function createNewUserRequest(): Request protected function createNewUserRequest(string $formKey = ""): Request
{ {
$user = $this->getUser(); $user = $this->getUser();
return $this->createRequest('POST', '/register') return $this->createRequest('POST', '/register')
@ -27,11 +27,12 @@ class RegisterUserActionTest extends TestCase
'email' => $user->getEmail(), 'email' => $user->getEmail(),
'name' => $user->getName(), 'name' => $user->getName(),
'password' => $user->getPassword(), 'password' => $user->getPassword(),
'checked_password' => $user->getPassword() 'checked_password' => $user->getPassword(),
'form_key' => $formKey,
]); ]);
} }
protected function createNewUserRequestWithMismatchedPasswords(): Request protected function createNewUserRequestWithMismatchedPasswords(string $formKey = ""): Request
{ {
$user = $this->getUser(); $user = $this->getUser();
return $this->createRequest('POST', '/register') return $this->createRequest('POST', '/register')
@ -39,10 +40,16 @@ class RegisterUserActionTest extends TestCase
'email' => $user->getEmail(), 'email' => $user->getEmail(),
'name' => $user->getName(), 'name' => $user->getName(),
'password' => $user->getPassword(), 'password' => $user->getPassword(),
'checked_password' => 'no-math' 'checked_password' => 'no-match',
'form_key' => $formKey,
]); ]);
} }
protected function createNewUserRequestForFormKey(): Request
{
return $this->createRequest('GET', '/register');
}
/** /**
* Set up the Application, the DI Container, and the Repository mock * Set up the Application, the DI Container, and the Repository mock
@ -66,7 +73,13 @@ class RegisterUserActionTest extends TestCase
$userRepository->reveal() $userRepository->reveal()
); );
$request = $this->createNewUserRequest(); // get the form key
$getUserRequest = $this->createNewUserRequestForFormKey();
$response = $app->handle($getUserRequest);
$formKey = $container->get(SessionInterface::class)->get("form_key");
$this->assertIsString($formKey);
$request = $this->createNewUserRequest($formKey);
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());