diff options
| author | Alex Schofield <git@ajschof.me> | 2025-02-26 19:48:48 +0000 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2025-02-26 19:48:48 +0000 |
| commit | e9dc959f1285bb61098f2c39dc4ec7ce25420f4c (patch) | |
| tree | c2ebc797a004bc00958245e51b872cfa88fab293 /test | |
| parent | 1b8ee8a812e58cbb0632d49d1ff2fc6a0b925d6b (diff) | |
| download | gdpr-obfuscator-e9dc959f1285bb61098f2c39dc4ec7ce25420f4c.tar.gz gdpr-obfuscator-e9dc959f1285bb61098f2c39dc4ec7ce25420f4c.zip | |
add tests for cli.py
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_cli.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/test/test_cli.py b/test/test_cli.py new file mode 100644 index 0000000..817da13 --- /dev/null +++ b/test/test_cli.py @@ -0,0 +1,91 @@ +import json +import pytest +from unittest.mock import patch +from cli import main + + +@pytest.fixture +def mock_obfuscator(): + with patch("cli.Obfuscator") as MockObfuscator: + mock_instance = MockObfuscator.return_value + mock_instance.process_local.return_value = ( + '{"status": "success", "data": "local_obfuscated_data"}' + ) + mock_instance.process_s3.return_value = ( + '{"status": "success", "data": "s3_obfuscated_data"}' + ) + yield mock_instance + + +def test_local_obfuscation_successfully_runs(mock_obfuscator): + test_args = [ + "GDPR-Obfuscator", + "--local", + "test_local_file.json", + "--pii", + "name", + "email", + ] + with patch("sys.argv", test_args): + with patch("builtins.print") as mock_print: + main() + expected_payload = json.dumps( + {"file_path": "test_local_file.json", "pii_fields": ["name", "email"]} + ) + mock_obfuscator.process_local.assert_called_once_with(expected_payload) + mock_print.assert_called_once_with( + '{"status": "success", "data": "local_obfuscated_data"}' + ) + + +def test_s3_obfuscation_successfully_runs(mock_obfuscator): + test_args = [ + "GDPR-Obfuscator", + "--s3", + "s3://bucket/test_file.json", + "--pii", + "name", + "email", + ] + with patch("sys.argv", test_args): + with patch("builtins.print") as mock_print: + main() + expected_payload = json.dumps( + { + "file_path": "s3://bucket/test_file.json", + "pii_fields": ["name", "email"], + } + ) + mock_obfuscator.process_s3.assert_called_once_with(expected_payload) + mock_print.assert_called_once_with( + '{"status": "success", "data": "s3_obfuscated_data"}' + ) + + +def test_execution_fails_with_missing_required_arguments(): + test_args = ["GDPR-Obfuscator"] + with patch("sys.argv", test_args): + with pytest.raises(SystemExit): + main() + + +def test_execution_fails_with_missing_pii_argument(): + test_args = ["GDPR-Obfuscator", "--local", "test_local_file.json"] + with patch("sys.argv", test_args): + with pytest.raises(SystemExit): + main() + + +def test_execution_fails_with_both_local_and_s3_arguments_present(): + test_args = [ + "GDPR-Obfuscator", + "--local", + "test_local_file.json", + "--s3", + "s3://bucket/test_file.json", + "--pii", + "name", + ] + with patch("sys.argv", test_args): + with pytest.raises(SystemExit): + main() |
