diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index 1dfed8b6c9..10fa9fa5b9 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -27,7 +27,7 @@ from pathlib import Path from typing import Any, Dict, List, Optional, Union from unittest.mock import Mock, patch -from urllib.parse import quote +from urllib.parse import quote, urlparse import pytest import requests @@ -81,7 +81,6 @@ from .testing_constants import ( ENDPOINT_STAGING, - ENDPOINT_STAGING_BASIC_AUTH, FULL_NAME, OTHER_TOKEN, OTHER_USER, @@ -2132,9 +2131,11 @@ def tearDown(self): self._api.delete_repo(repo_id=self.repo_id) def setup_local_clone(self) -> None: - REMOTE_URL_AUTH = self.repo_url.replace(ENDPOINT_STAGING, ENDPOINT_STAGING_BASIC_AUTH) + scheme = urlparse(self.repo_url).scheme + repo_url_auth = self.repo_url.replace(f"{scheme}://", f"{scheme}://user:{TOKEN}@") + subprocess.run( - ["git", "clone", REMOTE_URL_AUTH, str(self.cache_dir)], + ["git", "clone", repo_url_auth, str(self.cache_dir)], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, diff --git a/tests/test_repocard.py b/tests/test_repocard.py index a4255012b2..454d3df3ee 100644 --- a/tests/test_repocard.py +++ b/tests/test_repocard.py @@ -18,7 +18,6 @@ from pathlib import Path import pytest -import requests import yaml from huggingface_hub import ( @@ -30,6 +29,8 @@ RepoCard, SpaceCard, SpaceCardData, + get_hf_file_metadata, + hf_hub_url, metadata_eval_result, metadata_load, metadata_save, @@ -40,11 +41,10 @@ from huggingface_hub.hf_api import HfApi from huggingface_hub.repocard import REGEX_YAML_BLOCK from huggingface_hub.repocard_data import CardData -from huggingface_hub.utils import SoftTemporaryDirectory, is_jinja_available, logging +from huggingface_hub.utils import EntryNotFoundError, SoftTemporaryDirectory, is_jinja_available, logging from .testing_constants import ( ENDPOINT_STAGING, - ENDPOINT_STAGING_BASIC_AUTH, TOKEN, USER, ) @@ -653,19 +653,16 @@ def test_push_to_hub(self): content = f"---\n{card_data.to_yaml()}\n---\n\n# MyModel\n\nHello, world!" card = RepoCard(content) - url = f"{ENDPOINT_STAGING_BASIC_AUTH}/{repo_id}/resolve/main/README.md" - # Check this file doesn't exist (sanity check) - with pytest.raises(requests.exceptions.HTTPError): - r = requests.get(url) - r.raise_for_status() + readme_url = hf_hub_url(repo_id, "README.md") + with self.assertRaises(EntryNotFoundError): + get_hf_file_metadata(readme_url) # Push the card up to README.md in the repo card.push_to_hub(repo_id, token=TOKEN) # No error should occur now, as README.md should exist - r = requests.get(url) - r.raise_for_status() + get_hf_file_metadata(readme_url) self._api.delete_repo(repo_id=repo_id) @@ -684,14 +681,12 @@ def test_push_and_create_pr(self): content = f"---\n{card_data.to_yaml()}\n---\n\n# MyModel\n\nHello, world!" card = RepoCard(content) - url = f"{ENDPOINT_STAGING_BASIC_AUTH}/api/models/{repo_id}/discussions" - r = requests.get(url) - data = r.json() - self.assertEqual(data["count"], 0) + discussions = list(self._api.get_repo_discussions(repo_id)) + self.assertEqual(len(discussions), 0) + card.push_to_hub(repo_id, token=TOKEN, create_pr=True) - r = requests.get(url) - data = r.json() - self.assertEqual(data["count"], 1) + discussions = list(self._api.get_repo_discussions(repo_id)) + self.assertEqual(len(discussions), 1) self._api.delete_repo(repo_id=repo_id) diff --git a/tests/testing_constants.py b/tests/testing_constants.py index fd7a258263..487aaeeac6 100644 --- a/tests/testing_constants.py +++ b/tests/testing_constants.py @@ -15,7 +15,6 @@ ENDPOINT_PRODUCTION = "https://huggingface.co" ENDPOINT_STAGING = "https://hub-ci.huggingface.co" -ENDPOINT_STAGING_BASIC_AUTH = f"https://{USER}:{PASS}@hub-ci.huggingface.co" ENDPOINT_PRODUCTION_URL_SCHEME = ENDPOINT_PRODUCTION + "/{repo_id}/resolve/{revision}/{filename}"