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75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""Unit tests for InMemoryEncryptionAdapter.

Tests couvrant:
- Chiffrement/déchiffrement de chaînes
- Chiffrement/déchiffrement de bytes
- Chiffrement de clés API
- Hachage et vérification
- Rotation de clés
- Méthodes utilitaires
"""

import pytest
from cryptography.fernet import Fernet

from backend.adapters.encryption.in_memory_adapter import InMemoryEncryptionAdapter


# =============================================================================
# Fixtures
# =============================================================================


@pytest.fixture
def adapter():
    """Adapter avec clé générée."""
    return InMemoryEncryptionAdapter()


@pytest.fixture
def adapter_with_key():
    """Adapter avec clé spécifique."""
    key = Fernet.generate_key().decode()
    return InMemoryEncryptionAdapter(key=key)


@pytest.fixture
def adapter_from_secret():
    """Adapter avec clé dérivée d'un secret."""
    return InMemoryEncryptionAdapter(
        derive_from_secret="my-secret-password",
        salt="test-salt",
    )


# =============================================================================
# Tests: Core Encryption
# =============================================================================


class TestCoreEncryption:
    """Tests pour le chiffrement de base."""

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_string(self, adapter):
        """Chiffre et déchiffre une chaîne."""
        original = "Hello, World!"
        encrypted = await adapter.encrypt(original)

        assert encrypted != original
        assert len(encrypted) > len(original)

        decrypted = await adapter.decrypt(encrypted)
        assert decrypted == original

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_unicode(self, adapter):
        """Chiffre et déchiffre des caractères Unicode."""
        original = "Bonjour le monde! 🌍 中文 العربية"
        encrypted = await adapter.encrypt(original)
        decrypted = await adapter.decrypt(encrypted)

        assert decrypted == original

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_empty_string(self, adapter):
        """Chiffre et déchiffre une chaîne vide."""
        original = ""
        encrypted = await adapter.encrypt(original)
        decrypted = await adapter.decrypt(encrypted)

        assert decrypted == original

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_long_string(self, adapter):
        """Chiffre et déchiffre une longue chaîne."""
        original = "x" * 10000
        encrypted = await adapter.encrypt(original)
        decrypted = await adapter.decrypt(encrypted)

        assert decrypted == original

    @pytest.mark.asyncio
    async def test_decrypt_invalid_data(self, adapter):
        """Échoue sur données invalides."""
        with pytest.raises(ValueError, match="Invalid encrypted data"):
            await adapter.decrypt("not-encrypted-data")

    @pytest.mark.asyncio
    async def test_decrypt_wrong_key(self):
        """Échoue avec mauvaise clé."""
        adapter1 = InMemoryEncryptionAdapter()
        adapter2 = InMemoryEncryptionAdapter()

        encrypted = await adapter1.encrypt("secret")

        with pytest.raises(ValueError, match="Invalid encrypted data"):
            await adapter2.decrypt(encrypted)


# =============================================================================
# Tests: Bytes Encryption
# =============================================================================


class TestBytesEncryption:
    """Tests pour le chiffrement de bytes."""

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_bytes(self, adapter):
        """Chiffre et déchiffre des bytes."""
        original = b"Binary data \x00\x01\x02"
        encrypted = await adapter.encrypt_bytes(original)

        assert encrypted != original
        assert isinstance(encrypted, bytes)

        decrypted = await adapter.decrypt_bytes(encrypted)
        assert decrypted == original

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_empty_bytes(self, adapter):
        """Chiffre et déchiffre des bytes vides."""
        original = b""
        encrypted = await adapter.encrypt_bytes(original)
        decrypted = await adapter.decrypt_bytes(encrypted)

        assert decrypted == original


# =============================================================================
# Tests: API Key Encryption
# =============================================================================


