Skip to content

Commit d127c8b

Browse files
committed
Add unit tests for fishbans. These /probably/ work :)
1 parent 5c5271e commit d127c8b

3 files changed

Lines changed: 132 additions & 11 deletions

File tree

plugins/fishbans.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
from urllib.parse import quote_plus
2-
import asyncio
32

43
import requests
54
import requests.exceptions
6-
import functools
75

86
from cloudbot import hook
97
from cloudbot.util import formatting
108

119
api_url = "http://api.fishbans.com/stats/{}/"
1210

1311

14-
@asyncio.coroutine
1512
@hook.command("bans", "fishbans")
16-
def fishbans(text, bot, loop):
13+
def fishbans(text, bot):
1714
"""<user> - gets information on <user>'s minecraft bans from fishbans"""
1815
user = text.strip()
1916
headers = {'User-Agent': bot.user_agent}
2017

2118
try:
22-
_func = functools.partial(requests.get, api_url.format(quote_plus(user)), headers=headers)
23-
request = yield from loop.run_in_executor(None, _func)
19+
request = requests.get(api_url.format(quote_plus(user)), headers=headers)
2420
request.raise_for_status()
2521
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
2622
return "Could not fetch ban data from the Fishbans API: {}".format(e)
@@ -44,16 +40,14 @@ def fishbans(text, bot, loop):
4440
return "The user \x02{}\x02 has no bans - {}".format(user, user_url)
4541

4642

47-
@asyncio.coroutine
4843
@hook.command()
49-
def bancount(text, bot, loop):
44+
def bancount(text, bot):
5045
"""<user> - gets a count of <user>'s minecraft bans from fishbans"""
5146
user = text.strip()
5247
headers = {'User-Agent': bot.user_agent}
5348

5449
try:
55-
_func = functools.partial(requests.get, api_url.format(quote_plus(user)), headers=headers)
56-
request = yield from loop.run_in_executor(None, _func)
50+
request = requests.get(api_url.format(quote_plus(user)), headers=headers)
5751
request.raise_for_status()
5852
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
5953
return "Could not fetch ban data from the Fishbans API: {}".format(e)
@@ -63,6 +57,9 @@ def bancount(text, bot, loop):
6357
except ValueError:
6458
return "Could not fetch ban data from the Fishbans API: Invalid Response"
6559

60+
if not json["success"]:
61+
return "Could not fetch ban data for {}.".format(user)
62+
6663
user_url = "http://fishbans.com/u/{}/".format(user)
6764
services = json["stats"]["service"]
6865

plugins/test/test_fishbans.py

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,123 @@
1-
__author__ = 'Luke'
1+
import httpretty
2+
import pytest
3+
4+
from plugins.fishbans import fishbans, bancount
5+
6+
test_user = "notch"
7+
8+
test_api = """
9+
{"success":true,"stats":{"username":"notch","uuid":"069a79f444e94726a5befca90e38aaf5","totalbans":11,"service":{"mcbans":0,"mcbouncer":11,"mcblockit":0,"minebans":0,"glizer":0}}}
10+
"""
11+
test_api_single = """
12+
{"success":true,"stats":{"username":"notch","uuid":"069a79f444e94726a5befca90e38aaf5","totalbans":1,"service":{"mcbans":0,"mcbouncer":1,"mcblockit":0,"minebans":0,"glizer":0}}}
13+
"""
14+
test_api_none = """
15+
{"success":true,"stats":{"username":"notch","uuid":"069a79f444e94726a5befca90e38aaf5","totalbans":0,"service":{"mcbans":0,"mcbouncer":0,"mcblockit":0,"minebans":0,"glizer":0}}}
16+
"""
17+
test_api_failed = """
18+
{"success":false}
19+
"""
20+
21+
bans_reply = "The user \x02notch\x02 has \x0211\x02 bans - http://fishbans.com/u/notch/"
22+
count_reply = "Bans for \x02notch\x02: mcbouncer: \x0211\x02 - http://fishbans.com/u/notch/"
23+
24+
bans_reply_single = "The user \x02notch\x02 has \x021\x02 ban - http://fishbans.com/u/notch/"
25+
26+
bans_reply_none = "The user \x02notch\x02 has no bans - http://fishbans.com/u/notch/"
27+
count_reply_none = "The user \x02notch\x02 has no bans - http://fishbans.com/u/notch/"
28+
29+
reply_failed = "Could not fetch ban data for notch."
30+
reply_error = "Could not fetch ban data from the Fishbans API: 404 Client Error: Not Found"
31+
32+
33+
class DummyBot():
34+
user_agent = "CloudBot/3.0"
35+
36+
37+
@pytest.mark.httpretty
38+
def test_bans():
39+
"""
40+
tests fishbans with a successful API response having multiple bans
41+
"""
42+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api)
43+
44+
assert fishbans(test_user, DummyBot) == bans_reply
45+
46+
47+
@pytest.mark.httpretty
48+
def test_bans_single():
49+
"""
50+
tests fishbans with a successful API response having a single ban
51+
"""
52+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api_single)
53+
54+
assert fishbans(test_user, DummyBot) == bans_reply_single
55+
56+
@pytest.mark.httpretty
57+
def test_bans_failed():
58+
"""
59+
tests fishbans with a failed API response
60+
"""
61+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api_failed)
62+
63+
assert fishbans(test_user, DummyBot) == reply_failed
64+
65+
66+
@pytest.mark.httpretty
67+
def test_bans_none():
68+
"""
69+
tests fishbans with a successful API response having no bans
70+
"""
71+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api_none)
72+
73+
assert fishbans(test_user, DummyBot) == bans_reply_none
74+
75+
76+
@pytest.mark.httpretty
77+
def test_bans_error():
78+
"""
79+
tests fishbans with a HTTP error
80+
"""
81+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', status=404)
82+
83+
assert fishbans(test_user, DummyBot) == reply_error
84+
85+
86+
@pytest.mark.httpretty
87+
def test_count():
88+
"""
89+
tests bancount with a successful API response having multiple bans
90+
"""
91+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api)
92+
93+
assert bancount(test_user, DummyBot) == count_reply
94+
95+
96+
@pytest.mark.httpretty
97+
def test_count_failed():
98+
"""
99+
tests bancount with a failed API response
100+
"""
101+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api_failed)
102+
103+
assert bancount(test_user, DummyBot) == reply_failed
104+
105+
106+
@pytest.mark.httpretty
107+
def test_count_none():
108+
"""
109+
tests bancount with a successful API response having no bans
110+
"""
111+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', body=test_api_none)
112+
113+
assert bancount(test_user, DummyBot) == count_reply_none
114+
115+
116+
@pytest.mark.httpretty
117+
def test_count_error():
118+
"""
119+
tests bancount with a HTTP error
120+
"""
121+
httpretty.register_uri(httpretty.GET, 'http://api.fishbans.com/stats/notch/', status=404)
122+
123+
assert bancount(test_user, DummyBot) == reply_error

travis/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pytest
2+
httpretty
23
pytest-cov
34
pytest-pep8
5+
pytest-httpretty
46
flake8
57
python-coveralls

0 commit comments

Comments
 (0)