Files
DeepAudit/backend/tests/test_core_encryption.py
lintsinghuaandClaude Opus 4.6 51d94acb4c test: add comprehensive test suite for production readiness
Backend (951 tests):
- Core security: JWT, password hashing, encryption
- Agent core: state, config, validation, retry, circuit breaker,
  rate limiter, persistence, fallback, context, executor
- API endpoints: tasks, projects, rules, prompts, auth deps
- Services: json_parser, event_manager, stream_handler, tools,
  tokenizer, prompt_cache, report generator, scanner utils
- Models, schemas, LLM factory/base adapter

Frontend (331 tests, vitest + @testing-library/react):
- API clients: serverClient, agentTasks, prompts, rules
- Utilities: fetchWrapper, errorHandler, logger, projectUtils,
  zipStorage, performanceMonitor, utils
- Hooks: useAsync, useDebounce, useLocalStorage
- Config, services, AgentAudit utils
- Test infra: vitest.config.ts, setup.ts, coverage-v8

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-01 09:19:45 +08:00

103 lines
3.0 KiB
Python

"""
Tests for app.core.encryption module.
"""
import pytest
from app.core.encryption import (
EncryptionService,
encrypt_sensitive_data,
decrypt_sensitive_data,
)
@pytest.fixture(autouse=True)
def reset_encryption_singleton():
"""Reset EncryptionService singleton between tests."""
EncryptionService._instance = None
EncryptionService._fernet = None
yield
EncryptionService._instance = None
EncryptionService._fernet = None
class TestEncryptionBasic:
"""Tests for basic encrypt/decrypt operations."""
def test_encrypt_returns_non_empty_string(self):
svc = EncryptionService()
result = svc.encrypt("hello world")
assert isinstance(result, str)
assert len(result) > 0
assert result != "hello world"
def test_encrypt_decrypt_roundtrip(self):
svc = EncryptionService()
plaintext = "sensitive_api_key_12345"
encrypted = svc.encrypt(plaintext)
decrypted = svc.decrypt(encrypted)
assert decrypted == plaintext
def test_encrypt_empty_string(self):
svc = EncryptionService()
result = svc.encrypt("")
assert result == ""
def test_decrypt_empty_string(self):
svc = EncryptionService()
result = svc.decrypt("")
assert result == ""
def test_decrypt_non_encrypted_returns_original(self):
svc = EncryptionService()
plain = "this_is_not_encrypted"
result = svc.decrypt(plain)
assert result == plain
def test_encrypt_decrypt_unicode(self):
svc = EncryptionService()
plaintext = "\u4e2d\u6587\u6d4b\u8bd5\u5bc6\u7801"
encrypted = svc.encrypt(plaintext)
decrypted = svc.decrypt(encrypted)
assert decrypted == plaintext
class TestIsEncrypted:
"""Tests for the is_encrypted check."""
def test_is_encrypted_true_for_encrypted_value(self):
svc = EncryptionService()
encrypted = svc.encrypt("some_value")
assert svc.is_encrypted(encrypted) is True
def test_is_encrypted_false_for_plain_text(self):
svc = EncryptionService()
assert svc.is_encrypted("plain_text_value") is False
def test_is_encrypted_false_for_empty_string(self):
svc = EncryptionService()
assert svc.is_encrypted("") is False
class TestSingleton:
"""Tests for singleton pattern."""
def test_singleton_returns_same_instance(self):
instance1 = EncryptionService()
instance2 = EncryptionService()
assert instance1 is instance2
class TestConvenienceFunctions:
"""Tests for module-level convenience functions."""
def test_encrypt_sensitive_data_convenience_function(self):
encrypted = encrypt_sensitive_data("my_secret")
assert isinstance(encrypted, str)
assert encrypted != "my_secret"
def test_decrypt_sensitive_data_convenience_function(self):
encrypted = encrypt_sensitive_data("my_secret")
decrypted = decrypt_sensitive_data(encrypted)
assert decrypted == "my_secret"