|
| 1 | +"""Tests for feedback endpoint anonymization functionality.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest.mock import patch, MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from app.endpoints.feedback import store_feedback |
| 9 | + |
| 10 | + |
| 11 | +# Set up test environment variable before importing the module |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def setup_test_pepper(): |
| 14 | + """Set up test pepper environment variable for all tests.""" |
| 15 | + test_pepper = "test-pepper-for-feedback-tests" |
| 16 | + with patch.dict(os.environ, {"USER_ANON_PEPPER": test_pepper}): |
| 17 | + yield |
| 18 | + |
| 19 | + |
| 20 | +class TestFeedbackAnonymization: |
| 21 | + """Test feedback storage with user anonymization.""" |
| 22 | + |
| 23 | + @patch("app.endpoints.feedback.get_anonymous_user_id") |
| 24 | + @patch("app.endpoints.feedback.get_suid") |
| 25 | + @patch("app.endpoints.feedback.json") |
| 26 | + @patch("app.endpoints.feedback.Path") |
| 27 | + def test_store_feedback_anonymizes_user_id( |
| 28 | + self, mock_path, mock_json, mock_get_suid, mock_get_anonymous |
| 29 | + ): |
| 30 | + """Test that store_feedback uses anonymous user ID.""" |
| 31 | + # Setup mocks |
| 32 | + mock_get_anonymous.return_value = "anon-feedback-123" |
| 33 | + mock_get_suid.return_value = "feedback-uuid" |
| 34 | + mock_path.return_value = MagicMock() |
| 35 | + |
| 36 | + # Mock configuration |
| 37 | + with ( |
| 38 | + patch("app.endpoints.feedback.configuration") as mock_config, |
| 39 | + patch("builtins.open"), |
| 40 | + ): |
| 41 | + mock_config.user_data_collection_configuration.feedback_storage = ( |
| 42 | + "/tmp/feedback" |
| 43 | + ) |
| 44 | + |
| 45 | + # Call store_feedback |
| 46 | + store_feedback( |
| 47 | + |
| 48 | + feedback={ |
| 49 | + "feedback": "This is test feedback", |
| 50 | + "sentiment": 1, |
| 51 | + "categories": ["helpful"], |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + # Verify anonymous user ID was used |
| 56 | + mock_get_anonymous. assert_called_once_with( "[email protected]") |
| 57 | + |
| 58 | + # Verify stored data uses anonymous ID |
| 59 | + stored_data = mock_json.dump.call_args[0][0] |
| 60 | + assert stored_data["anonymous_user_id"] == "anon-feedback-123" |
| 61 | + assert "user_id" not in stored_data |
| 62 | + assert stored_data["feedback"] == "This is test feedback" |
| 63 | + assert stored_data["sentiment"] == 1 |
| 64 | + assert stored_data["categories"] == ["helpful"] |
| 65 | + |
| 66 | + @patch("app.endpoints.feedback.get_anonymous_user_id") |
| 67 | + def test_store_feedback_different_users_get_different_anonymous_ids( |
| 68 | + self, mock_get_anonymous |
| 69 | + ): |
| 70 | + """Test that different users get different anonymous IDs for feedback.""" |
| 71 | + |
| 72 | + def mock_anonymous_side_effect(user_id): |
| 73 | + if user_id == "[email protected]": |
| 74 | + return "anon-feedback-user1" |
| 75 | + if user_id == "[email protected]": |
| 76 | + return "anon-feedback-user2" |
| 77 | + return "anon-unknown" |
| 78 | + |
| 79 | + mock_get_anonymous.side_effect = mock_anonymous_side_effect |
| 80 | + |
| 81 | + with ( |
| 82 | + patch("app.endpoints.feedback.json") as mock_json, |
| 83 | + patch("app.endpoints.feedback.Path") as mock_path, |
| 84 | + patch("builtins.open"), |
| 85 | + patch("app.endpoints.feedback.get_suid", return_value="uuid"), |
| 86 | + patch("app.endpoints.feedback.configuration") as mock_config, |
| 87 | + ): |
| 88 | + |
| 89 | + mock_config.user_data_collection_configuration.feedback_storage = ( |
| 90 | + "/tmp/feedback" |
| 91 | + ) |
| 92 | + mock_path.return_value = MagicMock() |
| 93 | + |
| 94 | + # Store feedback for user 1 |
| 95 | + store_feedback( "[email protected]", { "feedback": "Test 1"}) |
| 96 | + first_call_data = mock_json.dump.call_args[0][0] |
| 97 | + |
| 98 | + # Reset mock for second call |
| 99 | + mock_json.reset_mock() |
| 100 | + |
| 101 | + # Store feedback for user 2 |
| 102 | + store_feedback( "[email protected]", { "feedback": "Test 2"}) |
| 103 | + second_call_data = mock_json.dump.call_args[0][0] |
| 104 | + |
| 105 | + # Verify different anonymous IDs were used |
| 106 | + assert first_call_data["anonymous_user_id"] == "anon-feedback-user1" |
| 107 | + assert second_call_data["anonymous_user_id"] == "anon-feedback-user2" |
| 108 | + assert ( |
| 109 | + first_call_data["anonymous_user_id"] |
| 110 | + != second_call_data["anonymous_user_id"] |
| 111 | + ) |
| 112 | + |
| 113 | + @patch("app.endpoints.feedback.get_anonymous_user_id") |
| 114 | + @patch("app.endpoints.feedback.logger") |
| 115 | + def test_feedback_logging_uses_anonymous_id(self, mock_logger, mock_get_anonymous): |
| 116 | + """Test that feedback logging uses anonymous user ID.""" |
| 117 | + mock_get_anonymous.return_value = "anon-feedback-logging" |
| 118 | + |
| 119 | + with ( |
| 120 | + patch("app.endpoints.feedback.json"), |
| 121 | + patch("app.endpoints.feedback.Path") as mock_path, |
| 122 | + patch("builtins.open"), |
| 123 | + patch("app.endpoints.feedback.get_suid", return_value="uuid"), |
| 124 | + patch("app.endpoints.feedback.configuration") as mock_config, |
| 125 | + ): |
| 126 | + |
| 127 | + mock_config.user_data_collection_configuration.feedback_storage = ( |
| 128 | + "/tmp/feedback" |
| 129 | + ) |
| 130 | + mock_path.return_value = MagicMock() |
| 131 | + |
| 132 | + # Store feedback |
| 133 | + store_feedback( "[email protected]", { "feedback": "Test feedback"}) |
| 134 | + |
| 135 | + # Verify logging uses anonymous ID |
| 136 | + mock_logger.debug.assert_called_once_with( |
| 137 | + "Storing feedback for anonymous user %s", "anon-feedback-logging" |
| 138 | + ) |
0 commit comments