test_local_config.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. Tests for backend.app.core.local_config — the reader for
  3. /etc/bambuddy/local.toml that the appliance setup wizard writes.
  4. Defensive on bad input: every failure mode returns an empty dict
  5. (never raises), so a malformed file never blocks startup.
  6. """
  7. from __future__ import annotations
  8. from pathlib import Path
  9. import pytest
  10. from backend.app.core.local_config import read_local_toml
  11. def test_missing_file_returns_empty(tmp_path: Path):
  12. assert read_local_toml(tmp_path / "nope.toml") == {}
  13. def test_empty_file_returns_empty(tmp_path: Path):
  14. path = tmp_path / "local.toml"
  15. path.write_text("")
  16. assert read_local_toml(path) == {}
  17. def test_comment_only_file_returns_empty(tmp_path: Path):
  18. path = tmp_path / "local.toml"
  19. path.write_text("# Written by bambuddy-wizard during firstboot.\n")
  20. assert read_local_toml(path) == {}
  21. def test_full_config_parses(tmp_path: Path):
  22. path = tmp_path / "local.toml"
  23. path.write_text(
  24. "# Written by bambuddy-wizard during firstboot.\n"
  25. 'hostname = "workshop-pi"\n'
  26. 'timezone = "Europe/Berlin"\n'
  27. 'locale = "de"\n'
  28. )
  29. result = read_local_toml(path)
  30. assert result == {
  31. "hostname": "workshop-pi",
  32. "timezone": "Europe/Berlin",
  33. "locale": "de",
  34. }
  35. def test_partial_config_only_returns_present_keys(tmp_path: Path):
  36. path = tmp_path / "local.toml"
  37. path.write_text('locale = "ja"\n')
  38. result = read_local_toml(path)
  39. assert result == {"locale": "ja"}
  40. assert "hostname" not in result
  41. assert "timezone" not in result
  42. def test_invalid_toml_returns_empty(tmp_path: Path, caplog: pytest.LogCaptureFixture):
  43. path = tmp_path / "local.toml"
  44. path.write_text("not = valid = toml = at all\n")
  45. result = read_local_toml(path)
  46. assert result == {}
  47. assert any("could not be parsed" in r.message for r in caplog.records)
  48. def test_non_string_value_is_dropped(tmp_path: Path, caplog: pytest.LogCaptureFixture):
  49. path = tmp_path / "local.toml"
  50. path.write_text(
  51. "hostname = 42\n" # not a string
  52. 'locale = "de"\n'
  53. )
  54. result = read_local_toml(path)
  55. assert result == {"locale": "de"}
  56. assert any("expected str" in r.message for r in caplog.records)
  57. def test_unknown_keys_are_ignored(tmp_path: Path):
  58. """A hand-edited config with extra keys must not leak them to the response."""
  59. path = tmp_path / "local.toml"
  60. path.write_text('locale = "de"\nunknown_key = "value"\nadmin_password = "should not surface"\n')
  61. result = read_local_toml(path)
  62. assert set(result.keys()) <= {"hostname", "timezone", "locale"}
  63. assert "admin_password" not in result
  64. def test_escaped_characters_round_trip(tmp_path: Path):
  65. """The wizard escapes backslash and quote when writing; the reader parses them back."""
  66. path = tmp_path / "local.toml"
  67. path.write_text('hostname = "with\\"quote"\n')
  68. result = read_local_toml(path)
  69. assert result == {"hostname": 'with"quote'}