2020-04-18 11:45:44 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2016-10-28 22:21:41 +00:00
|
|
|
# Copyright (C) 2004-2008 Tristan Seligmann and Jonathan Jacobs
|
2016-05-05 21:33:48 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2020-01-04 14:51:01 +00:00
|
|
|
# Copyright (C) 2015-2020 Tobias Gruetzmacher
|
2019-12-01 21:36:49 +00:00
|
|
|
import json
|
2020-01-26 16:42:35 +00:00
|
|
|
import os
|
2019-12-01 21:36:49 +00:00
|
|
|
import re
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
import pytest
|
2017-10-12 22:34:37 +00:00
|
|
|
import responses
|
2016-05-05 21:33:48 +00:00
|
|
|
|
2017-10-12 21:56:39 +00:00
|
|
|
import dosagelib.cmd
|
2017-10-12 22:34:37 +00:00
|
|
|
import httpmocks
|
2013-03-04 18:10:27 +00:00
|
|
|
|
2019-11-03 23:16:25 +00:00
|
|
|
|
2017-10-12 21:56:39 +00:00
|
|
|
def cmd(*options):
|
|
|
|
"""'Fake' run dosage with given options."""
|
|
|
|
return dosagelib.cmd.main(('--allow-multiple',) + options)
|
2013-03-04 18:10:27 +00:00
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
|
2017-10-12 21:56:39 +00:00
|
|
|
def cmd_ok(*options):
|
|
|
|
assert cmd(*options) == 0
|
2013-03-04 18:10:27 +00:00
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
|
2017-10-12 21:56:39 +00:00
|
|
|
def cmd_err(*options):
|
|
|
|
assert cmd(*options) == 1
|
2013-03-04 18:10:27 +00:00
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
|
2020-04-18 11:03:02 +00:00
|
|
|
@pytest.mark.usefixtures("_nosleep")
|
2016-03-07 00:08:57 +00:00
|
|
|
class TestDosage(object):
|
2013-03-04 18:10:27 +00:00
|
|
|
"""Test the dosage commandline client."""
|
|
|
|
|
2019-12-01 21:36:49 +00:00
|
|
|
# This shouldn't hit the network at all, so add responses without mocks to
|
|
|
|
# make sure it doesn't do that
|
|
|
|
@responses.activate
|
2016-03-07 00:08:57 +00:00
|
|
|
def test_list_comics(self):
|
2013-03-04 18:10:27 +00:00
|
|
|
for option in ("-l", "--list", "--singlelist"):
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_ok(option)
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2019-12-05 21:23:48 +00:00
|
|
|
@responses.activate
|
2016-03-07 00:08:57 +00:00
|
|
|
def test_display_version(self):
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_ok("--version")
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2019-12-05 21:23:48 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_update_available(self, capsys):
|
|
|
|
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
|
2020-01-26 16:42:35 +00:00
|
|
|
json={'tag_name': '9999.0', 'assets': [
|
|
|
|
{'browser_download_url': 'TEST.whl'},
|
|
|
|
{'browser_download_url': 'TEST.exe'},
|
|
|
|
]})
|
2019-12-05 21:23:48 +00:00
|
|
|
cmd_ok('--version', '-v')
|
|
|
|
captured = capsys.readouterr()
|
2020-01-26 16:42:35 +00:00
|
|
|
best = 'TEST.exe' if os.name == 'nt' else 'TEST.whl'
|
|
|
|
assert best in captured.out
|
2019-12-05 21:23:48 +00:00
|
|
|
assert 'A new version' in captured.out
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_no_update_available(self, capsys):
|
|
|
|
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
|
2020-01-26 16:42:35 +00:00
|
|
|
json={'tag_name': '1.0'})
|
2019-12-05 21:23:48 +00:00
|
|
|
cmd_ok('--version', '-v')
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert 'Detected local or development' in captured.out
|
|
|
|
|
2020-01-26 16:42:35 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_current(self, capsys):
|
|
|
|
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
|
|
|
|
json={'tag_name': dosagelib.__version__})
|
|
|
|
cmd_ok('--version', '-v')
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert captured.out.endswith('issues\n')
|
|
|
|
|
2019-12-05 21:23:48 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_update_broken(self, capsys):
|
|
|
|
responses.add(responses.GET, re.compile(r'https://api\.github\.com/'),
|
|
|
|
json={})
|
|
|
|
cmd_ok('--version', '-v')
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
assert 'invalid update file' in captured.out
|
|
|
|
|
2016-03-07 00:08:57 +00:00
|
|
|
def test_display_help(self):
|
2013-03-04 18:10:27 +00:00
|
|
|
for option in ("-h", "--help"):
|
2017-10-12 21:56:39 +00:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
cmd(option)
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2020-10-04 20:14:06 +00:00
|
|
|
def test_module_help(self, capfd):
|
|
|
|
cmd_ok("-m", "-t", "xkcd")
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert re.match(r'([0-9][0-9]:){2}.. xkcd>', out)
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def test_no_comics_specified(self):
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_err()
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def test_unknown_option(self):
|
2017-10-12 21:56:39 +00:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
cmd('--imadoofus')
|
2016-03-07 00:08:57 +00:00
|
|
|
|
|
|
|
def test_multiple_comics_match(self):
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_err('Garfield')
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
@responses.activate
|
2016-05-05 21:33:48 +00:00
|
|
|
def test_fetch_html_and_rss_json(self, tmpdir):
|
2017-10-12 22:34:37 +00:00
|
|
|
httpmocks.xkcd()
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "-o", "html", "-o", "rss",
|
2020-01-04 14:51:01 +00:00
|
|
|
"-o", "json", "--no-downscale", "xkcd")
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
@responses.activate
|
2020-01-04 14:51:01 +00:00
|
|
|
def test_fetch_html_and_rss_2(self, tmp_path):
|
2019-12-01 21:36:49 +00:00
|
|
|
httpmocks.page('http://www.bloomingfaeries.com/', 'bf-home')
|
|
|
|
httpmocks.page(re.compile('http://www.*faeries-405/'), 'bf-405')
|
2020-01-04 14:51:01 +00:00
|
|
|
httpmocks.png(re.compile(r'http://www\.blooming.*405.*jpg'))
|
|
|
|
httpmocks.png(re.compile(r'http://www\.blooming.*406.*jpg'), 'tall')
|
2019-12-01 21:36:49 +00:00
|
|
|
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_ok("--numstrips", "2", "--baseurl", "bla", "--basepath",
|
2020-01-04 14:51:01 +00:00
|
|
|
str(tmp_path), "--output", "rss", "--output", "html", "--adult",
|
2019-12-01 21:36:49 +00:00
|
|
|
"BloomingFaeries")
|
2016-03-07 00:08:57 +00:00
|
|
|
|
2020-01-04 14:51:01 +00:00
|
|
|
html = next((tmp_path / 'html').glob('*.html')).read_text()
|
|
|
|
assert "width=" in html
|
|
|
|
|
|
|
|
@responses.activate
|
|
|
|
def test_fetch_html_broken_img(self, tmp_path):
|
|
|
|
httpmocks.page('http://www.bloomingfaeries.com/', 'bf-home')
|
|
|
|
httpmocks.page(re.compile('http://www.*faeries-405/'), 'bf-405')
|
|
|
|
responses.add(responses.GET, re.compile(r'.*\.jpg'), body=b'\377\330',
|
|
|
|
content_type='image/jpeg')
|
|
|
|
|
|
|
|
cmd_ok("--numstrips", "2", "--baseurl", "bla", "--basepath",
|
|
|
|
str(tmp_path), "--output", "html", "--adult", "BloomingFaeries")
|
|
|
|
|
|
|
|
html = next((tmp_path / 'html').glob('*.html')).read_text()
|
|
|
|
assert "width=" not in html
|
|
|
|
|
2017-10-12 22:34:37 +00:00
|
|
|
@responses.activate
|
2016-03-28 14:29:57 +00:00
|
|
|
def test_fetch_indexed(self, tmpdir):
|
2017-10-12 22:34:37 +00:00
|
|
|
httpmocks.xkcd()
|
2017-10-12 21:56:39 +00:00
|
|
|
cmd_ok("-n", "2", "-v", "-b", str(tmpdir), "xkcd:303")
|
2019-06-19 05:09:33 +00:00
|
|
|
|
2020-10-01 12:35:33 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_fetch_all_existing(self, tmp_path):
|
|
|
|
httpmocks.xkcd()
|
|
|
|
xkcd = tmp_path / 'xkcd'
|
|
|
|
xkcd.mkdir()
|
|
|
|
other = tmp_path / 'randomdir'
|
|
|
|
other.mkdir()
|
|
|
|
cmd_ok('-v', '-b', str(tmp_path), '@')
|
|
|
|
assert len(list(xkcd.glob('*'))) == 2
|
|
|
|
assert len(list(other.glob('*'))) == 0
|
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
@responses.activate
|
|
|
|
def test_json_page_key_bounce_and_multi_image(self, tmpdir):
|
2019-12-01 21:36:49 +00:00
|
|
|
httpmocks.page('https://zenpencils.com/', 'zp-home')
|
|
|
|
httpmocks.page('https://zenpencils.com/comic/missing/', 'zp-223')
|
|
|
|
httpmocks.page('https://zenpencils.com/comic/lifejacket/', 'zp-222')
|
|
|
|
httpmocks.jpeg(re.compile(r'https://cdn-.*\.jpg'))
|
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
cmd_ok("-v", "-b", str(tmpdir), "-o", "json", "ZenPencils")
|
2019-11-03 23:16:25 +00:00
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
directory = tmpdir.join('ZenPencils')
|
|
|
|
f = directory.join('dosage.json').open(encoding='utf-8')
|
|
|
|
data = json.load(f)
|
|
|
|
f.close()
|
2019-11-03 23:16:25 +00:00
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
pages = data['pages']
|
|
|
|
assert len(pages) == 1
|
2019-11-03 23:16:25 +00:00
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
page = list(pages.keys())[0]
|
|
|
|
assert page == 'https://zenpencils.com/comic/missing/'
|
2019-11-03 23:16:25 +00:00
|
|
|
|
2019-06-19 05:09:33 +00:00
|
|
|
images = data['pages'][page]['images']
|
|
|
|
assert len(images) == 2
|
|
|
|
|
|
|
|
for imgurl, imgfile in images.items():
|
|
|
|
assert directory.join(imgfile).check(file=1)
|