Spaces:
Running on Zero

File size: 3,818 Bytes
51e9717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The HTML event boundary must never accept arbitrary server files."""
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from tiger_ui import source
from tiger_ui.source import resolve_source, settings
from tiger_ui.media import MediaError


class SourceTests(unittest.TestCase):
    def test_only_actual_upload_folder_files_are_accepted(self):
        with tempfile.TemporaryDirectory() as temp:
            root=Path(temp);uploads=root/'uploads';uploads.mkdir()
            valid=uploads/'audio.wav';valid.write_bytes(b'RIFF')
            outside=root/'private.txt';outside.write_text('not an upload')
            alias=uploads/'alias.wav';alias.symlink_to(outside)
            with patch('tiger_ui.source.get_upload_folder',return_value=str(uploads)):
                self.assertEqual(resolve_source({'path':str(valid)}),str(valid))
                for path in [outside,alias,uploads/'../private.txt','https://example.org/audio.wav']:
                    with self.assertRaises(MediaError):resolve_source({'path':str(path)})
                with self.assertRaises(MediaError):resolve_source({'origin':'example','key':'../../private.txt'})

    def test_settings_validate_both_enums(self):
        for field in ['task','media']:
            invalid={'task':'stems','media':'audio',field:'anything'}
            with self.assertRaises(MediaError):settings(invalid)

    def test_local_example_does_not_need_network(self):
        with tempfile.TemporaryDirectory() as temp:
            audio=Path(temp)/'audio.wav';audio.write_bytes(b'RIFF audio')
            with patch.dict(source.EXAMPLES, {'stems':audio}), patch.object(source,'hf_hub_download') as download:
                self.assertEqual(source.example_path('stems'),audio)
                download.assert_not_called()

    def test_missing_examples_use_pinned_history_and_reuse_cache(self):
        with tempfile.TemporaryDirectory() as temp:
            root=Path(temp)
            def download(**kwargs):
                self.assertEqual(kwargs['repo_id'],source.EXAMPLE_REPO)
                self.assertEqual(kwargs['revision'],source.EXAMPLE_REVISION)
                self.assertFalse(kwargs['token'])
                path=Path(kwargs['local_dir'])/kwargs['filename']
                path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(b'RIFF audio')
                return str(path)
            missing={key:root/'missing'/name for key,name in source.EXAMPLE_FILES.items()}
            with patch.dict(source.EXAMPLES,missing), patch.object(source,'get_upload_folder',return_value=str(root/'uploads')), patch.object(source,'hf_hub_download',side_effect=download) as fetch:
                for key in source.EXAMPLE_FILES:
                    first=source.example_path(key)
                    self.assertTrue(first.is_relative_to(root/'uploads'))
                    self.assertEqual(resolve_source({'origin':'example','key':key}),str(first))
                self.assertEqual(fetch.call_count,3)
                with self.assertRaises(MediaError):source.example_path('../../private.txt')
                self.assertEqual(fetch.call_count,3)

    def test_git_lfs_pointer_is_not_used_as_audio(self):
        with tempfile.TemporaryDirectory() as temp:
            root=Path(temp);pointer=root/'pointer.wav';pointer.write_text('version https://git-lfs.github.com/spec/v1\n')
            real=root/'real.wav';real.write_bytes(b'RIFF audio')
            with patch.dict(source.EXAMPLES,{'stems':pointer}), patch.object(source,'get_upload_folder',return_value=str(root/'uploads')), patch.object(source,'hf_hub_download',return_value=str(real)) as fetch:
                self.assertEqual(source.example_path('stems'),real)
                fetch.assert_called_once()


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