def setUp(self): self.team = 'rocket' self.teamid = 4934950 self.newteam = 'someteam' self.newteamid = 4028940 self.created_payload = team_default_payload(self.newteam, self.newteamid) self.deleted_payload = team_default_payload(self.team, self.teamid) self.edited_payload = team_default_payload(self.team, self.teamid) self.added_to_repo_payload = team_default_payload( self.team, self.teamid) self.rm_from_repo_payload = team_default_payload( self.team, self.teamid) self.empty_payload = team_default_payload(self.team, self.teamid) self.created_payload['action'] = 'created' self.deleted_payload['action'] = 'deleted' self.edited_payload['action'] = 'edited' self.added_to_repo_payload['action'] = 'added_to_repository' self.rm_from_repo_payload['action'] = 'removed_from_repository' self.empty_payload['action'] = '' self.t = Team(str(self.teamid), self.team, self.team.capitalize()) self.db = MemoryDB(teams=[self.t]) self.gh = mock.Mock() self.conf = mock.Mock() self.webhook_handler = TeamEventHandler(self.db, self.gh, self.conf)
def test_handle_team_event_edit_team(team_edited_payload): """Test that teams can be edited if they are in the db.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_edited_payload) assert rsp == "updated team with id 2723476" assert code == 200
def test_handle_team_event_rm_from_repo(team_rm_from_repository_payload): """Test that rocket knows when team is removed from a repo.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) rsp, code = \ webhook_handler.handle(team_rm_from_repository_payload) assert rsp == "team with id 2723476 removed repository Hello-World" assert code == 200
def test_handle_team_event_deleted_miss(team_deleted_payload): """Test that attempts to delete a missing team are handled.""" mock_facade = mock.MagicMock(DBFacade) mock_facade.retrieve.side_effect = LookupError webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_deleted_payload) assert rsp == "team with github id 2723476 not found" assert code == 404
def test_handle_team_event_delete_team(team_deleted_payload): """Test that teams can be deleted if they are in the db.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_deleted_payload) mock_facade.delete.assert_called_once_with(Team, "2723476") assert rsp == "deleted team with github id 2723476" assert code == 200
def test_handle_team_event_create_update(mock_logging, team_created_payload): """Test that teams can be updated if they are in the db.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_created_payload) mock_logging.warning.assert_called_with(("team github with id 2723476 " "already exists.")) mock_facade.store.assert_called_once() assert rsp == "created team with github id 2723476" assert code == 200
def test_handle_team_event_created_team(mock_logging, team_created_payload): """Test that teams can be created if they are not in the db.""" mock_facade = mock.MagicMock(DBFacade) mock_facade.retrieve.side_effect = LookupError webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_created_payload) mock_logging.debug.assert_called_with(("team github with id 2723476 " "added to organization.")) mock_facade.store.assert_called_once() assert rsp == "created team with github id 2723476" assert code == 200
def test_org_supported_action_list(): """Confirm the supported action list of the handler.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) assert webhook_handler.supported_action_list == [ "created", "deleted", "edited", "added_to_repository", "removed_from_repository" ]
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) ]
class TestTeamHandles(TestCase): def setUp(self): self.team = 'rocket' self.teamid = 4934950 self.newteam = 'someteam' self.newteamid = 4028940 self.created_payload = team_default_payload(self.newteam, self.newteamid) self.deleted_payload = team_default_payload(self.team, self.teamid) self.edited_payload = team_default_payload(self.team, self.teamid) self.added_to_repo_payload = team_default_payload( self.team, self.teamid) self.rm_from_repo_payload = team_default_payload( self.team, self.teamid) self.empty_payload = team_default_payload(self.team, self.teamid) self.created_payload['action'] = 'created' self.deleted_payload['action'] = 'deleted' self.edited_payload['action'] = 'edited' self.added_to_repo_payload['action'] = 'added_to_repository' self.rm_from_repo_payload['action'] = 'removed_from_repository' self.empty_payload['action'] = '' self.t = Team(str(self.teamid), self.team, self.team.capitalize()) self.db = MemoryDB(teams=[self.t]) self.gh = mock.Mock() self.conf = mock.Mock() self.webhook_handler = TeamEventHandler(self.db, self.gh, self.conf) def test_handle_team_event_create_team(self): rsp, code = self.webhook_handler.handle(self.created_payload) self.assertEqual(rsp, f'created team with github id {self.newteamid}') self.assertEqual(code, 200) team = self.db.retrieve(Team, str(self.newteamid)) self.assertEqual(team.github_team_name, self.newteam) def test_handle_team_event_create_update(self): self.t.github_team_id = str(self.newteamid) self.db.teams = {str(self.newteamid): self.t} self.assertNotEqual(self.t.github_team_name, self.newteam) rsp, code = self.webhook_handler.handle(self.created_payload) self.assertEqual(rsp, f'created team with github id {self.newteamid}') self.assertEqual(code, 200) self.assertEqual(self.t.github_team_name, self.newteam) def test_handle_team_event_delete_team(self): rsp, code = self.webhook_handler.handle(self.deleted_payload) self.assertEqual(rsp, f'deleted team with github id {self.teamid}') self.assertEqual(code, 200) self.assertNotIn(self.t, self.db.teams.values()) def test_handle_team_event_deleted_not_in_db(self): self.db.teams = {} rsp, code = self.webhook_handler.handle(self.deleted_payload) self.assertEqual(rsp, f'team with github id {self.teamid} not found') self.assertEqual(code, 404) def test_handle_team_event_edit_team(self): rsp, code = self.webhook_handler.handle(self.edited_payload) self.assertEqual(rsp, f'updated team with id {self.teamid}') self.assertEqual(code, 200) def test_handle_team_event_edit_not_in_db(self): self.db.teams = {} rsp, code = self.webhook_handler.handle(self.edited_payload) self.assertEqual(rsp, f'team with github id {self.teamid} not found') self.assertEqual(code, 404) def test_handle_team_event_add_to_repo(self): rsp, code = self.webhook_handler.handle(self.added_to_repo_payload) self.assertEqual( rsp, f'team with id {self.teamid} added to repository Hello-World') self.assertEqual(code, 200) def test_handle_team_event_rm_from_repo(self): rsp, code = self.webhook_handler.handle(self.rm_from_repo_payload) self.assertEqual( rsp, f'team with id {self.teamid} removed repository Hello-World') self.assertEqual(code, 200) def test_handle_team_event_empty_payload(self): """Test empty/invalid payloads can be handled.""" rsp, code = self.webhook_handler.handle(self.empty_payload) self.assertEqual(rsp, 'invalid payload')
def test_handle_team_event_empty_payload(team_empty_payload): """Test that empty/invalid payloads can be handled.""" mock_facade = mock.MagicMock(DBFacade) webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_empty_payload) assert rsp == "invalid payload"
def test_handle_team_event_edit_miss(team_edited_payload): """Test that attempts to edit a missing team are handled.""" mock_facade = mock.MagicMock(DBFacade) mock_facade.retrieve.side_effect = LookupError webhook_handler = TeamEventHandler(mock_facade) rsp, code = webhook_handler.handle(team_edited_payload)