| Total Complexity | 9 |
| Total Lines | 50 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import tempfile |
||
| 2 | import filecmp |
||
| 3 | import contextlib |
||
| 4 | import pathlib |
||
| 5 | import sys |
||
| 6 | import subprocess |
||
| 7 | import time |
||
| 8 | |||
| 9 | from org_fedora_oscap import data_fetch |
||
| 10 | |||
| 11 | |||
| 12 | PORT = 8001 |
||
| 13 | |||
| 14 | |||
| 15 | @contextlib.contextmanager |
||
| 16 | def serve_directory_in_separate_process(port): |
||
| 17 | args = [sys.executable, "-m", "http.server", str(port)] |
||
| 18 | proc = subprocess.Popen( |
||
| 19 | args, |
||
| 20 | stdout=subprocess.DEVNULL, |
||
| 21 | stderr=subprocess.DEVNULL) |
||
| 22 | # give the server some time to start |
||
| 23 | time.sleep(0.4) |
||
| 24 | yield |
||
| 25 | proc.terminate() |
||
| 26 | proc.wait() |
||
| 27 | |||
| 28 | |||
| 29 | def test_file_retreival(): |
||
| 30 | filename_to_test = pathlib.Path(__file__) |
||
| 31 | relative_filename_to_test = filename_to_test.relative_to(pathlib.Path.cwd()) |
||
| 32 | |||
| 33 | temp_file = tempfile.NamedTemporaryFile() |
||
| 34 | temp_filename = temp_file.name |
||
| 35 | |||
| 36 | with serve_directory_in_separate_process(PORT): |
||
| 37 | data_fetch._curl_fetch( |
||
| 38 | "http://localhost:{}/{}".format(PORT, relative_filename_to_test), temp_filename) |
||
| 39 | |||
| 40 | assert filecmp.cmp(relative_filename_to_test, temp_filename) |
||
| 41 | |||
| 42 | |||
| 43 | def test_supported_url(): |
||
| 44 | assert data_fetch.can_fetch_from("http://example.com") |
||
| 45 | assert data_fetch.can_fetch_from("https://example.com") |
||
| 46 | |||
| 47 | |||
| 48 | def test_unsupported_url(): |
||
| 49 | assert not data_fetch.can_fetch_from("aaaaa") |
||
| 50 |