Skip to content

Commit 88b25b3

Browse files
serhiyeccles
authored andcommitted
Use the session pattern for REST communication, rather than raw requests
Problem: All current REST communication uses raw request calls, rather than a session object. Solution: Use the Requests session object for communication Signed off: serhiy: Serhiy1@live.co.uk Fix inaccurate doc string Update TEST_AUTHTOKEN to TEST_AUTHTOKEN_FILENAME Add a check for identical files func tests
1 parent f549ae5 commit 88b25b3

16 files changed

Lines changed: 186 additions & 128 deletions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ login is 'githubUserHandle'):
125125
> rights. If using ~/.ssh/config to manage ssh identities then replace all mentions of
126126
> 'git@github.com' with the clause name in ~/.ssh/config which references the appropriate
127127
> ssh key::
128-
>
128+
>
129129
> For example:
130130
```
131131
Host ssh-githubUserHandle
@@ -215,14 +215,14 @@ If you have access to an archivist instance then one can run functional tests. T
215215
and authtoken are required. The authtoken must be stored in a file in the credentials
216216
subdirectory credentials/authtoken (say).
217217

218-
These tests will create artefacts on the archivist instance so it is **not** recommended that
218+
These tests will create artefacts on the archivist instance so it is **not** recommended that
219219
they be run in a production environment.
220220

221221
Set 2 environment variables and execute:
222222

223223
```bash
224224
export TEST_ARCHIVIST="https://rkvst.poc.jitsuin.io"
225-
export TEST_AUTHTOKEN=credentials/authtoken
225+
export TEST_AUTHTOKEN_FILENAME=credentials/authtoken
226226
task functests
227227
```
228228

@@ -333,7 +333,7 @@ Push the changes upstream(the set-upstream option is only required the first tim
333333
git push --set-upstream origin dev/githubUserHandle/some-proposed-fix
334334
```
335335

336-
Enter the github ui at https://github.com/jitsuin-inc/archivist-python and
336+
Enter the github ui at https://github.com/jitsuin-inc/archivist-python and
337337
generate a pull request.
338338

339339
Reviewers will be notified when a PR is generated and you will receive feedback.

