-
-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #1515 [make:security:form-login] add ability to generate tests
- Loading branch information
Showing
4 changed files
with
180 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Resources/skeleton/security/formLogin/Test.LoginController.tpl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?= "<?php\n" ?> | ||
namespace App\Tests; | ||
|
||
<?= $use_statements ?> | ||
|
||
class LoginControllerTest extends WebTestCase | ||
{ | ||
private KernelBrowser $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = static::createClient(); | ||
$container = static::getContainer(); | ||
$em = $container->get('doctrine.orm.entity_manager'); | ||
$userRepository = $em->getRepository(<?= $user_short_name ?>::class); | ||
|
||
// Remove any existing users from the test database | ||
foreach ($userRepository->findAll() as $user) { | ||
$em->remove($user); | ||
} | ||
|
||
$em->flush(); | ||
|
||
// Create a <?= $user_short_name ?> fixture | ||
/** @var UserPasswordHasherInterface $passwordHasher */ | ||
$passwordHasher = $container->get('security.user_password_hasher'); | ||
|
||
$user = (new <?= $user_short_name ?>())->setEmail('[email protected]'); | ||
$user->setPassword($passwordHasher->hashPassword($user, 'password')); | ||
|
||
$em->persist($user); | ||
$em->flush(); | ||
} | ||
|
||
public function testLogin(): void | ||
{ | ||
// Denied - Can't login with invalid email address. | ||
$this->client->request('GET', '/login'); | ||
self::assertResponseIsSuccessful(); | ||
|
||
$this->client->submitForm('Sign in', [ | ||
'_username' => '[email protected]', | ||
'_password' => 'password', | ||
]); | ||
|
||
self::assertResponseRedirects('/login'); | ||
$this->client->followRedirect(); | ||
|
||
// Ensure we do not reveal if the user exists or not. | ||
self::assertSelectorTextContains('.alert-danger', 'Invalid credentials.'); | ||
|
||
// Denied - Can't login with invalid password. | ||
$this->client->request('GET', '/login'); | ||
self::assertResponseIsSuccessful(); | ||
|
||
$this->client->submitForm('Sign in', [ | ||
'_username' => '[email protected]', | ||
'_password' => 'bad-password', | ||
]); | ||
|
||
self::assertResponseRedirects('/login'); | ||
$this->client->followRedirect(); | ||
|
||
// Ensure we do not reveal the user exists but the password is wrong. | ||
self::assertSelectorTextContains('.alert-danger', 'Invalid credentials.'); | ||
|
||
// Success - Login with valid credentials is allowed. | ||
$this->client->submitForm('Sign in', [ | ||
'_username' => '[email protected]', | ||
'_password' => 'password', | ||
]); | ||
|
||
self::assertResponseRedirects('/'); | ||
$this->client->followRedirect(); | ||
|
||
self::assertSelectorNotExists('.alert-danger'); | ||
self::assertResponseIsSuccessful(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tests/fixtures/security/make-form-login/FixtureController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | ||
use Symfony\Component\Routing\Attribute\Route; | ||
|
||
class FixtureController extends AbstractController | ||
{ | ||
public function __construct( | ||
public UserPasswordHasherInterface $passwordHasher | ||
) { | ||
} | ||
|
||
#[Route(name: 'app_index')] | ||
public function index(): Response | ||
{ | ||
return $this->render('base.html.twig'); | ||
} | ||
} |