Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 6acf0ee

Browse files
committed
add missing tests
1 parent 59a78ff commit 6acf0ee

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

src/codegate/pipeline/sensitive_data/manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ def __init__(self):
3232
self.session_store = SessionStore()
3333

3434
def store(self, session_id: str, value: SensitiveData) -> Optional[str]:
35+
print("in store")
3536
if not session_id or not value.original:
3637
return None
38+
print("i call add mapping")
39+
print(self.session_store)
3740
return self.session_store.add_mapping(session_id, value.to_json())
3841

3942
def get_by_session_id(self, session_id: str) -> Optional[Dict]:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
from unittest.mock import MagicMock, patch
3+
import pytest
4+
from codegate.pipeline.sensitive_data.manager import SensitiveData, SensitiveDataManager
5+
from codegate.pipeline.sensitive_data.session_store import SessionStore
6+
7+
8+
class TestSensitiveDataManager:
9+
@pytest.fixture
10+
def mock_session_store(self):
11+
"""Mock the SessionStore instance used within SensitiveDataManager."""
12+
return MagicMock(spec=SessionStore)
13+
14+
@pytest.fixture
15+
def manager(self, mock_session_store):
16+
"""Patch SensitiveDataManager to use the mocked SessionStore."""
17+
with patch.object(SensitiveDataManager, "__init__", lambda self: None):
18+
manager = SensitiveDataManager()
19+
manager.session_store = mock_session_store # Manually inject the mock
20+
return manager
21+
22+
def test_store_success(self, manager, mock_session_store):
23+
"""Test storing a SensitiveData object successfully."""
24+
session_id = "session-123"
25+
sensitive_data = SensitiveData("secret_value", "AWS", "API_KEY")
26+
27+
# Mock session store behavior
28+
mock_session_store.add_mapping.return_value = "uuid-123"
29+
30+
result = manager.store(session_id, sensitive_data)
31+
32+
# Verify correct function calls
33+
mock_session_store.add_mapping.assert_called_once_with(session_id, sensitive_data.to_json())
34+
assert result == "uuid-123"
35+
36+
def test_store_invalid_session_id(self, manager):
37+
"""Test storing data with an invalid session ID (should return None)."""
38+
sensitive_data = SensitiveData("secret_value", "AWS", "API_KEY")
39+
result = manager.store("", sensitive_data) # Empty session ID
40+
assert result is None
41+
42+
def test_store_missing_original_value(self, manager):
43+
"""Test storing data without an original value (should return None)."""
44+
sensitive_data = SensitiveData(original="", service="AWS", type="API_KEY") # Empty original
45+
result = manager.store("session-123", sensitive_data)
46+
assert result is None
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import uuid
2+
import pytest
3+
from codegate.pipeline.sensitive_data.session_store import SessionStore
4+
5+
6+
class TestSessionStore:
7+
@pytest.fixture
8+
def session_store(self):
9+
"""Fixture to create a fresh SessionStore instance before each test."""
10+
return SessionStore()
11+
12+
def test_add_mapping_creates_uuid(self, session_store):
13+
"""Test that add_mapping correctly stores data and returns a UUID."""
14+
session_id = "session-123"
15+
data = "test-data"
16+
17+
uuid_placeholder = session_store.add_mapping(session_id, data)
18+
19+
# Ensure the returned placeholder follows the expected format
20+
assert uuid_placeholder.startswith("#") and uuid_placeholder.endswith("#")
21+
assert len(uuid_placeholder) > 2 # Should have a UUID inside
22+
23+
# Verify data is correctly stored
24+
stored_data = session_store.get_mapping(session_id, uuid_placeholder)
25+
assert stored_data == data
26+
27+
def test_add_mapping_creates_unique_uuids(self, session_store):
28+
"""Ensure multiple calls to add_mapping generate unique UUIDs."""
29+
session_id = "session-123"
30+
data1 = "data1"
31+
data2 = "data2"
32+
33+
uuid_placeholder1 = session_store.add_mapping(session_id, data1)
34+
uuid_placeholder2 = session_store.add_mapping(session_id, data2)
35+
36+
assert uuid_placeholder1 != uuid_placeholder2 # UUIDs must be unique
37+
38+
# Ensure data is correctly stored
39+
assert session_store.get_mapping(session_id, uuid_placeholder1) == data1
40+
assert session_store.get_mapping(session_id, uuid_placeholder2) == data2
41+
42+
def test_get_by_session_id(self, session_store):
43+
"""Test retrieving all stored mappings for a session ID."""
44+
session_id = "session-123"
45+
data1 = "data1"
46+
data2 = "data2"
47+
48+
uuid1 = session_store.add_mapping(session_id, data1)
49+
uuid2 = session_store.add_mapping(session_id, data2)
50+
51+
stored_session_data = session_store.get_by_session_id(session_id)
52+
53+
assert uuid1 in stored_session_data
54+
assert uuid2 in stored_session_data
55+
assert stored_session_data[uuid1] == data1
56+
assert stored_session_data[uuid2] == data2
57+
58+
def test_get_by_session_id_not_found(self, session_store):
59+
"""Test get_by_session_id when session does not exist (should return None)."""
60+
session_id = "non-existent-session"
61+
assert session_store.get_by_session_id(session_id) is None
62+
63+
def test_get_mapping_success(self, session_store):
64+
"""Test retrieving a specific mapping."""
65+
session_id = "session-123"
66+
data = "test-data"
67+
68+
uuid_placeholder = session_store.add_mapping(session_id, data)
69+
70+
assert session_store.get_mapping(session_id, uuid_placeholder) == data
71+
72+
def test_get_mapping_not_found(self, session_store):
73+
"""Test retrieving a mapping that does not exist (should return None)."""
74+
session_id = "session-123"
75+
uuid_placeholder = "#non-existent-uuid#"
76+
77+
assert session_store.get_mapping(session_id, uuid_placeholder) is None
78+
79+
def test_cleanup_session(self, session_store):
80+
"""Test that cleanup_session removes all data for a session ID."""
81+
session_id = "session-123"
82+
session_store.add_mapping(session_id, "test-data")
83+
84+
# Ensure session exists before cleanup
85+
assert session_store.get_by_session_id(session_id) is not None
86+
87+
session_store.cleanup_session(session_id)
88+
89+
# Ensure session is removed after cleanup
90+
assert session_store.get_by_session_id(session_id) is None
91+
92+
def test_cleanup_session_non_existent(self, session_store):
93+
"""Test cleanup_session on a non-existent session (should not raise errors)."""
94+
session_id = "non-existent-session"
95+
session_store.cleanup_session(session_id) # Should not fail
96+
assert session_store.get_by_session_id(session_id) is None
97+
98+
def test_cleanup(self, session_store):
99+
"""Test global cleanup removes all stored sessions."""
100+
session_id1 = "session-1"
101+
session_id2 = "session-2"
102+
103+
session_store.add_mapping(session_id1, "data1")
104+
session_store.add_mapping(session_id2, "data2")
105+
106+
# Ensure sessions exist before cleanup
107+
assert session_store.get_by_session_id(session_id1) is not None
108+
assert session_store.get_by_session_id(session_id2) is not None
109+
110+
session_store.cleanup()
111+
112+
# Ensure all sessions are removed after cleanup
113+
assert session_store.get_by_session_id(session_id1) is None
114+
assert session_store.get_by_session_id(session_id2) is None

0 commit comments

Comments
 (0)