Skip to content

Commit

Permalink
Fix LFS tests after password auth deprecation (#1713)
Browse files Browse the repository at this point in the history
* fix LFS tests after password auth deprecation

* fix import
  • Loading branch information
Wauplin authored Oct 5, 2023
1 parent d4f6ac0 commit 90c1182
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
9 changes: 5 additions & 4 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,7 +81,6 @@

from .testing_constants import (
ENDPOINT_STAGING,
ENDPOINT_STAGING_BASIC_AUTH,
FULL_NAME,
OTHER_TOKEN,
OTHER_USER,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 12 additions & 17 deletions tests/test_repocard.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from pathlib import Path

import pytest
import requests
import yaml

from huggingface_hub import (
Expand All @@ -30,6 +29,8 @@
RepoCard,
SpaceCard,
SpaceCardData,
get_hf_file_metadata,
hf_hub_url,
metadata_eval_result,
metadata_load,
metadata_save,
Expand All @@ -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,
)
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
1 change: 0 additions & 1 deletion tests/testing_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down

0 comments on commit 90c1182

Please sign in to comment.