|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Jobs\CheckForUpdatesJob; |
| 4 | +use App\Models\InstanceSettings; |
| 5 | +use Illuminate\Support\Facades\Cache; |
| 6 | +use Illuminate\Support\Facades\File; |
| 7 | +use Illuminate\Support\Facades\Http; |
| 8 | + |
| 9 | +beforeEach(function () { |
| 10 | + Cache::flush(); |
| 11 | + |
| 12 | + // Mock InstanceSettings |
| 13 | + $this->settings = Mockery::mock(InstanceSettings::class); |
| 14 | + $this->settings->shouldReceive('update')->andReturn(true); |
| 15 | +}); |
| 16 | + |
| 17 | +afterEach(function () { |
| 18 | + Mockery::close(); |
| 19 | +}); |
| 20 | + |
| 21 | +it('has correct job configuration', function () { |
| 22 | + $job = new CheckForUpdatesJob; |
| 23 | + |
| 24 | + $interfaces = class_implements($job); |
| 25 | + expect($interfaces)->toContain(\Illuminate\Contracts\Queue\ShouldQueue::class); |
| 26 | + expect($interfaces)->toContain(\Illuminate\Contracts\Queue\ShouldBeEncrypted::class); |
| 27 | +}); |
| 28 | + |
| 29 | +it('uses max of CDN and cache versions', function () { |
| 30 | + // CDN has older version |
| 31 | + Http::fake([ |
| 32 | + '*' => Http::response([ |
| 33 | + 'coolify' => ['v4' => ['version' => '4.0.0']], |
| 34 | + 'traefik' => ['v3.5' => '3.5.6'], |
| 35 | + ], 200), |
| 36 | + ]); |
| 37 | + |
| 38 | + // Cache has newer version |
| 39 | + File::shouldReceive('exists') |
| 40 | + ->with(base_path('versions.json')) |
| 41 | + ->andReturn(true); |
| 42 | + |
| 43 | + File::shouldReceive('get') |
| 44 | + ->with(base_path('versions.json')) |
| 45 | + ->andReturn(json_encode(['coolify' => ['v4' => ['version' => '4.0.10']]])); |
| 46 | + |
| 47 | + File::shouldReceive('put') |
| 48 | + ->once() |
| 49 | + ->with(base_path('versions.json'), Mockery::on(function ($json) { |
| 50 | + $data = json_decode($json, true); |
| 51 | + // Should use cached version (4.0.10), not CDN version (4.0.0) |
| 52 | + return $data['coolify']['v4']['version'] === '4.0.10'; |
| 53 | + })); |
| 54 | + |
| 55 | + Cache::shouldReceive('forget')->once(); |
| 56 | + |
| 57 | + config(['constants.coolify.version' => '4.0.5']); |
| 58 | + |
| 59 | + // Mock instanceSettings function |
| 60 | + $this->app->instance('App\Models\InstanceSettings', function () { |
| 61 | + return $this->settings; |
| 62 | + }); |
| 63 | + |
| 64 | + $job = new CheckForUpdatesJob(); |
| 65 | + $job->handle(); |
| 66 | +}); |
| 67 | + |
| 68 | +it('never downgrades from current running version', function () { |
| 69 | + // CDN has older version |
| 70 | + Http::fake([ |
| 71 | + '*' => Http::response([ |
| 72 | + 'coolify' => ['v4' => ['version' => '4.0.0']], |
| 73 | + 'traefik' => ['v3.5' => '3.5.6'], |
| 74 | + ], 200), |
| 75 | + ]); |
| 76 | + |
| 77 | + // Cache also has older version |
| 78 | + File::shouldReceive('exists') |
| 79 | + ->with(base_path('versions.json')) |
| 80 | + ->andReturn(true); |
| 81 | + |
| 82 | + File::shouldReceive('get') |
| 83 | + ->with(base_path('versions.json')) |
| 84 | + ->andReturn(json_encode(['coolify' => ['v4' => ['version' => '4.0.5']]])); |
| 85 | + |
| 86 | + File::shouldReceive('put') |
| 87 | + ->once() |
| 88 | + ->with(base_path('versions.json'), Mockery::on(function ($json) { |
| 89 | + $data = json_decode($json, true); |
| 90 | + // Should use running version (4.0.10), not CDN (4.0.0) or cache (4.0.5) |
| 91 | + return $data['coolify']['v4']['version'] === '4.0.10'; |
| 92 | + })); |
| 93 | + |
| 94 | + Cache::shouldReceive('forget')->once(); |
| 95 | + |
| 96 | + // Running version is newest |
| 97 | + config(['constants.coolify.version' => '4.0.10']); |
| 98 | + |
| 99 | + \Illuminate\Support\Facades\Log::shouldReceive('warning') |
| 100 | + ->once() |
| 101 | + ->with('Version downgrade prevented in CheckForUpdatesJob', Mockery::type('array')); |
| 102 | + |
| 103 | + $this->app->instance('App\Models\InstanceSettings', function () { |
| 104 | + return $this->settings; |
| 105 | + }); |
| 106 | + |
| 107 | + $job = new CheckForUpdatesJob(); |
| 108 | + $job->handle(); |
| 109 | +}); |
| 110 | + |
| 111 | +it('uses data_set for safe version mutation', function () { |
| 112 | + Http::fake([ |
| 113 | + '*' => Http::response([ |
| 114 | + 'coolify' => ['v4' => ['version' => '4.0.10']], |
| 115 | + ], 200), |
| 116 | + ]); |
| 117 | + |
| 118 | + File::shouldReceive('exists')->andReturn(false); |
| 119 | + File::shouldReceive('put')->once(); |
| 120 | + Cache::shouldReceive('forget')->once(); |
| 121 | + |
| 122 | + config(['constants.coolify.version' => '4.0.5']); |
| 123 | + |
| 124 | + $this->app->instance('App\Models\InstanceSettings', function () { |
| 125 | + return $this->settings; |
| 126 | + }); |
| 127 | + |
| 128 | + $job = new CheckForUpdatesJob(); |
| 129 | + |
| 130 | + // Should not throw even if structure is unexpected |
| 131 | + // data_set() handles nested path creation |
| 132 | + $job->handle(); |
| 133 | +})->skip('Needs better mock setup for instanceSettings'); |
| 134 | + |
| 135 | +it('preserves other component versions when preventing Coolify downgrade', function () { |
| 136 | + // CDN has older Coolify but newer Traefik |
| 137 | + Http::fake([ |
| 138 | + '*' => Http::response([ |
| 139 | + 'coolify' => ['v4' => ['version' => '4.0.0']], |
| 140 | + 'traefik' => ['v3.6' => '3.6.2'], |
| 141 | + 'sentinel' => ['version' => '1.0.5'], |
| 142 | + ], 200), |
| 143 | + ]); |
| 144 | + |
| 145 | + File::shouldReceive('exists')->andReturn(true); |
| 146 | + File::shouldReceive('get') |
| 147 | + ->andReturn(json_encode([ |
| 148 | + 'coolify' => ['v4' => ['version' => '4.0.5']], |
| 149 | + 'traefik' => ['v3.5' => '3.5.6'], |
| 150 | + ])); |
| 151 | + |
| 152 | + File::shouldReceive('put') |
| 153 | + ->once() |
| 154 | + ->with(base_path('versions.json'), Mockery::on(function ($json) { |
| 155 | + $data = json_decode($json, true); |
| 156 | + // Coolify should use running version |
| 157 | + expect($data['coolify']['v4']['version'])->toBe('4.0.10'); |
| 158 | + // Traefik should use CDN version (newer) |
| 159 | + expect($data['traefik']['v3.6'])->toBe('3.6.2'); |
| 160 | + // Sentinel should use CDN version |
| 161 | + expect($data['sentinel']['version'])->toBe('1.0.5'); |
| 162 | + return true; |
| 163 | + })); |
| 164 | + |
| 165 | + Cache::shouldReceive('forget')->once(); |
| 166 | + |
| 167 | + config(['constants.coolify.version' => '4.0.10']); |
| 168 | + |
| 169 | + \Illuminate\Support\Facades\Log::shouldReceive('warning') |
| 170 | + ->once() |
| 171 | + ->with('CDN served older Coolify version than cache', Mockery::type('array')); |
| 172 | + |
| 173 | + \Illuminate\Support\Facades\Log::shouldReceive('warning') |
| 174 | + ->once() |
| 175 | + ->with('Version downgrade prevented in CheckForUpdatesJob', Mockery::type('array')); |
| 176 | + |
| 177 | + $this->app->instance('App\Models\InstanceSettings', function () { |
| 178 | + return $this->settings; |
| 179 | + }); |
| 180 | + |
| 181 | + $job = new CheckForUpdatesJob(); |
| 182 | + $job->handle(); |
| 183 | +}); |
0 commit comments