-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathinspect_gcs_test.py
More file actions
264 lines (220 loc) · 7.8 KB
/
inspect_gcs_test.py
File metadata and controls
264 lines (220 loc) · 7.8 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import random
from unittest import mock
from unittest.mock import MagicMock
import google.cloud.dlp_v2
import google.cloud.pubsub
import inspect_gcs as inspect_content
import pytest
GCLOUD_PROJECT = os.getenv("GOOGLE_CLOUD_PROJECT")
TIMEOUT = 900 # 15 minutes
def mock_job_and_subscriber(
mock_dlp_instance: MagicMock,
mock_subscriber_instance: MagicMock,
dlp_job_path: str,
finding_name: str = None,
finding_count: int = None,
):
# Configure the mock CreateDlpJob DLP method and its behavior.
mock_dlp_instance.create_dlp_job.return_value.name = dlp_job_path
# Configure the mock GetDlpJob DLP method and its behavior.
mock_job = mock_dlp_instance.get_dlp_job.return_value
mock_job.name = dlp_job_path
mock_job.state = google.cloud.dlp_v2.DlpJob.JobState.DONE
# mock result of GetDlpJob DLP method.
if finding_name:
finding = mock_job.inspect_details.result.info_type_stats.info_type
finding.name = finding_name
mock_job.inspect_details.result.info_type_stats = [
MagicMock(info_type=finding, count=finding_count),
]
else:
mock_job.inspect_details.result.info_type_stats = None
# Mock class for google.cloud.pubsub.subscriber.message.Message
class MockMessage:
def __init__(self, data, attributes=None):
self.data = data
self.attributes = attributes or {}
def ack(self):
pass
# Replace the real subscribe logic with a custom side effect
def mock_subscribe(*args, **kwargs):
# Get the callback function from the arguments.
callback = kwargs.get("callback")
# In this example, we'll call the callback function with a mock message
message = MockMessage(args, {"DlpJobName": dlp_job_path})
callback(message)
# Patch the original method with the mock function
mock_subscriber_instance.subscribe = mock_subscribe
@mock.patch("google.cloud.dlp_v2.DlpServiceClient")
@mock.patch("google.cloud.pubsub.SubscriberClient")
def test_inspect_gcs_file(
subscriber_client: MagicMock,
dlp_client: MagicMock,
capsys: pytest.CaptureFixture,
) -> None:
# Mock DLP client and subscriber client along with their behavior
mock_dlp_instance = dlp_client.return_value
mock_subscriber_instance = subscriber_client.return_value
mock_job_and_subscriber(
mock_dlp_instance,
mock_subscriber_instance,
f"projects/{GCLOUD_PROJECT}/dlpJobs/test_job",
"EMAIL_ADDRESS",
1,
)
# Call the sample.
inspect_content.inspect_gcs_file(
GCLOUD_PROJECT,
"MY_BUCKET",
"test.txt",
"topic_id",
"subscription_id",
["EMAIL_ADDRESS", "PHONE_NUMBER"],
timeout=1,
)
out, _ = capsys.readouterr()
assert "Job name:" in out
assert "Info type: EMAIL_ADDRESS" in out
mock_dlp_instance.create_dlp_job.assert_called_once()
mock_dlp_instance.get_dlp_job.assert_called_once()
@mock.patch("google.cloud.dlp_v2.DlpServiceClient")
@mock.patch("google.cloud.pubsub.SubscriberClient")
def test_inspect_gcs_file_with_custom_info_types(
subscriber_client: MagicMock,
dlp_client: MagicMock,
capsys: pytest.CaptureFixture,
) -> None:
# Mock DLP client and subscriber client along with their behavior
mock_dlp_instance = dlp_client.return_value
mock_subscriber_instance = subscriber_client.return_value
mock_job_and_subscriber(
mock_dlp_instance,
mock_subscriber_instance,
f"projects/{GCLOUD_PROJECT}/dlpJobs/test_job",
"EMAIL_ADDRESS",
1,
)
dictionaries = ["gary@somedomain.com"]
regexes = ["\\(\\d{3}\\) \\d{3}-\\d{4}"]
# Call the sample.
inspect_content.inspect_gcs_file(
GCLOUD_PROJECT,
"MY_BUCKET",
"test.txt",
"topic_id",
"subscription_id",
[],
custom_dictionaries=dictionaries,
custom_regexes=regexes,
timeout=1,
)
out, _ = capsys.readouterr()
assert "Info type: EMAIL_ADDRESS" in out
assert "Job name:" in out
mock_dlp_instance.create_dlp_job.assert_called_once()
mock_dlp_instance.get_dlp_job.assert_called_once()
@mock.patch("google.cloud.dlp_v2.DlpServiceClient")
@mock.patch("google.cloud.pubsub.SubscriberClient")
def test_inspect_gcs_file_no_results(
subscriber_client: MagicMock,
dlp_client: MagicMock,
capsys: pytest.CaptureFixture,
) -> None:
# Mock DLP client and subscriber client along with their behavior
mock_dlp_instance = dlp_client.return_value
mock_subscriber_instance = subscriber_client.return_value
mock_job_and_subscriber(
mock_dlp_instance,
mock_subscriber_instance,
f"projects/{GCLOUD_PROJECT}/dlpJobs/test_job",
)
inspect_content.inspect_gcs_file(
GCLOUD_PROJECT,
"MY_BUCKET",
"harmless.txt",
"topic_id",
"subscription_id",
["EMAIL_ADDRESS", "PHONE_NUMBER"],
timeout=TIMEOUT,
)
out, _ = capsys.readouterr()
assert "No findings" in out
assert "Job name:" in out
mock_dlp_instance.create_dlp_job.assert_called_once()
mock_dlp_instance.get_dlp_job.assert_called_once()
@mock.patch("google.cloud.dlp_v2.DlpServiceClient")
@mock.patch("google.cloud.pubsub.SubscriberClient")
def test_inspect_gcs_image_file(
subscriber_client: MagicMock,
dlp_client: MagicMock,
capsys: pytest.CaptureFixture,
) -> None:
# Mock DLP client and subscriber client along with their behavior
mock_dlp_instance = dlp_client.return_value
mock_subscriber_instance = subscriber_client.return_value
mock_job_and_subscriber(
mock_dlp_instance,
mock_subscriber_instance,
f"projects/{GCLOUD_PROJECT}/dlpJobs/test_job",
"EMAIL_ADDRESS",
1,
)
inspect_content.inspect_gcs_file(
GCLOUD_PROJECT,
"MY_BUCKET",
"test.png",
"topic_id",
"subscription_id",
["EMAIL_ADDRESS", "PHONE_NUMBER"],
timeout=1,
)
out, _ = capsys.readouterr()
assert "Info type: EMAIL_ADDRESS" in out
assert "Job name:" in out
mock_dlp_instance.create_dlp_job.assert_called_once()
mock_dlp_instance.get_dlp_job.assert_called_once()
@mock.patch("google.cloud.dlp_v2.DlpServiceClient")
@mock.patch("google.cloud.pubsub.SubscriberClient")
def test_inspect_gcs_multiple_files(
subscriber_client: MagicMock,
dlp_client: MagicMock,
capsys: pytest.CaptureFixture,
) -> None:
# Mock DLP client and subscriber client along with their behavior
mock_dlp_instance = dlp_client.return_value
mock_subscriber_instance = subscriber_client.return_value
mock_job_and_subscriber(
mock_dlp_instance,
mock_subscriber_instance,
f"projects/{GCLOUD_PROJECT}/dlpJobs/test_job",
"EMAIL_ADDRESS",
random.randint(0, 1000),
)
inspect_content.inspect_gcs_file(
GCLOUD_PROJECT,
"MY_BUCKET",
"*",
"topic_id",
"subscription_id",
["EMAIL_ADDRESS", "PHONE_NUMBER"],
timeout=1,
)
out, _ = capsys.readouterr()
assert "Info type: EMAIL_ADDRESS" in out
assert "Job name:" in out
mock_dlp_instance.create_dlp_job.assert_called_once()
mock_dlp_instance.get_dlp_job.assert_called_once()