Skip to content

Commit

Permalink
labhub.py: Add error message when nick is empty
Browse files Browse the repository at this point in the history
All of the backends do not have `nick` attribute.
Hence an error will be shown whenever nick is empty.

Closes coala#632
  • Loading branch information
abhishalya committed Nov 6, 2018
1 parent b9d569d commit 2c02447
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugins/labhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 ''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/labhub_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -59,6 +60,9 @@ def test_invite_cmd(self):

mock_dict['is_room_member'].return_value = True

mock_nick = create_autospec(Message)
mock_nick.frm.nick.return_value = True

# invite by maintainer
mock_team_newcomers.is_member.return_value = True
mock_team_developers.is_member.return_value = True
Expand Down Expand Up @@ -117,6 +121,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
Expand All @@ -142,6 +152,9 @@ def test_create_issue_cmd(self):
self.mock_team.is_member.return_value = True
testbot_public = self

mock_nick = create_autospec(Message)
mock_nick.frm.nick.return_value = True

testbot_public.assertCommand(
textwrap.dedent('''\
!new issue repository this is the title
Expand Down Expand Up @@ -205,6 +218,8 @@ def test_unassign_cmd(self):
mock_iss.unassign = MagicMock()
self.mock_team.is_member.return_value = True
testbot = self
mock_nick = create_autospec(Message)
mock_nick.frm.nick.return_value = True

testbot.assertCommand(
'!unassign https://github.com/coala/example/issues/999',
Expand Down Expand Up @@ -241,6 +256,8 @@ def test_assign_cmd(self):
mock_maint_team = create_autospec(github3.orgs.Team)
mock_dev_team.is_member.return_value = False
mock_maint_team.is_member.return_value = False
mock_nick = create_autospec(Message)
mock_nick.frm.nick.return_value = True

self.teams['coala developers'] = mock_dev_team
self.teams['coala maintainers'] = mock_maint_team
Expand Down

0 comments on commit 2c02447

Please sign in to comment.