Skip to content

Commit 9415c2b

Browse files
committed
Create tests for MFA pages [temp]
1 parent f9d56f2 commit 9415c2b

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

src/Controller/Dashboard/DashboardAccountController.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ public function mfa(Request $request, #[CurrentUser] User $user): Response
112112
$totpCode = $form->get('totpCode')->getData();
113113

114114
if (!$this->totpAuthenticator->checkCode($user, $totpCode)) {
115-
$user->setTotpSecret(null);
116-
117115
$form->get('totpCode')->addError(new FormError('account.error.totp-incorrect'));
118116
}
119117

src/Doctrine/Entity/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,6 @@ public function getTotpAuthenticationUsername(): string
189189

190190
public function getTotpAuthenticationConfiguration(): ?TotpConfigurationInterface
191191
{
192-
return new TotpConfiguration($this->totpSecret, TotpConfiguration::ALGORITHM_SHA1, 30, 6);
192+
return $this->totpSecret ? new TotpConfiguration($this->totpSecret, TotpConfiguration::ALGORITHM_SHA1, 30, 6) : null;
193193
}
194194
}

tests/FunctionalTests/Controller/Dashboard/DashboardAccountControllerTest.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use CodedMonkey\Dirigent\Tests\Helper\WebTestCaseTrait;
99
use Doctrine\ORM\EntityManagerInterface;
1010
use PHPUnit\Framework\Attributes\CoversClass;
11-
use PHPUnit\Framework\Attributes\RequiresFunction;
1211
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpFactory;
1312
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1413
use Symfony\Component\HttpFoundation\Response;
@@ -28,7 +27,7 @@ public function testMfaUnauthenticated(): void
2827
$this->assertResponseRedirects('/login', Response::HTTP_FOUND);
2928
}
3029

31-
#[RequiresFunction('\Safe\unpack')] // todo test fails with Lowest Composer dependencies
30+
// #[RequiresFunction('\Safe\unpack')] // todo test fails with Lowest Composer dependencies
3231
public function testMfaSetup(): void
3332
{
3433
$client = static::createClient();
@@ -48,9 +47,58 @@ public function testMfaSetup(): void
4847

4948
$this->assertResponseRedirects('/account', Response::HTTP_FOUND);
5049

50+
$user = $this->getService(EntityManagerInterface::class)->find(User::class, $user->getId());
51+
5152
$this->assertNotNull($user->getTotpSecret());
5253
}
5354

55+
public function testMfaSetupWrongPassword(): void
56+
{
57+
$client = static::createClient();
58+
$totpFactory = $this->getService(TotpFactory::class, 'scheb_two_factor.security.totp_factory');
59+
60+
$user = $this->createMockUser();
61+
$this->persistEntities($user);
62+
63+
$client->loginUser($user);
64+
65+
$client->request('GET', '/account/mfa');
66+
67+
$client->submitForm('Enable MFA authentication', [
68+
'mfa_setup_form[currentPassword]' => 'OddPassword11',
69+
'mfa_setup_form[totpCode]' => $totpFactory->createTotpForUser($user)->now(),
70+
]);
71+
72+
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
73+
74+
$user = $this->getService(EntityManagerInterface::class)->find(User::class, $user->getId());
75+
76+
$this->assertNull($user->getTotpSecret());
77+
}
78+
79+
public function testMfaSetupWrongTotpCode(): void
80+
{
81+
$client = static::createClient();
82+
83+
$user = $this->createMockUser();
84+
$this->persistEntities($user);
85+
86+
$client->loginUser($user);
87+
88+
$client->request('GET', '/account/mfa');
89+
90+
$client->submitForm('Enable MFA authentication', [
91+
'mfa_setup_form[currentPassword]' => 'PlainPassword99',
92+
'mfa_setup_form[totpCode]' => 'abcdef',
93+
]);
94+
95+
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
96+
97+
$user = $this->getService(EntityManagerInterface::class)->find(User::class, $user->getId());
98+
99+
$this->assertNull($user->getTotpSecret());
100+
}
101+
54102
public function testMfaClear(): void
55103
{
56104
$client = static::createClient();
@@ -73,6 +121,28 @@ public function testMfaClear(): void
73121
$this->assertNull($user->getTotpSecret());
74122
}
75123

124+
public function testMfaClearWrongPassword(): void
125+
{
126+
$client = static::createClient();
127+
128+
$user = $this->createMockUser(mfaEnabled: true);
129+
$this->persistEntities($user);
130+
131+
$client->loginUser($user);
132+
133+
$client->request('GET', '/account/mfa');
134+
135+
$client->submitForm('Disable MFA authentication', [
136+
'mfa_clear_form[currentPassword]' => 'OddPassword11',
137+
]);
138+
139+
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
140+
141+
$user = $this->getService(EntityManagerInterface::class)->find(User::class, $user->getId());
142+
143+
$this->assertNotNull($user->getTotpSecret());
144+
}
145+
76146
public function testMfaQrCodeUnauthenticated(): void
77147
{
78148
$client = static::createClient();

0 commit comments

Comments
 (0)