|
| 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\OTP\Infrastructure\Repository; |
| 11 | + |
| 12 | +use DateTimeImmutable; |
| 13 | +use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface; |
| 14 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\DTO\OtpChallengeStateInterface; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Infrastructure\Repository\OtpChallengeStateRepository; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\OTP\Infrastructure\Repository\OtpChallengeStateRepositoryInterface; |
| 17 | +use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | + |
| 20 | +class OtpChallengeStateRepositoryTest extends IntegrationTestCase |
| 21 | +{ |
| 22 | + public function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + $this->cleanupTable(); |
| 27 | + } |
| 28 | + |
| 29 | + #[Test] |
| 30 | + public function findByUserIdReturnsNullWhenNotFound(): void |
| 31 | + { |
| 32 | + $sut = $this->getSut(); |
| 33 | + |
| 34 | + $result = $sut->findByUserId(uniqid()); |
| 35 | + |
| 36 | + $this->assertNull($result); |
| 37 | + } |
| 38 | + |
| 39 | + #[Test] |
| 40 | + public function createChallengeStateAndFind(): void |
| 41 | + { |
| 42 | + $sut = $this->getSut(); |
| 43 | + |
| 44 | + $sut->createChallengeState( |
| 45 | + userId: $userId = uniqid(), |
| 46 | + codeHash: $codeHash = uniqid(), |
| 47 | + expiresAt: $expiresAt = new DateTimeImmutable('+5 minutes'), |
| 48 | + ); |
| 49 | + |
| 50 | + $result = $sut->findByUserId($userId); |
| 51 | + |
| 52 | + $this->assertInstanceOf(OtpChallengeStateInterface::class, $result); |
| 53 | + $this->assertSame($userId, $result->getUserId()); |
| 54 | + $this->assertSame($codeHash, $result->getCodeHash()); |
| 55 | + $this->assertSame(0, $result->getAttempts()); |
| 56 | + $this->assertNotNull($result->getLastSentAt()); |
| 57 | + $this->assertSame($expiresAt->format('Y-m-d H:i:s'), $result->getExpiresAt()->format('Y-m-d H:i:s')); |
| 58 | + $this->assertNull($result->getVerifiedAt()); |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + public function createChallengeStateOverwritesExistingChallengeState(): void |
| 63 | + { |
| 64 | + $sut = $this->getSut(); |
| 65 | + |
| 66 | + $sut->createChallengeState( |
| 67 | + userId: $userId = uniqid(), |
| 68 | + codeHash: 'first_hash', |
| 69 | + expiresAt: new DateTimeImmutable('+5 minutes'), |
| 70 | + ); |
| 71 | + |
| 72 | + $sut->createChallengeState( |
| 73 | + userId: $userId, |
| 74 | + codeHash: 'second_hash', |
| 75 | + expiresAt: $secondExpiresAt = new DateTimeImmutable('+10 minutes'), |
| 76 | + ); |
| 77 | + |
| 78 | + $result = $sut->findByUserId($userId); |
| 79 | + |
| 80 | + $this->assertSame('second_hash', $result->getCodeHash()); |
| 81 | + $this->assertSame( |
| 82 | + $secondExpiresAt->format('Y-m-d H:i:s'), |
| 83 | + $result->getExpiresAt()->format('Y-m-d H:i:s') |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + #[Test] |
| 88 | + public function markVerifiedSetsVerifiedAt(): void |
| 89 | + { |
| 90 | + $sut = $this->getSut(); |
| 91 | + $sut->createChallengeState( |
| 92 | + userId: $userId = uniqid(), |
| 93 | + codeHash: uniqid(), |
| 94 | + expiresAt: new DateTimeImmutable('+5 minutes'), |
| 95 | + ); |
| 96 | + |
| 97 | + $sut->markVerified($userId); |
| 98 | + |
| 99 | + $result = $sut->findByUserId($userId); |
| 100 | + $this->assertNotNull($result->getVerifiedAt()); |
| 101 | + } |
| 102 | + |
| 103 | + #[Test] |
| 104 | + public function markResentUpdatesLastSentAtAndExpiresAt(): void |
| 105 | + { |
| 106 | + $sut = $this->getSut(); |
| 107 | + $sut->createChallengeState( |
| 108 | + userId: $userId = uniqid(), |
| 109 | + codeHash: uniqid(), |
| 110 | + expiresAt: new DateTimeImmutable('+5 minutes'), |
| 111 | + ); |
| 112 | + |
| 113 | + $sut->markResent( |
| 114 | + userId: $userId, |
| 115 | + expiresAt: $newExpiresAt = new DateTimeImmutable('+10 minutes'), |
| 116 | + ); |
| 117 | + |
| 118 | + $result = $sut->findByUserId($userId); |
| 119 | + $this->assertGreaterThanOrEqual( |
| 120 | + new DateTimeImmutable('-2 seconds'), |
| 121 | + $result->getLastSentAt() |
| 122 | + ); |
| 123 | + $this->assertSame($newExpiresAt->format('Y-m-d H:i:s'), $result->getExpiresAt()->format('Y-m-d H:i:s')); |
| 124 | + } |
| 125 | + |
| 126 | + #[Test] |
| 127 | + public function incrementAttempts(): void |
| 128 | + { |
| 129 | + $sut = $this->getSut(); |
| 130 | + $sut->createChallengeState( |
| 131 | + userId: $userId = uniqid(), |
| 132 | + codeHash: uniqid(), |
| 133 | + expiresAt: new DateTimeImmutable('+5 minutes'), |
| 134 | + ); |
| 135 | + |
| 136 | + $sut->incrementAttempts($userId); |
| 137 | + |
| 138 | + $result = $sut->findByUserId($userId); |
| 139 | + $this->assertSame(1, $result->getAttempts()); |
| 140 | + } |
| 141 | + |
| 142 | + #[Test] |
| 143 | + public function deleteChallengeStateRemovesChallenge(): void |
| 144 | + { |
| 145 | + $sut = $this->getSut(); |
| 146 | + $sut->createChallengeState( |
| 147 | + userId: $userId = uniqid(), |
| 148 | + codeHash: uniqid(), |
| 149 | + expiresAt: new DateTimeImmutable('+5 minutes'), |
| 150 | + ); |
| 151 | + |
| 152 | + $sut->deleteChallengeState($userId); |
| 153 | + |
| 154 | + $this->assertNull($sut->findByUserId($userId)); |
| 155 | + } |
| 156 | + |
| 157 | + #[Test] |
| 158 | + public function deleteChallengeStateNonExistentUserDoesNotExplode(): void |
| 159 | + { |
| 160 | + $sut = $this->getSut(); |
| 161 | + |
| 162 | + $sut->deleteChallengeState(uniqid()); |
| 163 | + |
| 164 | + $this->addToAssertionCount(1); |
| 165 | + } |
| 166 | + |
| 167 | + private function getSut(): OtpChallengeStateRepositoryInterface |
| 168 | + { |
| 169 | + return new OtpChallengeStateRepository( |
| 170 | + queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class), |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + private function cleanupTable(): void |
| 175 | + { |
| 176 | + $this->get(QueryBuilderFactoryInterface::class) |
| 177 | + ->create() |
| 178 | + ->getConnection() |
| 179 | + ->executeStatement('DELETE FROM oesm_2fa_otp'); |
| 180 | + } |
| 181 | +} |
0 commit comments