test_api_backup.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """
  2. Unit tests for the backup API endpoints.
  3. Tests the following endpoints:
  4. - GET /api/backup/settings
  5. - GET /api/backup/custom_patterns
  6. """
  7. import io
  8. import json
  9. import os
  10. import zipfile
  11. import pytest
  12. class TestBackupSettings:
  13. """Tests for /api/backup/settings endpoint."""
  14. @pytest.mark.asyncio
  15. async def test_backup_settings_returns_zip(self, async_client, tmp_path, monkeypatch):
  16. """Returns a zip that contains settings.json and playlists.json."""
  17. settings_file = tmp_path / "settings.json"
  18. settings_file.write_text(json.dumps({"speed": 42}))
  19. playlists_file = tmp_path / "playlists.json"
  20. playlists_file.write_text(json.dumps({"My Playlist": ["a.thr"]}))
  21. import main
  22. monkeypatch.setattr(main.state, "SETTINGS_FILE", str(settings_file))
  23. monkeypatch.setattr(main.state, "custom_logo", None)
  24. monkeypatch.setattr(main.state, "save", lambda: None)
  25. monkeypatch.setattr(main.playlist_manager, "PLAYLISTS_FILE", str(playlists_file))
  26. response = await async_client.get("/api/backup/settings")
  27. assert response.status_code == 200
  28. assert response.headers["content-type"] == "application/zip"
  29. assert "attachment" in response.headers["content-disposition"]
  30. with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
  31. names = zf.namelist()
  32. assert "settings.json" in names
  33. assert "playlists.json" in names
  34. assert json.loads(zf.read("settings.json")) == {"speed": 42}
  35. assert json.loads(zf.read("playlists.json")) == {"My Playlist": ["a.thr"]}
  36. @pytest.mark.asyncio
  37. async def test_backup_settings_includes_custom_logo(self, async_client, tmp_path, monkeypatch):
  38. """Includes the custom logo file in the archive when set."""
  39. settings_file = tmp_path / "settings.json"
  40. settings_file.write_text("{}")
  41. playlists_file = tmp_path / "playlists.json"
  42. playlists_file.write_text("{}")
  43. branding_dir = tmp_path / "branding"
  44. branding_dir.mkdir()
  45. logo_file = branding_dir / "logo-xyz.png"
  46. logo_file.write_bytes(b"\x89PNG\r\n\x1a\nfake")
  47. import main
  48. monkeypatch.setattr(main.state, "SETTINGS_FILE", str(settings_file))
  49. monkeypatch.setattr(main.state, "custom_logo", "logo-xyz.png")
  50. monkeypatch.setattr(main.state, "save", lambda: None)
  51. monkeypatch.setattr(main.playlist_manager, "PLAYLISTS_FILE", str(playlists_file))
  52. monkeypatch.setattr(main, "CUSTOM_BRANDING_DIR", str(branding_dir))
  53. response = await async_client.get("/api/backup/settings")
  54. assert response.status_code == 200
  55. with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
  56. assert "custom/logo-xyz.png" in zf.namelist()
  57. assert zf.read("custom/logo-xyz.png") == b"\x89PNG\r\n\x1a\nfake"
  58. class TestBackupCustomPatterns:
  59. """Tests for /api/backup/custom_patterns endpoint."""
  60. @pytest.mark.asyncio
  61. async def test_backup_custom_patterns_returns_zip(self, async_client, tmp_path, monkeypatch):
  62. """Zips every file under patterns/custom_patterns/."""
  63. theta_rho_dir = tmp_path / "patterns"
  64. custom = theta_rho_dir / "custom_patterns"
  65. custom.mkdir(parents=True)
  66. (custom / "one.thr").write_text("0 0\n1 1\n")
  67. (custom / "two.thr").write_text("2 2\n")
  68. import main
  69. monkeypatch.setattr(main.pattern_manager, "THETA_RHO_DIR", str(theta_rho_dir))
  70. response = await async_client.get("/api/backup/custom_patterns")
  71. assert response.status_code == 200
  72. assert response.headers["content-type"] == "application/zip"
  73. with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
  74. names = sorted(zf.namelist())
  75. assert names == ["one.thr", "two.thr"]
  76. assert zf.read("one.thr").decode() == "0 0\n1 1\n"
  77. @pytest.mark.asyncio
  78. async def test_backup_custom_patterns_skips_hidden_files(self, async_client, tmp_path, monkeypatch):
  79. """The archive omits dotfiles like .gitignore."""
  80. theta_rho_dir = tmp_path / "patterns"
  81. custom = theta_rho_dir / "custom_patterns"
  82. custom.mkdir(parents=True)
  83. (custom / ".gitignore").write_text("*\n")
  84. (custom / "keep.thr").write_text("0 0\n")
  85. import main
  86. monkeypatch.setattr(main.pattern_manager, "THETA_RHO_DIR", str(theta_rho_dir))
  87. response = await async_client.get("/api/backup/custom_patterns")
  88. assert response.status_code == 200
  89. with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
  90. names = zf.namelist()
  91. assert names == ["keep.thr"]
  92. @pytest.mark.asyncio
  93. async def test_backup_custom_patterns_empty_when_directory_missing(
  94. self, async_client, tmp_path, monkeypatch
  95. ):
  96. """Returns an empty zip (not a 500) when the directory does not exist."""
  97. theta_rho_dir = tmp_path / "patterns"
  98. theta_rho_dir.mkdir()
  99. import main
  100. monkeypatch.setattr(main.pattern_manager, "THETA_RHO_DIR", str(theta_rho_dir))
  101. assert not os.path.isdir(os.path.join(str(theta_rho_dir), "custom_patterns"))
  102. response = await async_client.get("/api/backup/custom_patterns")
  103. assert response.status_code == 200
  104. with zipfile.ZipFile(io.BytesIO(response.content)) as zf:
  105. assert zf.namelist() == []