diff --git a/swh/web/client/tests/conftest.py b/swh/web/client/tests/conftest.py index e988487..4d918b6 100644 --- a/swh/web/client/tests/conftest.py +++ b/swh/web/client/tests/conftest.py @@ -1,26 +1,30 @@ +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information import pytest from .api_data import API_URL, API_DATA from swh.web.client import WebAPIClient @pytest.fixture def web_api_mock(requests_mock): for api_call, data in API_DATA.items(): headers = {} if api_call == "snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/": # monkey patch the only URL that require a special response headers # (to make the client insit and follow pagination) headers = { "Link": f"<{API_URL}/{api_call}?branches_count=1000&branches_from=refs/tags/v3.0-rc7>; rel=\"next\"" # NoQA: E501 } print("XXX", headers) requests_mock.get(f"{API_URL}/{api_call}", text=data, headers=headers) @pytest.fixture def web_api_client(): # use the fake base API URL that matches API data return WebAPIClient(api_url=API_URL) diff --git a/swh/web/client/tests/gen-api-data.sh b/swh/web/client/tests/gen-api-data.sh index 70337bd..9245f26 100755 --- a/swh/web/client/tests/gen-api-data.sh +++ b/swh/web/client/tests/gen-api-data.sh @@ -1,29 +1,34 @@ #!/bin/bash +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + # Depends: curl, jq API_URL_real="https://archive.softwareheritage.org/api/1" API_URL_test="https://invalid-test-only.archive.softwareheritage.org/api/1" urls="" urls="${urls} content/sha1_git:fe95a46679d128ff167b7c55df5d02356c5a1ae1/" urls="${urls} directory/977fc4b98c0e85816348cebd3b12026407c368b6/" urls="${urls} release/b9db10d00835e9a43e2eebef2db1d04d4ae82342/" urls="${urls} revision/aafb16d69fd30ff58afdd69036a26047f3aebdc6/" urls="${urls} snapshot/6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a/" urls="${urls} snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/" urls="${urls} snapshot/cabcc7d7bf639bbe1cc3b41989e1806618dd5764/?branches_count=1000&branches_from=refs/tags/v3.0-rc7" echo "# GENERATED FILE, DO NOT EDIT." echo "# Run './gen-api-data.sh > api_data.py' instead." echo echo "API_URL = \"${API_URL_test}\"" echo echo 'API_DATA = {' for url in ${urls} ; do echo " \"${url}\": # NoQA: E501" echo ' r"""' curl --silent "${API_URL_real}/${url}" | jq --monochrome-output . echo ' """, # NoQA: E501' done echo '}' diff --git a/swh/web/client/tests/test_web_api_client.py b/swh/web/client/tests/test_web_api_client.py index eba3fa6..f85d7b2 100644 --- a/swh/web/client/tests/test_web_api_client.py +++ b/swh/web/client/tests/test_web_api_client.py @@ -1,91 +1,96 @@ +# Copyright (C) 2020 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + from dateutil.parser import parse as parse_date from swh.model.identifiers import parse_persistent_identifier as parse_pid def test_get_content(web_api_client, web_api_mock): pid = parse_pid("swh:1:cnt:fe95a46679d128ff167b7c55df5d02356c5a1ae1") obj = web_api_client.get(pid) assert obj["length"] == 151810 for key in ("length", "status", "checksums", "data_url"): assert key in obj assert obj["checksums"]["sha1_git"] == str(pid).split(":")[3] assert obj["checksums"]["sha1"] == \ "dc2830a9e72f23c1dfebef4413003221baa5fb62" def test_get_directory(web_api_client, web_api_mock): pid = parse_pid("swh:1:dir:977fc4b98c0e85816348cebd3b12026407c368b6") obj = web_api_client.get(pid) assert len(obj) == 35 # number of directory entries assert all(map(lambda entry: entry["dir_id"] == pid, obj)) dir_entry = obj[0] assert dir_entry["type"] == "file" assert dir_entry["target"] == parse_pid( "swh:1:cnt:58471109208922c9ee8c4b06135725f03ed16814") assert dir_entry["name"] == ".bzrignore" assert dir_entry["length"] == 582 def test_get_release(web_api_client, web_api_mock): pid = parse_pid("swh:1:rel:b9db10d00835e9a43e2eebef2db1d04d4ae82342") obj = web_api_client.get(pid) assert obj["id"] == pid assert obj["author"]["fullname"] == "Paul Tagliamonte " assert obj["author"]["name"] == "Paul Tagliamonte" assert obj["date"] == parse_date("2013-07-06T19:34:11-04:00") assert obj["name"] == "0.9.9" assert obj["target_type"] == "revision" assert obj["target"] == parse_pid( "swh:1:rev:e005cb773c769436709ca6a1d625dc784dbc1636") assert not obj["synthetic"] def test_get_revision(web_api_client, web_api_mock): pid = parse_pid("swh:1:rev:aafb16d69fd30ff58afdd69036a26047f3aebdc6") obj = web_api_client.get(pid) assert obj["id"] == pid for role in ("author", "committer"): assert obj[role]["fullname"] == \ "Nicolas Dandrimont " assert obj[role]["name"] == "Nicolas Dandrimont" timestamp = parse_date("2014-08-18T18:18:25+02:00") assert obj["date"] == timestamp assert obj["committer_date"] == timestamp assert obj["message"].startswith("Merge branch") assert obj["merge"] assert len(obj["parents"]) == 2 assert obj["parents"][0]["id"] == parse_pid( "swh:1:rev:26307d261279861c2d9c9eca3bb38519f951bea4") assert obj["parents"][1]["id"] == parse_pid( "swh:1:rev:37fc9e08d0c4b71807a4f1ecb06112e78d91c283") def test_get_snapshot(web_api_client, web_api_mock): # small snapshot, the one from Web API doc pid = parse_pid("swh:1:snp:6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a") obj = web_api_client.get(pid) assert len(obj) == 4 assert obj["refs/heads/master"]["target_type"] == "revision" assert obj["refs/heads/master"]["target"] == parse_pid( "swh:1:rev:83c20a6a63a7ebc1a549d367bc07a61b926cecf3") assert obj["refs/tags/dpkt-1.7"]["target_type"] == "revision" assert obj["refs/tags/dpkt-1.7"]["target"] == parse_pid( "swh:1:rev:0c9dbfbc0974ec8ac1d8253aa1092366a03633a8") def test_iter_snapshot(web_api_client, web_api_mock): # large snapshot from the Linux kernel, usually spanning two pages pid = parse_pid("swh:1:snp:cabcc7d7bf639bbe1cc3b41989e1806618dd5764") obj = web_api_client.snapshot(pid) snp = {} for partial in obj: snp.update(partial) assert len(snp) == 1391