archivist/archivist.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def __init__(
125125

126126
self._cert = cert
127127
self._response_ring_buffer = deque(maxlen=self.RING_BUFFER_MAX_LEN)
128+
self._session = requests.Session()
128129

129130
# keep these in sync with CLIENTS map above
130131
self.assets: _AssetsClient
@@ -188,7 +189,7 @@ def get(
188189
dict representing the response body (entity).
189190
190191
"""
191-
response = requests.get(
192+
response = self._session.get(
192193
SEP.join((self.url, ROOT, subpath, identity)),
193194
headers=self.__add_headers(headers),
194195
verify=self.verify,
@@ -226,7 +227,7 @@ def get_file(
226227
REST response (not the response body)
227228
228229
"""
229-
response = requests.get(
230+
response = self._session.get(
230231
SEP.join((self.url, ROOT, subpath, identity)),
231232
headers=self.__add_headers(headers),
232233
verify=self.verify,
@@ -261,7 +262,7 @@ def post(self, path: str, request: Dict, *, headers: Optional[Dict] = None) -> D
261262
262263
"""
263264

264-
response = requests.post(
265+
response = self._session.post(
265266
SEP.join((self.url, ROOT, path)),
266267
data=json.dumps(request),
267268
headers=self.__add_headers(headers),
@@ -298,7 +299,7 @@ def post_file(self, path: str, fd: BinaryIO, mtype: str) -> Dict:
298299
headers = {
299300
"content-type": multipart.content_type,
300301
}
301-
response = requests.post(
302+
response = self._session.post(
302303
SEP.join((self.url, ROOT, path)),
303304
data=multipart, # type: ignore https://github.com/requests/toolbelt/issues/312
304305
headers=self.__add_headers(headers),
@@ -330,7 +331,7 @@ def delete(
330331
dict representing the response body (entity).
331332
332333
"""
333-
response = requests.delete(
334+
response = self._session.delete(
334335
SEP.join((self.url, ROOT, subpath, identity)),
335336
headers=self.__add_headers(headers),
336337
verify=self.verify,
@@ -368,7 +369,7 @@ def patch(
368369
369370
"""
370371

371-
response = requests.patch(
372+
response = self._session.patch(
372373
SEP.join((self.url, ROOT, subpath, identity)),
373374
data=json.dumps(request),
374375
headers=self.__add_headers(headers),
@@ -388,7 +389,7 @@ def __list(self, path, args, *, headers=None) -> Response:
388389
if args:
389390
path = "?".join((path, args))
390391

391-
response = requests.get(
392+
response = self._session.get(
392393
SEP.join((self.url, ROOT, path)),
393394
headers=self.__add_headers(headers),
394395
verify=self.verify,

functests/execaccess_policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TestAccessPolicies(TestCase):
6464

6565
@classmethod
6666
def setUpClass(cls):
67-
with open(environ["TEST_AUTHTOKEN"]) as fd:
67+
with open(environ["TEST_AUTHTOKEN_FILENAME"]) as fd:
6868
auth = fd.read().strip()
6969
cls.arch = Archivist(environ["TEST_ARCHIVIST"], auth=auth, verify=False)
7070
cls.props = deepcopy(PROPS)

functests/execassets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ class TestAssetCreate(TestCase):
3434

3535
maxDiff = None
3636

37+
@classmethod
3738
def setUp(cls):
38-
with open(environ["TEST_AUTHTOKEN"]) as fd:
39+
with open(environ["TEST_AUTHTOKEN_FILENAME"]) as fd:
3940
auth = fd.read().strip()
4041
cls.arch = Archivist(environ["TEST_ARCHIVIST"], auth=auth, verify=False)
4142
cls.attrs = deepcopy(ATTRS)

functests/execattachments.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Tests the upload and download functionality of the SDK
3+
"""
4+
import os
5+
from os import environ
6+
from unittest import TestCase
7+
from contextlib import suppress
8+
from filecmp import cmp
9+
10+
from archivist.archivist import Archivist
11+
12+
13+
# pylint: disable=fixme
14+
# pylint: disable=missing-docstring
15+
# pylint: disable=unused-variable
16+
17+
18+
class TestAssetCreate(TestCase):
19+
"""
20+
Test Archivist Asset Create method
21+
"""
22+
23+
TEST_IMAGE_PATH = "functests/test_resources/Jitsuin_Logo_RGB.jpg"
24+
TEST_IMAGE_DOWNLOAD_PATH = "functests/test_resources/downloaded_image.jpg"
25+
26+
@classmethod
27+
def setUp(cls):
28+
with open(environ["TEST_AUTHTOKEN_FILENAME"]) as fd:
29+
auth = fd.read().strip()
30+
cls.arch = Archivist(environ["TEST_ARCHIVIST"], auth=auth, verify=False)
31+
cls.file_uuid: str = ""
32+
33+
with suppress(FileNotFoundError):
34+
os.remove(cls.TEST_IMAGE_DOWNLOAD_PATH)
35+
36+
@classmethod
37+
def tearDown(cls) -> None:
38+
"""Remove the downloaded image for subsequent test runs"""
39+
with suppress(FileNotFoundError):
40+
os.remove(cls.TEST_IMAGE_DOWNLOAD_PATH)
41+
42+
def testfile_upload_and_download(self):
43+
"""
44+
Test file upload through the SDK
45+
Test file download through the SDK
46+
"""
47+
with open(self.TEST_IMAGE_PATH, "rb") as image:
48+
attachment = self.arch.attachments.upload(image)
49+
file_uuid = attachment["identity"]
50+
51+
with open(self.TEST_IMAGE_DOWNLOAD_PATH, "wb") as image:
52+
attachment = self.arch.attachments.download(file_uuid, image)
53+
54+
# Check the downloaded file is identical to the one that was uploaded
55+
self.assertTrue(
56+
cmp(self.TEST_IMAGE_PATH, self.TEST_IMAGE_DOWNLOAD_PATH, shallow=False)
57+
)

functests/execsubjects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class TestSubjects(TestCase):
3131

3232
@classmethod
3333
def setUpClass(cls):
34-
with open(environ["TEST_AUTHTOKEN"]) as fd:
34+
with open(environ["TEST_AUTHTOKEN_FILENAME"]) as fd:
3535
auth = fd.read().strip()
3636
cls.arch = Archivist(environ["TEST_ARCHIVIST"], auth=auth, verify=False)
3737
cls.display_name = f"{DISPLAY_NAME} {uuid4()}"
61.3 KB
Loading

scripts/builder.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ docker run \
1212
-v $(pwd):/home/builder \
1313
-u $(id -u):$(id -g) \
1414
-e TEST_ARCHIVIST \
15-
-e TEST_AUTHTOKEN \
15+
-e TEST_AUTHTOKEN_FILENAME \
1616
jitsuin-archivist-python-builder \
1717
"$@"

scripts/functests.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ then
77
echo "TEST_ARCHIVIST is undefined"
88
exit 1
99
fi
10-
if [ -z "${TEST_AUTHTOKEN}" ]
10+
if [ -z "${TEST_AUTHTOKEN_FILENAME}" ]
1111
then
12-
echo "TEST_AUTHTOKEN is undefined"
12+
echo "TEST_AUTHTOKEN_FILENAME is undefined"
1313
exit 1
1414
fi
15-
if [ ! -s "${TEST_AUTHTOKEN}" ]
15+
if [ ! -s "${TEST_AUTHTOKEN_FILENAME}" ]
1616
then
17-
echo "${TEST_AUTHTOKEN} does not exist"
17+
echo "${TEST_AUTHTOKEN_FILENAME} does not exist"
1818
exit 1
1919
fi
2020

unittests/testaccess_policies.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class TestAccessPolicies(TestCase):
9393
def setUp(self):
9494
self.arch = Archivist("url", auth="authauthauth")
9595

96-
@mock.patch("requests.post")
96+
@mock.patch("requests.Session.post")
9797
def test_access_policies_create(self, mock_post):
9898
"""
9999
Test access_policy creation
@@ -125,7 +125,7 @@ def test_access_policies_create(self, mock_post):
125125
msg="CREATE method called incorrectly",
126126
)
127127

128-
@mock.patch("requests.get")
128+
@mock.patch("requests.Session.get")
129129
def test_access_policies_read(self, mock_get):
130130
"""
131131
Test access_policy reading
@@ -149,7 +149,7 @@ def test_access_policies_read(self, mock_get):
149149
msg="GET method called incorrectly",
150150
)
151151

152-
@mock.patch("requests.delete")
152+
@mock.patch("requests.Session.delete")
153153
def test_access_policies_delete(self, mock_delete):
154154
"""
155155
Test access_policy deleting
@@ -173,7 +173,7 @@ def test_access_policies_delete(self, mock_delete):
173173
msg="DELETE method called incorrectly",
174174
)
175175

176-
@mock.patch("requests.patch")
176+
@mock.patch("requests.Session.patch")
177177
def test_access_policies_update(self, mock_patch):
178178
"""
179179
Test access_policy deleting
@@ -201,7 +201,7 @@ def test_access_policies_update(self, mock_patch):
201201
msg="PATCH method called incorrectly",
202202
)
203203

204-
@mock.patch("requests.get")
204+
@mock.patch("requests.Session.get")
205205
def test_access_policies_read_with_error(self, mock_get):
206206
"""
207207
Test read method with error
@@ -210,7 +210,7 @@ def test_access_policies_read_with_error(self, mock_get):
210210
with self.assertRaises(ArchivistBadRequestError):
211211
resp = self.arch.access_policies.read(IDENTITY)
212212

213-
@mock.patch("requests.get")
213+
@mock.patch("requests.Session.get")
214214
def test_access_policies_count(self, mock_get):
215215
"""
216216
Test access_policy counting
@@ -246,7 +246,7 @@ def test_access_policies_count(self, mock_get):
246246
msg="Incorrect count",
247247
)
248248

249-
@mock.patch("requests.get")
249+
@mock.patch("requests.Session.get")
250250
def test_access_policies_count_by_name(self, mock_get):
251251
"""
252252
Test access_policy counting
@@ -285,7 +285,7 @@ def test_access_policies_count_by_name(self, mock_get):
285285
msg="GET method called incorrectly",
286286
)
287287

288-
@mock.patch("requests.get")
288+
@mock.patch("requests.Session.get")
289289
def test_access_policies_list(self, mock_get):
290290
"""
291291
Test access_policy listing
@@ -327,7 +327,7 @@ def test_access_policies_list(self, mock_get):
327327
msg="GET method called incorrectly",
328328
)
329329

330-
@mock.patch("requests.get")
330+
@mock.patch("requests.Session.get")
331331
def test_access_policies_list_by_name(self, mock_get):
332332
"""
333333
Test access_policy listing
@@ -379,7 +379,7 @@ def test_access_policies_list_by_name(self, mock_get):
379379
msg="GET method called incorrectly",
380380
)
381381

382-
@mock.patch("requests.get")
382+
@mock.patch("requests.Session.get")
383383
def test_access_policies_count_matching_access_policies(self, mock_get):
384384
"""
385385
Test access_policy counting
@@ -420,7 +420,7 @@ def test_access_policies_count_matching_access_policies(self, mock_get):
420420
msg="Incorrect count",
421421
)
422422

423-
@mock.patch("requests.get")
423+
@mock.patch("requests.Session.get")
424424
def test_access_policies_list_matching_access_policies(self, mock_get):
425425
"""
426426
Test access_policy counting
@@ -467,7 +467,7 @@ def test_access_policies_list_matching_access_policies(self, mock_get):
467467
msg="GET method called incorrectly",
468468
)
469469

470-
@mock.patch("requests.get")
470+
@mock.patch("requests.Session.get")
471471
def test_access_policies_count_matching_assets(self, mock_get):
472472
"""
473473
Test access_policy counting
@@ -508,7 +508,7 @@ def test_access_policies_count_matching_assets(self, mock_get):
508508
msg="Incorrect count",
509509
)
510510

511-
@mock.patch("requests.get")
511+
@mock.patch("requests.Session.get")
512512
def test_access_policies_list_matching_assets(self, mock_get):
513513
"""
514514
Test access_policy counting

0 commit comments

Comments
 (0)