| 1 |
from io import BytesIO |
| 2 |
from unittest.mock import Mock, patch |
| 3 |
|
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
from dbbackup.db.postgresql import ( |
| 7 |
PgDumpBinaryConnector, |
| 8 |
PgDumpConnector, |
| 9 |
PgDumpGisConnector, |
| 10 |
parse_postgres_settings, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
@patch( |
| 15 |
"dbbackup.db.postgresql.PgDumpConnector.run_command", |
| 16 |
return_value=(BytesIO(b"foo"), BytesIO()), |
| 17 |
) |
| 18 |
class PgDumpConnectorTest(TestCase): |
| 19 |
def setUp(self): |
| 20 |
self.connector = PgDumpConnector() |
| 21 |
self.connector.settings["ENGINE"] = "django.db.backends.postgresql" |
| 22 |
self.connector.settings["NAME"] = "dbname" |
| 23 |
self.connector.settings["HOST"] = "hostname" |
| 24 |
|
| 25 |
def test_user_uses_special_characters(self, mock_dump_cmd): |
| 26 |
self.connector.settings["USER"] = "@" |
| 27 |
self.connector.create_dump() |
| 28 |
assert "postgresql://%40@hostname/dbname" in mock_dump_cmd.call_args[0][0] |
| 29 |
|
| 30 |
def test_create_dump(self, mock_dump_cmd): |
| 31 |
dump = self.connector.create_dump() |
| 32 |
# Test dump |
| 33 |
dump_content = dump.read() |
| 34 |
assert dump_content |
| 35 |
assert dump_content == b"foo" |
| 36 |
# Test cmd |
| 37 |
assert mock_dump_cmd.called |
| 38 |
|
| 39 |
def test_create_dump_without_host(self, mock_dump_cmd): |
| 40 |
# this is allowed now: https://github.com/Archmonger/django-dbbackup/issues/520 |
| 41 |
self.connector.settings.pop("HOST", None) |
| 42 |
self.connector.create_dump() |
| 43 |
|
| 44 |
def test_password_but_no_user(self, mock_dump_cmd): |
| 45 |
self.connector.settings.pop("USER", None) |
| 46 |
self.connector.settings["PASSWORD"] = "hello" |
| 47 |
self.connector.create_dump() |
| 48 |
assert "postgresql://hostname/dbname" in mock_dump_cmd.call_args[0][0] |
| 49 |
|
| 50 |
def test_create_dump_host(self, mock_dump_cmd): |
| 51 |
# With |
| 52 |
self.connector.settings["HOST"] = "foo" |
| 53 |
self.connector.create_dump() |
| 54 |
assert "postgresql://foo/dbname" in mock_dump_cmd.call_args[0][0] |
| 55 |
|
| 56 |
def test_create_dump_port(self, mock_dump_cmd): |
| 57 |
# Without |
| 58 |
self.connector.settings.pop("PORT", None) |
| 59 |
self.connector.create_dump() |
| 60 |
assert "postgresql://hostname/dbname" in mock_dump_cmd.call_args[0][0] |
| 61 |
# With |
| 62 |
self.connector.settings["PORT"] = 42 |
| 63 |
self.connector.create_dump() |
| 64 |
assert "postgresql://hostname:42/dbname" in mock_dump_cmd.call_args[0][0] |
| 65 |
|
| 66 |
def test_create_dump_user(self, mock_dump_cmd): |
| 67 |
# Without |
| 68 |
self.connector.settings.pop("USER", None) |
| 69 |
self.connector.create_dump() |
| 70 |
assert "postgresql://hostname/dbname" in mock_dump_cmd.call_args[0][0] |
| 71 |
# With |
| 72 |
self.connector.settings["USER"] = "foo" |
| 73 |
self.connector.create_dump() |
| 74 |
assert "postgresql://foo@hostname/dbname" in mock_dump_cmd.call_args[0][0] |
| 75 |
|
| 76 |
def test_create_dump_exclude(self, mock_dump_cmd): |
| 77 |
# Without |
| 78 |
self.connector.create_dump() |
| 79 |
assert " --exclude-table-data=" not in mock_dump_cmd.call_args[0][0] |
| 80 |
# With |
| 81 |
self.connector.exclude = ("foo",) |
| 82 |
self.connector.create_dump() |
| 83 |
assert " --exclude-table-data=foo" in mock_dump_cmd.call_args[0][0] |
| 84 |
# With several |
| 85 |
self.connector.exclude = ("foo", "bar") |
| 86 |
self.connector.create_dump() |
| 87 |
assert " --exclude-table-data=foo" in mock_dump_cmd.call_args[0][0] |
| 88 |
assert " --exclude-table-data=bar" in mock_dump_cmd.call_args[0][0] |
| 89 |
|
| 90 |
def test_create_dump_drop(self, mock_dump_cmd): |
| 91 |
# Without |
| 92 |
self.connector.drop = False |
| 93 |
self.connector.create_dump() |
| 94 |
assert " --clean" not in mock_dump_cmd.call_args[0][0] |
| 95 |
# With |
| 96 |
self.connector.drop = True |
| 97 |
self.connector.create_dump() |
| 98 |
assert " --clean" in mock_dump_cmd.call_args[0][0] |
| 99 |
|
| 100 |
@patch( |
| 101 |
"dbbackup.db.postgresql.PgDumpConnector.run_command", |
| 102 |
return_value=(BytesIO(), BytesIO()), |
| 103 |
) |
| 104 |
def test_restore_dump(self, mock_dump_cmd, mock_restore_cmd): |
| 105 |
dump = self.connector.create_dump() |
| 106 |
self.connector.restore_dump(dump) |
| 107 |
# Test cmd |
| 108 |
assert mock_restore_cmd.called |
| 109 |
|
| 110 |
@patch( |
| 111 |
"dbbackup.db.postgresql.PgDumpConnector.run_command", |
| 112 |
return_value=(BytesIO(), BytesIO()), |
| 113 |
) |
| 114 |
def test_restore_dump_user(self, mock_dump_cmd, mock_restore_cmd): |
| 115 |
dump = self.connector.create_dump() |
| 116 |
# Without |
| 117 |
self.connector.settings.pop("USER", None) |
| 118 |
self.connector.restore_dump(dump) |
| 119 |
|
| 120 |
assert "postgresql://hostname/dbname" in mock_restore_cmd.call_args[0][0] |
| 121 |
|
| 122 |
assert " --username=" not in mock_restore_cmd.call_args[0][0] |
| 123 |
# With |
| 124 |
self.connector.settings["USER"] = "foo" |
| 125 |
self.connector.restore_dump(dump) |
| 126 |
assert "postgresql://foo@hostname/dbname" in mock_restore_cmd.call_args[0][0] |
| 127 |
|
| 128 |
def test_create_dump_schema(self, mock_dump_cmd): |
| 129 |
# Without |
| 130 |
self.connector.create_dump() |
| 131 |
assert " -n " not in mock_dump_cmd.call_args[0][0] |
| 132 |
# With |
| 133 |
self.connector.schemas = ["public"] |
| 134 |
self.connector.create_dump() |
| 135 |
assert " -n public" in mock_dump_cmd.call_args[0][0] |
| 136 |
# With several |
| 137 |
self.connector.schemas = ["public", "foo"] |
| 138 |
self.connector.create_dump() |
| 139 |
assert " -n public" in mock_dump_cmd.call_args[0][0] |
| 140 |
assert " -n foo" in mock_dump_cmd.call_args[0][0] |
| 141 |
|
| 142 |
@patch( |
| 143 |
"dbbackup.db.postgresql.PgDumpConnector.run_command", |
| 144 |
return_value=(BytesIO(), BytesIO()), |
| 145 |
) |
| 146 |
def test_restore_dump_schema(self, mock_dump_cmd, mock_restore_cmd): |
| 147 |
# Without |
| 148 |
dump = self.connector.create_dump() |
| 149 |
self.connector.restore_dump(dump) |
| 150 |
assert " -n " not in mock_restore_cmd.call_args[0][0] |
| 151 |
# With |
| 152 |
self.connector.schemas = ["public"] |
| 153 |
dump = self.connector.create_dump() |
| 154 |
self.connector.restore_dump(dump) |
| 155 |
assert " -n public" in mock_restore_cmd.call_args[0][0] |
| 156 |
# With several |
| 157 |
self.connector.schemas = ["public", "foo"] |
| 158 |
dump = self.connector.create_dump() |
| 159 |
self.connector.restore_dump(dump) |
| 160 |
assert " -n public" in mock_restore_cmd.call_args[0][0] |
| 161 |
assert " -n foo" in mock_restore_cmd.call_args[0][0] |
| 162 |
|
| 163 |
def test_create_dump_password_excluded(self, mock_dump_cmd): |
| 164 |
self.connector.settings["PASSWORD"] = "secret" |
| 165 |
self.connector.create_dump() |
| 166 |
assert "secret" not in mock_dump_cmd.call_args[0][0] |
| 167 |
|
| 168 |
def test_restore_dump_password_excluded(self, mock_dump_cmd): |
| 169 |
self.connector.settings["PASSWORD"] = "secret" |
| 170 |
dump = self.connector.create_dump() |
| 171 |
self.connector.restore_dump(dump) |
| 172 |
assert "secret" not in mock_dump_cmd.call_args[0][0] |
| 173 |
|
| 174 |
|
| 175 |
@patch( |
| 176 |
"dbbackup.db.postgresql.PgDumpBinaryConnector.run_command", |
| 177 |
return_value=(BytesIO(b"foo"), BytesIO()), |
| 178 |
) |
| 179 |
class PgDumpBinaryConnectorTest(TestCase): |
| 180 |
def setUp(self): |
| 181 |
self.connector = PgDumpBinaryConnector() |
| 182 |
self.connector.settings["HOST"] = "hostname" |
| 183 |
self.connector.settings["ENGINE"] = "django.db.backends.postgresql" |
| 184 |
self.connector.settings["NAME"] = "dbname" |
| 185 |
|
| 186 |
def test_create_dump(self, mock_dump_cmd): |
| 187 |
dump = self.connector.create_dump() |
| 188 |
# Test dump |
| 189 |
dump_content = dump.read() |
| 190 |
assert dump_content |
| 191 |
assert dump_content == b"foo" |
| 192 |
# Test cmd |
| 193 |
assert mock_dump_cmd.called |
| 194 |
assert "--format=custom" in mock_dump_cmd.call_args[0][0] |
| 195 |
|
| 196 |
def test_create_dump_exclude(self, mock_dump_cmd): |
| 197 |
# Without |
| 198 |
self.connector.create_dump() |
| 199 |
assert " --exclude-table-data=" not in mock_dump_cmd.call_args[0][0] |
| 200 |
# With |
| 201 |
self.connector.exclude = ("foo",) |
| 202 |
self.connector.create_dump() |
| 203 |
assert " --exclude-table-data=foo" in mock_dump_cmd.call_args[0][0] |
| 204 |
# With several |
| 205 |
self.connector.exclude = ("foo", "bar") |
| 206 |
self.connector.create_dump() |
| 207 |
assert " --exclude-table-data=foo" in mock_dump_cmd.call_args[0][0] |
| 208 |
assert " --exclude-table-data=bar" in mock_dump_cmd.call_args[0][0] |
| 209 |
|
| 210 |
def test_create_dump_drop(self, mock_dump_cmd): |
| 211 |
# Without |
| 212 |
self.connector.drop = False |
| 213 |
self.connector.create_dump() |
| 214 |
assert " --clean" not in mock_dump_cmd.call_args[0][0] |
| 215 |
# Binary drop at restore level |
| 216 |
self.connector.drop = True |
| 217 |
self.connector.create_dump() |
| 218 |
assert " --clean" not in mock_dump_cmd.call_args[0][0] |
| 219 |
|
| 220 |
def test_create_dump_if_exists(self, mock_run_command): |
| 221 |
dump = self.connector.create_dump() |
| 222 |
|
| 223 |
# Default now includes --if-exists even without --clean |
| 224 |
self.connector.drop = False |
| 225 |
self.connector.restore_dump(dump) |
| 226 |
cmd_args = mock_run_command.call_args[0][0] |
| 227 |
assert " --clean" not in cmd_args |
| 228 |
assert " --if-exists" in cmd_args |
| 229 |
|
| 230 |
# Disabling if_exists explicitly should remove the flag (when drop=False) |
| 231 |
self.connector.if_exists = False |
| 232 |
self.connector.drop = False |
| 233 |
self.connector.restore_dump(dump) |
| 234 |
cmd_args = mock_run_command.call_args[0][0] |
| 235 |
assert " --clean" not in cmd_args |
| 236 |
assert " --if-exists" not in cmd_args |
| 237 |
|
| 238 |
def test_clean_automatically_enables_if_exists(self, mock_run_command): |
| 239 |
"""Test that --if-exists is automatically added when using --clean to prevent identity column errors.""" |
| 240 |
dump = self.connector.create_dump() |
| 241 |
|
| 242 |
# When drop=True (which adds --clean), --if-exists should be automatically added even if disabled |
| 243 |
self.connector.drop = True |
| 244 |
self.connector.if_exists = False # Explicitly set to False |
| 245 |
self.connector.restore_dump(dump) |
| 246 |
cmd_args = mock_run_command.call_args[0][0] |
| 247 |
assert " --clean" in cmd_args |
| 248 |
assert " --if-exists" in cmd_args |
| 249 |
|
| 250 |
# When drop=False and if_exists=False, --if-exists should not be added |
| 251 |
self.connector.drop = False |
| 252 |
self.connector.if_exists = False |
| 253 |
self.connector.restore_dump(dump) |
| 254 |
cmd_args = mock_run_command.call_args[0][0] |
| 255 |
assert " --clean" not in cmd_args |
| 256 |
assert " --if-exists" not in cmd_args |
| 257 |
|
| 258 |
def test_pg_options(self, mock_run_command): |
| 259 |
dump = self.connector.create_dump() |
| 260 |
self.connector.pg_options = "--foo" |
| 261 |
self.connector.restore_dump(dump) |
| 262 |
cmd_args = mock_run_command.call_args[0][0] |
| 263 |
assert "--foo" in cmd_args |
| 264 |
|
| 265 |
def test_restore_prefix(self, mock_run_command): |
| 266 |
dump = self.connector.create_dump() |
| 267 |
self.connector.restore_prefix = "foo" |
| 268 |
self.connector.restore_dump(dump) |
| 269 |
cmd_args = mock_run_command.call_args[0][0] |
| 270 |
assert cmd_args.startswith("foo ") |
| 271 |
|
| 272 |
def test_restore_suffix(self, mock_run_command): |
| 273 |
dump = self.connector.create_dump() |
| 274 |
self.connector.restore_suffix = "foo" |
| 275 |
self.connector.restore_dump(dump) |
| 276 |
cmd_args = mock_run_command.call_args[0][0] |
| 277 |
assert cmd_args.endswith(" foo") |
| 278 |
|
| 279 |
@patch( |
| 280 |
"dbbackup.db.postgresql.PgDumpBinaryConnector.run_command", |
| 281 |
return_value=(BytesIO(), BytesIO()), |
| 282 |
) |
| 283 |
def test_restore_dump(self, mock_dump_cmd, mock_restore_cmd): |
| 284 |
dump = self.connector.create_dump() |
| 285 |
self.connector.restore_dump(dump) |
| 286 |
# Test cmd |
| 287 |
assert mock_restore_cmd.called |
| 288 |
|
| 289 |
def test_create_dump_schema(self, mock_dump_cmd): |
| 290 |
# Without |
| 291 |
self.connector.create_dump() |
| 292 |
assert " -n " not in mock_dump_cmd.call_args[0][0] |
| 293 |
# With |
| 294 |
self.connector.schemas = ["public"] |
| 295 |
self.connector.create_dump() |
| 296 |
assert " -n public" in mock_dump_cmd.call_args[0][0] |
| 297 |
# With several |
| 298 |
self.connector.schemas = ["public", "foo"] |
| 299 |
self.connector.create_dump() |
| 300 |
assert " -n public" in mock_dump_cmd.call_args[0][0] |
| 301 |
assert " -n foo" in mock_dump_cmd.call_args[0][0] |
| 302 |
|
| 303 |
@patch( |
| 304 |
"dbbackup.db.postgresql.PgDumpBinaryConnector.run_command", |
| 305 |
return_value=(BytesIO(), BytesIO()), |
| 306 |
) |
| 307 |
def test_restore_dump_schema(self, mock_dump_cmd, mock_restore_cmd): |
| 308 |
# Without |
| 309 |
dump = self.connector.create_dump() |
| 310 |
self.connector.restore_dump(dump) |
| 311 |
assert " -n " not in mock_restore_cmd.call_args[0][0] |
| 312 |
# With |
| 313 |
self.connector.schemas = ["public"] |
| 314 |
dump = self.connector.create_dump() |
| 315 |
self.connector.restore_dump(dump) |
| 316 |
assert " -n public" in mock_restore_cmd.call_args[0][0] |
| 317 |
# With several |
| 318 |
self.connector.schemas = ["public", "foo"] |
| 319 |
dump = self.connector.create_dump() |
| 320 |
self.connector.restore_dump(dump) |
| 321 |
assert " -n public" in mock_restore_cmd.call_args[0][0] |
| 322 |
assert " -n foo" in mock_restore_cmd.call_args[0][0] |
| 323 |
|
| 324 |
def test_create_dump_password_excluded(self, mock_dump_cmd): |
| 325 |
self.connector.settings["PASSWORD"] = "secret" |
| 326 |
self.connector.create_dump() |
| 327 |
assert "secret" not in mock_dump_cmd.call_args[0][0] |
| 328 |
|
| 329 |
def test_restore_dump_password_excluded(self, mock_dump_cmd): |
| 330 |
self.connector.settings["PASSWORD"] = "secret" |
| 331 |
dump = self.connector.create_dump() |
| 332 |
self.connector.restore_dump(dump) |
| 333 |
assert "secret" not in mock_dump_cmd.call_args[0][0] |
| 334 |
|
| 335 |
def test_restore_dump_quotes_unix_socket_host(self, mock_run_command): |
| 336 |
self.connector.settings["HOST"] = "/run/postgresql" |
| 337 |
self.connector.settings["PORT"] = 5432 |
| 338 |
self.connector.settings["USER"] = "example" |
| 339 |
|
| 340 |
dump = self.connector.create_dump() |
| 341 |
self.connector.restore_dump(dump) |
| 342 |
|
| 343 |
assert "--dbname=postgresql://example@%2Frun%2Fpostgresql:5432/dbname" in mock_run_command.call_args[0][0] |
| 344 |
|
| 345 |
|
| 346 |
@patch( |
| 347 |
"dbbackup.db.postgresql.PgDumpGisConnector.run_command", |
| 348 |
return_value=(BytesIO(b"foo"), BytesIO()), |
| 349 |
) |
| 350 |
class PgDumpGisConnectorTest(TestCase): |
| 351 |
def setUp(self): |
| 352 |
self.connector = PgDumpGisConnector() |
| 353 |
self.connector.settings["HOST"] = "hostname" |
| 354 |
|
| 355 |
@patch( |
| 356 |
"dbbackup.db.postgresql.PgDumpGisConnector.run_command", |
| 357 |
return_value=(BytesIO(b"foo"), BytesIO()), |
| 358 |
) |
| 359 |
def test_restore_dump(self, mock_dump_cmd, mock_restore_cmd): |
| 360 |
dump = self.connector.create_dump() |
| 361 |
# Without ADMINUSER |
| 362 |
self.connector.settings.pop("ADMIN_USER", None) |
| 363 |
self.connector.restore_dump(dump) |
| 364 |
assert mock_restore_cmd.called |
| 365 |
# With |
| 366 |
self.connector.settings["ADMIN_USER"] = "foo" |
| 367 |
self.connector.restore_dump(dump) |
| 368 |
assert mock_restore_cmd.called |
| 369 |
|
| 370 |
def test_enable_postgis(self, mock_dump_cmd): |
| 371 |
self.connector.settings["ADMIN_USER"] = "foo" |
| 372 |
self.connector._enable_postgis() |
| 373 |
assert '"CREATE EXTENSION IF NOT EXISTS postgis;"' in mock_dump_cmd.call_args[0][0] |
| 374 |
assert "--username=foo" in mock_dump_cmd.call_args[0][0] |
| 375 |
|
| 376 |
def test_enable_postgis_host(self, mock_dump_cmd): |
| 377 |
self.connector.settings["ADMIN_USER"] = "foo" |
| 378 |
# Without |
| 379 |
self.connector.settings.pop("HOST", None) |
| 380 |
self.connector._enable_postgis() |
| 381 |
assert " --host=" not in mock_dump_cmd.call_args[0][0] |
| 382 |
# With |
| 383 |
self.connector.settings["HOST"] = "foo" |
| 384 |
self.connector._enable_postgis() |
| 385 |
assert " --host=foo" in mock_dump_cmd.call_args[0][0] |
| 386 |
|
| 387 |
def test_enable_postgis_port(self, mock_dump_cmd): |
| 388 |
self.connector.settings["ADMIN_USER"] = "foo" |
| 389 |
# Without |
| 390 |
self.connector.settings.pop("PORT", None) |
| 391 |
self.connector._enable_postgis() |
| 392 |
assert " --port=" not in mock_dump_cmd.call_args[0][0] |
| 393 |
# With |
| 394 |
self.connector.settings["PORT"] = 42 |
| 395 |
self.connector._enable_postgis() |
| 396 |
assert " --port=42" in mock_dump_cmd.call_args[0][0] |
| 397 |
|
| 398 |
def test_enable_postgis_special_characters(self, mock_dump_cmd): |
| 399 |
"""Test that special characters in GIS connector parameters are properly escaped""" |
| 400 |
# Test admin user with special characters |
| 401 |
self.connector.settings["ADMIN_USER"] = "admin user" # Space |
| 402 |
self.connector.settings["HOST"] = "localhost" |
| 403 |
self.connector._enable_postgis() |
| 404 |
command = mock_dump_cmd.call_args[0][0] |
| 405 |
assert "'admin user'" in command # Should be quoted |
| 406 |
|
| 407 |
# Test host with special characters |
| 408 |
self.connector.settings["ADMIN_USER"] = "admin" |
| 409 |
self.connector.settings["HOST"] = "[email protected]" # @ symbol |
| 410 |
self.connector._enable_postgis() |
| 411 |
command = mock_dump_cmd.call_args[0][0] |
| 412 |
assert "[email protected]" in command # @ in hostname is safe, no quotes needed |
| 413 |
|
| 414 |
# Test admin user with quotes |
| 415 |
self.connector.settings["ADMIN_USER"] = "admin'user" # Single quote |
| 416 |
self.connector.settings["HOST"] = "localhost" |
| 417 |
self.connector._enable_postgis() |
| 418 |
command = mock_dump_cmd.call_args[0][0] |
| 419 |
# Should escape the single quote properly |
| 420 |
assert "'admin'\"'\"'user'" in command |
| 421 |
|
| 422 |
|
| 423 |
@patch( |
| 424 |
"dbbackup.db.base.Popen", |
| 425 |
**{ |
| 426 |
"return_value.wait.return_value": True, |
| 427 |
"return_value.poll.return_value": False, |
| 428 |
}, |
| 429 |
) |
| 430 |
class PgDumpConnectorRunCommandTest(TestCase): |
| 431 |
def test_run_command(self, mock_popen): |
| 432 |
connector = PgDumpConnector() |
| 433 |
connector.settings["HOST"] = "hostname" |
| 434 |
connector.create_dump() |
| 435 |
assert mock_popen.call_args[0][0][0] == "pg_dump" |
| 436 |
|
| 437 |
def test_run_command_with_password(self, mock_popen): |
| 438 |
connector = PgDumpConnector() |
| 439 |
connector.settings["HOST"] = "hostname" |
| 440 |
connector.settings["PASSWORD"] = "foo" |
| 441 |
connector.create_dump() |
| 442 |
assert mock_popen.call_args[0][0][0] == "pg_dump" |
| 443 |
assert "foo" not in " ".join(mock_popen.call_args[0][0]) |
| 444 |
assert mock_popen.call_args[1]["env"]["PGPASSWORD"] == "foo" |
| 445 |
|
| 446 |
def test_run_command_with_password_and_other(self, mock_popen): |
| 447 |
connector = PgDumpConnector(env={"foo": "bar"}) |
| 448 |
connector.settings["HOST"] = "hostname" |
| 449 |
connector.settings["PASSWORD"] = "foo" |
| 450 |
connector.create_dump() |
| 451 |
assert mock_popen.call_args[0][0][0] == "pg_dump" |
| 452 |
assert "foo" in mock_popen.call_args[1]["env"] |
| 453 |
assert mock_popen.call_args[1]["env"]["foo"] == "bar" |
| 454 |
assert "foo" not in " ".join(mock_popen.call_args[0][0]) |
| 455 |
assert mock_popen.call_args[1]["env"]["PGPASSWORD"] == "foo" |
| 456 |
|
| 457 |
|
| 458 |
class CreatePostgresDbNameAndEnvTest(TestCase): |
| 459 |
"""Test the create_postgres_dbname_and_env helper function""" |
| 460 |
|
| 461 |
def test_function_with_normal_parameters(self): |
| 462 |
"""Test function with normal parameters""" |
| 463 |
connector = Mock() |
| 464 |
connector.settings = { |
| 465 |
"HOST": "localhost", |
| 466 |
"PORT": 5432, |
| 467 |
"NAME": "testdb", |
| 468 |
"USER": "testuser", |
| 469 |
"PASSWORD": "testpass", |
| 470 |
} |
| 471 |
|
| 472 |
cmd_part, env = parse_postgres_settings(connector) |
| 473 |
|
| 474 |
assert cmd_part == "--dbname=postgresql://testuser@localhost:5432/testdb" |
| 475 |
assert env == {"PGPASSWORD": "testpass"} |
| 476 |
|
| 477 |
def test_function_with_special_characters(self): |
| 478 |
"""Test function with special characters in user and password""" |
| 479 |
connector = Mock() |
| 480 |
connector.settings = { |
| 481 |
"HOST": "localhost", |
| 482 |
"PORT": 5432, |
| 483 |
"NAME": "testdb", |
| 484 |
"USER": "[email protected]", # Email-style username |
| 485 |
"PASSWORD": "my'pass\"word", # Password with quotes |
| 486 |
} |
| 487 |
|
| 488 |
cmd_part, env = parse_postgres_settings(connector) |
| 489 |
|
| 490 |
# User should be URL-encoded in the connection string |
| 491 |
assert "user%40domain.com" in cmd_part |
| 492 |
# Password should be in environment variable unchanged |
| 493 |
assert env["PGPASSWORD"] == "my'pass\"word" |
| 494 |
# Password should not appear in URL |
| 495 |
assert "my'pass" not in cmd_part |
| 496 |
|
| 497 |
def test_function_url_quotes_unix_socket_host(self): |
| 498 |
"""Test function URL-quotes unix socket hosts in the connection string""" |
| 499 |
connector = Mock() |
| 500 |
connector.settings = { |
| 501 |
"HOST": "/run/postgresql", |
| 502 |
"PORT": 5432, |
| 503 |
"NAME": "inventree", |
| 504 |
"USER": "inventree", |
| 505 |
} |
| 506 |
|
| 507 |
cmd_part, env = parse_postgres_settings(connector) |
| 508 |
|
| 509 |
assert cmd_part == "--dbname=postgresql://inventree@%2Frun%2Fpostgresql:5432/inventree" |
| 510 |
assert env == {} |
| 511 |
|
| 512 |
def test_function_preserves_ipv6_host(self): |
| 513 |
"""Test function preserves valid IPv6 host syntax in the URI hostspec""" |
| 514 |
connector = Mock() |
| 515 |
connector.settings = { |
| 516 |
"HOST": "[2001:db8::1234]", |
| 517 |
"PORT": 5432, |
| 518 |
"NAME": "testdb", |
| 519 |
"USER": "testuser", |
| 520 |
} |
| 521 |
|
| 522 |
cmd_part, env = parse_postgres_settings(connector) |
| 523 |
|
| 524 |
assert cmd_part == "--dbname=postgresql://testuser@[2001:db8::1234]:5432/testdb" |
| 525 |
assert env == {} |
| 526 |
|
| 527 |
def test_function_defaults_none_host_to_localhost(self): |
| 528 |
"""Test function defaults an unset HOST value to localhost""" |
| 529 |
connector = Mock() |
| 530 |
connector.settings = {"HOST": None, "PORT": 5432, "NAME": "testdb"} |
| 531 |
|
| 532 |
cmd_part, env = parse_postgres_settings(connector) |
| 533 |
|
| 534 |
assert cmd_part == "--dbname=postgresql://localhost:5432/testdb" |
| 535 |
assert env == {} |
| 536 |
|
| 537 |
def test_function_without_user_or_password(self): |
| 538 |
"""Test function without user or password""" |
| 539 |
connector = Mock() |
| 540 |
connector.settings = {"HOST": "localhost", "PORT": 5432, "NAME": "testdb"} |
| 541 |
|
| 542 |
cmd, env = parse_postgres_settings(connector) |
| 543 |
|
| 544 |
# No user means no @ in the URL |
| 545 |
assert cmd == "--dbname=postgresql://localhost:5432/testdb" |
| 546 |
# No password means empty environment |
| 547 |
assert env == {} |
| 548 |
|
| 549 |
def test_function_with_empty_password(self): |
| 550 |
"""Test function with empty password""" |
| 551 |
connector = Mock() |
| 552 |
connector.settings = { |
| 553 |
"HOST": "localhost", |
| 554 |
"PORT": 5432, |
| 555 |
"NAME": "testdb", |
| 556 |
"USER": "testuser", |
| 557 |
"PASSWORD": "", # Empty password |
| 558 |
} |
| 559 |
|
| 560 |
cmd_part, env = parse_postgres_settings(connector) |
| 561 |
|
| 562 |
assert "testuser@localhost" in cmd_part |
| 563 |
# Empty password should not create PGPASSWORD env var |
| 564 |
assert env == {} |
| 565 |
|
| 566 |
connector.settings = { |
| 567 |
"HOST": "localhost", |
| 568 |
"PORT": 5432, |
| 569 |
"NAME": "testdb", |
| 570 |
"USER": "testuser", |
| 571 |
"PASSWORD": None, # "no password" case |
| 572 |
} |
| 573 |
|
| 574 |
cmd_part, env = parse_postgres_settings(connector) |
| 575 |
|
| 576 |
assert "testuser@localhost" in cmd_part |
| 577 |
# "None" password should not create PGPASSWORD env var, but should add --no-password flag |
| 578 |
assert env == {} |
| 579 |
assert "--no-password" in cmd_part |