class TestAPIKeyEncryption:
    """Tests pour le chiffrement de clés API."""

    @pytest.mark.asyncio
    async def test_encrypt_decrypt_api_key(self, adapter):
        """Chiffre et déchiffre une clé API."""
        api_key = "sk-1234567890abcdef"
        encrypted = await adapter.encrypt_api_key(api_key)

        assert encrypted != api_key
        assert "sk-" not in encrypted

        decrypted = await adapter.decrypt_api_key(encrypted)
        assert decrypted == api_key

    @pytest.mark.asyncio
    async def test_encrypt_openai_key(self, adapter):
        """Chiffre une clé OpenAI."""
        api_key = "sk-proj-abc123def456ghi789"
        encrypted = await adapter.encrypt_api_key(api_key)
        decrypted = await adapter.decrypt_api_key(encrypted)

        assert decrypted == api_key

    @pytest.mark.asyncio
    async def test_encrypt_anthropic_key(self, adapter):
        """Chiffre une clé Anthropic."""
        api_key = "sk-ant-api03-abc123"
        encrypted = await adapter.encrypt_api_key(api_key)
        decrypted = await adapter.decrypt_api_key(encrypted)

        assert decrypted == api_key


# =============================================================================
# Tests: Hashing
# =============================================================================


class TestHashing:
    """Tests pour le hachage de clés."""

    @pytest.mark.asyncio
    async def test_hash_key(self, adapter):
        """Hache une clé."""
        key = "my-api-key"
        hash1 = await adapter.hash_key(key)

        assert len(hash1) == 64  # SHA-256 hex
        assert hash1 != key

    @pytest.mark.asyncio
    async def test_hash_key_deterministic(self, adapter):
        """Le hachage est déterministe."""
        key = "my-api-key"
        hash1 = await adapter.hash_key(key)
        hash2 = await adapter.hash_key(key)

        assert hash1 == hash2

    @pytest.mark.asyncio
    async def test_hash_key_with_salt(self, adapter):
        """Le sel modifie le hash."""
        key = "my-api-key"
        hash1 = await adapter.hash_key(key)
        hash2 = await adapter.hash_key(key, salt="custom-salt")

        assert hash1 != hash2

    @pytest.mark.asyncio
    async def test_verify_key_hash_valid(self, adapter):
        """Vérifie un hash valide."""
        key = "my-api-key"
        hash_value = await adapter.hash_key(key)

        result = await adapter.verify_key_hash(key, hash_value)
        assert result is True

    @pytest.mark.asyncio
    async def test_verify_key_hash_invalid(self, adapter):
        """Rejette un hash invalide."""
        key = "my-api-key"
        wrong_hash = "0" * 64

        result = await adapter.verify_key_hash(key, wrong_hash)
        assert result is False

    @pytest.mark.asyncio
    async def test_verify_key_hash_with_salt(self, adapter):
        """Vérifie un hash avec sel."""
        key = "my-api-key"
        salt = "my-salt"
        hash_value = await adapter.hash_key(key, salt)

        result = await adapter.verify_key_hash(key, hash_value, salt)
        assert result is True


# =============================================================================
# Tests: Key Rotation
# =============================================================================


class TestKeyRotation:
    """Tests pour la rotation de clés."""

    @pytest.mark.asyncio
    async def test_setup_key_rotation(self):
        """Configure la rotation de clés."""
        old_key = Fernet.generate_key().decode()
        new_key = Fernet.generate_key().decode()

        adapter = InMemoryEncryptionAdapter(key=old_key)

        # Chiffrer avec l'ancienne clé
        await adapter.encrypt("secret")

        # Configurer la rotation
        await adapter.setup_key_rotation(old_key, new_key)

        status = await adapter.get_rotation_status()
        assert status.rotation_in_progress is True
        assert status.old_key_valid is True

    @pytest.mark.asyncio
    async def test_decrypt_after_rotation(self):
        """Déchiffre avec les deux clés pendant rotation."""
        old_key = Fernet.generate_key().decode()
        new_key = Fernet.generate_key().decode()

        adapter = InMemoryEncryptionAdapter(key=old_key)

        # Chiffrer avec l'ancienne clé
        encrypted_old = await adapter.encrypt("secret-old")

        # Configurer rotation
        await adapter.setup_key_rotation(old_key, new_key)

        # Chiffrer avec la nouvelle clé
        encrypted_new = await adapter.encrypt("secret-new")

        # Les deux doivent être déchiffrables
        decrypted_old = await adapter.decrypt(encrypted_old)
        decrypted_new = await adapter.decrypt(encrypted_new)

        assert decrypted_old == "secret-old"
        assert decrypted_new == "secret-new"

    @pytest.mark.asyncio
    async def test_rotate_encrypted_value(self):
        """Migre une valeur vers la nouvelle clé."""
        old_key = Fernet.generate_key().decode()
        new_key = Fernet.generate_key().decode()

        adapter = InMemoryEncryptionAdapter(key=old_key)
        encrypted_old = await adapter.encrypt("secret")

        await adapter.setup_key_rotation(old_key, new_key)

        # Migrer vers nouvelle clé
        rotated = await adapter.rotate_encrypted_value(encrypted_old)

        # Doit être différent (nouveau token)
        assert rotated != encrypted_old

        # Doit toujours déchiffrer correctement
        decrypted = await adapter.decrypt(rotated)
        assert decrypted == "secret"

    @pytest.mark.asyncio
    async def test_complete_rotation(self):
        """Termine la rotation."""
        old_key = Fernet.generate_key().decode()
        new_key = Fernet.generate_key().decode()

        adapter = InMemoryEncryptionAdapter(key=old_key)
        await adapter.setup_key_rotation(old_key, new_key)
        await adapter.complete_rotation()

        status = await adapter.get_rotation_status()
        assert status.rotation_in_progress is False
        assert status.old_key_valid is False


