|
| 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; |
| 11 | + |
| 12 | +use DateTimeImmutable; |
| 13 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\OtpFacade; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpChallengeStateServiceInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpCodeValidatorServiceInterface; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeStateInterface; |
| 17 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Service\TwoFAServiceInterface; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | + |
| 21 | +class OTPServiceTest extends TestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + public function isVerifiedReturnsFalseWhenNoChallengeState(): void |
| 25 | + { |
| 26 | + $stateServiceMock = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 27 | + $stateServiceMock->expects($this->once()) |
| 28 | + ->method('getChallengeState') |
| 29 | + ->with($userId = uniqid()) |
| 30 | + ->willReturn(null); |
| 31 | + |
| 32 | + $sut = $this->getSut(stateService: $stateServiceMock); |
| 33 | + |
| 34 | + $this->assertFalse($sut->isVerified(userId: $userId)); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function isVerifiedReturnsFalseWhenVerifiedAtIsNull(): void |
| 39 | + { |
| 40 | + $stateStub = $this->createStub(OtpChallengeStateInterface::class); |
| 41 | + $stateStub->method('getVerifiedAt')->willReturn(null); |
| 42 | + |
| 43 | + $stateServiceMock = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 44 | + $stateServiceMock->expects($this->once()) |
| 45 | + ->method('getChallengeState') |
| 46 | + ->with($userId = uniqid()) |
| 47 | + ->willReturn($stateStub); |
| 48 | + |
| 49 | + $sut = $this->getSut(stateService: $stateServiceMock); |
| 50 | + |
| 51 | + $this->assertFalse($sut->isVerified(userId: $userId)); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function isVerifiedReturnsFalseWhenExpired(): void |
| 56 | + { |
| 57 | + $stateStub = $this->createStub(OtpChallengeStateInterface::class); |
| 58 | + $stateStub->method('getVerifiedAt')->willReturn(new DateTimeImmutable()); |
| 59 | + $stateStub->method('getExpiresAt')->willReturn(new DateTimeImmutable('-1 second')); |
| 60 | + |
| 61 | + $stateServiceMock = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 62 | + $stateServiceMock->expects($this->once()) |
| 63 | + ->method('getChallengeState') |
| 64 | + ->with($userId = uniqid()) |
| 65 | + ->willReturn($stateStub); |
| 66 | + |
| 67 | + $sut = $this->getSut(stateService: $stateServiceMock); |
| 68 | + |
| 69 | + $this->assertFalse($sut->isVerified(userId: $userId)); |
| 70 | + } |
| 71 | + |
| 72 | + #[Test] |
| 73 | + public function isVerifiedReturnsTrueWhenVerifiedAtIsSet(): void |
| 74 | + { |
| 75 | + $stateStub = $this->createStub(OtpChallengeStateInterface::class); |
| 76 | + $stateStub->method('getVerifiedAt')->willReturn(new DateTimeImmutable()); |
| 77 | + $stateStub->method('getExpiresAt')->willReturn(new DateTimeImmutable('+5 minutes')); |
| 78 | + |
| 79 | + $stateServiceMock = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 80 | + $stateServiceMock->expects($this->once()) |
| 81 | + ->method('getChallengeState') |
| 82 | + ->with($userId = uniqid()) |
| 83 | + ->willReturn($stateStub); |
| 84 | + |
| 85 | + $sut = $this->getSut(stateService: $stateServiceMock); |
| 86 | + |
| 87 | + $this->assertTrue($sut->isVerified(userId: $userId)); |
| 88 | + } |
| 89 | + |
| 90 | + #[Test] |
| 91 | + public function invalidateChallengeDeletesChallengeState(): void |
| 92 | + { |
| 93 | + $stateServiceSpy = $this->createMock(OtpChallengeStateServiceInterface::class); |
| 94 | + $stateServiceSpy->expects($this->once()) |
| 95 | + ->method('deleteChallengeState') |
| 96 | + ->with($userId = uniqid()); |
| 97 | + |
| 98 | + $sut = $this->getSut(stateService: $stateServiceSpy); |
| 99 | + |
| 100 | + $sut->invalidateChallenge(userId: $userId); |
| 101 | + } |
| 102 | + |
| 103 | + #[Test] |
| 104 | + public function verifyTriggersCodeValidator(): void |
| 105 | + { |
| 106 | + $codeValidatorSpy = $this->createMock(OtpCodeValidatorServiceInterface::class); |
| 107 | + $codeValidatorSpy->expects($this->once()) |
| 108 | + ->method('validateCode') |
| 109 | + ->with($userId = uniqid(), $code = uniqid()); |
| 110 | + |
| 111 | + $sut = $this->getSut(codeValidator: $codeValidatorSpy); |
| 112 | + |
| 113 | + $sut->verify(userId: $userId, code: $code); |
| 114 | + } |
| 115 | + |
| 116 | + private function getSut( |
| 117 | + OtpChallengeStateServiceInterface $stateService = null, |
| 118 | + OtpCodeValidatorServiceInterface $codeValidator = null, |
| 119 | + ): OtpFacade { |
| 120 | + return new OtpFacade( |
| 121 | + stateService: $stateService ?? $this->createStub(OtpChallengeStateServiceInterface::class), |
| 122 | + codeValidator: $codeValidator ?? $this->createStub(OtpCodeValidatorServiceInterface::class), |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments