import os
import pytest
from szurubooru import errors
from szurubooru.func import net
from szurubooru.func.util import get_sha1
@pytest.fixture(autouse=True)
def inject_config(tmpdir, config_injector):
config_injector(
{
"user_agent": None,
"max_dl_filesize": 1.0e6,
"data_dir": str(tmpdir.mkdir("data")),
}
)
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
def test_download():
url = "http://info.cern.ch/hypertext/WWW/TheProject.html"
expected_content = (
b'\nThe World Wide Web project\n\n\n
\nWorld Wide Web
The WorldWideWeb'
+ b' (W3) is a wide-area\nhypermedia'
+ b"A> information retrieval\ninitiative aiming to give universal\na"
+ b"ccess to a large universe of documents.\nEverything there is "
+ b"online about\nW3 is linked directly or indirectly\nto this docum"
+ b'ent, including an executive\nsum'
+ b'mary of the project, Mailing lists\n, Policy , November\'s W3 news ,\nFrequently Ask'
+ b'ed Questions .\n
\n- What's out there?\n
- Pointers to the\nworld's "
+ b'online information, subjects\n, W3 servers, etc.\n
- Help\n
- on the browser you are using\n
- Software Products\n
- A list of W'
+ b"3 project\ncomponents and their current state.\n(e.g. Line Mode ,X11 Viola , NeXTStep\n, Servers , Tools , Mail robot ,<'
+ b'A\nNAME=52 HREF="Status.html#57">\nLibrary
)\nTechnical\n Details of protocols'
+ b', formats,\nprogram internals etc\nBibliography\n Paper documentation\non W3 a'
+ b'nd references.\nPeople\n<'
+ b"DD> A list of some people involved\nin the project.\nHistory\n A summary of the hist'
+ b'ory\nof the project.\nHow ca'
+ b"n I help ?\n If you would like\nto support the web..\nGetting code\n Getti'
+ b'ng the code by\nanonymous FTP , etc.\n\n\n'
)
actual_content = net.download(url)
assert actual_content == expected_content
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
@pytest.mark.parametrize(
"url",
[
"https://samples.ffmpeg.org/MPEG-4/video.mp4",
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
],
)
def test_too_large_download(url):
with pytest.raises(net.DownloadTooLargeError):
net.download(url, use_video_downloader=True)
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
@pytest.mark.parametrize(
"url,expected_sha1",
[
(
"https://gfycat.com/immaterialchillyiberianmole",
"0125976d2439e651b6863438db30de58f79f7754",
),
(
"https://upload.wikimedia.org/wikipedia/commons/a/ad/Utah_teapot.png", # noqa: E501
"cfadcbdeda1204dc1363ee5c1969191f26be2e41",
),
(
"https://i.imgur.com/GPgh0AN.jpg",
"26861a4663fedae48e5beed3eec5156ded20640f",
),
],
)
def test_content_download(url, expected_sha1):
actual_content = net.download(url, use_video_downloader=True)
assert get_sha1(actual_content) == expected_sha1
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
def test_bad_content_downlaod():
url = "http://info.cern.ch/hypertext/WWW/TheProject.html"
with pytest.raises(errors.ThirdPartyError):
net.download(url, use_video_downloader=True)
def test_no_webhooks(config_injector):
config_injector({"webhooks": []})
res = net.post_to_webhooks(None)
assert len(res) == 0
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
@pytest.mark.parametrize(
"webhook,status_code",
[
("https://postman-echo.com/post", 200),
("https://postman-echo.com/get", 400),
],
)
def test_single_webhook(config_injector, webhook, status_code):
ret = net._post_to_webhook(webhook, {"test_arg": "test_value"})
assert ret == status_code
@pytest.mark.skipif(
"TEST_NET" not in os.environ, reason="Network tests skipped by default."
)
def test_multiple_webhooks(config_injector):
config_injector(
{
"webhooks": [
"https://postman-echo.com/post",
"https://postman-echo.com/get",
]
}
)
threads = net.post_to_webhooks({"test_arg": "test_value"})
assert len(threads) == 2
def test_malformed_webhooks(config_injector):
with pytest.raises(ValueError):
net._post_to_webhook("malformed_url", {"test_arg": "test_value"})