test_github_backup_schemas.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Unit tests for GitHub backup Pydantic schemas."""
  2. import pytest
  3. from pydantic import ValidationError
  4. from backend.app.schemas.github_backup import GitHubBackupConfigCreate, ProviderType
  5. class TestProviderTypeEnum:
  6. def test_has_expected_string_values(self):
  7. assert ProviderType.GITHUB == "github"
  8. assert ProviderType.GITEA == "gitea"
  9. assert ProviderType.FORGEJO == "forgejo"
  10. assert ProviderType.GITLAB == "gitlab"
  11. class TestGitHubBackupConfigCreate:
  12. BASE_FIELDS = {
  13. "repository_url": "https://github.com/owner/repo",
  14. "access_token": "ghp_token",
  15. }
  16. def test_plain_github_is_valid(self):
  17. config = GitHubBackupConfigCreate(**self.BASE_FIELDS)
  18. assert config.provider == ProviderType.GITHUB
  19. def test_gitea_is_valid_without_api_base_url(self):
  20. config = GitHubBackupConfigCreate(
  21. repository_url="https://git.example.com/owner/repo",
  22. access_token="token",
  23. provider="gitea",
  24. )
  25. assert config.provider == ProviderType.GITEA
  26. def test_forgejo_is_valid(self):
  27. config = GitHubBackupConfigCreate(
  28. repository_url="https://forgejo.example.com/owner/repo",
  29. access_token="token",
  30. provider="forgejo",
  31. )
  32. assert config.provider == ProviderType.FORGEJO
  33. def test_url_regex_accepts_self_hosted_https_url(self):
  34. config = GitHubBackupConfigCreate(
  35. repository_url="https://git.example.com/owner/repo",
  36. access_token="token",
  37. provider="gitea",
  38. )
  39. assert "git.example.com" in config.repository_url
  40. def test_url_regex_accepts_ssh_url(self):
  41. config = GitHubBackupConfigCreate(
  42. repository_url="git@github.com:owner/repo",
  43. access_token="ghp_token",
  44. )
  45. assert config.repository_url == "git@github.com:owner/repo"
  46. def test_invalid_url_raises_validation_error(self):
  47. with pytest.raises(ValidationError, match="Invalid Git repository URL"):
  48. GitHubBackupConfigCreate(
  49. repository_url="not-a-url",
  50. access_token="ghp_token",
  51. )
  52. def test_unknown_provider_raises_validation_error(self):
  53. with pytest.raises(ValidationError):
  54. GitHubBackupConfigCreate(
  55. **self.BASE_FIELDS,
  56. provider="not-a-provider",
  57. )
  58. def test_http_url_without_allow_insecure_raises(self):
  59. with pytest.raises(ValidationError, match="Allow insecure HTTP"):
  60. GitHubBackupConfigCreate(
  61. repository_url="http://git.example.com/owner/repo",
  62. access_token="token",
  63. )
  64. def test_http_url_with_allow_insecure_is_valid(self):
  65. config = GitHubBackupConfigCreate(
  66. repository_url="http://git.example.com/owner/repo",
  67. access_token="token",
  68. allow_insecure_http=True,
  69. )
  70. assert config.repository_url == "http://git.example.com/owner/repo"