blob: da316e2814d625e1984656623963bc6b85562e38 [file] [log] [blame]
James Kuszmaulb1091162023-12-27 16:16:45 -08001import unittest
2import unittest.mock
3from tools.rehosting import rehost
4from pathlib import Path
5import os
6
7
8class TestRehost(unittest.TestCase):
9
10 def test_url_validation(self):
11 self.assertEqual("http://google.com",
12 rehost.validate_url("http://google.com"))
13 self.assertRaisesRegex(Exception, "Invalid URL", rehost.validate_url,
14 "file:///some/secret")
15 self.assertRaisesRegex(Exception, "Invalid URL", rehost.validate_url,
16 "http://10.0.0.0/secret")
17
18 def test_url_to_path(self):
19 test_dir = os.getenv("TEST_TMPDIR", "/tmp/")
20 with unittest.mock.patch.object(rehost, "BUILD_DEPENDENCIES_PATH",
21 test_dir):
22 existing_file = test_dir + "/exists.com"
23 with open(existing_file, 'w') as f:
24 f.write('string')
25 self.assertEqual(
26 Path(test_dir) / "example.com/foo/bar",
27 rehost.url_to_path("https://example.com/foo/bar"))
28 self.assertRaisesRegex(ValueError,
29 f"not in the subpath of '{test_dir}'",
30 rehost.url_to_path,
31 "https://example.com/../../bar")
32 self.assertRaisesRegex(FileExistsError, "There is already a file",
33 rehost.url_to_path, "https://exists.com")
34
35
36if __name__ == "__main__":
37 unittest.main()