test_config.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """
  2. Tests for the configuration module.
  3. """
  4. import os
  5. from unittest.mock import patch
  6. from web.core.config import Settings
  7. class TestSettings:
  8. """Test suite for Settings class."""
  9. def test_default_settings(self):
  10. """Test that default settings are initialized correctly."""
  11. with patch.dict(os.environ, {}, clear=True):
  12. settings = Settings()
  13. assert settings.app_name == "CustomBuild API"
  14. assert settings.app_version == "1.0.0"
  15. assert settings.debug is False
  16. assert settings.redis_host == "localhost"
  17. assert settings.redis_port == "6379"
  18. assert settings.log_level == "INFO"
  19. assert settings.ap_git_url == "https://github.com/ardupilot/ardupilot.git"
  20. assert settings.enable_inbuilt_builder is True
  21. assert settings.ap_firmware_manifest_url.endswith(".xz")
  22. def test_env_var_overrides(self):
  23. """Test that environment variables override default settings."""
  24. env_overrides = {
  25. "CBS_BASEDIR": "/custom/base/path",
  26. "CBS_REDIS_HOST": "redis.example.com",
  27. "CBS_REDIS_PORT": "6380",
  28. "CBS_LOG_LEVEL": "DEBUG",
  29. "CBS_ENABLE_INBUILT_BUILDER": "0"
  30. }
  31. with patch.dict(os.environ, env_overrides, clear=True):
  32. settings = Settings()
  33. assert settings.base_dir == "/custom/base/path"
  34. assert settings.redis_host == "redis.example.com"
  35. assert settings.redis_port == "6380"
  36. assert settings.log_level == "DEBUG"
  37. assert settings.enable_inbuilt_builder is False
  38. class TestAdminToken:
  39. """Test suite for admin_token property."""
  40. def test_token_from_file(self, tmp_path):
  41. """Test that token is read from file when it exists."""
  42. secrets_dir = tmp_path / 'secrets'
  43. secrets_dir.mkdir()
  44. token_file = secrets_dir / 'admin_token'
  45. expected_token = "test-token-from-file"
  46. token_file.write_text(f" {expected_token} \n") # Test whitespace stripping
  47. with patch.dict(os.environ, {"CBS_BASEDIR": str(tmp_path)}, clear=True):
  48. settings = Settings()
  49. assert settings.admin_token == expected_token
  50. def test_token_file_takes_precedence_over_env(self, tmp_path):
  51. """Test that token from file takes precedence over environment variable."""
  52. secrets_dir = tmp_path / 'secrets'
  53. secrets_dir.mkdir()
  54. token_file = secrets_dir / 'admin_token'
  55. file_token = "token-from-file"
  56. env_token = "token-from-env"
  57. token_file.write_text(file_token)
  58. with patch.dict(os.environ, {
  59. "CBS_BASEDIR": str(tmp_path),
  60. "CBS_ADMIN_TOKEN": env_token
  61. }, clear=True):
  62. settings = Settings()
  63. assert settings.admin_token == file_token
  64. def test_token_from_env_when_file_not_found(self, tmp_path):
  65. """Test that token falls back to environment variable when file doesn't exist."""
  66. expected_token = "test-token-from-env"
  67. with patch.dict(os.environ, {
  68. "CBS_BASEDIR": str(tmp_path),
  69. "CBS_ADMIN_TOKEN": expected_token
  70. }, clear=True):
  71. settings = Settings()
  72. assert settings.admin_token == expected_token
  73. def test_token_from_env_on_file_read_error(self, tmp_path):
  74. """Test that token falls back to env var when file cannot be read."""
  75. env_token = "env-fallback-token"
  76. with patch.dict(os.environ, {
  77. "CBS_BASEDIR": str(tmp_path),
  78. "CBS_ADMIN_TOKEN": env_token
  79. }, clear=True):
  80. with patch("builtins.open", side_effect=PermissionError("No access")):
  81. settings = Settings()
  82. assert settings.admin_token == env_token
  83. def test_token_none_when_not_configured(self, tmp_path):
  84. """Test that token is None when neither file nor env var is set."""
  85. with patch.dict(os.environ, {"CBS_BASEDIR": str(tmp_path)}, clear=True):
  86. settings = Settings()
  87. assert settings.admin_token is None
  88. def test_token_none_when_env_is_empty_string(self, tmp_path):
  89. """Test that token is None when env var is empty string."""
  90. with patch.dict(os.environ, {
  91. "CBS_BASEDIR": str(tmp_path),
  92. "CBS_ADMIN_TOKEN": ""
  93. }, clear=True):
  94. settings = Settings()
  95. assert settings.admin_token is None