-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ban_test to have 100% coverage #669
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,7 +8,6 @@ class Ban(BotPlugin): | |||||
""" | ||||||
Ban/Unban from all rooms at once. | ||||||
""" | ||||||
|
||||||
@botcmd(split_args_with=None, | ||||||
admin_only=True) | ||||||
def ban(self, msg, args): | ||||||
|
@@ -22,6 +21,7 @@ def ban(self, msg, args): | |||||
sinner = sinner[1:] | ||||||
|
||||||
joined_rooms = self.bot_config.ROOMS_TO_JOIN | ||||||
self.log.info(joined_rooms) | ||||||
headers = { | ||||||
'Content-Type': 'application/json', | ||||||
'Accept': 'application/json', | ||||||
|
@@ -31,18 +31,27 @@ def ban(self, msg, args): | |||||
|
||||||
r = requests.get('https://api.gitter.im/v1/rooms', headers=headers) | ||||||
room_data = json.loads(r.text) | ||||||
self.log.info(room_data) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, I don't think this is required. |
||||||
banned_rooms = [] | ||||||
|
||||||
for room in filter(lambda x: x.get('uri', None) in joined_rooms, | ||||||
room_data): | ||||||
if room is not None: | ||||||
url = 'https://api.gitter.im/v1/rooms/' + \ | ||||||
room['id'] + '/bans' | ||||||
if room_data == []: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
yield 'No rooms found:(' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
else: | ||||||
for room in filter(lambda x: x.get('uri', None) in joined_rooms, | ||||||
room_data): | ||||||
url = 'https://api.gitter.im/v1/rooms/' + room['id'] + '/bans' | ||||||
rq = requests.post(url, data=data, headers=headers) | ||||||
self.log.info(rq.status_code) | ||||||
self.log.info(rq.json()) | ||||||
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need to log everything. Also, looking at this will not be obvious to where the log came from. The log ideally should have all the information required to pin point itself to the source. |
||||||
|
||||||
if rq.status_code == 200: | ||||||
banned_rooms.append(room['uri']) | ||||||
else: | ||||||
self.log.info('Error ' + str(rq.status_code)) | ||||||
yield 'Error ' + str(rq.status_code) + \ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a rule, we don't use |
||||||
': Something went wrong:( Pls try again!' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
yield sinner + ' has been banned from: ' + ', '.join(banned_rooms) | ||||||
yield sinner + ' has been banned from: ' + ', '.join(banned_rooms) | ||||||
|
||||||
@botcmd(split_args_with=None, | ||||||
admin_only=True) | ||||||
|
@@ -67,13 +76,20 @@ def unban(self, msg, args): | |||||
room_data = json.loads(r.text) | ||||||
unbanned_rooms = [] | ||||||
|
||||||
for room in filter(lambda x: x.get('uri', None) in joined_rooms, | ||||||
room_data): | ||||||
if room is not None: | ||||||
if room_data == []: | ||||||
yield 'No rooms found:(' | ||||||
else: | ||||||
for room in filter(lambda x: x.get('uri', None) in joined_rooms, | ||||||
room_data): | ||||||
url = 'https://api.gitter.im/v1/rooms/' + \ | ||||||
room['id'] + '/bans/' + sinner | ||||||
rq = requests.delete(url, headers=headers) | ||||||
if rq.status_code == 200: | ||||||
unbanned_rooms.append(room['uri']) | ||||||
else: | ||||||
self.log.info('Error ' + str(rq.status_code)) | ||||||
yield 'Error ' + str(rq.status_code) + \ | ||||||
': Something went wrong:( Pls try again!' | ||||||
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add changes as mentioned above, not repeating them here to keep it simple. |
||||||
|
||||||
yield sinner + ' has been unbanned from: ' + ', '.join(unbanned_rooms) | ||||||
yield sinner + ' has been unbanned from: ' + \ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, avoid using |
||||||
', '.join(unbanned_rooms) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,29 @@ def test_ban_cmd(self, mockjson, mockreq): | |
{'id': '234', 'name': 'Nitanshu'}, | ||
{'id': '897', 'uri': 'coala/coala-bears'} | ||
] | ||
mockjson.loads.return_value = fake_room_data | ||
testbot = self | ||
mockjson.loads.return_value = fake_room_data | ||
testbot.assertCommand('!ban @nvzard', | ||
'nvzard has been banned from: coala/coala, ' | ||
'coala/coala-bears') | ||
testbot.assertCommand('!ban nvzard', | ||
'nvzard has been banned from: coala/coala, ' | ||
'coala/coala-bears') | ||
|
||
mockjson.loads.return_value = [] | ||
testbot.assertCommand('!ban @nvzard', 'No rooms found:(') | ||
|
||
mockjson.loads.return_value = fake_room_data | ||
status_mock = MagicMock() | ||
type(status_mock).status_code = PropertyMock(return_value=403) | ||
mockreq.post.return_value = status_mock | ||
|
||
with testbot.assertLogs() as cm: | ||
testbot.assertCommand('!ban @nvzard', | ||
'Error 403: ' | ||
'Something went wrong:( Pls try again!') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adjust the tests accordingly. |
||
testbot.assertIn('INFO:errbot.plugins.Ban:' | ||
'Error 403', cm.output) | ||
|
||
@patch('plugins.ban.requests') | ||
@patch('plugins.ban.json') | ||
|
@@ -49,3 +67,21 @@ def test_unban_cmd(self, mockjson, mockreq): | |
testbot.assertCommand('!unban @nvzard', | ||
'nvzard has been unbanned from: coala/coala, ' | ||
'coala/coala-bears') | ||
testbot.assertCommand('!unban nvzard', | ||
'nvzard has been unbanned from: coala/coala, ' | ||
'coala/coala-bears') | ||
|
||
mockjson.loads.return_value = [] | ||
testbot.assertCommand('!unban @nvzard', 'No rooms found:(') | ||
|
||
mockjson.loads.return_value = fake_room_data | ||
status_mock = MagicMock() | ||
type(status_mock).status_code = PropertyMock(return_value=403) | ||
mockreq.delete.return_value = status_mock | ||
|
||
with testbot.assertLogs() as cm: | ||
testbot.assertCommand('!unban @nvzard', | ||
'Error 403: ' | ||
'Something went wrong:( Pls try again!') | ||
testbot.assertIn('INFO:errbot.plugins.Ban:' | ||
'Error 403', cm.output) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is required.