-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_migrate_v1_perms_by_app.py
More file actions
141 lines (104 loc) · 4.3 KB
/
test_migrate_v1_perms_by_app.py
File metadata and controls
141 lines (104 loc) · 4.3 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import io
import json
from unittest.mock import MagicMock, patch
import pytest
from botocore.exceptions import ClientError
from migrate_v1_perms_by_app import (
CONSUMER_OR_PRODUCER,
_read_and_transform,
migrate_v1_perms_by_app,
)
ENV = "dev"
FOLDER = "my-app-folder"
BUCKET = f"nhsd-nrlf--{ENV}-authorization-store"
SAMPLE_V1_PERMS = [
"http://snomed.info/sct|736253002",
"http://snomed.info/sct|1363501000000100",
"http://snomed.info/sct|736366004",
]
# ---------------------------------------------------------------------------
# Helper functions
# ---------------------------------------------------------------------------
def _make_client_error(code: str, message: str) -> ClientError:
return ClientError(
{"Error": {"Code": code, "Message": message}},
operation_name="S3Operation",
)
def _mock_s3_client_with_response(data_to_return: bytes) -> MagicMock:
s3 = MagicMock()
s3.get_object.return_value = {"Body": io.BytesIO(data_to_return)}
return s3
# ---------------------------------------------------------------------------
# Unit tests for _read_and_transform
# ---------------------------------------------------------------------------
FILE_PATH = f"{FOLDER}/perms.json"
def test_read_and_transform_returns_wrapped_json_and_count():
s3 = _mock_s3_client_with_response(json.dumps(SAMPLE_V1_PERMS).encode())
body, count = _read_and_transform(s3, BUCKET, FILE_PATH)
assert count == len(SAMPLE_V1_PERMS)
assert json.loads(body) == {"types": SAMPLE_V1_PERMS}
s3.get_object.assert_called_once_with(Bucket=BUCKET, Key=FILE_PATH)
def test_read_and_transform_empty_list():
s3 = _mock_s3_client_with_response(b"[]")
body, count = _read_and_transform(s3, BUCKET, FILE_PATH)
assert count == 0
assert json.loads(body) == {"types": []}
def test_read_and_transform_raises_value_error_for_non_list():
s3 = _mock_s3_client_with_response(b'{"key": "value"}')
with pytest.raises(ValueError, match="Expected a JSON array, got dict"):
_read_and_transform(s3, BUCKET, FILE_PATH)
def test_read_and_transform_raises_runtime_error_on_client_error():
s3 = MagicMock()
s3.get_object.side_effect = _make_client_error(
"NoSuchKey", "The specified key does not exist"
)
with pytest.raises(
RuntimeError,
match=f"Failed to read s3://{BUCKET}/{FILE_PATH}.*The specified key does not exist",
):
_read_and_transform(s3, BUCKET, FILE_PATH)
# ---------------------------------------------------------------------------
# Unit tests for migrate_v1_perms_by_app
# ---------------------------------------------------------------------------
MODULE = "migrate_v1_perms_by_app"
TRANSFORMED_BODY = '{"types": ["http://snomed.info/sct|736253002"]}'
ENTRY_COUNT = 1
@patch(f"{MODULE}._write_v2_consumer_and_producer_files")
@patch(f"{MODULE}._read_and_transform")
@patch(f"{MODULE}._list_json_files")
@patch(f"{MODULE}._get_s3_client")
@patch(f"{MODULE}._get_bucket_name")
def test_migrate_processes_each_file(
mock_bucket, mock_s3, mock_list, mock_transform, mock_write
):
mock_bucket.return_value = BUCKET
s3 = MagicMock()
mock_s3.return_value = s3
mock_list.return_value = [f"{FOLDER}/a.json", f"{FOLDER}/b.json"]
mock_transform.return_value = (TRANSFORMED_BODY, ENTRY_COUNT)
migrate_v1_perms_by_app(ENV, FOLDER)
mock_bucket.assert_called_once_with(ENV)
mock_s3.assert_called_once_with(ENV)
mock_list.assert_called_once_with(s3, BUCKET, FOLDER)
assert mock_transform.call_count == 2
assert mock_write.call_count == 2
mock_write.assert_any_call(
s3, BUCKET, f"{FOLDER}/a.json", TRANSFORMED_BODY, ENTRY_COUNT
)
mock_write.assert_any_call(
s3, BUCKET, f"{FOLDER}/b.json", TRANSFORMED_BODY, ENTRY_COUNT
)
@patch(f"{MODULE}._write_v2_consumer_and_producer_files")
@patch(f"{MODULE}._read_and_transform")
@patch(f"{MODULE}._list_json_files")
@patch(f"{MODULE}._get_s3_client")
@patch(f"{MODULE}._get_bucket_name")
def test_migrate_no_files_skips_transform_and_write(
mock_bucket, mock_s3, mock_list, mock_transform, mock_write
):
mock_bucket.return_value = BUCKET
mock_s3.return_value = MagicMock()
mock_list.return_value = []
migrate_v1_perms_by_app(ENV, FOLDER)
mock_transform.assert_not_called()
mock_write.assert_not_called()