Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/loader/test_client.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import os | import os | ||||
import shutil | import shutil | ||||
import tempfile | import tempfile | ||||
import unittest | import unittest | ||||
from nose.plugins.attrib import attr | from nose.plugins.attrib import attr | ||||
from nose.tools import istest | |||||
from swh.deposit.client import PrivateApiDepositClient | from swh.deposit.client import PrivateApiDepositClient | ||||
from swh.deposit.config import DEPOSIT_STATUS_LOAD_SUCCESS | from swh.deposit.config import DEPOSIT_STATUS_LOAD_SUCCESS | ||||
from swh.deposit.config import DEPOSIT_STATUS_LOAD_FAILURE | from swh.deposit.config import DEPOSIT_STATUS_LOAD_FAILURE | ||||
from .common import CLIENT_TEST_CONFIG | from .common import CLIENT_TEST_CONFIG | ||||
class StreamedResponse: | class StreamedResponse: | ||||
Show All 26 Lines | class PrivateApiDepositClientReadArchiveTest(unittest.TestCase): | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
self.temporary_directory = tempfile.mkdtemp(dir='/tmp') | self.temporary_directory = tempfile.mkdtemp(dir='/tmp') | ||||
def tearDown(self): | def tearDown(self): | ||||
super().setUp() | super().setUp() | ||||
shutil.rmtree(self.temporary_directory) | shutil.rmtree(self.temporary_directory) | ||||
@istest | def test_archive_get(self): | ||||
def archive_get(self): | |||||
"""Reading archive should write data in temporary directory | """Reading archive should write data in temporary directory | ||||
""" | """ | ||||
stream_content = [b"some", b"streamed", b"response"] | stream_content = [b"some", b"streamed", b"response"] | ||||
response = StreamedResponse( | response = StreamedResponse( | ||||
ok=True, | ok=True, | ||||
stream=(s for s in stream_content)) | stream=(s for s in stream_content)) | ||||
_client = FakeRequestClientGet(response) | _client = FakeRequestClientGet(response) | ||||
Show All 10 Lines | def test_archive_get(self): | ||||
actual_content = f.read() | actual_content = f.read() | ||||
self.assertEquals(actual_content, b''.join(stream_content)) | self.assertEquals(actual_content, b''.join(stream_content)) | ||||
self.assertEquals(_client.args, ('http://nowhere:9000/some/url', )) | self.assertEquals(_client.args, ('http://nowhere:9000/some/url', )) | ||||
self.assertEquals(_client.kwargs, { | self.assertEquals(_client.kwargs, { | ||||
'stream': True | 'stream': True | ||||
}) | }) | ||||
@istest | def test_archive_get_with_authentication(self): | ||||
def archive_get_with_authentication(self): | |||||
"""Reading archive should write data in temporary directory | """Reading archive should write data in temporary directory | ||||
""" | """ | ||||
stream_content = [b"some", b"streamed", b"response", b"for", b"auth"] | stream_content = [b"some", b"streamed", b"response", b"for", b"auth"] | ||||
response = StreamedResponse( | response = StreamedResponse( | ||||
ok=True, | ok=True, | ||||
stream=(s for s in stream_content)) | stream=(s for s in stream_content)) | ||||
_client = FakeRequestClientGet(response) | _client = FakeRequestClientGet(response) | ||||
Show All 15 Lines | def test_archive_get_with_authentication(self): | ||||
self.assertEquals(actual_content, b''.join(stream_content)) | self.assertEquals(actual_content, b''.join(stream_content)) | ||||
self.assertEquals(_client.args, ('http://nowhere:9000/some/url', )) | self.assertEquals(_client.args, ('http://nowhere:9000/some/url', )) | ||||
self.assertEquals(_client.kwargs, { | self.assertEquals(_client.kwargs, { | ||||
'stream': True, | 'stream': True, | ||||
'auth': ('user', 'pass') | 'auth': ('user', 'pass') | ||||
}) | }) | ||||
@istest | def test_archive_get_can_fail(self): | ||||
def archive_get_can_fail(self): | |||||
"""Reading archive can fail for some reasons | """Reading archive can fail for some reasons | ||||
""" | """ | ||||
response = StreamedResponse(ok=False, stream=None) | response = StreamedResponse(ok=False, stream=None) | ||||
_client = FakeRequestClientGet(response) | _client = FakeRequestClientGet(response) | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
Show All 11 Lines | def __init__(self, ok, response): | ||||
self.ok = ok | self.ok = ok | ||||
self.response = response | self.response = response | ||||
def json(self): | def json(self): | ||||
return self.response | return self.response | ||||
class PrivateApiDepositClientReadMetadataTest(unittest.TestCase): | class PrivateApiDepositClientReadMetadataTest(unittest.TestCase): | ||||
@istest | def test_metadata_get(self): | ||||
def metadata_get(self): | |||||
"""Reading archive should write data in temporary directory | """Reading archive should write data in temporary directory | ||||
""" | """ | ||||
expected_response = {"some": "dict"} | expected_response = {"some": "dict"} | ||||
response = JsonResponse( | response = JsonResponse( | ||||
ok=True, | ok=True, | ||||
response=expected_response) | response=expected_response) | ||||
_client = FakeRequestClientGet(response) | _client = FakeRequestClientGet(response) | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
actual_metadata = deposit_client.metadata_get('/metadata') | actual_metadata = deposit_client.metadata_get('/metadata') | ||||
self.assertEquals(actual_metadata, expected_response) | self.assertEquals(actual_metadata, expected_response) | ||||
@istest | def test_metadata_get_can_fail(self): | ||||
def metadata_get_can_fail(self): | |||||
"""Reading metadata can fail for some reasons | """Reading metadata can fail for some reasons | ||||
""" | """ | ||||
_client = FakeRequestClientGet(JsonResponse(ok=False, response=None)) | _client = FakeRequestClientGet(JsonResponse(ok=False, response=None)) | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
with self.assertRaisesRegex( | with self.assertRaisesRegex( | ||||
ValueError, | ValueError, | ||||
Show All 9 Lines | class FakeRequestClientPut: | ||||
kwargs = None | kwargs = None | ||||
def put(self, *args, **kwargs): | def put(self, *args, **kwargs): | ||||
self.args = args | self.args = args | ||||
self.kwargs = kwargs | self.kwargs = kwargs | ||||
class PrivateApiDepositClientStatusUpdateTest(unittest.TestCase): | class PrivateApiDepositClientStatusUpdateTest(unittest.TestCase): | ||||
@istest | def test_status_update(self): | ||||
def status_update(self): | |||||
"""Update status | """Update status | ||||
""" | """ | ||||
_client = FakeRequestClientPut() | _client = FakeRequestClientPut() | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
deposit_client.status_update('/update/status', | deposit_client.status_update('/update/status', | ||||
DEPOSIT_STATUS_LOAD_SUCCESS, | DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
revision_id='some-revision-id') | revision_id='some-revision-id') | ||||
self.assertEquals(_client.args, | self.assertEquals(_client.args, | ||||
('http://nowhere:9000/update/status', )) | ('http://nowhere:9000/update/status', )) | ||||
self.assertEquals(_client.kwargs, { | self.assertEquals(_client.kwargs, { | ||||
'json': { | 'json': { | ||||
'status': DEPOSIT_STATUS_LOAD_SUCCESS, | 'status': DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
'revision_id': 'some-revision-id', | 'revision_id': 'some-revision-id', | ||||
} | } | ||||
}) | }) | ||||
@istest | def test_status_update_with_no_revision_id(self): | ||||
def status_update_with_no_revision_id(self): | |||||
"""Reading metadata can fail for some reasons | """Reading metadata can fail for some reasons | ||||
""" | """ | ||||
_client = FakeRequestClientPut() | _client = FakeRequestClientPut() | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
deposit_client.status_update('/update/status/fail', | deposit_client.status_update('/update/status/fail', | ||||
DEPOSIT_STATUS_LOAD_FAILURE) | DEPOSIT_STATUS_LOAD_FAILURE) | ||||
self.assertEquals(_client.args, | self.assertEquals(_client.args, | ||||
('http://nowhere:9000/update/status/fail', )) | ('http://nowhere:9000/update/status/fail', )) | ||||
self.assertEquals(_client.kwargs, { | self.assertEquals(_client.kwargs, { | ||||
'json': { | 'json': { | ||||
'status': DEPOSIT_STATUS_LOAD_FAILURE, | 'status': DEPOSIT_STATUS_LOAD_FAILURE, | ||||
} | } | ||||
}) | }) | ||||
class PrivateApiDepositClientCheckTest(unittest.TestCase): | class PrivateApiDepositClientCheckTest(unittest.TestCase): | ||||
@istest | def test_check(self): | ||||
def check(self): | |||||
"""When check ok, this should return the deposit's status | """When check ok, this should return the deposit's status | ||||
""" | """ | ||||
_client = FakeRequestClientGet( | _client = FakeRequestClientGet( | ||||
JsonResponse(ok=True, response={'status': 'something'})) | JsonResponse(ok=True, response={'status': 'something'})) | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
r = deposit_client.check('/check') | r = deposit_client.check('/check') | ||||
self.assertEquals(_client.args, | self.assertEquals(_client.args, | ||||
('http://nowhere:9000/check', )) | ('http://nowhere:9000/check', )) | ||||
self.assertEquals(_client.kwargs, {}) | self.assertEquals(_client.kwargs, {}) | ||||
self.assertEquals(r, 'something') | self.assertEquals(r, 'something') | ||||
@istest | def test_check_fails(self): | ||||
def check_fails(self): | |||||
"""Checking deposit can fail for some reason | """Checking deposit can fail for some reason | ||||
""" | """ | ||||
_client = FakeRequestClientGet( | _client = FakeRequestClientGet( | ||||
JsonResponse(ok=False, response=None)) | JsonResponse(ok=False, response=None)) | ||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG, | ||||
_client=_client) | _client=_client) | ||||
with self.assertRaisesRegex( | with self.assertRaisesRegex( | ||||
ValueError, | ValueError, | ||||
'Problem when checking deposit'): | 'Problem when checking deposit'): | ||||
deposit_client.check('/check/fails') | deposit_client.check('/check/fails') | ||||
self.assertEquals(_client.args, | self.assertEquals(_client.args, | ||||
('http://nowhere:9000/check/fails', )) | ('http://nowhere:9000/check/fails', )) | ||||
self.assertEquals(_client.kwargs, {}) | self.assertEquals(_client.kwargs, {}) |