Skip to content

Commit c88b002

Browse files
csc-jmJoonatan Mäkinen
authored andcommitted
Add unit tests for cryptupload module
1 parent 43dab98 commit c88b002

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

tests/common/mockups.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ async def __aexit__(self, *_):
208208
"project": "test-id-0",
209209
"container": "test-container",
210210
"object": "test-object",
211+
"object_name": "test-object1",
211212
"receiver": "test-project-1",
212213
},
213214
"cookies": {},
214-
"query": {},
215+
"query": {"total": 1},
215216
"headers": {},
216217
"query_string": "",
217218
"remote": "test-remote",
@@ -223,13 +224,25 @@ async def __aexit__(self, *_):
223224
"Log": unittest.mock.MagicMock(logging.Logger),
224225
"test-id": "placeholder",
225226
"oidc_client": self.mock_oidc_client,
227+
"vault_client": types.SimpleNamespace(
228+
**{
229+
"put_header": unittest.mock.AsyncMock(),
230+
}
231+
),
226232
},
227233
"url": types.SimpleNamespace(
228234
**{
229235
"host": "https://localhost",
230236
}
231237
),
232238
"path": "/",
239+
"content": types.SimpleNamespace(
240+
**{
241+
"read": unittest.mock.AsyncMock(
242+
return_value=b"{'header': 'header'}"
243+
)
244+
}
245+
),
233246
}
234247
)
235248
super().setUp()

tests/upload/test_cryptupload.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Unit tests for swift_browser_ui.upload.cryptupload module."""
2+
3+
import asyncio
4+
import aiohttp.web
5+
import unittest.mock
6+
7+
from swift_browser_ui.upload.cryptupload import EncryptedUploadProxy
8+
import tests.common.mockups
9+
10+
11+
class CryptTestClass(tests.common.mockups.APITestBase):
12+
"""Test class for swift_browser_ui.upload.download functions."""
13+
14+
def setUp(self):
15+
"""Set up mocks."""
16+
super().setUp()
17+
self.mock_session = {
18+
"endpoint": "https://test-endpoint-0/v1/AUTH_test-id",
19+
"token": "test-token1",
20+
}
21+
22+
async def test_add_headers(self):
23+
"""Test adding header for the upload file."""
24+
proxy = EncryptedUploadProxy(self.mock_session, self.mock_client)
25+
# When container creation fails
26+
with self.assertRaises(aiohttp.web.HTTPForbidden):
27+
await proxy.add_header(self.mock_request)
28+
29+
alt_client_resp = self.mock_client_response
30+
alt_client_resp.status = 204
31+
alt_client = self.mock_client
32+
alt_client.head = unittest.mock.Mock(
33+
return_value=self.MockHandler(
34+
alt_client_resp,
35+
)
36+
)
37+
resp = await proxy.add_header(self.mock_request)
38+
self.assertTrue(proxy.header_uploaded)
39+
self.assertEqual(resp.status, 201)

tests/upload/test_download.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@ def setUp(self):
2323
"endpoint": "https://test-endpoint-0/v1/AUTH_test-id",
2424
"token": "test-token1",
2525
}
26-
26+
2727
def mocked_requests_get(self, *args, **kwargs):
2828
class MockResponse:
2929
def __init__(self, headers, status_code):
3030
self.status_code = status_code
3131
self.headers = headers
32+
3233
def __enter__(self):
3334
return self
35+
3436
def __exit__(self, exc_type, exc_value, traceback):
3537
pass
38+
3639
def iter_content(self, chunk_size):
3740
cont = []
3841
for i in range(chunk_size):
3942
cont.append(1)
4043
return cont
41-
44+
4245
if kwargs["headers"]["X-Auth-Token"] == "test-token1":
4346
return MockResponse({}, 200)
4447
elif kwargs["headers"]["X-Auth-Token"] == "test-token2":
@@ -75,7 +78,9 @@ def test_file_wrapper(self):
7578

7679
def test_parse_archive(self):
7780
"""Test parsing a list of paths."""
78-
cont_proxy = ContainerArchiveDownloadProxy(self.mock_session, "project", "container", "object")
81+
cont_proxy = ContainerArchiveDownloadProxy(
82+
self.mock_session, "project", "container", "object"
83+
)
7984
with self.assertRaises(ValueError):
8085
cont_proxy._parse_archive_fs([""])
8186

@@ -92,7 +97,9 @@ def test_parse_archive(self):
9297

9398
def test_download_loop(self):
9499
"""Test loop to initialize downloads."""
95-
cont_proxy = ContainerArchiveDownloadProxy(self.mock_session, "project", "container", "object")
100+
cont_proxy = ContainerArchiveDownloadProxy(
101+
self.mock_session, "project", "container", "object"
102+
)
96103
cont_proxy.archive = tarfile.open(
97104
name=cont_proxy.container + ".tar",
98105
mode="w|",
@@ -107,9 +114,9 @@ def test_download_loop(self):
107114
"file": {
108115
"name": "/file",
109116
"type": "file",
110-
"tar_info": tarfile.TarInfo(name="/file")
117+
"tar_info": tarfile.TarInfo(name="/file"),
111118
}
112-
}
119+
},
113120
}
114121
}
115122
cont_proxy.download_init()
@@ -123,13 +130,15 @@ def mocked_queue_get(*args, **kwargs):
123130
@mock.patch("queue.Queue.get", side_effect=mocked_queue_get)
124131
def test_tar_archiving_loop(self, mocked_queue):
125132
"""Test end of loop to initialize tarballing."""
126-
cont_proxy = ContainerArchiveDownloadProxy(self.mock_session, "project", "container", "object")
133+
cont_proxy = ContainerArchiveDownloadProxy(
134+
self.mock_session, "project", "container", "object"
135+
)
127136
cont_proxy.archive = tarfile.open(
128137
name=cont_proxy.container + ".tar",
129138
mode="w|",
130139
fileobj=cont_proxy.output_queue,
131140
)
132141
cont_proxy.tar_archiving_loop()
133142
with self.assertRaises(OSError):
134-
# Archive was closed
135-
cont_proxy.archive.getnames()
143+
# Archive was closed
144+
cont_proxy.archive.getnames()

0 commit comments

Comments
 (0)