Ejemplo n.º 1
0
    def setUp(self):
        self.team = 'rocket'
        self.teamid = 395830
        self.member = 'theflatearth'
        self.memberid = 3058493
        self.add_payload = mem_default_payload(self.team, self.teamid,
                                               self.member, self.memberid)
        self.rm_payload = mem_default_payload(self.team, self.teamid,
                                              self.member, self.memberid)
        self.empty_payload = mem_default_payload(self.team, self.teamid,
                                                 self.member, self.memberid)

        self.add_payload['action'] = 'added'
        self.rm_payload['action'] = 'removed'
        self.empty_payload['action'] = ''

        self.u = User('U4058409')
        self.u.github_id = str(self.memberid)
        self.u.github_username = self.member
        self.t = Team(str(self.teamid), self.team, self.team.capitalize())
        self.db = MemoryDB(users=[self.u], teams=[self.t])

        self.gh = mock.Mock()
        self.conf = mock.Mock()
        self.conf.github_team_all = 'all'
        self.webhook_handler = MembershipEventHandler(self.db, self.gh,
                                                      self.conf)
Ejemplo n.º 2
0
def test_handle_mem_event_add_missing_member(mock_logging, mem_add_payload):
    """Test that instances when members are added to the mem are logged."""
    mock_facade = mock.MagicMock(DBFacade)
    mock_facade.query.return_value = []
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_add_payload)
    mock_logging.error.assert_called_once_with("could not find user 21031067")
    assert rsp == "could not find user Codertocat"
    assert code == 404
Ejemplo n.º 3
0
def test_handle_mem_event_empty_action(mock_logging, mem_empty_payload):
    """Test that instances where there is no/invalid action are logged."""
    mock_facade = mock.MagicMock(DBFacade)
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_empty_payload)
    mock_logging.error.assert_called_once_with(
        ("membership webhook "
         "triggered, invalid "
         "action specified: {}".format(mem_empty_payload)))
    assert rsp == "invalid membership webhook triggered"
    assert code == 405
Ejemplo n.º 4
0
def test_handle_mem_event_rm_member_wrong_team(mock_logging, mem_rm_payload):
    """Test what happens when member removed from a team they are not in."""
    mock_facade = mock.MagicMock(DBFacade)
    mock_facade.query.return_value = []
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_rm_payload)
    mock_facade.query\
        .assert_called_once_with(User, [('github_user_id', "21031067")])
    mock_logging.error.assert_called_once_with("could not find user 21031067")
    assert rsp == "could not find user 21031067"
    assert code == 404
Ejemplo n.º 5
0
def test_handle_mem_event_rm_member_missing(mock_logging, mem_rm_payload):
    """Test that members not in rocket db are handled correctly."""
    mock_facade = mock.MagicMock(DBFacade)
    return_user = User("SLACKID")
    return_team = Team("2723476", "rocket", "rocket")
    mock_facade.query.return_value = [return_user]
    mock_facade.retrieve.return_value = return_team
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_rm_payload)
    mock_facade.query\
        .assert_called_once_with(User, [('github_user_id', "21031067")])
    mock_logging.error.assert_called_once_with("slack user SLACKID "
                                               "not in rocket")
    assert rsp == "slack user SLACKID not in rocket"
    assert code == 404
Ejemplo n.º 6
0
def test_handle_mem_event_add_member(mock_logging, mem_add_payload):
    """Test that instances when members are added to the mem are logged."""
    mock_facade = mock.MagicMock(DBFacade)
    return_user = User("SLACKID")
    return_team = Team("2723476", "rocket", "rocket")
    return_team.add_member("SLACKID")
    mock_facade.query.return_value = [return_user]
    mock_facade.retrieve.return_value = return_team
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_add_payload)
    mock_facade.store.assert_called_once_with(return_team)
    mock_logging.info.assert_called_once_with(("user Codertocat added "
                                               "to rocket"))
    assert rsp == "added slack ID SLACKID"
    assert code == 200
Ejemplo n.º 7
0
def test_handle_mem_event_rm_mult_members(mock_logging, mem_rm_payload):
    """Test that multiple members with the same github name can be deleted."""
    mock_facade = mock.MagicMock(DBFacade)
    user1 = User("SLACKUSER1")
    user2 = User("SLACKUSER2")
    user3 = User("SLACKUSER3")
    mock_facade.query.return_value = [user1, user2, user3]
    webhook_handler = MembershipEventHandler(mock_facade)
    rsp, code = webhook_handler.handle(mem_rm_payload)
    mock_facade.query\
        .assert_called_once_with(User, [('github_user_id', "21031067")])
    mock_logging.error.assert_called_once_with("Error: found github ID "
                                               "connected to multiple"
                                               " slack IDs")
    assert rsp == "Error: found github ID connected to multiple slack IDs"
    assert code == 412
