|
| 1 | +<?php |
| 2 | + |
| 3 | +use App\Models\InstanceSettings; |
| 4 | +use App\Models\Server; |
| 5 | +use App\Notifications\Server\ServerPatchCheck; |
| 6 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 7 | + |
| 8 | +uses(RefreshDatabase::class); |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + // Create a real InstanceSettings record in the test database |
| 12 | + // This avoids Mockery alias/overload issues that pollute global state |
| 13 | + $this->setInstanceSettings = function ($fqdn = null, $publicIpv4 = null, $publicIpv6 = null) { |
| 14 | + InstanceSettings::query()->delete(); |
| 15 | + InstanceSettings::create([ |
| 16 | + 'id' => 0, |
| 17 | + 'fqdn' => $fqdn, |
| 18 | + 'public_ipv4' => $publicIpv4, |
| 19 | + 'public_ipv6' => $publicIpv6, |
| 20 | + ]); |
| 21 | + }; |
| 22 | + |
| 23 | + $this->createMockServer = function ($uuid, $name = 'Test Server') { |
| 24 | + $mockServer = Mockery::mock(Server::class); |
| 25 | + $mockServer->shouldReceive('getAttribute') |
| 26 | + ->with('uuid') |
| 27 | + ->andReturn($uuid); |
| 28 | + $mockServer->shouldReceive('getAttribute') |
| 29 | + ->with('name') |
| 30 | + ->andReturn($name); |
| 31 | + $mockServer->shouldReceive('setAttribute')->andReturnSelf(); |
| 32 | + $mockServer->shouldReceive('getSchemalessAttributes')->andReturn([]); |
| 33 | + $mockServer->uuid = $uuid; |
| 34 | + $mockServer->name = $name; |
| 35 | + |
| 36 | + return $mockServer; |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +afterEach(function () { |
| 41 | + Mockery::close(); |
| 42 | +}); |
| 43 | + |
| 44 | +it('generates url using base_url instead of APP_URL', function () { |
| 45 | + // Set InstanceSettings to return a specific FQDN |
| 46 | + ($this->setInstanceSettings)('https://coolify.example.com'); |
| 47 | + |
| 48 | + $mockServer = ($this->createMockServer)('test-server-uuid'); |
| 49 | + |
| 50 | + $patchData = [ |
| 51 | + 'total_updates' => 5, |
| 52 | + 'updates' => [], |
| 53 | + 'osId' => 'ubuntu', |
| 54 | + 'package_manager' => 'apt', |
| 55 | + ]; |
| 56 | + |
| 57 | + $notification = new ServerPatchCheck($mockServer, $patchData); |
| 58 | + |
| 59 | + // The URL should use the FQDN from InstanceSettings, not APP_URL |
| 60 | + expect($notification->serverUrl)->toBe('https://coolify.example.com/server/test-server-uuid/security/patches'); |
| 61 | +}); |
| 62 | + |
| 63 | +it('falls back to public_ipv4 with port when fqdn is not set', function () { |
| 64 | + // Set InstanceSettings to return public IPv4 |
| 65 | + ($this->setInstanceSettings)(null, '192.168.1.100'); |
| 66 | + |
| 67 | + $mockServer = ($this->createMockServer)('test-server-uuid'); |
| 68 | + |
| 69 | + $patchData = [ |
| 70 | + 'total_updates' => 3, |
| 71 | + 'updates' => [], |
| 72 | + 'osId' => 'debian', |
| 73 | + 'package_manager' => 'apt', |
| 74 | + ]; |
| 75 | + |
| 76 | + $notification = new ServerPatchCheck($mockServer, $patchData); |
| 77 | + |
| 78 | + // The URL should use public IPv4 with default port 8000 |
| 79 | + expect($notification->serverUrl)->toBe('http://192.168.1.100:8000/server/test-server-uuid/security/patches'); |
| 80 | +}); |
| 81 | + |
| 82 | +it('includes server url in all notification channels', function () { |
| 83 | + ($this->setInstanceSettings)('https://coolify.test'); |
| 84 | + |
| 85 | + $mockServer = ($this->createMockServer)('abc-123', 'Test Server'); |
| 86 | + |
| 87 | + $patchData = [ |
| 88 | + 'total_updates' => 10, |
| 89 | + 'updates' => [ |
| 90 | + [ |
| 91 | + 'package' => 'nginx', |
| 92 | + 'current_version' => '1.18', |
| 93 | + 'new_version' => '1.20', |
| 94 | + 'architecture' => 'amd64', |
| 95 | + 'repository' => 'main', |
| 96 | + ], |
| 97 | + ], |
| 98 | + 'osId' => 'ubuntu', |
| 99 | + 'package_manager' => 'apt', |
| 100 | + ]; |
| 101 | + |
| 102 | + $notification = new ServerPatchCheck($mockServer, $patchData); |
| 103 | + |
| 104 | + // Check Discord |
| 105 | + $discord = $notification->toDiscord(); |
| 106 | + expect($discord->description)->toContain('https://coolify.test/server/abc-123/security/patches'); |
| 107 | + |
| 108 | + // Check Telegram |
| 109 | + $telegram = $notification->toTelegram(); |
| 110 | + expect($telegram['buttons'][0]['url'])->toBe('https://coolify.test/server/abc-123/security/patches'); |
| 111 | + |
| 112 | + // Check Pushover |
| 113 | + $pushover = $notification->toPushover(); |
| 114 | + expect($pushover->buttons[0]['url'])->toBe('https://coolify.test/server/abc-123/security/patches'); |
| 115 | + |
| 116 | + // Check Slack |
| 117 | + $slack = $notification->toSlack(); |
| 118 | + expect($slack->description)->toContain('https://coolify.test/server/abc-123/security/patches'); |
| 119 | + |
| 120 | + // Check Webhook |
| 121 | + $webhook = $notification->toWebhook(); |
| 122 | + expect($webhook['url'])->toBe('https://coolify.test/server/abc-123/security/patches'); |
| 123 | +}); |
| 124 | + |
| 125 | +it('uses correct url in error notifications', function () { |
| 126 | + ($this->setInstanceSettings)('https://coolify.production.com'); |
| 127 | + |
| 128 | + $mockServer = ($this->createMockServer)('error-server-uuid', 'Error Server'); |
| 129 | + |
| 130 | + $patchData = [ |
| 131 | + 'error' => 'Failed to connect to package manager', |
| 132 | + 'osId' => 'ubuntu', |
| 133 | + 'package_manager' => 'apt', |
| 134 | + ]; |
| 135 | + |
| 136 | + $notification = new ServerPatchCheck($mockServer, $patchData); |
| 137 | + |
| 138 | + // Check error Discord notification |
| 139 | + $discord = $notification->toDiscord(); |
| 140 | + expect($discord->description)->toContain('https://coolify.production.com/server/error-server-uuid/security/patches'); |
| 141 | + |
| 142 | + // Check error webhook |
| 143 | + $webhook = $notification->toWebhook(); |
| 144 | + expect($webhook['url'])->toBe('https://coolify.production.com/server/error-server-uuid/security/patches') |
| 145 | + ->and($webhook['event'])->toBe('server_patch_check_error'); |
| 146 | +}); |
0 commit comments