Skip to content

Commit 218e369

Browse files
committed
Add test
1 parent 5c5593d commit 218e369

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

testing/test_pastebin.py

+54
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@ class TestPaste:
8282
def pastebin(self, request):
8383
return request.config.pluginmanager.getplugin("pastebin")
8484

85+
@pytest.fixture
86+
def mocked_urlopen_fail(self, monkeypatch):
87+
"""
88+
monkeypatch the actual urlopen call to emulate a HTTP Error 400
89+
"""
90+
calls = []
91+
92+
import urllib.error
93+
import urllib.request
94+
95+
def mocked(url, data):
96+
calls.append((url, data))
97+
raise urllib.error.HTTPError(url, 400, "Bad request", None, None)
98+
99+
monkeypatch.setattr(urllib.request, "urlopen", mocked)
100+
return calls
101+
102+
@pytest.fixture
103+
def mocked_urlopen_invalid(self, monkeypatch):
104+
"""
105+
monkeypatch the actual urlopen calls done by the internal plugin
106+
function that connects to bpaste service, but return a url in an
107+
unexpected format
108+
"""
109+
calls = []
110+
111+
def mocked(url, data):
112+
calls.append((url, data))
113+
114+
class DummyFile:
115+
def read(self):
116+
# part of html of a normal response
117+
return b'View <a href="/invalid/3c0c6750bd">raw</a>.'
118+
119+
return DummyFile()
120+
121+
import urllib.request
122+
123+
monkeypatch.setattr(urllib.request, "urlopen", mocked)
124+
return calls
125+
85126
@pytest.fixture
86127
def mocked_urlopen(self, monkeypatch):
87128
"""
@@ -105,6 +146,19 @@ def read(self):
105146
monkeypatch.setattr(urllib.request, "urlopen", mocked)
106147
return calls
107148

149+
def test_pastebin_invalid_url(self, pastebin, mocked_urlopen_invalid):
150+
result = pastebin.create_new_paste(b"full-paste-contents")
151+
assert (
152+
result
153+
== "bad response: invalid format ('View <a href=\"/invalid/3c0c6750bd\">raw</a>.')"
154+
)
155+
assert len(mocked_urlopen_invalid) == 1
156+
157+
def test_pastebin_http_error(self, pastebin, mocked_urlopen_fail):
158+
result = pastebin.create_new_paste(b"full-paste-contents")
159+
assert result == "bad response: HTTP Error 400: Bad request"
160+
assert len(mocked_urlopen_fail) == 1
161+
108162
def test_create_new_paste(self, pastebin, mocked_urlopen):
109163
result = pastebin.create_new_paste(b"full-paste-contents")
110164
assert result == "https://bpaste.net/show/3c0c6750bd"

0 commit comments

Comments
 (0)