|
| 1 | +"""Smoke tests for the DataFog CLI. |
| 2 | +
|
| 3 | +These tests verify basic CLI functionality using temporary files. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import tempfile |
| 8 | +from pathlib import Path |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +import pytest |
| 12 | +from typer.testing import CliRunner |
| 13 | + |
| 14 | +from datafog.client import app |
| 15 | +from datafog.models.anonymizer import Anonymizer, AnonymizerType |
| 16 | +from datafog.models.spacy_nlp import SpacyAnnotator |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def runner(): |
| 21 | + """Create a CLI runner for testing.""" |
| 22 | + return CliRunner() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def temp_text_file(): |
| 27 | + """Create a temporary text file with sample content.""" |
| 28 | + # Create a temporary file with sample text containing PII |
| 29 | + with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, mode="w") as f: |
| 30 | + f.write("My name is John Doe and my email is john.doe@example.com.\n") |
| 31 | + f.write("My phone number is (555) 123-4567 and my SSN is 123-45-6789.\n") |
| 32 | + temp_file = f.name |
| 33 | + |
| 34 | + yield temp_file |
| 35 | + |
| 36 | + # Clean up the temporary file after the test |
| 37 | + if os.path.exists(temp_file): |
| 38 | + os.remove(temp_file) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.integration |
| 42 | +def test_health_command(runner): |
| 43 | + """Test the health command.""" |
| 44 | + result = runner.invoke(app, ["health"]) |
| 45 | + assert result.exit_code == 0 |
| 46 | + assert "DataFog is running" in result.stdout |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.integration |
| 50 | +def test_show_config_command(runner): |
| 51 | + """Test the show-config command.""" |
| 52 | + result = runner.invoke(app, ["show-config"]) |
| 53 | + assert result.exit_code == 0 |
| 54 | + # Check that the output contains some expected config fields |
| 55 | + assert "api_key" in result.stdout.lower() |
| 56 | + assert "log_level" in result.stdout.lower() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.integration |
| 60 | +def test_scan_text_with_file_content(runner, temp_text_file): |
| 61 | + """Test the scan-text command with content from a temporary file.""" |
| 62 | + # Read the content of the temporary file |
| 63 | + with open(temp_text_file, "r") as f: |
| 64 | + text_content = f.read().strip() |
| 65 | + |
| 66 | + # Run the scan-text command with the file content |
| 67 | + result = runner.invoke(app, ["scan-text", text_content]) |
| 68 | + |
| 69 | + # Verify the command executed successfully |
| 70 | + assert result.exit_code == 0 |
| 71 | + |
| 72 | + # Check that the output contains expected PII types |
| 73 | + assert ( |
| 74 | + "PERSON" in result.stdout |
| 75 | + or "EMAIL" in result.stdout |
| 76 | + or "PHONE" in result.stdout |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.integration |
| 81 | +def test_redact_text_command(runner): |
| 82 | + """Test the redact-text command.""" |
| 83 | + test_text = "My name is John Doe and my email is john.doe@example.com." |
| 84 | + |
| 85 | + result = runner.invoke(app, ["redact-text", test_text]) |
| 86 | + |
| 87 | + assert result.exit_code == 0 |
| 88 | + # Check that PII has been redacted (replaced with [REDACTED]) |
| 89 | + assert "[REDACTED]" in result.stdout |
| 90 | + # The person name should be redacted |
| 91 | + assert "John Doe" not in result.stdout |
| 92 | + # Note: The current implementation might not redact emails correctly |
| 93 | + # This is a known limitation we're accepting for the smoke test |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.integration |
| 97 | +def test_replace_text_command(runner): |
| 98 | + """Test the replace-text command.""" |
| 99 | + test_text = "My name is John Doe and my email is john.doe@example.com." |
| 100 | + |
| 101 | + result = runner.invoke(app, ["replace-text", test_text]) |
| 102 | + |
| 103 | + assert result.exit_code == 0 |
| 104 | + # The person name should be replaced with a pseudonym |
| 105 | + assert "John Doe" not in result.stdout |
| 106 | + # Check that the text contains a replacement pattern for person (like [PERSON_HASH]) |
| 107 | + assert "[PERSON_" in result.stdout or "PERSON-" in result.stdout |
| 108 | + # But the text should still have some content (not just replacements) |
| 109 | + assert "My name is" in result.stdout |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.integration |
| 113 | +def test_list_entities_command(runner): |
| 114 | + """Test the list-entities command.""" |
| 115 | + result = runner.invoke(app, ["list-entities"]) |
| 116 | + |
| 117 | + assert result.exit_code == 0 |
| 118 | + # Should list some common entity types |
| 119 | + assert "PERSON" in result.stdout |
| 120 | + assert "ORG" in result.stdout |
0 commit comments