Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/tests/loader/test_client.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 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 json | import json | ||||
import os | import os | ||||
from typing import Any, Callable, Optional | from typing import Any, Callable, Optional | ||||
import unittest | |||||
from urllib.parse import urlparse | from urllib.parse import urlparse | ||||
import pytest | import pytest | ||||
from swh.deposit.client import PrivateApiDepositClient | from swh.deposit.client import PrivateApiDepositClient | ||||
from swh.deposit.config import DEPOSIT_STATUS_LOAD_FAILURE, DEPOSIT_STATUS_LOAD_SUCCESS | from swh.deposit.config import DEPOSIT_STATUS_LOAD_FAILURE, DEPOSIT_STATUS_LOAD_SUCCESS | ||||
CLIENT_TEST_CONFIG = { | CLIENT_TEST_CONFIG = { | ||||
"url": "https://nowhere.org/", | "url": "https://nowhere.org/", | ||||
"auth": {}, # no authentication in test scenario | "auth": {}, # no authentication in test scenario | ||||
} | } | ||||
@pytest.fixture | @pytest.fixture | ||||
def deposit_config(): | def deposit_config(): | ||||
return CLIENT_TEST_CONFIG | return CLIENT_TEST_CONFIG | ||||
def test_client_config(deposit_config_path): | def test_client_config(deposit_config_path): | ||||
for client in [ | for client in [ | ||||
# config passed as constructor parameter | # config passed as constructor parameter | ||||
PrivateApiDepositClient(config=CLIENT_TEST_CONFIG), | PrivateApiDepositClient(config=CLIENT_TEST_CONFIG), | ||||
# config loaded from environment | # config loaded from environment | ||||
PrivateApiDepositClient() | PrivateApiDepositClient(), | ||||
]: | ]: | ||||
assert client.config == CLIENT_TEST_CONFIG | assert client.base_url == CLIENT_TEST_CONFIG["url"] | ||||
assert client.auth is None | |||||
def build_expected_path(datadir, base_url: str, api_url: str) -> str: | def build_expected_path(datadir, base_url: str, api_url: str) -> str: | ||||
vlorentz: not covered?
also, "passzord" | |||||
Done Inline Actionsnot sure I get the "not covered?" part here douardda: not sure I get the "not covered?" part here | |||||
Not Done Inline Actionsthe jenkins coverage report on the right (once jenkins build is done) for the else block is orange (so it's not covered). ardumont: the jenkins coverage report on the right (once jenkins build is done) for the else block is… | |||||
"""Build expected path from api to served file | """Build expected path from api to served file | ||||
""" | """ | ||||
url = urlparse(base_url) | url = urlparse(base_url) | ||||
dirname = "%s_%s" % (url.scheme, url.hostname) | dirname = "%s_%s" % (url.scheme, url.hostname) | ||||
if api_url.endswith("/"): | if api_url.endswith("/"): | ||||
api_url = api_url[:-1] | api_url = api_url[:-1] | ||||
if api_url.startswith("/"): | if api_url.startswith("/"): | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | def test_check_fails(requests_mock_datadir): | ||||
with pytest.raises(ValueError, match="Problem when checking deposit"): | with pytest.raises(ValueError, match="Problem when checking deposit"): | ||||
client.check(unknown_api_url) | client.check(unknown_api_url) | ||||
# private api update status | # private api update status | ||||
class FakeRequestClientPut: | def test_status_update(mocker): | ||||
"""Fake Request client dedicated to put request method calls. | |||||
""" | |||||
args = None | |||||
kwargs = None | |||||
def put(self, *args, **kwargs): | |||||
self.args = args | |||||
self.kwargs = kwargs | |||||
class PrivateApiDepositClientStatusUpdateTest(unittest.TestCase): | |||||
def test_status_update(self): | |||||
"""Update status | """Update status | ||||
""" | """ | ||||
_client = FakeRequestClientPut() | mocked_put = mocker.patch("swh.deposit.client.requests.put") | ||||
deposit_client = PrivateApiDepositClient( | |||||
config=CLIENT_TEST_CONFIG, _client=_client | |||||
) | |||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG) | |||||
deposit_client.status_update( | deposit_client.status_update( | ||||
"/update/status", | "/update/status", DEPOSIT_STATUS_LOAD_SUCCESS, revision_id="some-revision-id", | ||||
DEPOSIT_STATUS_LOAD_SUCCESS, | |||||
revision_id="some-revision-id", | |||||
) | ) | ||||
self.assertEqual(_client.args, ("https://nowhere.org/update/status",)) | mocked_put.assert_called_once_with( | ||||
self.assertEqual( | "https://nowhere.org/update/status", | ||||
_client.kwargs, | json={ | ||||
{ | |||||
"json": { | |||||
"status": DEPOSIT_STATUS_LOAD_SUCCESS, | "status": DEPOSIT_STATUS_LOAD_SUCCESS, | ||||
"revision_id": "some-revision-id", | "revision_id": "some-revision-id", | ||||
} | |||||
}, | }, | ||||
) | ) | ||||
def test_status_update_with_no_revision_id(self): | |||||
def test_status_update_with_no_revision_id(mocker): | |||||
"""Reading metadata can fail for some reasons | """Reading metadata can fail for some reasons | ||||
""" | """ | ||||
_client = FakeRequestClientPut() | mocked_put = mocker.patch("swh.deposit.client.requests.put") | ||||
deposit_client = PrivateApiDepositClient( | |||||
config=CLIENT_TEST_CONFIG, _client=_client | |||||
) | |||||
deposit_client = PrivateApiDepositClient(config=CLIENT_TEST_CONFIG) | |||||
deposit_client.status_update("/update/status/fail", DEPOSIT_STATUS_LOAD_FAILURE) | deposit_client.status_update("/update/status/fail", DEPOSIT_STATUS_LOAD_FAILURE) | ||||
self.assertEqual(_client.args, ("https://nowhere.org/update/status/fail",)) | mocked_put.assert_called_once_with( | ||||
self.assertEqual( | "https://nowhere.org/update/status/fail", | ||||
_client.kwargs, {"json": {"status": DEPOSIT_STATUS_LOAD_FAILURE,}} | json={"status": DEPOSIT_STATUS_LOAD_FAILURE,}, | ||||
) | ) |
not covered?
also, "passzord"