Changeset View
Changeset View
Standalone View
Standalone View
swh/core/github/tests/test_github_utils.py
Show All 26 Lines | [ | ||||
("user/repo/", KNOWN_GH_REPO), | ("user/repo/", KNOWN_GH_REPO), | ||||
("user/repo", KNOWN_GH_REPO), | ("user/repo", KNOWN_GH_REPO), | ||||
("user/repo/.git", KNOWN_GH_REPO), | ("user/repo/.git", KNOWN_GH_REPO), | ||||
# edge cases | # edge cases | ||||
("https://github.com/unknown-page", None), # unknown gh origin returns None | ("https://github.com/unknown-page", None), # unknown gh origin returns None | ||||
("user/repo/with/some/deps", None), # url kind is not dealt with for now | ("user/repo/with/some/deps", None), # url kind is not dealt with for now | ||||
], | ], | ||||
) | ) | ||||
def test_get_canonical_github_origin_url(user_repo, expected_url, requests_mock): | def test_get_canonical_github_origin_url( | ||||
user_repo, expected_url, requests_mock, github_credentials | |||||
): | |||||
"""It should return a canonical github origin when it exists, None otherwise""" | """It should return a canonical github origin when it exists, None otherwise""" | ||||
html_url = _url_github_html(user_repo) | html_url = _url_github_html(user_repo) | ||||
api_url = _url_github_api(_sanitize_github_url(user_repo)) | api_url = _url_github_api(_sanitize_github_url(user_repo)) | ||||
if expected_url is not None: | if expected_url is not None: | ||||
status_code = 200 | status_code = 200 | ||||
response = {"html_url": _sanitize_github_url(html_url)} | response = {"html_url": _sanitize_github_url(html_url)} | ||||
else: | else: | ||||
status_code = 404 | status_code = 404 | ||||
response = {} | response = {} | ||||
requests_mock.get(api_url, [{"status_code": status_code, "json": response}]) | requests_mock.get(api_url, [{"status_code": status_code, "json": response}]) | ||||
# anonymous | |||||
assert get_canonical_github_origin_url(html_url) == expected_url | assert get_canonical_github_origin_url(html_url) == expected_url | ||||
# with credentials | |||||
assert ( | |||||
get_canonical_github_origin_url(html_url, credentials=github_credentials) | |||||
== expected_url | |||||
) | |||||
# anonymous | |||||
assert ( | |||||
GitHubSession( | |||||
user_agent="GitHub Session Test", | |||||
).get_canonical_url(html_url) | |||||
== expected_url | |||||
) | |||||
# with credentials | |||||
assert ( | |||||
GitHubSession( | |||||
user_agent="GitHub Session Test", credentials=github_credentials | |||||
).get_canonical_url(html_url) | |||||
== expected_url | |||||
) | |||||
def test_get_canonical_github_origin_url_not_gh_origin(): | def test_get_canonical_github_origin_url_not_gh_origin(): | ||||
"""It should return the input url when that origin is not a github one""" | """It should return the input url when that origin is not a github one""" | ||||
url = "https://example.org" | url = "https://example.org" | ||||
assert get_canonical_github_origin_url(url) == url | assert get_canonical_github_origin_url(url) == url | ||||
assert ( | |||||
GitHubSession( | |||||
user_agent="GitHub Session Test", | |||||
).get_canonical_url(url) | |||||
== url | |||||
) | |||||
def test_github_session_anonymous_session(): | def test_github_session_anonymous_session(): | ||||
user_agent = ("GitHub Session Test",) | user_agent = ("GitHub Session Test",) | ||||
github_session = GitHubSession( | github_session = GitHubSession( | ||||
user_agent=user_agent, | user_agent=user_agent, | ||||
) | ) | ||||
assert github_session.anonymous is True | assert github_session.anonymous is True | ||||
▲ Show 20 Lines • Show All 96 Lines • Show Last 20 Lines |