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 \x02 notch\x02 has \x02 11\x02 bans - http://fishbans.com/u/notch/"
22+ count_reply = "Bans for \x02 notch\x02 : mcbouncer: \x02 11\x02 - http://fishbans.com/u/notch/"
23+
24+ bans_reply_single = "The user \x02 notch\x02 has \x02 1\x02 ban - http://fishbans.com/u/notch/"
25+
26+ bans_reply_none = "The user \x02 notch\x02 has no bans - http://fishbans.com/u/notch/"
27+ count_reply_none = "The user \x02 notch\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
0 commit comments