# =============================================================================
# Tests: Utility Methods
# =============================================================================


class TestUtilityMethods:
    """Tests pour les méthodes utilitaires."""

    @pytest.mark.asyncio
    async def test_is_encrypted_true(self, adapter):
        """Détecte les données chiffrées."""
        encrypted = await adapter.encrypt("secret")
        result = await adapter.is_encrypted(encrypted)

        assert result is True

    @pytest.mark.asyncio
    async def test_is_encrypted_false(self, adapter):
        """Détecte les données non chiffrées."""
        result = await adapter.is_encrypted("not-encrypted")
        assert result is False

    @pytest.mark.asyncio
    async def test_is_encrypted_empty(self, adapter):
        """Gère les chaînes vides."""
        result = await adapter.is_encrypted("")
        assert result is False

    @pytest.mark.asyncio
    async def test_safe_decrypt_success(self, adapter):
        """Déchiffre avec safe_decrypt."""
        encrypted = await adapter.encrypt("secret")
        result = await adapter.safe_decrypt(encrypted)

        assert result == "secret"

    @pytest.mark.asyncio
    async def test_safe_decrypt_failure(self, adapter):
        """Retourne None sur échec."""
        result = await adapter.safe_decrypt("not-encrypted")
        assert result is None

    @pytest.mark.asyncio
    async def test_generate_key(self, adapter):
        """Génère une nouvelle clé."""
        key = await adapter.generate_key()

        assert len(key) == 44  # Base64-encoded Fernet key
        # Vérifie que c'est une clé valide
        Fernet(key.encode())


# =============================================================================
# Tests: Key Derivation
# =============================================================================


class TestKeyDerivation:
    """Tests pour la dérivation de clés."""

    @pytest.mark.asyncio
    async def test_derived_key_works(self, adapter_from_secret):
        """La clé dérivée fonctionne."""
        encrypted = await adapter_from_secret.encrypt("secret")
        decrypted = await adapter_from_secret.decrypt(encrypted)

        assert decrypted == "secret"

    @pytest.mark.asyncio
    async def test_same_secret_same_key(self):
        """Le même secret donne la même clé."""
        adapter1 = InMemoryEncryptionAdapter(
            derive_from_secret="password",
            salt="salt",
        )
        adapter2 = InMemoryEncryptionAdapter(
            derive_from_secret="password",
            salt="salt",
        )

        encrypted = await adapter1.encrypt("secret")
        decrypted = await adapter2.decrypt(encrypted)

        assert decrypted == "secret"

    @pytest.mark.asyncio
    async def test_different_salt_different_key(self):
        """Des sels différents donnent des clés différentes."""
        adapter1 = InMemoryEncryptionAdapter(
            derive_from_secret="password",
            salt="salt1",
        )
        adapter2 = InMemoryEncryptionAdapter(
            derive_from_secret="password",
            salt="salt2",
        )

        encrypted = await adapter1.encrypt("secret")

        with pytest.raises(ValueError):
            await adapter2.decrypt(encrypted)