|
10 | 10 | Cache::spy(); |
11 | 11 | }); |
12 | 12 |
|
13 | | -function mockServerWithDbConfig(?string $savedConfig): object |
| 13 | +function mockServerWithDbConfig(?string $savedConfig, string $proxyType = 'TRAEFIK'): object |
14 | 14 | { |
15 | 15 | $proxyAttributes = Mockery::mock(SchemalessAttributes::class); |
16 | 16 | $proxyAttributes->shouldReceive('get') |
17 | 17 | ->with('last_saved_proxy_configuration') |
18 | 18 | ->andReturn($savedConfig); |
19 | 19 |
|
| 20 | + $proxyPath = match ($proxyType) { |
| 21 | + 'CADDY' => '/data/coolify/proxy/caddy', |
| 22 | + 'NGINX' => '/data/coolify/proxy/nginx', |
| 23 | + default => '/data/coolify/proxy/', |
| 24 | + }; |
| 25 | + |
20 | 26 | $server = Mockery::mock('App\Models\Server'); |
21 | 27 | $server->shouldIgnoreMissing(); |
22 | 28 | $server->shouldReceive('getAttribute')->with('proxy')->andReturn($proxyAttributes); |
23 | 29 | $server->shouldReceive('getAttribute')->with('id')->andReturn(1); |
24 | 30 | $server->shouldReceive('getAttribute')->with('name')->andReturn('Test Server'); |
25 | | - $server->shouldReceive('proxyType')->andReturn('TRAEFIK'); |
26 | | - $server->shouldReceive('proxyPath')->andReturn('/data/coolify/proxy'); |
| 31 | + $server->shouldReceive('proxyType')->andReturn($proxyType); |
| 32 | + $server->shouldReceive('proxyPath')->andReturn($proxyPath); |
27 | 33 |
|
28 | 34 | return $server; |
29 | 35 | } |
@@ -107,3 +113,61 @@ function mockServerWithDbConfig(?string $savedConfig): object |
107 | 113 |
|
108 | 114 | expect($result)->toBe($savedConfig); |
109 | 115 | }); |
| 116 | + |
| 117 | +it('rejects stored Traefik config when proxy type is CADDY', function () { |
| 118 | + Log::swap(new \Illuminate\Log\LogManager(app())); |
| 119 | + Log::spy(); |
| 120 | + |
| 121 | + $traefikConfig = "services:\n traefik:\n image: traefik:v3.6\n"; |
| 122 | + $server = mockServerWithDbConfig($traefikConfig, 'CADDY'); |
| 123 | + |
| 124 | + // Config type mismatch should trigger regeneration, which will try |
| 125 | + // backfillFromDisk (instant_remote_process) then generateDefault. |
| 126 | + // Both will fail in test env, but the warning log proves mismatch was detected. |
| 127 | + try { |
| 128 | + GetProxyConfiguration::run($server); |
| 129 | + } catch (\Throwable $e) { |
| 130 | + // Expected — regeneration requires SSH/full server setup |
| 131 | + } |
| 132 | + |
| 133 | + Log::shouldHaveReceived('warning') |
| 134 | + ->withArgs(fn ($message) => str_contains($message, 'does not match current proxy type')) |
| 135 | + ->once(); |
| 136 | +}); |
| 137 | + |
| 138 | +it('rejects stored Caddy config when proxy type is TRAEFIK', function () { |
| 139 | + Log::swap(new \Illuminate\Log\LogManager(app())); |
| 140 | + Log::spy(); |
| 141 | + |
| 142 | + $caddyConfig = "services:\n caddy:\n image: lucaslorentz/caddy-docker-proxy:2.8-alpine\n"; |
| 143 | + $server = mockServerWithDbConfig($caddyConfig, 'TRAEFIK'); |
| 144 | + |
| 145 | + try { |
| 146 | + GetProxyConfiguration::run($server); |
| 147 | + } catch (\Throwable $e) { |
| 148 | + // Expected — regeneration requires SSH/full server setup |
| 149 | + } |
| 150 | + |
| 151 | + Log::shouldHaveReceived('warning') |
| 152 | + ->withArgs(fn ($message) => str_contains($message, 'does not match current proxy type')) |
| 153 | + ->once(); |
| 154 | +}); |
| 155 | + |
| 156 | +it('accepts stored Caddy config when proxy type is CADDY', function () { |
| 157 | + $caddyConfig = "services:\n caddy:\n image: lucaslorentz/caddy-docker-proxy:2.8-alpine\n"; |
| 158 | + $server = mockServerWithDbConfig($caddyConfig, 'CADDY'); |
| 159 | + |
| 160 | + $result = GetProxyConfiguration::run($server); |
| 161 | + |
| 162 | + expect($result)->toBe($caddyConfig); |
| 163 | +}); |
| 164 | + |
| 165 | +it('accepts stored config when YAML parsing fails', function () { |
| 166 | + $invalidYaml = "this: is: not: [valid yaml: {{{}}}"; |
| 167 | + $server = mockServerWithDbConfig($invalidYaml, 'TRAEFIK'); |
| 168 | + |
| 169 | + // Invalid YAML should not block — configMatchesProxyType returns true on parse failure |
| 170 | + $result = GetProxyConfiguration::run($server); |
| 171 | + |
| 172 | + expect($result)->toBe($invalidYaml); |
| 173 | +}); |
0 commit comments