|
4 | 4 |
|
5 | 5 | use App\Enums\DatabaseStatus; |
6 | 6 | use App\Enums\DatabaseUserStatus; |
| 7 | +use App\Enums\ServiceStatus; |
7 | 8 | use App\Facades\SSH; |
8 | 9 | use App\Models\Database; |
9 | 10 | use App\Models\DatabaseUser; |
| 11 | +use App\Services\Database\Mysql; |
| 12 | +use App\Services\Database\Postgresql; |
10 | 13 | use Illuminate\Foundation\Testing\RefreshDatabase; |
11 | 14 | use Inertia\Testing\AssertableInertia; |
12 | 15 | use Tests\TestCase; |
@@ -143,4 +146,99 @@ public function test_sync_databases(): void |
143 | 146 | $this->patch(route('databases.sync', $this->server)) |
144 | 147 | ->assertSessionDoesntHaveErrors(); |
145 | 148 | } |
| 149 | + |
| 150 | + public function test_create_postgresql_database_with_icu_collation(): void |
| 151 | + { |
| 152 | + $this->actingAs($this->user); |
| 153 | + |
| 154 | + $this->usePostgresql(); |
| 155 | + |
| 156 | + SSH::fake(); |
| 157 | + |
| 158 | + $this->post(route('databases.store', $this->server), [ |
| 159 | + 'name' => 'pg_database', |
| 160 | + 'charset' => 'UTF8', |
| 161 | + 'collation' => 'en-US-x-icu', |
| 162 | + ])->assertSessionDoesntHaveErrors(); |
| 163 | + |
| 164 | + $this->assertDatabaseHas('databases', [ |
| 165 | + 'name' => 'pg_database', |
| 166 | + 'collation' => 'en-US-x-icu', |
| 167 | + 'status' => DatabaseStatus::READY, |
| 168 | + ]); |
| 169 | + } |
| 170 | + |
| 171 | + public function test_create_database_rejects_malicious_collation(): void |
| 172 | + { |
| 173 | + $this->actingAs($this->user); |
| 174 | + |
| 175 | + SSH::fake(); |
| 176 | + |
| 177 | + $this->post(route('databases.store', $this->server), [ |
| 178 | + 'name' => 'database', |
| 179 | + 'charset' => 'utf8mb4', |
| 180 | + 'collation' => "x'; DROP DATABASE postgres; --", |
| 181 | + ])->assertSessionHasErrors('collation'); |
| 182 | + |
| 183 | + $this->assertDatabaseMissing('databases', [ |
| 184 | + 'name' => 'database', |
| 185 | + ]); |
| 186 | + } |
| 187 | + |
| 188 | + public function test_postgresql_create_script_applies_collation_locale(): void |
| 189 | + { |
| 190 | + $rendered = view('ssh.services.database.postgresql.create', [ |
| 191 | + 'name' => 'pg_database', |
| 192 | + 'charset' => 'UTF8', |
| 193 | + 'collation' => 'en-US-x-icu', |
| 194 | + ])->render(); |
| 195 | + |
| 196 | + $this->assertStringContainsString('CREATE DATABASE', $rendered); |
| 197 | + $this->assertStringContainsString('LOCALE_PROVIDER', $rendered); |
| 198 | + $this->assertStringContainsString('\gexec', $rendered); |
| 199 | + $this->assertStringContainsString('en-US-x-icu', $rendered); |
| 200 | + } |
| 201 | + |
| 202 | + public function test_sync_postgresql_preserves_icu_collation(): void |
| 203 | + { |
| 204 | + $this->actingAs($this->user); |
| 205 | + |
| 206 | + $this->usePostgresql(); |
| 207 | + |
| 208 | + Database::factory()->create([ |
| 209 | + 'server_id' => $this->server, |
| 210 | + 'name' => 'pg_database', |
| 211 | + 'charset' => 'UTF8', |
| 212 | + 'collation' => 'af-NA-x-icu', |
| 213 | + 'status' => DatabaseStatus::READY, |
| 214 | + ]); |
| 215 | + |
| 216 | + SSH::fake(<<<'EOD' |
| 217 | + database_name | charset | collation |
| 218 | + ---------------+---------+------------- |
| 219 | + pg_database | UTF8 | af-NA-x-icu |
| 220 | + (1 row) |
| 221 | + EOD); |
| 222 | + |
| 223 | + $this->patch(route('databases.sync', $this->server)) |
| 224 | + ->assertSessionDoesntHaveErrors(); |
| 225 | + |
| 226 | + $this->assertDatabaseHas('databases', [ |
| 227 | + 'server_id' => $this->server->id, |
| 228 | + 'name' => 'pg_database', |
| 229 | + 'collation' => 'af-NA-x-icu', |
| 230 | + ]); |
| 231 | + } |
| 232 | + |
| 233 | + private function usePostgresql(): void |
| 234 | + { |
| 235 | + $this->server->services()->where('type', Mysql::type())->delete(); |
| 236 | + $this->server->services()->create([ |
| 237 | + 'type' => Postgresql::type(), |
| 238 | + 'name' => Postgresql::id(), |
| 239 | + 'version' => '15', |
| 240 | + 'status' => ServiceStatus::READY, |
| 241 | + ]); |
| 242 | + $this->server->refresh(); |
| 243 | + } |
146 | 244 | } |
0 commit comments