Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions plugins/ban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Copy link
Member

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.

headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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 == []:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if room_data == []:
if not room_data:

yield 'No rooms found:('
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
yield 'No rooms found:('
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'
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
Copy link
Member

Choose a reason for hiding this comment

The 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) + \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a rule, we don't use \. The alternative is to use parenthesis.

': Something went wrong:( Pls try again!'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
': Something went wrong:( Pls try again!'
': Something went wrong. Please try again.'


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)
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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: ' + \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, avoid using \.

', '.join(unbanned_rooms)
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ omit =
answers/utils.py
utils/filters.py
utils/utils.py
plugins/ban.py
plugins/labhub.py

[coverage:report]
Expand Down
38 changes: 37 additions & 1 deletion tests/ban_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Copy link
Member

Choose a reason for hiding this comment

The 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')
Expand All @@ -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)