|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright ©2025 Maatify.dev |
| 5 | + * @Library maatify/security-guard |
| 6 | + * @Project maatify:security-guard |
| 7 | + * @author Mohamed Abdulalim (megyptm) <mohamed@maatify.dev> |
| 8 | + * @since 2025-02-24 10:00 |
| 9 | + * @see https://www.maatify.dev Maatify.dev |
| 10 | + * @link https://github.com/Maatify/security-guard view project on GitHub |
| 11 | + * @note Distributed in the hope that it will be useful - WITHOUT WARRANTY. |
| 12 | + */ |
| 13 | + |
| 14 | +declare(strict_types=1); |
| 15 | + |
| 16 | +namespace Maatify\SecurityGuard\Tests\IntegrationV2\Mongo; |
| 17 | + |
| 18 | +use Maatify\Bootstrap\Core\EnvironmentLoader; |
| 19 | +use Maatify\Common\Contracts\Adapter\AdapterInterface; |
| 20 | +use Maatify\DataAdapters\Core\DatabaseResolver; |
| 21 | +use Maatify\DataAdapters\Core\EnvironmentConfig; |
| 22 | +use Maatify\SecurityGuard\Drivers\Mongo\MongoSecurityGuard; |
| 23 | +use Maatify\SecurityGuard\DTO\LoginAttemptDTO; |
| 24 | +use Maatify\SecurityGuard\DTO\SecurityBlockDTO; |
| 25 | +use Maatify\SecurityGuard\Enums\BlockTypeEnum; |
| 26 | +use Maatify\SecurityGuard\Tests\IntegrationV2\BaseIntegrationV2TestCase; |
| 27 | + |
| 28 | +/** |
| 29 | + * MongoIntegrationFlowTest |
| 30 | + * |
| 31 | + * Verifies the full authenticated login failure and blocking flow using a real Mongo adapter |
| 32 | + * resolved via the system's DatabaseResolver. |
| 33 | + * |
| 34 | + * Flow: |
| 35 | + * Authenticated subject -> Record Failures -> Max Failures Reached -> Block Applied -> Unblock -> Verify Unblock |
| 36 | + */ |
| 37 | +class MongoIntegrationFlowTest extends BaseIntegrationV2TestCase |
| 38 | +{ |
| 39 | + private ?MongoSecurityGuard $guard = null; |
| 40 | + |
| 41 | + protected function validateEnvironment(): void |
| 42 | + { |
| 43 | + // STRICT: Environment validation is delegated to DatabaseResolver / EnvironmentLoader. |
| 44 | + } |
| 45 | + |
| 46 | + protected function createAdapter(): AdapterInterface |
| 47 | + { |
| 48 | + // STRICT: Use DatabaseResolver to fetch the configured Mongo adapter. |
| 49 | + $rootPath = dirname(__DIR__, 3); |
| 50 | + |
| 51 | + (new EnvironmentLoader($rootPath))->load(); |
| 52 | + |
| 53 | + $config = new EnvironmentConfig($rootPath); |
| 54 | + $resolver = new DatabaseResolver($config); |
| 55 | + |
| 56 | + // Resolve 'mongo.main' profile with auto-connect enabled |
| 57 | + return $resolver->resolve('mongo.main', true); |
| 58 | + } |
| 59 | + |
| 60 | + protected function setUp(): void |
| 61 | + { |
| 62 | + parent::setUp(); |
| 63 | + |
| 64 | + // STRICT: Fail if not connected. No skipping allowed. |
| 65 | + if (!$this->adapter->isConnected()) { |
| 66 | + $this->fail('Mongo adapter (mongo.main) failed to connect. Ensure connection configuration is valid and MongoDB is running.'); |
| 67 | + } |
| 68 | + |
| 69 | + $this->guard = new MongoSecurityGuard($this->adapter, $this->identifierStrategy); |
| 70 | + } |
| 71 | + |
| 72 | + public function testAuthenticatedSubjectBlockFlow(): void |
| 73 | + { |
| 74 | + $this->assertNotNull($this->guard, 'Guard should have been initialized in setUp'); |
| 75 | + $guard = $this->guard; |
| 76 | + |
| 77 | + // 1. Setup Identity |
| 78 | + $ip = '10.0.0.6'; |
| 79 | + $subject = 'user_mongo_' . bin2hex(random_bytes(4)); |
| 80 | + |
| 81 | + // Ensure clean state (using unblock/resetAttempts as per strict rules - no deleteMany/flush) |
| 82 | + $guard->resetAttempts($ip, $subject); |
| 83 | + $guard->unblock($ip, $subject); |
| 84 | + |
| 85 | + // 2. Record Login Failures |
| 86 | + $maxFailures = 5; |
| 87 | + $attempt = new LoginAttemptDTO( |
| 88 | + ip: $ip, |
| 89 | + subject: $subject, |
| 90 | + occurredAt: time(), |
| 91 | + resetAfter: 60 |
| 92 | + ); |
| 93 | + |
| 94 | + for ($i = 1; $i <= $maxFailures; $i++) { |
| 95 | + $count = $guard->recordFailure($attempt); |
| 96 | + $this->assertSame($i, $count, "Failure count should increment to $i"); |
| 97 | + |
| 98 | + if ($i < $maxFailures) { |
| 99 | + $this->assertFalse($guard->isBlocked($ip, $subject), "Should not be blocked at attempt $i"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // 3. Trigger Block |
| 104 | + $blockDTO = new SecurityBlockDTO( |
| 105 | + ip: $ip, |
| 106 | + subject: $subject, |
| 107 | + type: BlockTypeEnum::AUTO, |
| 108 | + expiresAt: time() + 300, |
| 109 | + createdAt: time() |
| 110 | + ); |
| 111 | + $guard->block($blockDTO); |
| 112 | + |
| 113 | + // Verify Blocked |
| 114 | + $this->assertTrue($guard->isBlocked($ip, $subject), 'Subject should be blocked after applying block'); |
| 115 | + $activeBlock = $guard->getActiveBlock($ip, $subject); |
| 116 | + $this->assertNotNull($activeBlock); |
| 117 | + $this->assertSame($subject, $activeBlock->subject); |
| 118 | + |
| 119 | + // 4. Unblock |
| 120 | + $guard->unblock($ip, $subject); |
| 121 | + |
| 122 | + // 5. Verify Unblock Success |
| 123 | + $this->assertFalse($guard->isBlocked($ip, $subject), 'Subject should be unblocked'); |
| 124 | + $this->assertNull($guard->getActiveBlock($ip, $subject)); |
| 125 | + } |
| 126 | +} |
0 commit comments