|
| 1 | +<?php |
| 2 | +namespace Tests\Unit; |
| 3 | +/** |
| 4 | + * Copyright 2026 OpenStack Foundation |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + **/ |
| 15 | + |
| 16 | +use App\Jobs\EmitAuditLogJob; |
| 17 | +use App\libs\Auth\Models\TwoFactorAuditLog; |
| 18 | +use App\Services\Auth\TwoFactorAuditService; |
| 19 | +use Auth\Repositories\ITwoFactorAuditLogRepository; |
| 20 | +use Auth\User; |
| 21 | +use Illuminate\Support\Facades\Config; |
| 22 | +use Illuminate\Support\Facades\Queue; |
| 23 | +use Mockery; |
| 24 | +use Tests\TestCase; |
| 25 | + |
| 26 | +/** |
| 27 | + * Class TwoFactorAuditServiceTest |
| 28 | + * @package Tests\Unit |
| 29 | + */ |
| 30 | +final class TwoFactorAuditServiceTest extends TestCase |
| 31 | +{ |
| 32 | + /** @var TwoFactorAuditService */ |
| 33 | + private TwoFactorAuditService $service; |
| 34 | + |
| 35 | + /** @var \Mockery\MockInterface&ITwoFactorAuditLogRepository */ |
| 36 | + private $repository; |
| 37 | + |
| 38 | + /** @var \Mockery\MockInterface&User */ |
| 39 | + private $user; |
| 40 | + |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + parent::setUp(); |
| 44 | + Queue::fake(); |
| 45 | + |
| 46 | + $this->repository = Mockery::mock(ITwoFactorAuditLogRepository::class); |
| 47 | + $this->service = new TwoFactorAuditService($this->repository); |
| 48 | + |
| 49 | + $this->user = Mockery::mock(User::class); |
| 50 | + $this->user->shouldReceive('getId')->andReturn(42); |
| 51 | + } |
| 52 | + |
| 53 | + protected function tearDown(): void |
| 54 | + { |
| 55 | + Mockery::close(); |
| 56 | + parent::tearDown(); |
| 57 | + } |
| 58 | + |
| 59 | + // ------------------------------------------------------------------------- |
| 60 | + // log() persists TwoFactorAuditLog with correct fields |
| 61 | + // ------------------------------------------------------------------------- |
| 62 | + |
| 63 | + public function testLogPersistsTwoFactorAuditLogWithCorrectFields(): void |
| 64 | + { |
| 65 | + /** @var TwoFactorAuditLog|null $persisted */ |
| 66 | + $persisted = null; |
| 67 | + |
| 68 | + $this->repository |
| 69 | + ->shouldReceive('add') |
| 70 | + ->once() |
| 71 | + ->withArgs(function (TwoFactorAuditLog $log, bool $sync) use (&$persisted) { |
| 72 | + $persisted = $log; |
| 73 | + return $sync === true; |
| 74 | + }); |
| 75 | + |
| 76 | + $this->service->log( |
| 77 | + $this->user, |
| 78 | + TwoFactorAuditLog::EventChallengeSucceeded, |
| 79 | + TwoFactorAuditLog::MethodEmailOtp, |
| 80 | + '127.0.0.1', |
| 81 | + ['attempt' => 1] |
| 82 | + ); |
| 83 | + |
| 84 | + $this->assertNotNull($persisted); |
| 85 | + $this->assertSame($this->user, $persisted->getUser()); |
| 86 | + $this->assertSame(TwoFactorAuditLog::EventChallengeSucceeded, $persisted->getEventType()); |
| 87 | + $this->assertSame(TwoFactorAuditLog::MethodEmailOtp, $persisted->getMethod()); |
| 88 | + $this->assertSame('127.0.0.1', $persisted->getIpAddress()); |
| 89 | + $this->assertSame(['attempt' => 1], $persisted->getMetadata()); |
| 90 | + } |
| 91 | + |
| 92 | + // ------------------------------------------------------------------------- |
| 93 | + // log() emits OTLP attributes |
| 94 | + // ------------------------------------------------------------------------- |
| 95 | + |
| 96 | + public function testLogEmitsOtlpAttributes(): void |
| 97 | + { |
| 98 | + Config::set('opentelemetry.enabled', true); |
| 99 | + |
| 100 | + $this->repository->shouldReceive('add')->once(); |
| 101 | + |
| 102 | + $this->service->log( |
| 103 | + $this->user, |
| 104 | + TwoFactorAuditLog::EventChallengeSucceeded, |
| 105 | + TwoFactorAuditLog::MethodEmailOtp, |
| 106 | + '10.0.0.1' |
| 107 | + ); |
| 108 | + |
| 109 | + Queue::assertPushed(EmitAuditLogJob::class, function (EmitAuditLogJob $job) { |
| 110 | + return $job->logMessage === 'two_factor.audit' |
| 111 | + && $job->auditData['two_factor.event_type'] === TwoFactorAuditLog::EventChallengeSucceeded |
| 112 | + && $job->auditData['two_factor.method'] === TwoFactorAuditLog::MethodEmailOtp |
| 113 | + && $job->auditData['two_factor.user_id'] === 42 |
| 114 | + && $job->auditData['two_factor.ip_address'] === '10.0.0.1' |
| 115 | + && $job->auditData['two_factor.success'] === true |
| 116 | + && $job->auditData['two_factor.device_trusted'] === false; |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + // ------------------------------------------------------------------------- |
| 121 | + // log() emits two_factor.success = false for challenge_failed |
| 122 | + // ------------------------------------------------------------------------- |
| 123 | + |
| 124 | + public function testLogEmitsSuccessFalseForChallengeFailed(): void |
| 125 | + { |
| 126 | + Config::set('opentelemetry.enabled', true); |
| 127 | + |
| 128 | + $this->repository->shouldReceive('add')->once(); |
| 129 | + |
| 130 | + $this->service->log( |
| 131 | + $this->user, |
| 132 | + TwoFactorAuditLog::EventChallengeFailed, |
| 133 | + TwoFactorAuditLog::MethodEmailOtp, |
| 134 | + '10.0.0.1' |
| 135 | + ); |
| 136 | + |
| 137 | + Queue::assertPushed(EmitAuditLogJob::class, function (EmitAuditLogJob $job) { |
| 138 | + return $job->auditData['two_factor.event_type'] === TwoFactorAuditLog::EventChallengeFailed |
| 139 | + && $job->auditData['two_factor.success'] === false; |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + // ------------------------------------------------------------------------- |
| 144 | + // log() does NOT dispatch job when OTLP is disabled (default) |
| 145 | + // ------------------------------------------------------------------------- |
| 146 | + |
| 147 | + public function testLogDoesNotDispatchJobWhenOtlpDisabled(): void |
| 148 | + { |
| 149 | + Config::set('opentelemetry.enabled', false); |
| 150 | + |
| 151 | + $this->repository->shouldReceive('add')->once(); |
| 152 | + |
| 153 | + $this->service->log( |
| 154 | + $this->user, |
| 155 | + TwoFactorAuditLog::EventChallengeSucceeded, |
| 156 | + TwoFactorAuditLog::MethodEmailOtp, |
| 157 | + '127.0.0.1' |
| 158 | + ); |
| 159 | + |
| 160 | + Queue::assertNotPushed(EmitAuditLogJob::class); |
| 161 | + } |
| 162 | + |
| 163 | + // ------------------------------------------------------------------------- |
| 164 | + // log() accepts null metadata |
| 165 | + // ------------------------------------------------------------------------- |
| 166 | + |
| 167 | + public function testLogAcceptsNullMetadata(): void |
| 168 | + { |
| 169 | + /** @var TwoFactorAuditLog|null $persisted */ |
| 170 | + $persisted = null; |
| 171 | + |
| 172 | + $this->repository |
| 173 | + ->shouldReceive('add') |
| 174 | + ->once() |
| 175 | + ->withArgs(function (TwoFactorAuditLog $log) use (&$persisted) { |
| 176 | + $persisted = $log; |
| 177 | + return true; |
| 178 | + }); |
| 179 | + |
| 180 | + $this->service->log( |
| 181 | + $this->user, |
| 182 | + TwoFactorAuditLog::EventChallengeIssued, |
| 183 | + TwoFactorAuditLog::MethodTotp, |
| 184 | + '192.168.1.1', |
| 185 | + null |
| 186 | + ); |
| 187 | + |
| 188 | + $this->assertNotNull($persisted); |
| 189 | + $this->assertNull($persisted->getMetadata()); |
| 190 | + } |
| 191 | + |
| 192 | + // ------------------------------------------------------------------------- |
| 193 | + // invalid event type throws InvalidArgumentException |
| 194 | + // ------------------------------------------------------------------------- |
| 195 | + |
| 196 | + public function testInvalidEventTypeThrowsInvalidArgumentException(): void |
| 197 | + { |
| 198 | + $this->expectException(\InvalidArgumentException::class); |
| 199 | + |
| 200 | + $this->repository->shouldNotReceive('add'); |
| 201 | + |
| 202 | + $this->service->log( |
| 203 | + $this->user, |
| 204 | + 'not_a_valid_event', |
| 205 | + TwoFactorAuditLog::MethodEmailOtp, |
| 206 | + '127.0.0.1' |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + // ------------------------------------------------------------------------- |
| 211 | + // invalid method throws InvalidArgumentException |
| 212 | + // ------------------------------------------------------------------------- |
| 213 | + |
| 214 | + public function testInvalidMethodThrowsInvalidArgumentException(): void |
| 215 | + { |
| 216 | + $this->expectException(\InvalidArgumentException::class); |
| 217 | + |
| 218 | + $this->repository->shouldNotReceive('add'); |
| 219 | + |
| 220 | + $this->service->log( |
| 221 | + $this->user, |
| 222 | + TwoFactorAuditLog::EventChallengeIssued, |
| 223 | + 'not_a_valid_method', |
| 224 | + '127.0.0.1' |
| 225 | + ); |
| 226 | + } |
| 227 | +} |
0 commit comments