|
| 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