Changeset View
Changeset View
Standalone View
Standalone View
swh/web/client/tests/conftest.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 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 pytest | import pytest | ||||
from .api_data import API_URL, API_DATA | from .api_data import API_URL, API_DATA | ||||
from swh.web.client import WebAPIClient | from swh.web.client.client import WebAPIClient | ||||
@pytest.fixture | @pytest.fixture | ||||
def web_api_mock(requests_mock): | def web_api_mock(requests_mock): | ||||
# monkey patch URLs that require a special response headers | # monkey patch URLs that require a special response headers | ||||
for api_call, data in API_DATA.items(): | for api_call, data in API_DATA.items(): | ||||
headers = {} | headers = {} | ||||
if api_call == "snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/": | if api_call == "snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/": | ||||
# to make the client init and follow pagination | # to make the client init and follow pagination | ||||
headers = { | headers = { | ||||
"Link": f'<{API_URL}/{api_call}?branches_count=1000&branches_from=refs/tags/v3.0-rc7>; rel="next"' # NoQA: E501 | "Link": f'<{API_URL}/{api_call}?branches_count=1000&branches_from=refs/tags/v3.0-rc7>; rel="next"' # NoQA: E501 | ||||
} | } | ||||
elif api_call == "origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=50&per_page=10": # NoQA: E501 | elif ( | ||||
api_call | |||||
== "origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=50&per_page=10" # NoQA: E501 | |||||
): | |||||
# to make the client follow pagination | # to make the client follow pagination | ||||
headers = { | headers = { | ||||
"Link": | "Link": f'<{API_URL}/origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=40&per_page=10>; rel="next"' # NoQA: E501 | ||||
f"<{API_URL}/origin/https://github.com/NixOS/nixpkgs/visits/?last_visit=40&per_page=10>; rel=\"next\"" # NoQA: E501 | |||||
} | } | ||||
requests_mock.get(f"{API_URL}/{api_call}", text=data, headers=headers) | requests_mock.get(f"{API_URL}/{api_call}", text=data, headers=headers) | ||||
return requests_mock | return requests_mock | ||||
@pytest.fixture | @pytest.fixture | ||||
def web_api_client(): | def web_api_client(): | ||||
# use the fake base API URL that matches API data | # use the fake base API URL that matches API data | ||||
return WebAPIClient(api_url=API_URL) | return WebAPIClient(api_url=API_URL) |