|
| 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\Service; |
| 11 | + |
| 12 | +use DateTimeImmutable; |
| 13 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeStateInterface; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Infrastructure\Repository\OtpChallengeStateRepositoryInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpChallengeStateService; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Service\OtpCodeHasherServiceInterface; |
| 17 | +use PHPUnit\Framework\Attributes\Test; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +class OtpChallengeStateServiceTest extends TestCase |
| 21 | +{ |
| 22 | + #[Test] |
| 23 | + public function getChallengeStateProxiesToRepository(): void |
| 24 | + { |
| 25 | + $stateStub = $this->createStub(OtpChallengeStateInterface::class); |
| 26 | + |
| 27 | + $repositoryMock = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 28 | + $repositoryMock->expects($this->once()) |
| 29 | + ->method('findByUserId') |
| 30 | + ->with($userId = uniqid()) |
| 31 | + ->willReturn($stateStub); |
| 32 | + |
| 33 | + $sut = $this->getSut(repository: $repositoryMock); |
| 34 | + |
| 35 | + $this->assertSame($stateStub, $sut->getChallengeState(userId: $userId)); |
| 36 | + } |
| 37 | + |
| 38 | + #[Test] |
| 39 | + public function createChallengeStateHashesCodeAndPassesToRepository(): void |
| 40 | + { |
| 41 | + $hasherMock = $this->createMock(OtpCodeHasherServiceInterface::class); |
| 42 | + $hasherMock->expects($this->once()) |
| 43 | + ->method('hash') |
| 44 | + ->with($code = uniqid()) |
| 45 | + ->willReturn($codeHash = uniqid()); |
| 46 | + |
| 47 | + $repositorySpy = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 48 | + $repositorySpy->expects($this->once()) |
| 49 | + ->method('createChallengeState') |
| 50 | + ->with( |
| 51 | + $userId = uniqid(), |
| 52 | + $codeHash, |
| 53 | + $this->callback(fn(DateTimeImmutable $expiresAt) => $expiresAt > new DateTimeImmutable()) |
| 54 | + ); |
| 55 | + |
| 56 | + $sut = $this->getSut(repository: $repositorySpy, hasher: $hasherMock); |
| 57 | + |
| 58 | + $sut->createChallengeState(userId: $userId, code: $code); |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + public function refreshChallengeStateHashesCodeAndPassesToRepository(): void |
| 63 | + { |
| 64 | + $hasherMock = $this->createMock(OtpCodeHasherServiceInterface::class); |
| 65 | + $hasherMock->expects($this->once()) |
| 66 | + ->method('hash') |
| 67 | + ->with($code = uniqid()) |
| 68 | + ->willReturn($codeHash = uniqid()); |
| 69 | + |
| 70 | + $repositorySpy = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 71 | + $repositorySpy->expects($this->once()) |
| 72 | + ->method('refreshChallengeState') |
| 73 | + ->with( |
| 74 | + $userId = uniqid(), |
| 75 | + $codeHash, |
| 76 | + $this->callback(fn(DateTimeImmutable $expiresAt) => $expiresAt > new DateTimeImmutable()) |
| 77 | + ); |
| 78 | + |
| 79 | + $sut = $this->getSut(repository: $repositorySpy, hasher: $hasherMock); |
| 80 | + |
| 81 | + $sut->refreshChallengeState(userId: $userId, code: $code); |
| 82 | + } |
| 83 | + |
| 84 | + #[Test] |
| 85 | + public function markVerifiedProxiesToRepository(): void |
| 86 | + { |
| 87 | + $repositorySpy = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 88 | + $repositorySpy->expects($this->once()) |
| 89 | + ->method('markVerified') |
| 90 | + ->with($userId = uniqid()); |
| 91 | + |
| 92 | + $sut = $this->getSut(repository: $repositorySpy); |
| 93 | + |
| 94 | + $sut->markVerified(userId: $userId); |
| 95 | + } |
| 96 | + |
| 97 | + #[Test] |
| 98 | + public function incrementAttemptsProxiesToRepository(): void |
| 99 | + { |
| 100 | + $repositorySpy = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 101 | + $repositorySpy->expects($this->once()) |
| 102 | + ->method('incrementAttempts') |
| 103 | + ->with($userId = uniqid()); |
| 104 | + |
| 105 | + $sut = $this->getSut(repository: $repositorySpy); |
| 106 | + |
| 107 | + $sut->incrementAttempts(userId: $userId); |
| 108 | + } |
| 109 | + |
| 110 | + #[Test] |
| 111 | + public function deleteChallengeStateProxiesToRepository(): void |
| 112 | + { |
| 113 | + $repositorySpy = $this->createMock(OtpChallengeStateRepositoryInterface::class); |
| 114 | + $repositorySpy->expects($this->once()) |
| 115 | + ->method('deleteChallengeState') |
| 116 | + ->with($userId = uniqid()); |
| 117 | + |
| 118 | + $sut = $this->getSut(repository: $repositorySpy); |
| 119 | + |
| 120 | + $sut->deleteChallengeState(userId: $userId); |
| 121 | + } |
| 122 | + |
| 123 | + private function getSut( |
| 124 | + OtpChallengeStateRepositoryInterface $repository = null, |
| 125 | + OtpCodeHasherServiceInterface $hasher = null, |
| 126 | + ): OtpChallengeStateService { |
| 127 | + return new OtpChallengeStateService( |
| 128 | + challengeStateRepository: $repository ?? $this->createStub(OtpChallengeStateRepositoryInterface::class), |
| 129 | + codeHasher: $hasher ?? $this->createStub(OtpCodeHasherServiceInterface::class), |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
0 commit comments