diff --git a/README.asciidoc b/README.asciidoc index a3e54db88..c8abad993 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -137,8 +137,8 @@ Contributors, sorted by the number of commits in descending order: * Antoni Boucher * Bruno Oliveira * Martin Tournoij -* Raphael Pierzina * Alexander Cogneau +* Raphael Pierzina * Joel Torstensson * Claude * Lamar Pavel diff --git a/qutebrowser/misc/autoupdate.py b/qutebrowser/misc/autoupdate.py index 6ea150300..2126a1296 100644 --- a/qutebrowser/misc/autoupdate.py +++ b/qutebrowser/misc/autoupdate.py @@ -49,9 +49,12 @@ class PyPIVersionClient(QObject): success = pyqtSignal(str) error = pyqtSignal(str) - def __init__(self, parent=None): + def __init__(self, parent=None, client=None): super().__init__(parent) - self._client = httpclient.HTTPClient(self) + if client is None: + self._client = httpclient.HTTPClient(self) + else: + self._client = client self._client.error.connect(self.error) self._client.success.connect(self.on_client_success) diff --git a/scripts/dev/check_coverage.py b/scripts/dev/check_coverage.py index 54195665b..e4eb71d24 100644 --- a/scripts/dev/check_coverage.py +++ b/scripts/dev/check_coverage.py @@ -48,6 +48,7 @@ PERFECT_FILES = [ 'qutebrowser/keyinput/basekeyparser.py', + 'qutebrowser/misc/autoupdate.py', 'qutebrowser/misc/readline.py', 'qutebrowser/misc/split.py', 'qutebrowser/misc/msgbox.py', diff --git a/tests/unit/misc/test_autoupdate.py b/tests/unit/misc/test_autoupdate.py new file mode 100644 index 000000000..f03db7bec --- /dev/null +++ b/tests/unit/misc/test_autoupdate.py @@ -0,0 +1,103 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Alexander Cogneau (acogneau) : +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Tests for qutebrowser.misc.autoupdate""" + +import pytest +from PyQt5.QtTest import QSignalSpy +from PyQt5.QtCore import QUrl + +from qutebrowser.misc import autoupdate, httpclient + +INVALID_JSON = ['{"invalid": { "json"}', '{"wrong": "keys"}'] + + +class HTTPGetStub(httpclient.HTTPClient): + """A stub class for HTTPClient. + + Attributes: + url: the last url used by get() + _success: Wether get() will emit a success signal. + """ + + def __init__(self, success=True, json=None): + super().__init__() + self._success = success + if json: + self._json = json + else: + self._json = '{"info": {"version": "test"}}' + + def get(self, url): + self.url = url + if self._success: + self.success.emit(self._json) + else: + self.error.emit("error") + + +def test_constructor(): + client = autoupdate.PyPIVersionClient() + assert isinstance(client._client, httpclient.HTTPClient) + + +def test_get_version_success(qtbot): + """Test get_version() when success is emitted.""" + http_stub = HTTPGetStub(success=True) + client = autoupdate.PyPIVersionClient(client=http_stub) + + # Use a spy to inspect the signal + error_spy = QSignalSpy(client.error) + + with qtbot.waitSignal(client.success, raising=True): + client.get_version('test') + + assert len(error_spy) == 0 + + assert http_stub.url == QUrl('https://pypi.python.org/pypi/test/json') + + +def test_get_version_error(qtbot): + """Test get_version() when error is emitted.""" + http_stub = HTTPGetStub(success=False) + client = autoupdate.PyPIVersionClient(client=http_stub) + + # Use a spy to inspect the signal + success_spy = QSignalSpy(client.success) + + with qtbot.waitSignal(client.error, raising=True): + client.get_version('test') + + assert len(success_spy) == 0 + + +@pytest.mark.parametrize('json', INVALID_JSON) +def test_invalid_json(qtbot, json): + """Test on_client_success() with invalid JSON.""" + http_stub = HTTPGetStub(json=json) + client = autoupdate.PyPIVersionClient(client=http_stub) + client.get_version('test') + + # Use a spy to inspect the signal + success_spy = QSignalSpy(client.success) + + with qtbot.waitSignal(client.error, raising=True): + client.get_version('test') + + assert len(success_spy) == 0