-
Notifications
You must be signed in to change notification settings - Fork 825
Expand file tree
/
Copy pathtest_storage_io.py
More file actions
342 lines (238 loc) · 12.9 KB
/
Copy pathtest_storage_io.py
File metadata and controls
342 lines (238 loc) · 12.9 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, Mock, patch
import pytest
from pyrit.models.storage_io import (
AzureBlobStorageIO,
DiskStorageIO,
SupportedContentType,
)
@pytest.fixture
def azure_blob_storage_io():
"""Fixture to create an instance of AzureBlobStorageIO."""
return AzureBlobStorageIO(container_url="dummy")
@pytest.mark.asyncio
async def test_disk_storage_io_read_file():
storage = DiskStorageIO()
path = "sample.txt"
content = b"Test content"
with patch("aiofiles.open", new_callable=MagicMock) as mock_open:
mock_file = mock_open.return_value.__aenter__.return_value
mock_file.read = AsyncMock(return_value=content)
result = await storage.read_file(path)
assert result == content
mock_open.assert_called_once_with(Path(path), "rb")
@pytest.mark.asyncio
async def test_disk_storage_io_write_file():
storage = DiskStorageIO()
path = "sample.txt"
content = b"Test content"
with patch("aiofiles.open", new_callable=MagicMock) as mock_open:
mock_file = mock_open.return_value.__aenter__.return_value
mock_file.write = AsyncMock()
await storage.write_file(path, content)
mock_open.assert_called_once_with(Path(path), "wb")
mock_file.write.assert_called_once_with(content)
@pytest.mark.asyncio
async def test_disk_storage_io_path_exists():
storage = DiskStorageIO()
path = "sample.txt"
with patch("os.path.exists", return_value=True) as mock_exists:
result = await storage.path_exists(path)
assert result is True
mock_exists.assert_called_once_with(Path(path))
@pytest.mark.asyncio
async def test_disk_storage_io_is_file():
storage = DiskStorageIO()
path = "sample.txt"
with patch("os.path.isfile", return_value=True) as mock_isfile:
result = await storage.is_file(path)
assert result is True
mock_isfile.assert_called_once_with(Path(path))
@pytest.mark.asyncio
async def test_disk_storage_io_create_directory_if_not_exists():
storage = DiskStorageIO()
directory_path = "sample_dir"
with patch("os.makedirs") as mock_mkdir, patch("pathlib.Path.exists", return_value=False) as mock_exists:
await storage.create_directory_if_not_exists(directory_path)
mock_exists.assert_called_once()
mock_mkdir.assert_called_once_with(Path(directory_path), exist_ok=True)
@pytest.mark.asyncio
async def test_azure_blob_storage_io_read_file(azure_blob_storage_io):
azure_blob_storage_io._client_async = AsyncMock() # Use Mock since get_blob_client is sync
mock_blob_client = AsyncMock()
mock_blob_stream = AsyncMock()
azure_blob_storage_io._client_async.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_client.download_blob = AsyncMock(return_value=mock_blob_stream)
mock_blob_stream.readall = AsyncMock(return_value=b"Test file content")
azure_blob_storage_io._client_async.close = AsyncMock()
result = await azure_blob_storage_io.read_file(
"https://account.blob.core.windows.net/container/dir1/dir2/sample.png"
)
assert result == b"Test file content"
@pytest.mark.asyncio
async def test_azure_blob_storage_io_read_file_with_relative_path(azure_blob_storage_io):
mock_container_client = AsyncMock()
azure_blob_storage_io._client_async = mock_container_client
mock_blob_client = AsyncMock()
mock_blob_stream = AsyncMock()
mock_container_client.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_client.download_blob = AsyncMock(return_value=mock_blob_stream)
mock_blob_stream.readall = AsyncMock(return_value=b"Test file content")
mock_container_client.close = AsyncMock()
result = await azure_blob_storage_io.read_file("dir1/dir2/sample.png")
assert result == b"Test file content"
mock_container_client.get_blob_client.assert_called_once_with(blob="dir1/dir2/sample.png")
@pytest.mark.asyncio
async def test_azure_blob_storage_io_write_file():
container_url = "https://youraccount.blob.core.windows.net/yourcontainer"
azure_blob_storage_io = AzureBlobStorageIO(
container_url=container_url, blob_content_type=SupportedContentType.PLAIN_TEXT
)
mock_blob_client = AsyncMock()
mock_container_client = AsyncMock()
mock_blob_client.upload_blob = AsyncMock()
mock_container_client.get_blob_client.return_value = mock_blob_client
with patch.object(azure_blob_storage_io, "_create_container_client_async", return_value=None):
azure_blob_storage_io._client_async = mock_container_client
azure_blob_storage_io._upload_blob_async = AsyncMock()
data_to_write = b"Test data"
path = "https://youraccount.blob.core.windows.net/yourcontainer/testfile.txt"
await azure_blob_storage_io.write_file(path, data_to_write)
azure_blob_storage_io._upload_blob_async.assert_awaited_with(
file_name="testfile.txt", data=data_to_write, content_type=SupportedContentType.PLAIN_TEXT.value
)
@pytest.mark.asyncio
async def test_azure_blob_storage_io_write_file_with_relative_path():
container_url = "https://youraccount.blob.core.windows.net/yourcontainer"
azure_blob_storage_io = AzureBlobStorageIO(
container_url=container_url, blob_content_type=SupportedContentType.PLAIN_TEXT
)
mock_container_client = AsyncMock()
with patch.object(azure_blob_storage_io, "_create_container_client_async", return_value=None):
azure_blob_storage_io._client_async = mock_container_client
azure_blob_storage_io._upload_blob_async = AsyncMock()
data_to_write = b"Test data"
await azure_blob_storage_io.write_file("dir1/dir2/testfile.txt", data_to_write)
azure_blob_storage_io._upload_blob_async.assert_awaited_with(
file_name="dir1/dir2/testfile.txt",
data=data_to_write,
content_type=SupportedContentType.PLAIN_TEXT.value,
)
@pytest.mark.asyncio
async def test_azure_blob_storage_io_create_container_client_uses_explicit_sas_token():
container_url = "https://youraccount.blob.core.windows.net/yourcontainer"
sas_token = "explicit-sas-token"
azure_blob_storage_io = AzureBlobStorageIO(container_url=container_url, sas_token=sas_token)
mock_container_client = AsyncMock()
with patch(
"pyrit.models.storage_io.AsyncContainerClient.from_container_url", return_value=mock_container_client
) as mock_from_container_url:
await azure_blob_storage_io._create_container_client_async()
mock_from_container_url.assert_called_once_with(container_url=container_url, credential=sas_token)
assert azure_blob_storage_io._client_async is mock_container_client
assert azure_blob_storage_io._credential is None
@pytest.mark.asyncio
async def test_azure_blob_storage_io_create_container_client_uses_default_credential_when_no_sas_token():
container_url = "https://youraccount.blob.core.windows.net/yourcontainer"
azure_blob_storage_io = AzureBlobStorageIO(container_url=container_url)
mock_container_client = AsyncMock()
mock_credential = AsyncMock()
with (
patch("pyrit.models.storage_io.DefaultAzureCredential", return_value=mock_credential) as mock_credential_cls,
patch("pyrit.models.storage_io.AsyncContainerClient", return_value=mock_container_client) as mock_container_cls,
):
await azure_blob_storage_io._create_container_client_async()
mock_credential_cls.assert_called_once()
mock_container_cls.assert_called_once_with(
account_url="https://youraccount.blob.core.windows.net",
container_name="yourcontainer",
credential=mock_credential,
)
assert azure_blob_storage_io._client_async is mock_container_client
assert azure_blob_storage_io._credential is mock_credential
@pytest.mark.asyncio
async def test_azure_blob_storage_io_close_client_async_closes_credential_and_client():
container_url = "https://youraccount.blob.core.windows.net/yourcontainer"
azure_blob_storage_io = AzureBlobStorageIO(container_url=container_url)
mock_client = AsyncMock()
mock_credential = AsyncMock()
azure_blob_storage_io._client_async = mock_client
azure_blob_storage_io._credential = mock_credential
await azure_blob_storage_io._close_client_async()
mock_client.close.assert_called_once()
mock_credential.close.assert_called_once()
assert azure_blob_storage_io._client_async is None
assert azure_blob_storage_io._credential is None
@pytest.mark.asyncio
async def test_azure_storage_io_path_exists(azure_blob_storage_io):
azure_blob_storage_io._client_async = AsyncMock()
mock_blob_client = AsyncMock()
azure_blob_storage_io._client_async.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_client.get_blob_properties = AsyncMock()
azure_blob_storage_io._client_async.close = AsyncMock()
file_path = "https://example.blob.core.windows.net/container/dir1/dir2/blob_name.txt"
exists = await azure_blob_storage_io.path_exists(file_path)
assert exists is True
@pytest.mark.asyncio
async def test_azure_storage_io_path_exists_with_relative_path(azure_blob_storage_io):
mock_container_client = AsyncMock()
azure_blob_storage_io._client_async = mock_container_client
mock_blob_client = AsyncMock()
mock_container_client.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_client.get_blob_properties = AsyncMock()
mock_container_client.close = AsyncMock()
exists = await azure_blob_storage_io.path_exists("dir1/dir2/blob_name.txt")
assert exists is True
mock_container_client.get_blob_client.assert_called_once_with(blob="dir1/dir2/blob_name.txt")
@pytest.mark.asyncio
async def test_azure_storage_io_is_file(azure_blob_storage_io):
azure_blob_storage_io._client_async = AsyncMock()
mock_blob_client = AsyncMock()
azure_blob_storage_io._client_async.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_properties = Mock(size=1024)
mock_blob_client.get_blob_properties = AsyncMock(return_value=mock_blob_properties)
azure_blob_storage_io._client_async.close = AsyncMock()
file_path = "https://example.blob.core.windows.net/container/dir1/dir2/blob_name.txt"
is_file = await azure_blob_storage_io.is_file(file_path)
assert is_file is True
@pytest.mark.asyncio
async def test_azure_storage_io_is_file_with_relative_path(azure_blob_storage_io):
mock_container_client = AsyncMock()
azure_blob_storage_io._client_async = mock_container_client
mock_blob_client = AsyncMock()
mock_container_client.get_blob_client = Mock(return_value=mock_blob_client)
mock_blob_properties = Mock(size=1024)
mock_blob_client.get_blob_properties = AsyncMock(return_value=mock_blob_properties)
mock_container_client.close = AsyncMock()
is_file = await azure_blob_storage_io.is_file("dir1/dir2/blob_name.txt")
assert is_file is True
mock_container_client.get_blob_client.assert_called_once_with(blob="dir1/dir2/blob_name.txt")
def test_azure_storage_io_parse_blob_url_valid(azure_blob_storage_io):
file_path = "https://example.blob.core.windows.net/container/dir1/dir2/blob_name.txt"
container_name, blob_name = azure_blob_storage_io.parse_blob_url(file_path)
assert container_name == "container"
assert blob_name == "dir1/dir2/blob_name.txt"
def test_azure_storage_io_parse_blob_url_invalid(azure_blob_storage_io):
with pytest.raises(ValueError, match="Invalid blob URL"):
azure_blob_storage_io.parse_blob_url("invalid_url")
def test_azure_storage_io_parse_blob_url_without_scheme(azure_blob_storage_io):
with pytest.raises(ValueError, match="Invalid blob URL"):
azure_blob_storage_io.parse_blob_url("example.blob.core.windows.net/container/dir1/blob_name.txt")
def test_azure_storage_io_parse_blob_url_without_netloc(azure_blob_storage_io):
with pytest.raises(ValueError, match="Invalid blob URL"):
azure_blob_storage_io.parse_blob_url("https:///container/dir1/blob_name.txt")
def test_resolve_blob_name_with_full_url(azure_blob_storage_io):
result = azure_blob_storage_io._resolve_blob_name("https://account.blob.core.windows.net/container/dir1/file.txt")
assert result == "dir1/file.txt"
def test_resolve_blob_name_with_relative_path(azure_blob_storage_io):
assert azure_blob_storage_io._resolve_blob_name("dir1/dir2/file.txt") == "dir1/dir2/file.txt"
def test_resolve_blob_name_with_simple_filename(azure_blob_storage_io):
assert azure_blob_storage_io._resolve_blob_name("file.txt") == "file.txt"
def test_resolve_blob_name_normalizes_backslashes(azure_blob_storage_io):
assert azure_blob_storage_io._resolve_blob_name("dir1\\dir2\\file.txt") == "dir1/dir2/file.txt"
def test_resolve_blob_name_with_path_object(azure_blob_storage_io):
from pathlib import PurePosixPath
result = azure_blob_storage_io._resolve_blob_name(PurePosixPath("dir1/dir2/file.txt"))
assert result == "dir1/dir2/file.txt"