Skip to content

Commit 7b5d760

Browse files
committed
OXDEV-9078 Reuse what we had for Email notification for the new implementation
1 parent 1b724a2 commit 7b5d760

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

src/Authentication/TwoFactorAuth/OTP/Notifier/Email/OtpEmailNotifier.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,27 @@
99

1010
namespace OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\Email;
1111

12+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\EmailFactoryInterface;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\NewUserRepositoryInterface;
1214
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\OtpNotifierInterface;
1315

1416
class OtpEmailNotifier implements OtpNotifierInterface
1517
{
18+
public function __construct(
19+
private EmailFactoryInterface $emailFactory,
20+
private NewUserRepositoryInterface $userRepository,
21+
) {
22+
}
23+
1624
public function notify(string $userId, #[\SensitiveParameter] string $code): void
1725
{
18-
// todo-critical: implement email notifier
26+
$email = $this->userRepository->getUserById($userId)->getEmail();
27+
28+
// todo-critical: load the translations
29+
$this->emailFactory->create()->sendEmail(
30+
$email,
31+
'Your verification code',
32+
"Your verification code is: {$code}"
33+
);
1934
}
2035
}

src/Authentication/TwoFactorAuth/OTP/OtpFacade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public function triggerChallenge(string $userId): void
4444
{
4545
$code = $this->codeGenerator->generateCode();
4646

47+
// todo-critical: check if we can trigger the notification
48+
4749
$this->stateService->createChallengeState($userId, $code);
4850
$this->notifierFactory->create($userId)->notify($userId, $code);
4951

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* Copyright © OXID eSales AG. All rights reserved.
5+
* See LICENSE file for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\OTP\Notifier\Email;
11+
12+
use OxidEsales\Eshop\Core\Email;
13+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\DTO\NewUserInterface;
14+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Factory\EmailFactoryInterface;
15+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Infrastructure\Repository\NewUserRepositoryInterface;
16+
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\Email\OtpEmailNotifier;
17+
use PHPUnit\Framework\Attributes\Test;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class OtpEmailNotifierTest extends TestCase
21+
{
22+
#[Test]
23+
public function notifySendsEmailToUserAddress(): void
24+
{
25+
$email = uniqid() . '@example.com';
26+
$code = uniqid();
27+
28+
$userStub = $this->createStub(NewUserInterface::class);
29+
$userStub->method('getEmail')->willReturn($email);
30+
31+
$userRepositoryMock = $this->createMock(NewUserRepositoryInterface::class);
32+
$userRepositoryMock->expects($this->once())
33+
->method('getUserById')
34+
->with($userId = uniqid())
35+
->willReturn($userStub);
36+
37+
$emailModelSpy = $this->createMock(Email::class);
38+
$emailModelSpy->expects($this->once())
39+
->method('sendEmail')
40+
->with(
41+
$email,
42+
'Your verification code',
43+
"Your verification code is: {$code}"
44+
);
45+
46+
$emailFactoryStub = $this->createStub(EmailFactoryInterface::class);
47+
$emailFactoryStub->method('create')->willReturn($emailModelSpy);
48+
49+
$sut = $this->getSut(
50+
emailFactory: $emailFactoryStub,
51+
userRepository: $userRepositoryMock,
52+
);
53+
54+
$sut->notify(userId: $userId, code: $code);
55+
}
56+
57+
private function getSut(
58+
EmailFactoryInterface $emailFactory = null,
59+
NewUserRepositoryInterface $userRepository = null,
60+
): OtpEmailNotifier {
61+
return new OtpEmailNotifier(
62+
emailFactory: $emailFactory ?? $this->createStub(EmailFactoryInterface::class),
63+
userRepository: $userRepository ?? $this->createStub(NewUserRepositoryInterface::class),
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)