aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_core.py
blob: 8ee41979b624d669e475a8f9481e43d7190ff1f3 (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
from gdpr_obfuscator import Obfuscator
import pytest
from moto import mock_aws
import boto3
import csv
import random
import json

obfuscator = Obfuscator()


def setup_s3(s3_client, bucket: str, key: str, content: str):
    s3_client.create_bucket(
        Bucket=bucket,
        CreateBucketConfiguration={"LocationConstraint": "eu-west-2"},
    )
    s3_client.put_object(Bucket=bucket, Key=key, Body=content)


@pytest.fixture(autouse=True)
def s3_client():
    with mock_aws():
        yield boto3.client("s3", "eu-west-2")


def test_imported_module_runs_successfully_with_local_data():
    with mock_aws():
        s3 = boto3.client("s3", region_name="eu-west-2")
        bucket = "test-bucket"
        key = "data/mock.csv"

        with open("test/data/mock_data.csv", "r") as f:
            csv_content = f.read()

        with open("test/data/mock_data.csv", "r") as f:
            reader = list(csv.DictReader(f))
            rand_row = random.randint(0, len(reader) - 1)
            rand_name = reader[rand_row]["name"]

        setup_s3(s3, bucket, key, csv_content)

        path = f"s3://{bucket}/{key}"

    json_input = json.dumps({"file_path": path, "pii_fields": ["name"]})

    result = obfuscator.process_s3(json_input)
    result_str = result.decode("utf-8")

    assert rand_name not in result_str
git.ajschof.me — hosted by ajschofield — powered by cgit