|
2 | 2 |
|
3 | 3 | namespace Dgtlss\Warden\Tests\Commands; |
4 | 4 |
|
5 | | -use Dgtlss\Warden\Commands\WardenAuditCommand; |
6 | | -use Illuminate\Support\Facades\Artisan; |
7 | | -use Orchestra\Testbench\TestCase; |
8 | 5 | use Dgtlss\Warden\Providers\WardenServiceProvider; |
| 6 | +use Dgtlss\Warden\Services\AuditCacheService; |
9 | 7 | use Dgtlss\Warden\Services\AuditExecutor; |
| 8 | +use Dgtlss\Warden\Services\Audits\ComposerAuditService; |
| 9 | +use Dgtlss\Warden\Services\Audits\DebugModeAuditService; |
| 10 | +use Dgtlss\Warden\Services\Audits\EnvAuditService; |
| 11 | +use Dgtlss\Warden\Services\Audits\StorageAuditService; |
| 12 | +use Illuminate\Support\Facades\Http; |
| 13 | +use Illuminate\Support\Facades\Mail; |
10 | 14 | use Mockery\MockInterface; |
| 15 | +use Orchestra\Testbench\TestCase; |
11 | 16 |
|
12 | 17 | class WardenAuditCommandTest extends TestCase |
13 | 18 | { |
@@ -56,4 +61,139 @@ public function testAuditCommandHandlesFindings(): void |
56 | 61 | ->expectsOutputToContain('1 security issue found.') |
57 | 62 | ->assertExitCode(1); |
58 | 63 | } |
| 64 | + |
| 65 | + public function testAuditCommandIgnoresConfiguredFindingsBeforeNotifications(): void |
| 66 | + { |
| 67 | + Http::fake(); |
| 68 | + Mail::fake(); |
| 69 | + |
| 70 | + config([ |
| 71 | + 'warden.webhook_url' => 'https://example.com/webhook', |
| 72 | + 'warden.email_recipients' => '[email protected]', |
| 73 | + 'warden.ignore_findings' => [ |
| 74 | + ['source' => 'debug-mode', 'package' => 'laravel/horizon'], |
| 75 | + ], |
| 76 | + ]); |
| 77 | + |
| 78 | + $findings = [ |
| 79 | + [ |
| 80 | + 'source' => 'debug-mode', |
| 81 | + 'title' => 'Development package detected in production', |
| 82 | + 'severity' => 'high', |
| 83 | + 'package' => 'laravel/horizon', |
| 84 | + ], |
| 85 | + ]; |
| 86 | + |
| 87 | + $this->mock(AuditExecutor::class, function (MockInterface $mock) use ($findings): void { |
| 88 | + $mock->shouldReceive('addAudit')->zeroOrMoreTimes(); |
| 89 | + $mock->shouldReceive('execute')->once()->andReturn([ |
| 90 | + 'debug-mode' => [ |
| 91 | + 'success' => true, |
| 92 | + 'findings' => $findings, |
| 93 | + 'service' => new \stdClass(), |
| 94 | + ], |
| 95 | + ]); |
| 96 | + }); |
| 97 | + |
| 98 | + $this->artisan('warden:audit') |
| 99 | + ->expectsOutputToContain('Warden') |
| 100 | + ->expectsOutputToContain('No security issues found.') |
| 101 | + ->assertExitCode(0); |
| 102 | + |
| 103 | + Http::assertNothingSent(); |
| 104 | + Mail::assertNothingSent(); |
| 105 | + } |
| 106 | + |
| 107 | + public function testAuditCommandSupportsWildcardIgnoreRulesInJsonOutput(): void |
| 108 | + { |
| 109 | + config([ |
| 110 | + 'warden.ignore_findings' => [ |
| 111 | + ['source' => 'debug-mode', 'title' => 'Testing routes*'], |
| 112 | + ], |
| 113 | + ]); |
| 114 | + |
| 115 | + $findings = [ |
| 116 | + [ |
| 117 | + 'source' => 'debug-mode', |
| 118 | + 'title' => 'Testing routes are exposed', |
| 119 | + 'severity' => 'high', |
| 120 | + 'package' => 'routes', |
| 121 | + ], |
| 122 | + ]; |
| 123 | + |
| 124 | + $this->mock(AuditExecutor::class, function (MockInterface $mock) use ($findings): void { |
| 125 | + $mock->shouldReceive('addAudit')->zeroOrMoreTimes(); |
| 126 | + $mock->shouldReceive('execute')->once()->andReturn([ |
| 127 | + 'debug-mode' => [ |
| 128 | + 'success' => true, |
| 129 | + 'findings' => $findings, |
| 130 | + 'service' => new \stdClass(), |
| 131 | + ], |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + $this->artisan('warden:audit', ['--output' => 'json']) |
| 136 | + ->expectsOutputToContain('"vulnerabilities_found": 0') |
| 137 | + ->assertExitCode(0); |
| 138 | + } |
| 139 | + |
| 140 | + public function testAuditCommandFiltersCachedFindingsInSequentialMode(): void |
| 141 | + { |
| 142 | + config([ |
| 143 | + 'warden.audits.parallel_execution' => false, |
| 144 | + 'warden.ignore_findings' => [ |
| 145 | + ['source' => 'debug-mode', 'package' => 'laravel/horizon'], |
| 146 | + ], |
| 147 | + ]); |
| 148 | + |
| 149 | + $this->mock(AuditCacheService::class, function (MockInterface $mock): void { |
| 150 | + $mock->shouldReceive('hasRecentAudit') |
| 151 | + ->times(4) |
| 152 | + ->andReturnUsing(fn (string $auditName): bool => $auditName === 'debug-mode'); |
| 153 | + |
| 154 | + $mock->shouldReceive('getCachedResult') |
| 155 | + ->once() |
| 156 | + ->with('debug-mode') |
| 157 | + ->andReturn([ |
| 158 | + 'result' => [ |
| 159 | + [ |
| 160 | + 'source' => 'debug-mode', |
| 161 | + 'title' => 'Development package detected in production', |
| 162 | + 'severity' => 'high', |
| 163 | + 'package' => 'laravel/horizon', |
| 164 | + ], |
| 165 | + ], |
| 166 | + 'timestamp' => now()->toIso8601String(), |
| 167 | + 'cached' => true, |
| 168 | + ]); |
| 169 | + }); |
| 170 | + |
| 171 | + $this->mock(ComposerAuditService::class, function (MockInterface $mock): void { |
| 172 | + $mock->shouldReceive('getName')->once()->andReturn('composer'); |
| 173 | + $mock->shouldReceive('run')->once()->andReturn(true); |
| 174 | + $mock->shouldReceive('getFindings')->once()->andReturn([]); |
| 175 | + $mock->shouldReceive('getAbandonedPackages')->once()->andReturn([]); |
| 176 | + }); |
| 177 | + |
| 178 | + $this->mock(EnvAuditService::class, function (MockInterface $mock): void { |
| 179 | + $mock->shouldReceive('getName')->once()->andReturn('environment'); |
| 180 | + $mock->shouldReceive('run')->once()->andReturn(true); |
| 181 | + $mock->shouldReceive('getFindings')->once()->andReturn([]); |
| 182 | + }); |
| 183 | + |
| 184 | + $this->mock(StorageAuditService::class, function (MockInterface $mock): void { |
| 185 | + $mock->shouldReceive('getName')->once()->andReturn('storage'); |
| 186 | + $mock->shouldReceive('run')->once()->andReturn(true); |
| 187 | + $mock->shouldReceive('getFindings')->once()->andReturn([]); |
| 188 | + }); |
| 189 | + |
| 190 | + $this->mock(DebugModeAuditService::class, function (MockInterface $mock): void { |
| 191 | + $mock->shouldReceive('getName')->once()->andReturn('debug-mode'); |
| 192 | + }); |
| 193 | + |
| 194 | + $this->artisan('warden:audit', ['--no-notify' => true]) |
| 195 | + ->expectsOutputToContain('Warden') |
| 196 | + ->expectsOutputToContain('No security issues found.') |
| 197 | + ->assertExitCode(0); |
| 198 | + } |
59 | 199 | } |
0 commit comments