|
10 | 10 | namespace OxidEsales\SecurityModule\Tests\Unit\Authentication\TwoFactorAuth\OTP; |
11 | 11 |
|
12 | 12 | use DateTimeImmutable; |
| 13 | +use OxidEsales\Eshop\Core\Utils; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\Factory\OtpNotifierFactoryInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Notifier\OtpNotifierInterface; |
13 | 16 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\OtpFacade; |
14 | 17 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpChallengeStateServiceInterface; |
| 18 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpCodeGeneratorServiceInterface; |
15 | 19 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpCodeValidatorServiceInterface; |
| 20 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFASettingsInterface; |
16 | 21 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeStateInterface; |
17 | 22 | use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface; |
18 | 23 | use PHPUnit\Framework\Attributes\Test; |
@@ -113,13 +118,59 @@ public function verifyTriggersCodeValidator(): void |
113 | 118 | $sut->verify(userId: $userId, code: $code); |
114 | 119 | } |
115 | 120 |
|
| 121 | + #[Test] |
| 122 | + public function triggerChallengeGeneratesCodeCreatesStateAndNotifies(): void |
| 123 | + { |
| 124 | + $codeGeneratorStub = $this->createStub(OtpCodeGeneratorServiceInterface::class); |
| 125 | + $codeGeneratorStub->method('generateCode')->willReturn($code = uniqid()); |
| 126 | + |
| 127 | + $stateServiceSpy = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 128 | + $stateServiceSpy->expects($this->once()) |
| 129 | + ->method('createChallengeState') |
| 130 | + ->with($userId = uniqid(), $code); |
| 131 | + |
| 132 | + $notifierSpy = $this->createMock(OtpNotifierInterface::class); |
| 133 | + $notifierSpy->expects($this->once()) |
| 134 | + ->method('notify') |
| 135 | + ->with($userId, $code); |
| 136 | + |
| 137 | + $notifierFactoryStub = $this->createStub(OtpNotifierFactoryInterface::class); |
| 138 | + $notifierFactoryStub->method('create')->willReturn($notifierSpy); |
| 139 | + |
| 140 | + $settingsStub = $this->createStub(TwoFASettingsInterface::class); |
| 141 | + $settingsStub->method('getVerificationUrl')->willReturn($verificationUrl = uniqid()); |
| 142 | + |
| 143 | + $utilsSpy = $this->createMock(Utils::class); |
| 144 | + $utilsSpy->expects($this->once()) |
| 145 | + ->method('redirect') |
| 146 | + ->with($verificationUrl); |
| 147 | + |
| 148 | + $sut = $this->getSut( |
| 149 | + stateService: $stateServiceSpy, |
| 150 | + codeGenerator: $codeGeneratorStub, |
| 151 | + notifierFactory: $notifierFactoryStub, |
| 152 | + settings: $settingsStub, |
| 153 | + utils: $utilsSpy, |
| 154 | + ); |
| 155 | + |
| 156 | + $sut->triggerChallenge(userId: $userId); |
| 157 | + } |
| 158 | + |
116 | 159 | private function getSut( |
117 | 160 | OtpChallengeStateServiceInterface $stateService = null, |
118 | 161 | OtpCodeValidatorServiceInterface $codeValidator = null, |
| 162 | + OtpCodeGeneratorServiceInterface $codeGenerator = null, |
| 163 | + OtpNotifierFactoryInterface $notifierFactory = null, |
| 164 | + TwoFASettingsInterface $settings = null, |
| 165 | + Utils $utils = null, |
119 | 166 | ): OtpFacade { |
120 | 167 | return new OtpFacade( |
121 | 168 | stateService: $stateService ?? $this->createStub(OtpChallengeStateServiceInterface::class), |
122 | 169 | codeValidator: $codeValidator ?? $this->createStub(OtpCodeValidatorServiceInterface::class), |
| 170 | + codeGenerator: $codeGenerator ?? $this->createStub(OtpCodeGeneratorServiceInterface::class), |
| 171 | + notifierFactory: $notifierFactory ?? $this->createStub(OtpNotifierFactoryInterface::class), |
| 172 | + settings: $settings ?? $this->createStub(TwoFASettingsInterface::class), |
| 173 | + utils: $utils ?? $this->createStub(Utils::class), |
123 | 174 | ); |
124 | 175 | } |
125 | 176 | } |
0 commit comments