2020
2121import pytest
2222
23+ import requests
24+ from huggingface_hub import HfApi
2325from huggingface_hub .constants import (
2426 CONFIG_NAME ,
2527 PYTORCH_WEIGHTS_NAME ,
4547)
4648from tests .testing_constants import TOKEN
4749
50+ from .testing_constants import ENDPOINT_STAGING , OTHER_TOKEN
4851from .testing_utils import (
4952 DUMMY_MODEL_ID ,
5053 DUMMY_MODEL_ID_PINNED_SHA1 ,
5659 SAMPLE_DATASET_IDENTIFIER ,
5760 OfflineSimulationMode ,
5861 offline ,
62+ repo_name ,
5963 with_production_testing ,
6064 xfail_on_windows ,
6165)
@@ -436,15 +440,28 @@ def test_download_from_a_gated_repo_with_hf_hub_download(self):
436440 Regression test for #1121.
437441 https://github.com/huggingface/huggingface_hub/pull/1121
438442 """
443+ # Create a gated repo on the fly. Repo is created by "other user" so that the
444+ # usual CI user don't have access to it.
445+ api = HfApi (token = OTHER_TOKEN )
446+ repo_url = api .create_repo (
447+ repo_id = "gated_repo_for_huggingface_hub_ci" , exist_ok = True
448+ )
449+ requests .put (
450+ f"{ repo_url .endpoint } /api/models/{ repo_url .repo_id } /settings" ,
451+ headers = api ._build_hf_headers (),
452+ json = {"gated" : True },
453+ ).raise_for_status ()
454+
455+ # Cannot download file as repo is gated
439456 with SoftTemporaryDirectory () as tmpdir :
440457 with self .assertRaisesRegex (
441458 GatedRepoError ,
442459 "Access to model .* is restricted and you are not in the authorized"
443460 " list" ,
444461 ):
445462 hf_hub_download (
446- repo_id = "datasets_server_org/gated_repo_for_huggingface_hub_ci" ,
447- filename = "config.json " ,
463+ repo_id = repo_url . repo_id ,
464+ filename = ".gitattributes " ,
448465 use_auth_token = TOKEN ,
449466 cache_dir = tmpdir ,
450467 )
@@ -460,33 +477,48 @@ class StagingCachedDownloadOnAwfulFilenamesTest(unittest.TestCase):
460477 """
461478
462479 cache_dir : Path
463- repo_id = "valid_org/repo_with_awful_filename"
464480 subfolder = "subfolder/to?"
465481 filename = "awful?filename%you:should,never.give"
466- filepath = "subfolder/to?/awful?filename%you:should,never.give"
467- expected_url = "https://hub-ci.huggingface.co/valid_org/repo_with_awful_filename/resolve/main/subfolder/to%3F/awful%3Ffilename%25you%3Ashould%2Cnever.give"
482+ filepath = f"subfolder/to?/{ filename } "
483+
484+ @classmethod
485+ def setUpClass (cls ):
486+ cls .api = HfApi (endpoint = ENDPOINT_STAGING , token = TOKEN )
487+ cls .repo_url = cls .api .create_repo (repo_id = repo_name ("awful_filename" ))
488+ cls .expected_resolve_url = f"{ cls .repo_url } /resolve/main/subfolder/to%3F/awful%3Ffilename%25you%3Ashould%2Cnever.give"
489+ cls .api .upload_file (
490+ path_or_fileobj = b"content" ,
491+ path_in_repo = cls .filepath ,
492+ repo_id = cls .repo_url .repo_id ,
493+ )
494+
495+ @classmethod
496+ def tearDownClass (cls ) -> None :
497+ cls .api .delete_repo (repo_id = cls .repo_url .repo_id )
468498
469499 def test_hf_hub_url_on_awful_filepath (self ):
470- self .assertEqual (hf_hub_url (self .repo_id , self .filepath ), self .expected_url )
500+ self .assertEqual (
501+ hf_hub_url (self .repo_url .repo_id , self .filepath ), self .expected_resolve_url
502+ )
471503
472504 def test_hf_hub_url_on_awful_subfolder_and_filename (self ):
473505 self .assertEqual (
474- hf_hub_url (self .repo_id , self .filename , subfolder = self .subfolder ),
475- self .expected_url ,
506+ hf_hub_url (self .repo_url . repo_id , self .filename , subfolder = self .subfolder ),
507+ self .expected_resolve_url ,
476508 )
477509
478510 @xfail_on_windows (reason = "Windows paths cannot contain a '?'." )
479511 def test_hf_hub_download_on_awful_filepath (self ):
480512 local_path = hf_hub_download (
481- self .repo_id , self .filepath , cache_dir = self .cache_dir
513+ self .repo_url . repo_id , self .filepath , cache_dir = self .cache_dir
482514 )
483515 # Local path is not url-encoded
484516 self .assertTrue (local_path .endswith (self .filepath ))
485517
486518 @xfail_on_windows (reason = "Windows paths cannot contain a '?'." )
487519 def test_hf_hub_download_on_awful_subfolder_and_filename (self ):
488520 local_path = hf_hub_download (
489- self .repo_id ,
521+ self .repo_url . repo_id ,
490522 self .filename ,
491523 subfolder = self .subfolder ,
492524 cache_dir = self .cache_dir ,
0 commit comments