|
| 1 | +<?php namespace Tests\Unit; |
| 2 | +/** |
| 3 | + * Copyright 2025 OpenStack Foundation |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + **/ |
| 14 | + |
| 15 | +use App\Services\Auth\IDeviceTrustService; |
| 16 | +use App\Services\Auth\MFAGateService; |
| 17 | +use Auth\User; |
| 18 | +use Mockery; |
| 19 | +use Tests\TestCase; |
| 20 | +use Mockery\MockInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Class MFAGateServiceTest |
| 24 | + * @package Tests\Unit |
| 25 | + */ |
| 26 | +final class MFAGateServiceTest extends TestCase |
| 27 | +{ |
| 28 | + private MFAGateService $service; |
| 29 | + |
| 30 | + private MockInterface&IDeviceTrustService $deviceTrustService; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + $this->deviceTrustService = Mockery::mock(IDeviceTrustService::class); |
| 36 | + $this->service = new MFAGateService($this->deviceTrustService); |
| 37 | + } |
| 38 | + |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + Mockery::close(); |
| 42 | + parent::tearDown(); |
| 43 | + } |
| 44 | + |
| 45 | + // ------------------------------------------------------------------------- |
| 46 | + // Non-admin/non-enforced user with 2FA disabled returns false |
| 47 | + // ------------------------------------------------------------------------- |
| 48 | + |
| 49 | + public function testRequiresChallengeReturnsFalseWhenUserDoesNotRequire2FA(): void |
| 50 | + { |
| 51 | + $user = Mockery::mock(User::class); |
| 52 | + $user->shouldReceive('shouldRequire2FA')->once()->andReturn(false); |
| 53 | + $this->deviceTrustService->shouldNotReceive('isDeviceTrusted'); |
| 54 | + |
| 55 | + $this->assertFalse($this->service->requiresChallenge($user, null)); |
| 56 | + } |
| 57 | + |
| 58 | + // ------------------------------------------------------------------------- |
| 59 | + // Admin/enforced user with no cookie returns true |
| 60 | + // ------------------------------------------------------------------------- |
| 61 | + |
| 62 | + public function testRequiresChallengeReturnsTrueWhenEnforcedAndNoCookie(): void |
| 63 | + { |
| 64 | + $user = Mockery::mock(User::class); |
| 65 | + $user->shouldReceive('shouldRequire2FA')->once()->andReturn(true); |
| 66 | + $this->deviceTrustService->shouldReceive('isDeviceTrusted')->once()->with($user, null)->andReturn(false); |
| 67 | + |
| 68 | + $this->assertTrue($this->service->requiresChallenge($user, null)); |
| 69 | + } |
| 70 | + |
| 71 | + // ------------------------------------------------------------------------- |
| 72 | + // Admin/enforced user with trusted device returns false |
| 73 | + // ------------------------------------------------------------------------- |
| 74 | + |
| 75 | + public function testRequiresChallengeReturnsFalseWhenEnforcedAndDeviceTrusted(): void |
| 76 | + { |
| 77 | + $user = Mockery::mock(User::class); |
| 78 | + $user->shouldReceive('shouldRequire2FA')->once()->andReturn(true); |
| 79 | + $this->deviceTrustService->shouldReceive('isDeviceTrusted')->once()->with($user, 'valid-token')->andReturn(true); |
| 80 | + |
| 81 | + $this->assertFalse($this->service->requiresChallenge($user, 'valid-token')); |
| 82 | + } |
| 83 | + |
| 84 | + // ------------------------------------------------------------------------- |
| 85 | + // Admin/enforced user with expired/revoked/wrong device returns true |
| 86 | + // ------------------------------------------------------------------------- |
| 87 | + |
| 88 | + public function testRequiresChallengeReturnsTrueWhenEnforcedAndDeviceNotTrusted(): void |
| 89 | + { |
| 90 | + $user = Mockery::mock(User::class); |
| 91 | + $user->shouldReceive('shouldRequire2FA')->once()->andReturn(true); |
| 92 | + $this->deviceTrustService->shouldReceive('isDeviceTrusted')->once()->with($user, 'expired-token')->andReturn(false); |
| 93 | + |
| 94 | + $this->assertTrue($this->service->requiresChallenge($user, 'expired-token')); |
| 95 | + } |
| 96 | + |
| 97 | + // ------------------------------------------------------------------------- |
| 98 | + // Empty-string cookie is forwarded as-is (not coerced to null) and treated |
| 99 | + // as untrusted — documents the ?string contract between gate and trust layers |
| 100 | + // ------------------------------------------------------------------------- |
| 101 | + |
| 102 | + public function testRequiresChallengePassesThroughEmptyStringCookieToDeviceTrustService(): void |
| 103 | + { |
| 104 | + $user = Mockery::mock(User::class); |
| 105 | + $user->shouldReceive('shouldRequire2FA')->once()->andReturn(true); |
| 106 | + $this->deviceTrustService->shouldReceive('isDeviceTrusted')->once()->with($user, '')->andReturn(false); |
| 107 | + |
| 108 | + $this->assertTrue($this->service->requiresChallenge($user, '')); |
| 109 | + } |
| 110 | +} |
0 commit comments