test_config.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. def test_env_var_overrides(self):
  22. """Test that environment variables override default settings."""
  23. env_overrides = {
  24. "CBS_BASEDIR": "/custom/base/path",
  25. "CBS_REDIS_HOST": "redis.example.com",
  26. "CBS_REDIS_PORT": "6380",
  27. "CBS_LOG_LEVEL": "DEBUG",
  28. "CBS_ENABLE_INBUILT_BUILDER": "0"
  29. }
  30. with patch.dict(os.environ, env_overrides, clear=True):
  31. settings = Settings()
  32. assert settings.base_dir == "/custom/base/path"
  33. assert settings.redis_host == "redis.example.com"
  34. assert settings.redis_port == "6380"
  35. assert settings.log_level == "DEBUG"
  36. assert settings.enable_inbuilt_builder is False
  37. class TestRemoteReloadToken:
  38. """Test suite for remote_reload_token property."""
  39. def test_token_from_file(self, tmp_path):
  40. """Test that token is read from file when it exists."""
  41. secrets_dir = tmp_path / 'secrets'
  42. secrets_dir.mkdir()
  43. token_file = secrets_dir / 'reload_token'
  44. expected_token = "test-token-from-file"
  45. token_file.write_text(f" {expected_token} \n") # Test whitespace stripping
  46. with patch.dict(os.environ, {"CBS_BASEDIR": str(tmp_path)}, clear=True):
  47. settings = Settings()
  48. assert settings.remote_reload_token == expected_token
  49. def test_token_file_takes_precedence_over_env(self, tmp_path):
  50. """Test that token from file takes precedence over environment variable."""
  51. secrets_dir = tmp_path / 'secrets'
  52. secrets_dir.mkdir()
  53. token_file = secrets_dir / 'reload_token'
  54. file_token = "token-from-file"
  55. env_token = "token-from-env"
  56. token_file.write_text(file_token)
  57. with patch.dict(os.environ, {
  58. "CBS_BASEDIR": str(tmp_path),
  59. "CBS_REMOTES_RELOAD_TOKEN": env_token
  60. }, clear=True):
  61. settings = Settings()
  62. assert settings.remote_reload_token == file_token
  63. def test_token_from_env_when_file_not_found(self, tmp_path):
  64. """Test that token falls back to environment variable when file doesn't exist."""
  65. expected_token = "test-token-from-env"
  66. with patch.dict(os.environ, {
  67. "CBS_BASEDIR": str(tmp_path),
  68. "CBS_REMOTES_RELOAD_TOKEN": expected_token
  69. }, clear=True):
  70. settings = Settings()
  71. assert settings.remote_reload_token == expected_token
  72. def test_token_from_env_on_file_read_error(self, tmp_path):
  73. """Test that token falls back to env var when file cannot be read."""
  74. env_token = "env-fallback-token"
  75. with patch.dict(os.environ, {
  76. "CBS_BASEDIR": str(tmp_path),
  77. "CBS_REMOTES_RELOAD_TOKEN": env_token
  78. }, clear=True):
  79. with patch("builtins.open", side_effect=PermissionError("No access")):
  80. settings = Settings()
  81. assert settings.remote_reload_token == env_token
  82. def test_token_none_when_not_configured(self, tmp_path):
  83. """Test that token is None when neither file nor env var is set."""
  84. with patch.dict(os.environ, {"CBS_BASEDIR": str(tmp_path)}, clear=True):
  85. settings = Settings()
  86. assert settings.remote_reload_token is None
  87. def test_token_none_when_env_is_empty_string(self, tmp_path):
  88. """Test that token is None when env var is empty string."""
  89. with patch.dict(os.environ, {
  90. "CBS_BASEDIR": str(tmp_path),
  91. "CBS_REMOTES_RELOAD_TOKEN": ""
  92. }, clear=True):
  93. settings = Settings()
  94. assert settings.remote_reload_token is None