James Kuszmaul | b109116 | 2023-12-27 16:16:45 -0800 | [diff] [blame^] | 1 | import unittest |
| 2 | import unittest.mock |
| 3 | from tools.rehosting import rehost |
| 4 | from pathlib import Path |
| 5 | import os |
| 6 | |
| 7 | |
| 8 | class 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 | |
| 36 | if __name__ == "__main__": |
| 37 | unittest.main() |