|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use App\Tests\TestCase; |
| 6 | +use Symfony\Component\DomCrawler\Crawler; |
| 7 | + |
| 8 | +class ValidatorConstraintsTest extends TestCase |
| 9 | +{ |
| 10 | + public function testValidatorConstraintsWithWrongCodes(): void |
| 11 | + { |
| 12 | + // Login |
| 13 | + $this->setAll2faProvidersEnabled(false); |
| 14 | + $this->performLogin(); |
| 15 | + |
| 16 | + $submittedPage = $this->submitValidatorForm('wrongCode', 'wrongCode'); |
| 17 | + |
| 18 | + $this->assertValidatedWrongCodes($submittedPage); |
| 19 | + } |
| 20 | + |
| 21 | + public function testValidatorConstraintsWithCorrectCodes(): void |
| 22 | + { |
| 23 | + // Login |
| 24 | + $this->setAll2faProvidersEnabled(false); |
| 25 | + $this->performLogin(); |
| 26 | + |
| 27 | + $submittedPage = $this->submitValidatorForm($this->getGoogleAuthenticatorCode(), $this->getTotpCode()); |
| 28 | + |
| 29 | + $this->assertValidatedCorrectCodes($submittedPage); |
| 30 | + } |
| 31 | + |
| 32 | + private function submitValidatorForm(string $googleTotpCode, string $totpCode): Crawler |
| 33 | + { |
| 34 | + $formPage = $this->navigateToValidatorsForm(); |
| 35 | + |
| 36 | + $form = $formPage->selectButton('submit-button')->form(); |
| 37 | + $form['form[googleTotpCode]'] = $googleTotpCode; |
| 38 | + $form['form[totpCode]'] = $totpCode; |
| 39 | + |
| 40 | + return $this->client->submit($form); |
| 41 | + } |
| 42 | + |
| 43 | + private function assertValidatedWrongCodes(Crawler $submitPage): void |
| 44 | + { |
| 45 | + $this->assertResponseStatusCode(422); // Unprocessable Entity |
| 46 | + $this->assertStringContainsString( |
| 47 | + 'The verification code is not valid', |
| 48 | + $submitPage->html(), |
| 49 | + 'The page must show an error message' |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private function assertValidatedCorrectCodes(Crawler $submitPage): void |
| 54 | + { |
| 55 | + $this->assertResponseStatusCode(200); // Unprocessable Entity |
| 56 | + $this->assertStringContainsString( |
| 57 | + 'All codes valid!', |
| 58 | + $submitPage->html(), |
| 59 | + 'The page must show the success message' |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
0 commit comments