From 9b2e2c7b374aa71ef6ce697825c68c806b58f288 Mon Sep 17 00:00:00 2001 From: abhishalya Date: Sat, 3 Nov 2018 00:42:06 +0530 Subject: [PATCH] labhub.py: Add error message when nick is empty All of the backends do not have `nick` attribute. Hence an error will be shown whenever nick is empty. Closes https://github.com/coala/corobo/issues/632 --- plugins/labhub.py | 14 ++++++++++++++ tests/labhub_test.py | 18 +++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/plugins/labhub.py b/plugins/labhub.py index 1d702b71..2f64c01e 100644 --- a/plugins/labhub.py +++ b/plugins/labhub.py @@ -13,6 +13,12 @@ from utils.mixin import DefaultConfigMixin +def validate_nick(msg): + if msg.frm.nick: + return True + return False + + class LabHub(DefaultConfigMixin, BotPlugin): """GitHub and GitLab utilities""" # Ignore QuotesBear @@ -122,6 +128,8 @@ def invite_cmd(self, msg, match): """ invitee = match.group(1) inviter = msg.frm.nick + if not validate_nick(msg): + yield 'ERROR: The above command cannot be operated without nick.' team = 'newcomers' if match.group(2) is None else match.group(2) team = team.lower() @@ -196,6 +204,8 @@ def callback_message(self, msg): def create_issue_cmd(self, msg, match): """Create issues on GitHub and GitLab repositories.""" # Ignore QuotesBear, LineLengthBear, PyCodeStyleBear user = msg.frm.nick + if not validate_nick(msg): + yield 'ERROR: The above command cannot be operated without nick.' repo_name = match.group(1) iss_title = match.group(2) iss_description = match.group(3) if match.group(3) is not None else '' @@ -234,6 +244,8 @@ def unassign_cmd(self, msg, match): issue_number = match.group(4) user = msg.frm.nick + if not validate_nick(msg): + yield 'ERROR: The above command cannot be operated without nick.' try: assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME @@ -317,6 +329,8 @@ def assign_cmd(self, msg, match): iss_number = match.group(4) user = msg.frm.nick + if not validate_nick(msg): + yield 'ERROR: The above command cannot be operated without nick.' try: assert org == self.GH_ORG_NAME or org == self.GL_ORG_NAME diff --git a/tests/labhub_test.py b/tests/labhub_test.py index 80492ce4..1d4aed13 100644 --- a/tests/labhub_test.py +++ b/tests/labhub_test.py @@ -12,6 +12,7 @@ import plugins.labhub from plugins.labhub import LabHub +from plugins.labhub import validate_nick from tests.labhub_test_case import LabHubTestCase @@ -33,6 +34,7 @@ def setUp(self): 'GH_ORG_NAME': 'coala', 'GL_ORG_NAME': 'coala', } + self.bot.sender._nick = 'batman' self.labhub = self.load_plugin('LabHub', self.global_mocks, configs) def test_invite_cmd(self): @@ -117,6 +119,12 @@ def test_is_room_member(self): msg.frm.room.occupants = ['batman', 'superman'] self.assertTrue(LabHub.is_room_member('batman', msg)) + def test_validate_nick(self): + msg = create_autospec(Message) + msg.frm.nick = PropertyMock() + msg.frm.nick = 'batman' + self.assertTrue(validate_nick(msg)) + def test_hello_world_callback(self): self.mock_team.is_member.return_value = False testbot = self @@ -156,7 +164,7 @@ def test_create_issue_cmd(self): textwrap.dedent('''\ first line of body second line of body - Opened by @None at [text]()''') + Opened by @batman at [text]()''') ) testbot_public.assertCommand( @@ -171,7 +179,7 @@ def test_create_issue_cmd(self): 'another title', textwrap.dedent('''\ body - Opened by @None at [text]()''') + Opened by @batman at [text]()''') ) testbot_public.assertCommand( @@ -201,7 +209,7 @@ def test_unassign_cmd(self): mock_iss = create_autospec(IGitt.GitHub.GitHubIssue) self.mock_repo.get_issue.return_value = mock_iss mock_iss.assignees = PropertyMock() - mock_iss.assignees = (None, ) + mock_iss.assignees = ('batman', ) mock_iss.unassign = MagicMock() self.mock_team.is_member.return_value = True testbot = self @@ -211,7 +219,7 @@ def test_unassign_cmd(self): 'you are unassigned now', timeout=10000) self.mock_repo.get_issue.assert_called_with(999) - mock_iss.unassign.assert_called_once_with(None) + mock_iss.unassign.assert_called_once_with('batman') mock_iss.assignees = ('meetmangukiya', ) testbot.assertCommand( @@ -335,7 +343,7 @@ def test_assign_cmd(self): 'already assigned to someone') # has assignee same as user - mock_issue.assignees = (None, ) + mock_issue.assignees = ('batman', ) testbot.assertCommand(cmd.format('coala', 'a', '23'), 'already assigned to you')