diff --git a/plugins/ban.py b/plugins/ban.py index e1124a8e..10d23620 100644 --- a/plugins/ban.py +++ b/plugins/ban.py @@ -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) 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 == []: + 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()) + 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) + \ + ': Something went wrong:( Pls 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) @@ -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!' - yield sinner + ' has been unbanned from: ' + ', '.join(unbanned_rooms) + yield sinner + ' has been unbanned from: ' + \ + ', '.join(unbanned_rooms) diff --git a/setup.cfg b/setup.cfg index 54cc770c..7ecf5bb1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -64,7 +64,6 @@ omit = answers/utils.py utils/filters.py utils/utils.py - plugins/ban.py plugins/labhub.py [coverage:report] diff --git a/tests/ban_test.py b/tests/ban_test.py index 4920a79c..59beee30 100644 --- a/tests/ban_test.py +++ b/tests/ban_test.py @@ -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!') + 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)