File size: 4,429 Bytes
33e9f46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Offline tests for the uploader. No credentials or remote calls are used."""
from contextlib import redirect_stdout
import hashlib
import importlib.util
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import Mock, patch


ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("release_upload", ROOT / "upload_to_huggingface.py")
uploader = importlib.util.module_from_spec(spec)
spec.loader.exec_module(uploader)


class UploadTests(unittest.TestCase):
    def test_canonical_username_case_is_preserved(self):
        self.assertEqual(uploader.canonical_repo_id("pureone/AELYRAITH-III", {"name": "PureOne"}), "PureOne/AELYRAITH-III")

    def test_canonical_organization_case_is_preserved(self):
        identity = {"name": "SomeUser", "orgs": [{"name": "ResearchLab"}]}
        self.assertEqual(uploader.canonical_repo_id("researchlab/Release", identity), "ResearchLab/Release")

    def test_other_user_is_not_silently_selected(self):
        with self.assertRaises(uploader.UploadError):
            uploader.canonical_repo_id("PureOne/AELYRAITH-III", {"name": "AnotherUser"})

    def test_manifest_hash_and_allowlist(self):
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            content = b"release evidence\n"
            (root / "README.md").write_bytes(content)
            (root / "private_token.txt").write_text("must never be uploaded", encoding="utf-8")
            record = {"bytes": len(content), "sha256": hashlib.sha256(content).hexdigest()}
            (root / uploader.MANIFEST_NAME).write_text(json.dumps({"files": {"README.md": record}}), encoding="utf-8")
            self.assertEqual(uploader.release_files(root), ["README.md", "release_manifest.json"])
            (root / "README.md").write_text("modified", encoding="utf-8")
            with self.assertRaises(uploader.UploadError):
                uploader.release_files(root)

    def test_manifest_rejects_globs_and_parent_traversal(self):
        with tempfile.TemporaryDirectory() as temporary:
            root = Path(temporary)
            for name in ("../secret", "*.txt", "file[1].md", "folder\\secret.txt"):
                with self.subTest(name=name):
                    (root / uploader.MANIFEST_NAME).write_text(json.dumps({"files": {name: {}}}), encoding="utf-8")
                    with self.assertRaises(uploader.UploadError):
                        uploader.release_files(root)

    def test_actual_upload_is_dataset_only_and_uses_exact_allowlist(self):
        api = Mock()
        api.whoami.return_value = {"name": "PureOne"}
        paths = ["README.md", "results/data.csv", "release_manifest.json"]
        with redirect_stdout(io.StringIO()):
            uploader.upload(api, Path("/release"), "pureone/AELYRAITH-III", paths, private=True)
        api.create_repo.assert_called_once_with(repo_id="PureOne/AELYRAITH-III", repo_type="dataset", private=True, exist_ok=True)
        kwargs = api.upload_folder.call_args.kwargs
        self.assertEqual(kwargs["repo_type"], "dataset")
        self.assertEqual(kwargs["repo_id"], "PureOne/AELYRAITH-III")
        self.assertEqual(kwargs["allow_patterns"], paths)
        self.assertNotIn("delete_patterns", kwargs)
        api.update_repo_settings.assert_not_called()

    def test_read_only_token_is_rejected_before_mutation(self):
        api = Mock()
        api.whoami.return_value = {"name": "PureOne", "auth": {"accessToken": {"role": "read"}}}
        with self.assertRaises(uploader.UploadError):
            uploader.upload(api, Path("/release"), "PureOne/AELYRAITH-III", ["README.md"])
        api.create_repo.assert_not_called()
        api.upload_folder.assert_not_called()

    def test_dry_run_never_imports_client_and_runs_verifier_first(self):
        with patch.object(uploader.subprocess, "run") as verify, patch.object(uploader, "release_files", return_value=["README.md", "release_manifest.json"]) as files:
            with redirect_stdout(io.StringIO()) as output:
                self.assertEqual(uploader.main(["--dry-run"]), 0)
            self.assertTrue(verify.call_args.kwargs["check"])
            self.assertIn("verify_release.py", verify.call_args.args[0][1])
            files.assert_called_once()
            self.assertIn("no network requests", output.getvalue())


if __name__ == "__main__":
    unittest.main()