aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_cli.py
blob: 817da1398a63d35f67d9251f34dea57d20025ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()
git.ajschof.me — hosted by ajschofield — powered by cgit