|
| 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\Integration\Authentication\TwoFactorAuth\Controller; |
| 11 | + |
| 12 | +use OxidEsales\Eshop\Core\UtilsView; |
| 13 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\TwoFactorAuthController; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAResendableInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAUserServiceInterface; |
| 17 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\AuthCodeRequestInterface; |
| 18 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Transput\JsonResponseInterface; |
| 19 | +use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase; |
| 20 | +use PHPUnit\Framework\Attributes\Test; |
| 21 | +use PHPUnit\Framework\MockObject\Generator\MockClass; |
| 22 | +use PHPUnit\Framework\MockObject\MockObject; |
| 23 | + |
| 24 | +class TwoFactorAuthControllerTest extends IntegrationTestCase |
| 25 | +{ |
| 26 | + #[Test] |
| 27 | + public function renderSetsResendableFalseWhenServiceIsNotResendable(): void |
| 28 | + { |
| 29 | + $sut = $this->getSut( |
| 30 | + twoFAService: $this->createStub(TwoFAServiceInterface::class), |
| 31 | + ); |
| 32 | + $sut->render(); |
| 33 | + |
| 34 | + $this->assertFalse($sut->getViewDataElement('resendable')); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function renderSetsResendableTrueAndParamsFromServiceWhenResendable(): void |
| 39 | + { |
| 40 | + $userId = uniqid(); |
| 41 | + |
| 42 | + $twoFAUserServiceStub = $this->createStub(TwoFAUserServiceInterface::class); |
| 43 | + $twoFAUserServiceStub->method('getPendingUserId')->willReturn($userId); |
| 44 | + |
| 45 | + /** @var TwoFAServiceInterface&TwoFAResendableInterface&MockObject $twoFAServiceStub */ |
| 46 | + $twoFAServiceStub = $this->createMockForIntersectionOfInterfaces([ |
| 47 | + TwoFAServiceInterface::class, |
| 48 | + TwoFAResendableInterface::class, |
| 49 | + ]); |
| 50 | + $twoFAServiceStub->method('getRemainingAttempts')->with($userId)->willReturn($remaining = random_int(1, 5)); |
| 51 | + $twoFAServiceStub->method('getCooldownRemaining')->with($userId)->willReturn($cooldown = random_int(0, 30)); |
| 52 | + |
| 53 | + $sut = $this->getSut( |
| 54 | + twoFAService: $twoFAServiceStub, |
| 55 | + twoFAUserService: $twoFAUserServiceStub, |
| 56 | + ); |
| 57 | + $sut->render(); |
| 58 | + |
| 59 | + $this->assertTrue($sut->getViewDataElement('resendable')); |
| 60 | + $this->assertSame($remaining, $sut->getViewDataElement('remainingAttempts')); |
| 61 | + $this->assertSame($cooldown, $sut->getViewDataElement('resendCooldownRemaining')); |
| 62 | + } |
| 63 | + |
| 64 | + private function getSut( |
| 65 | + TwoFAServiceInterface $twoFAService = null, |
| 66 | + TwoFAUserServiceInterface $twoFAUserService = null, |
| 67 | + AuthCodeRequestInterface $authCodeRequest = null, |
| 68 | + UtilsView $utilsView = null, |
| 69 | + JsonResponseInterface $jsonResponse = null, |
| 70 | + ): TwoFactorAuthController { |
| 71 | + return new TwoFactorAuthController( |
| 72 | + twoFAService: $twoFAService ?? $this->createStub(TwoFAServiceInterface::class), |
| 73 | + twoFAUserService: $twoFAUserService ?? $this->createStub(TwoFAUserServiceInterface::class), |
| 74 | + authCodeRequest: $authCodeRequest ?? $this->createStub(AuthCodeRequestInterface::class), |
| 75 | + utilsView: $utilsView ?? $this->createStub(UtilsView::class), |
| 76 | + jsonResponse: $jsonResponse ?? $this->createStub(JsonResponseInterface::class), |
| 77 | + ); |
| 78 | + } |
| 79 | +} |
0 commit comments