2021-12-22 09:42:32 +08:00
|
|
|
import email.message
|
2020-10-17 21:54:54 +08:00
|
|
|
import io
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import List
|
|
|
|
from typing import Union
|
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
import pytest
|
2020-12-02 02:55:59 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
from _pytest.pytester import Pytester
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestPasteCapture:
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
2020-05-01 19:40:17 +08:00
|
|
|
def pastebinlist(self, monkeypatch, request) -> List[Union[str, bytes]]:
|
2020-10-06 09:13:05 +08:00
|
|
|
pastebinlist: List[Union[str, bytes]] = []
|
2018-05-23 22:48:46 +08:00
|
|
|
plugin = request.config.pluginmanager.getplugin("pastebin")
|
|
|
|
monkeypatch.setattr(plugin, "create_new_paste", pastebinlist.append)
|
2010-07-27 03:15:15 +08:00
|
|
|
return pastebinlist
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_failed(self, pytester: Pytester, pastebinlist) -> None:
|
|
|
|
testpath = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_pass() -> None:
|
2009-09-06 22:59:39 +08:00
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-02 02:55:59 +08:00
|
|
|
reprec = pytester.inline_run(testpath, "--pastebin=failed")
|
2009-09-06 22:59:39 +08:00
|
|
|
assert len(pastebinlist) == 1
|
2014-10-23 07:52:40 +08:00
|
|
|
s = pastebinlist[0]
|
2009-09-06 22:59:39 +08:00
|
|
|
assert s.find("def test_fail") != -1
|
2017-07-17 07:25:08 +08:00
|
|
|
assert reprec.countoutcomes() == [1, 1, 1]
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_all(self, pytester: Pytester, pastebinlist) -> None:
|
2015-12-04 06:07:18 +08:00
|
|
|
from _pytest.pytester import LineMatcher
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
testpath = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
def test_fail():
|
|
|
|
assert 0
|
|
|
|
def test_skip():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-02 02:55:59 +08:00
|
|
|
reprec = pytester.inline_run(testpath, "--pastebin=all", "-v")
|
2017-07-17 07:25:08 +08:00
|
|
|
assert reprec.countoutcomes() == [1, 1, 1]
|
2009-09-06 22:59:39 +08:00
|
|
|
assert len(pastebinlist) == 1
|
2018-05-23 22:48:46 +08:00
|
|
|
contents = pastebinlist[0].decode("utf-8")
|
2015-12-04 06:07:18 +08:00
|
|
|
matcher = LineMatcher(contents.splitlines())
|
2018-05-23 22:48:46 +08:00
|
|
|
matcher.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_pass PASSED*",
|
|
|
|
"*test_fail FAILED*",
|
|
|
|
"*test_skip SKIPPED*",
|
|
|
|
"*== 1 failed, 1 passed, 1 skipped in *",
|
|
|
|
]
|
|
|
|
)
|
2015-12-04 06:07:18 +08:00
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_non_ascii_paste_text(self, pytester: Pytester, pastebinlist) -> None:
|
2015-12-04 06:07:18 +08:00
|
|
|
"""Make sure that text which contains non-ascii characters is pasted
|
|
|
|
correctly. See #1219.
|
|
|
|
"""
|
2020-12-02 02:55:59 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
test_unicode="""\
|
2015-12-04 06:07:18 +08:00
|
|
|
def test():
|
|
|
|
assert '☺' == 1
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-02 02:55:59 +08:00
|
|
|
result = pytester.runpytest("--pastebin=all")
|
2019-05-28 07:31:52 +08:00
|
|
|
expected_msg = "*assert '☺' == 1*"
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
expected_msg,
|
|
|
|
"*== 1 failed in *",
|
|
|
|
"*Sending information to Paste Service*",
|
|
|
|
]
|
|
|
|
)
|
2019-08-11 05:30:49 +08:00
|
|
|
assert len(pastebinlist) == 1
|
2014-10-23 07:52:40 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestPaste:
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def pastebin(self, request):
|
2018-05-23 22:48:46 +08:00
|
|
|
return request.config.pluginmanager.getplugin("pastebin")
|
2011-11-15 00:51:12 +08:00
|
|
|
|
2019-08-19 01:32:46 +08:00
|
|
|
@pytest.fixture
|
2020-12-02 02:55:59 +08:00
|
|
|
def mocked_urlopen_fail(self, monkeypatch: MonkeyPatch):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Monkeypatch the actual urlopen call to emulate a HTTP Error 400."""
|
2019-08-19 01:32:46 +08:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
import urllib.error
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
def mocked(url, data):
|
|
|
|
calls.append((url, data))
|
2021-12-22 09:42:32 +08:00
|
|
|
raise urllib.error.HTTPError(
|
|
|
|
url, 400, "Bad request", email.message.Message(), io.BytesIO()
|
|
|
|
)
|
2019-08-19 01:32:46 +08:00
|
|
|
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", mocked)
|
|
|
|
return calls
|
|
|
|
|
|
|
|
@pytest.fixture
|
2020-12-02 02:55:59 +08:00
|
|
|
def mocked_urlopen_invalid(self, monkeypatch: MonkeyPatch):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Monkeypatch the actual urlopen calls done by the internal plugin
|
2019-08-19 01:32:46 +08:00
|
|
|
function that connects to bpaste service, but return a url in an
|
2020-07-18 17:35:13 +08:00
|
|
|
unexpected format."""
|
2019-08-19 01:32:46 +08:00
|
|
|
calls = []
|
|
|
|
|
|
|
|
def mocked(url, data):
|
|
|
|
calls.append((url, data))
|
|
|
|
|
|
|
|
class DummyFile:
|
|
|
|
def read(self):
|
|
|
|
# part of html of a normal response
|
|
|
|
return b'View <a href="/invalid/3c0c6750bd">raw</a>.'
|
|
|
|
|
|
|
|
return DummyFile()
|
|
|
|
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", mocked)
|
|
|
|
return calls
|
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
@pytest.fixture
|
2020-12-02 02:55:59 +08:00
|
|
|
def mocked_urlopen(self, monkeypatch: MonkeyPatch):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Monkeypatch the actual urlopen calls done by the internal plugin
|
|
|
|
function that connects to bpaste service."""
|
2014-10-23 07:52:40 +08:00
|
|
|
calls = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
def mocked(url, data):
|
|
|
|
calls.append((url, data))
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyFile:
|
2014-10-23 07:52:40 +08:00
|
|
|
def read(self):
|
|
|
|
# part of html of a normal response
|
2015-12-02 09:30:05 +08:00
|
|
|
return b'View <a href="/raw/3c0c6750bd">raw</a>.'
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-10-23 07:52:40 +08:00
|
|
|
return DummyFile()
|
|
|
|
|
2019-05-28 07:31:52 +08:00
|
|
|
import urllib.request
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2019-05-28 07:31:52 +08:00
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", mocked)
|
2014-10-23 07:52:40 +08:00
|
|
|
return calls
|
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid) -> None:
|
2019-08-19 01:32:46 +08:00
|
|
|
result = pastebin.create_new_paste(b"full-paste-contents")
|
|
|
|
assert (
|
|
|
|
result
|
|
|
|
== "bad response: invalid format ('View <a href=\"/invalid/3c0c6750bd\">raw</a>.')"
|
|
|
|
)
|
|
|
|
assert len(mocked_urlopen_invalid) == 1
|
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_pastebin_http_error(self, pastebin, mocked_urlopen_fail) -> None:
|
2019-08-19 01:32:46 +08:00
|
|
|
result = pastebin.create_new_paste(b"full-paste-contents")
|
|
|
|
assert result == "bad response: HTTP Error 400: Bad request"
|
|
|
|
assert len(mocked_urlopen_fail) == 1
|
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_create_new_paste(self, pastebin, mocked_urlopen) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
result = pastebin.create_new_paste(b"full-paste-contents")
|
2021-10-06 19:12:48 +08:00
|
|
|
assert result == "https://bpa.st/show/3c0c6750bd"
|
2014-10-23 07:52:40 +08:00
|
|
|
assert len(mocked_urlopen) == 1
|
|
|
|
url, data = mocked_urlopen[0]
|
2015-12-02 09:30:05 +08:00
|
|
|
assert type(data) is bytes
|
2019-08-31 03:34:03 +08:00
|
|
|
lexer = "text"
|
2021-10-06 19:12:48 +08:00
|
|
|
assert url == "https://bpa.st"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "lexer=%s" % lexer in data.decode()
|
|
|
|
assert "code=full-paste-contents" in data.decode()
|
|
|
|
assert "expiry=1week" in data.decode()
|
2019-08-25 01:41:06 +08:00
|
|
|
|
2020-12-02 02:55:59 +08:00
|
|
|
def test_create_new_paste_failure(self, pastebin, monkeypatch: MonkeyPatch) -> None:
|
2019-08-25 01:41:06 +08:00
|
|
|
import io
|
|
|
|
import urllib.request
|
|
|
|
|
|
|
|
def response(url, data):
|
|
|
|
stream = io.BytesIO(b"something bad occurred")
|
|
|
|
return stream
|
|
|
|
|
|
|
|
monkeypatch.setattr(urllib.request, "urlopen", response)
|
|
|
|
result = pastebin.create_new_paste(b"full-paste-contents")
|
2019-08-19 01:32:46 +08:00
|
|
|
assert result == "bad response: invalid format ('something bad occurred')"
|