Ejemplo n.º 8
0
 def __init__(self, db_facade: DBFacade, config: Config) -> None:
     """Give handlers access to the database."""
     self.__secret = config.github_webhook_secret
     self.__event_handlers = [
         OrganizationEventHandler(db_facade),
         TeamEventHandler(db_facade),
         MembershipEventHandler(db_facade)
     ]
Ejemplo n.º 9
0
def test_handle_mem_event_rm_single_member(mock_logging, mem_rm_payload):
    """Test that members removed from the mem are deleted from rocket's db."""
    mock_facade = mock.MagicMock(DBFacade)
    return_user = User("SLACKID")
    return_team = Team("2723476", "rocket", "rocket")
    return_team.add_member("21031067")
    mock_facade.query.return_value = [return_user]
    mock_facade.retrieve.return_value = return_team
    webhook_handler = MembershipEventHandler(mock_facade)
    (rsp, code) = webhook_handler.handle(mem_rm_payload)
    mock_facade.query\
        .assert_called_once_with(User, [('github_user_id', "21031067")])
    mock_facade.retrieve \
        .assert_called_once_with(Team, "2723476")
    mock_facade.store.assert_called_once_with(return_team)
    mock_logging.info.assert_called_once_with("deleted slack user SLACKID"
                                              " from rocket")
    assert not return_team.has_member("21031067")
    assert rsp == "deleted slack ID SLACKID from rocket"
    assert code == 200
Ejemplo n.º 10
0
class TestMembershipHandles(TestCase):
    def setUp(self):
        self.team = 'rocket'
        self.teamid = 395830
        self.member = 'theflatearth'
        self.memberid = 3058493
        self.add_payload = mem_default_payload(self.team, self.teamid,
                                               self.member, self.memberid)
        self.rm_payload = mem_default_payload(self.team, self.teamid,
                                              self.member, self.memberid)
        self.empty_payload = mem_default_payload(self.team, self.teamid,
                                                 self.member, self.memberid)

        self.add_payload['action'] = 'added'
        self.rm_payload['action'] = 'removed'
        self.empty_payload['action'] = ''

        self.u = User('U4058409')
        self.u.github_id = str(self.memberid)
        self.u.github_username = self.member
        self.t = Team(str(self.teamid), self.team, self.team.capitalize())
        self.db = MemoryDB(users=[self.u], teams=[self.t])

        self.gh = mock.Mock()
        self.conf = mock.Mock()
        self.conf.github_team_all = 'all'
        self.webhook_handler = MembershipEventHandler(self.db, self.gh,
                                                      self.conf)

    def test_handle_mem_event_add_member(self):
        rsp, code = self.webhook_handler.handle(self.add_payload)
        self.assertEqual(rsp, f'added slack ID {self.u.slack_id}')
        self.assertEqual(code, 200)

    def test_handle_mem_event_add_member_not_found_in_db(self):
        self.db.users = {}
        rsp, code = self.webhook_handler.handle(self.add_payload)
        self.assertEqual(rsp, f'could not find user {self.member}')
        self.assertEqual(code, 200)

    def test_handle_mem_event_rm_member(self):
        self.t.add_member(self.u.github_id)
        rsp, code = self.webhook_handler.handle(self.rm_payload)
        self.assertFalse(self.t.has_member(self.u.github_id))
        self.assertIn(self.u.slack_id, rsp)
        self.assertEqual(code, 200)

    def test_handle_mem_event_rm_member_missing_from_team(self):
        rsp, code = self.webhook_handler.handle(self.rm_payload)
        self.assertEqual(rsp,
                         f'slack user {self.u.slack_id} not in {self.team}')
        self.assertEqual(code, 200)

    def test_handle_mem_event_rm_member_missing_from_db(self):
        self.db.users = {}
        rsp, code = self.webhook_handler.handle(self.rm_payload)
        self.assertEqual(rsp, f'could not find user {self.memberid}')
        self.assertEqual(code, 200)

    def test_handle_mem_event_rm_multiple_members(self):
        clone = User('Uclones')
        clone.github_id = str(self.memberid)
        clone.github_username = self.member
        self.db.users['Uclones'] = clone
        rsp, code = self.webhook_handler.handle(self.rm_payload)
        self.assertEqual(
            rsp, 'Error: found github ID connected to multiple slack IDs')
        self.assertEqual(code, 200)

    def test_handle_mem_event_invalid_action(self):
        rsp, code = self.webhook_handler.handle(self.empty_payload)
        self.assertEqual(rsp, 'Unsupported action triggered, ignoring.')
        self.assertEqual(code, 202)
Ejemplo n.º 11
0
def test_org_supported_action_list():
    """Confirm the supported action list of the handler."""
    mock_facade = mock.MagicMock(DBFacade)
    webhook_handler = MembershipEventHandler(mock_facade)
    assert webhook_handler.supported_action_list == ["removed", "added"]