|
| 1 | +<?php namespace Tests\Unit\MFA; |
| 2 | + |
| 3 | +use Auth\Exceptions\AuthenticationException; |
| 4 | +use Auth\Repositories\IUserRecoveryCodeRepository; |
| 5 | +use Auth\User; |
| 6 | +use Illuminate\Support\Facades\Hash; |
| 7 | +use Illuminate\Support\Facades\Session; |
| 8 | +use Models\OAuth2\Client; |
| 9 | +use Strategies\MFA\AbstractMFAChallengeStrategy; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class AbstractMFAChallengeStrategyTest extends TestCase |
| 13 | +{ |
| 14 | + private AbstractMFAChallengeStrategy $strategy; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + $repo = \Mockery::mock(IUserRecoveryCodeRepository::class); |
| 20 | + $this->strategy = new class($repo) extends AbstractMFAChallengeStrategy { |
| 21 | + public function issueChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 22 | + public function verifyChallenge(User $user, string $code): void {} |
| 23 | + public function resendChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 24 | + public function exposeStorePendingState(int $userId, bool $remember): void { |
| 25 | + $this->storePendingState($userId, $remember); |
| 26 | + } |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + protected function tearDown(): void |
| 31 | + { |
| 32 | + \Mockery::close(); |
| 33 | + parent::tearDown(); |
| 34 | + } |
| 35 | + |
| 36 | + public function testGetPendingState_withValidSession_returnsState(): void |
| 37 | + { |
| 38 | + $this->strategy->exposeStorePendingState(42, true); |
| 39 | + |
| 40 | + $state = $this->strategy->getPendingState(); |
| 41 | + |
| 42 | + $this->assertNotNull($state); |
| 43 | + $this->assertSame(42, $state['user_id']); |
| 44 | + $this->assertTrue($state['remember']); |
| 45 | + $this->assertArrayHasKey('pending_at', $state); |
| 46 | + } |
| 47 | + |
| 48 | + public function testGetPendingState_withExpiredSession_returnsNull(): void |
| 49 | + { |
| 50 | + Session::put('2fa_pending_user_id', 99); |
| 51 | + Session::put('2fa_pending_at', time() - 301); |
| 52 | + Session::put('2fa_remember', false); |
| 53 | + |
| 54 | + $state = $this->strategy->getPendingState(); |
| 55 | + |
| 56 | + $this->assertNull($state); |
| 57 | + $this->assertNull(Session::get('2fa_pending_user_id')); |
| 58 | + } |
| 59 | + |
| 60 | + public function testGetPendingState_withMissingSession_returnsNull(): void |
| 61 | + { |
| 62 | + $state = $this->strategy->getPendingState(); |
| 63 | + |
| 64 | + $this->assertNull($state); |
| 65 | + } |
| 66 | + |
| 67 | + public function testClearPendingState_removesAllSessionKeys(): void |
| 68 | + { |
| 69 | + Session::put('2fa_pending_user_id', 7); |
| 70 | + Session::put('2fa_pending_at', time()); |
| 71 | + Session::put('2fa_remember', true); |
| 72 | + Session::put('2fa_recovery_attempts', 1); |
| 73 | + |
| 74 | + $this->strategy->clearPendingState(); |
| 75 | + |
| 76 | + $this->assertNull(Session::get('2fa_pending_user_id')); |
| 77 | + $this->assertNull(Session::get('2fa_pending_at')); |
| 78 | + $this->assertNull(Session::get('2fa_remember')); |
| 79 | + $this->assertNull(Session::get('2fa_recovery_attempts')); |
| 80 | + } |
| 81 | + |
| 82 | + public function testVerifyRecoveryCode_withMatchingCode_marksAsUsed(): void |
| 83 | + { |
| 84 | + $user = new User(); |
| 85 | + $code = 'VALID-CODE'; |
| 86 | + |
| 87 | + $recoveryCode = \Mockery::mock(\App\libs\Auth\Models\UserRecoveryCode::class); |
| 88 | + $recoveryCode->shouldReceive('getCodeHash')->andReturn(Hash::make($code)); |
| 89 | + $recoveryCode->shouldReceive('markUsed')->once(); |
| 90 | + |
| 91 | + $repo = \Mockery::mock(IUserRecoveryCodeRepository::class); |
| 92 | + $repo->shouldReceive('getUnusedByUser')->with($user)->andReturn([$recoveryCode]); |
| 93 | + |
| 94 | + $strategy = new class($repo) extends AbstractMFAChallengeStrategy { |
| 95 | + public function issueChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 96 | + public function verifyChallenge(User $user, string $code): void {} |
| 97 | + public function resendChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 98 | + }; |
| 99 | + |
| 100 | + $strategy->verifyRecoveryCode($user, $code); |
| 101 | + $this->addToAssertionCount(1); // markUsed()->once() verified by Mockery in tearDown |
| 102 | + } |
| 103 | + |
| 104 | + public function testVerifyRecoveryCode_withNonMatchingCode_throwsException(): void |
| 105 | + { |
| 106 | + $user = new User(); |
| 107 | + |
| 108 | + $recoveryCode = \Mockery::mock(\App\libs\Auth\Models\UserRecoveryCode::class); |
| 109 | + $recoveryCode->shouldReceive('getCodeHash')->andReturn(Hash::make('CORRECT-CODE')); |
| 110 | + $recoveryCode->shouldNotReceive('markUsed'); |
| 111 | + |
| 112 | + $repo = \Mockery::mock(IUserRecoveryCodeRepository::class); |
| 113 | + $repo->shouldReceive('getUnusedByUser')->andReturn([$recoveryCode]); |
| 114 | + |
| 115 | + $strategy = new class($repo) extends AbstractMFAChallengeStrategy { |
| 116 | + public function issueChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 117 | + public function verifyChallenge(User $user, string $code): void {} |
| 118 | + public function resendChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 119 | + }; |
| 120 | + |
| 121 | + $this->expectException(AuthenticationException::class); |
| 122 | + $this->expectExceptionMessage("Invalid recovery code."); |
| 123 | + $strategy->verifyRecoveryCode($user, 'WRONG-CODE'); |
| 124 | + } |
| 125 | + |
| 126 | + public function testVerifyRecoveryCode_withAllCodesUsed_throwsException(): void |
| 127 | + { |
| 128 | + $user = new User(); |
| 129 | + |
| 130 | + $repo = \Mockery::mock(IUserRecoveryCodeRepository::class); |
| 131 | + $repo->shouldReceive('getUnusedByUser')->andReturn([]); |
| 132 | + |
| 133 | + $strategy = new class($repo) extends AbstractMFAChallengeStrategy { |
| 134 | + public function issueChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 135 | + public function verifyChallenge(User $user, string $code): void {} |
| 136 | + public function resendChallenge(User $user, ?Client $client, bool $remember): array { return []; } |
| 137 | + }; |
| 138 | + |
| 139 | + $this->expectException(AuthenticationException::class); |
| 140 | + $this->expectExceptionMessage("Invalid recovery code."); |
| 141 | + $strategy->verifyRecoveryCode($user, 'ANY-CODE'); |
| 142 | + } |
| 143 | +} |
0 commit comments