import pytest from code_privacy_shield.redactor import Redactor, RedactionMatch from code_privacy_shield.patterns import PatternLibrary class TestRedactor: def setup_method(self): self.pattern_library = PatternLibrary() self.redactor = Redactor(self.pattern_library) def test_redact_api_key(self): content = "API_KEY = 'sk-abc123def456ghi789'" result = self.redactor.redact(content) assert result.redacted != content assert len(result.matches) > 0 def test_redact_email(self): content = "CONTACT = 'user@example.com'" result = self.redactor.redact(content) assert result.redacted != content assert len(result.matches) > 0 def test_redact_postgresql_connection(self): content = "DB_URL = 'postgresql://user:pass@localhost:5432/db'" result = self.redactor.redact(content) assert result.redacted != content assert len(result.matches) > 0 def test_redact_multiple_matches(self): content = """ API_KEY = 'sk-abc123def456ghi789jklmno' EMAIL = 'test@example.com' """ result = self.redactor.redact(content) assert len(result.matches) >= 2 def test_no_matches_returns_original(self): content = "x = 1 + 2" result = self.redactor.redact(content) assert result.redacted == content assert len(result.matches) == 0 def test_get_match_summary(self): content = "email: test@example.com, api_key: sk-abc123" result = self.redactor.redact(content) summary = self.redactor.get_match_summary(result) assert len(summary) > 0 def test_custom_pattern(self): added = self.redactor.add_custom_pattern( name="Custom Secret", pattern=r"(?i)CUSTOM_SECRET\s*=\s*['\"]([^'\"]+)['\"]", category="custom" ) assert added content = "CUSTOM_SECRET = 'my-secret-value'" result = self.redactor.redact(content) assert len(result.matches) > 0 def test_invalid_custom_pattern(self): added = self.redactor.add_custom_pattern( name="Invalid", pattern=r"[", # Invalid regex category="custom" ) assert not added def test_redaction_replacement_format(self): content = "password = 'mysecretpassword'" result = self.redactor.redact(content) match = result.matches[0] assert match.replacement != content assert len(match.replacement) > 0 def test_categories_tracked(self): content = "email: test@example.com" result = self.redactor.redact(content) assert "pii" in result.categories def test_redact_file_creates_result(self, tmp_path): test_file = tmp_path / "test.py" test_file.write_text("api_key = 'sk-abc123def456ghi789jklmno'") result = self.redactor.redact_file(str(test_file)) assert len(result.matches) > 0 assert result.original == "api_key = 'sk-abc123def456ghi789jklmno'" def test_redactor_default_replacement(self): redactor = Redactor() content = "x = 'sk-abc123def456ghi789jklmno'" result = redactor.redact(content) assert len(result.matches) > 0 match = result.matches[0] assert len(match.replacement) > 0 assert match.replacement.startswith("█" * 8) assert match.replacement.endswith("lmno") def test_short_content_replacement(self): redactor = Redactor() content = "x = 'abc'" # Short content result = redactor.redact(content) if result.matches: match = result.matches[0] assert len(match.replacement) > 0