From 2510fde80fa051c7cbaf9dee9e6095a7ecc5eb54 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 4 Jun 2018 10:57:16 +1200 Subject: [PATCH] Add unit test for sanitized download filenames. I think this covers the right path despite not using either of the backend specific download code. They both either call `set_target()` directly with a guessed filename or from the prompt question. When called directly though that basename set by `_init_item()` is used. It's a little high level but we already know that sanitize_filenames does, I wanted to test that it gets called. --- tests/unit/browser/webkit/test_downloads.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/unit/browser/webkit/test_downloads.py b/tests/unit/browser/webkit/test_downloads.py index 571e21704..f14c417f9 100644 --- a/tests/unit/browser/webkit/test_downloads.py +++ b/tests/unit/browser/webkit/test_downloads.py @@ -96,3 +96,24 @@ class TestDownloadTarget: ]) def test_class_hierarchy(self, obj): assert isinstance(obj, downloads._DownloadTarget) + + +@pytest.mark.parametrize('raw, expected', [ + ('http://foo/bar', 'bar'), + ('A *|<>\\: bear!', 'A ______ bear!') +]) +def test_sanitized_filenames(raw, expected, config_stub, download_tmpdir): + manager = downloads.AbstractDownloadManager() + target = downloads.FileDownloadTarget(download_tmpdir) + item = downloads.AbstractDownloadItem() + + # Don't try to start a timer outside of a QThread + manager._update_timer.isActive = lambda: True + + # Abstract methods + item._ensure_can_set_filename = lambda *args: True + item._after_set_filename = lambda *args: True + + manager._init_item(item, True, raw) + item.set_target(target) + assert item._filename.endswith(expected)