diff --git a/api/command/__init__.py b/api/command/__init__.py deleted file mode 100644 index ff5f1c89..00000000 --- a/api/command/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -"""Encapsulate the common business logic of commands.""" -import api.command.core as core - -CommandApis = core.CommandApis diff --git a/api/command/core.py b/api/command/core.py deleted file mode 100644 index 3acd603a..00000000 --- a/api/command/core.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Encapsulate the common business logic of all command types.""" -from db.facade import DBFacade -from interface.github import GithubInterface -from interface.slack import Bot - -from api.command.mixins import UserCommandApis, TeamCommandApis - - -class CommandApis(UserCommandApis, TeamCommandApis): - """Encapsulate the various APIs of each command type.""" - - def __init__(self, - db_facade: DBFacade, - gh_interface: GithubInterface, - slack_client: Bot) -> None: - """Initialize the dependencies of command APIs.""" - self._db_facade = db_facade - self._gh_interface = gh_interface - self._slack_client = slack_client diff --git a/api/command/mixins/__init__.py b/api/command/mixins/__init__.py deleted file mode 100644 index 61474742..00000000 --- a/api/command/mixins/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -"""Define the APIs of the various command types.""" -import api.command.mixins.user as user -import api.command.mixins.team as team - -UserCommandApis = user.UserCommandApis -TeamCommandApis = team.TeamCommandApis diff --git a/api/command/mixins/team.py b/api/command/mixins/team.py deleted file mode 100644 index cfe394ed..00000000 --- a/api/command/mixins/team.py +++ /dev/null @@ -1,493 +0,0 @@ -"""Encapsulate the common business logic of team commands.""" -import logging -from typing import List -from app.model import User, Team -from interface.github import GithubAPIException, GithubInterface -from interface.slack import SlackAPIError, Bot -from utils.slack_parse import check_permissions -import db.utils as db_utils -from db.facade import DBFacade - - -class TeamCommandApis: - """Encapsulate the various APIs for team command logic.""" - - def __init__(self): - """Declare the interfaces needed.""" - self._db_facade: DBFacade = None - self._gh_interface: GithubInterface = None - self._slack_client: Bot = None - - def team_list(self) -> List[Team]: - """ - Return a list of all teams. - - :return: List of ``Team`` objects representing all teams - in the workspace - """ - logging.info("Team list command API called") - teams = self._db_facade.query(Team) - logging.info(f"{len(teams)} teams found") - return teams - - def team_view(self, gh_team_name: str) -> Team: - """ - View team information from the database. - - :param gh_team_name: name of the team to view - :raises: LookupError if a team with the specified team name - does not exist - :raises: RuntimeError if more than one team has the specified - team name - :return: ``Team`` object if found - """ - logging.info("Team view command API called") - return db_utils.get_team_by_name(self._db_facade, gh_team_name) - - def team_create(self, - caller_id: str, - gh_team_name: str, - display_name: str = None, - platform: str = None, - channel: str = None, - lead_id: str = None) -> bool: - """ - Create a team both in the Rocket database and Github organization. - - :param caller_id: Slack ID of the user who is calling the API - :param gh_team_name: desired team name to give the team on Github - :param display_name: display name to give the team when displayed - in various places - :param platform: main platform this team's projects are based on - :param channel: name of the Slack channel whose channel members - are to be added to the team - its members will be - added to the team in the Rocket database and added - to the Github team as well - :param lead_id: Slack ID of the user who will be made the lead of - this team - :raises: LookupError if the calling user or tech lead cannot be found - in the database - :raises: PermissionError if the calling user has insufficient - permissions to create a team, or if the specified user with - lead_id does not have the permission to be a lead - :raises: SlackAPIError if a channel name is provided by an error - is encountered retrieving the members of that channel - :raises: GithubAPIException if an error occurs on team creation or - Github team member addition - :raises: Exception for any other generic error - :return: True if the team creation was successful, False otherwise - """ - logging.info("Team create command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - if not check_permissions(command_user, None): - msg = f"Calling user with Slack ID {caller_id} has permission" \ - f" level {str(command_user.permissions_level)}, " \ - "insufficient for creating a team!" - logging.error(msg) - raise PermissionError(msg) - - if not command_user.github_id: - msg = f"User {command_user.slack_id} has yet to register a"\ - f" Github username in this system."\ - f" Register with `/rocket user edit --github username`." - logging.error(msg) - raise Exception(msg) - - gh_team_id = str(self._gh_interface.org_create_team(gh_team_name)) - logging.debug(f"Github team {gh_team_name} created with " - f"Github team ID {gh_team_id}") - team = Team(gh_team_id, gh_team_name, "") - - if display_name is not None: - logging.debug(f"Attaching display name {display_name} " - f"to {gh_team_name}") - team.display_name = display_name - - if platform is not None: - logging.debug(f"Attaching platform {platform} to {gh_team_name}") - team.platform = platform - - if channel is not None: - logging.debug(f"Adding channel members of #{channel} " - f"to {gh_team_name}") - try: - channel_member_ids = \ - self._slack_client.get_channel_users(channel) - logging.debug(f"Member IDs of members found in #{channel}: " - f"{channel_member_ids}") - except SlackAPIError as e: - msg = f"Channel member query on channel #{channel} failed: " \ - f"{e.error}" - logging.error(msg) - raise SlackAPIError(msg) - - channel_members = \ - self._db_facade.bulk_retrieve(User, - list(channel_member_ids.keys())) - - if len(channel_members) is not len(channel_member_ids): - retrieved_members_ids = [member.slack_id - for member in channel_members] - unaccounted_member_ids = [member_id for member_id - in channel_member_ids - if member_id - not in retrieved_members_ids] - logging.warning("Users not found for following Slack IDs: " - f"{unaccounted_member_ids}") - - for member in channel_members: - self._gh_interface.add_team_member(member.github_username, - gh_team_id) - team.add_member(member.github_id) - logging.debug(f"Member with ID {member.slack_id} added " - f"to {gh_team_name}") - else: - self._gh_interface.add_team_member(command_user.github_username, - gh_team_id) - team.add_member(command_user.github_id) - logging.debug(f"Calling user with ID {command_user.slack_id} " - f"added to {gh_team_name}") - - if lead_id is not None: - lead = self._db_facade.retrieve(User, lead_id) - - if check_permissions(lead, None): - lead_in_team = self._gh_interface.has_team_member( - lead.github_username, - gh_team_id) - if not lead_in_team: - self._gh_interface.add_team_member(lead.github_username, - gh_team_id) - - team.add_member(lead.github_id) - team.add_team_lead(lead.github_id) - logging.debug(f"User with ID {lead_id} set as tech lead of " - f"{gh_team_name}") - else: - msg = f"User specified with lead ID {lead_id} has" \ - f" permission level {str(lead.permissions_level)}, " \ - "insufficient to lead a team!" - logging.error(msg) - raise PermissionError(msg) - else: - team.add_team_lead(command_user.github_id) - logging.debug(f"Calling user with ID {command_user.github_id} set" - f" as tech lead of {gh_team_name}") - - created = self._db_facade.store(team) - return created - - def team_add(self, - caller_id: str, - add_user_id: str, - gh_team_name: str) -> bool: - """ - Add a user to a team. - - :param caller_id: Slack ID of user who called the API - :param add_user_id: Slack ID of user to add to a team - :param gh_team_name: Github team name of the team to add a user to - :raises: LookupError if the calling user, user to add, - or specified team cannot be found in the database - :raises: RuntimeError if more than one team has the specified - team name - :raises: PermissionError if the calling user has insufficient - permission to add members to the specified team - :raises: GithubAPIException if an error occurs when adding the user to - the Github team - :return: True if adding the member to the team was successful, - False otherwise - """ - logging.info("Team add command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - team = db_utils.get_team_by_name(self._db_facade, gh_team_name) - - if not check_permissions(command_user, team): - msg = f"User with ID {caller_id} has insufficient permissions" \ - f" to add members to team {gh_team_name}" - logging.error(msg) - raise PermissionError(msg) - - add_user = self._db_facade.retrieve(User, add_user_id) - logging.debug(f"User to add: {add_user.__str__()}") - - self._gh_interface.add_team_member(add_user.github_username, - team.github_team_id) - team.add_member(add_user.github_id) - - added = self._db_facade.store(team) - return added - - def team_remove(self, - caller_id: str, - gh_team_name: str, - rem_user_id: str) -> bool: - """ - Remove the specified user from a team. - - If the user is also a team lead, removes team lead status from Team. - - :param caller_id: Slack ID of user who called command - :param gh_team_name: Github team name of the team to remove user from - :param rem_user_id: Slack ID of user to remove from team - :raises: LookupError if the calling user, user to remove, - or specified team cannot be found in the database - :raises: RuntimeError if more than one team has the specified - team name - :raises: PermissionError if the calling user has insufficient - permission to remove members to the specified team - :raises: GithubAPIException if an error occured removing the user from - the Github team - :return: True if user was removed from team successfully, - False otherwise - """ - logging.info("Team remove command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - team = db_utils.get_team_by_name(self._db_facade, gh_team_name) - - if not check_permissions(command_user, team): - msg = f"User with ID {caller_id} has insufficient permissions" \ - f" to remove members to team {gh_team_name}" - logging.error(msg) - raise PermissionError(msg) - - rem_user = self._db_facade.retrieve(User, rem_user_id) - logging.debug(f"User to remove: {rem_user.__str__()}") - - if not self._gh_interface.has_team_member(rem_user.github_username, - team.github_team_id): - msg = f"Github user {rem_user.github_username} not a member" \ - f" of Github team with ID {team.github_team_id}" - logging.error(msg) - raise GithubAPIException(msg) - self._gh_interface.remove_team_member(rem_user.github_username, - team.github_team_id) - team.discard_member(rem_user.github_id) - if team.has_team_lead(rem_user.github_id): - team.discard_team_lead(rem_user.github_id) - - removed = self._db_facade.store(team) - return removed - - def team_edit(self, - caller_id: str, - gh_team_name: str, - display_name: str = None, - platform: str = None) -> bool: - """ - Edit the properties of a specific team. - - Team leads can only edit the teams that they are a part of, but admins - can edit any team. - - :param caller_id: Slack ID of user who called command - :param display_name: display name to change to if not None - :param platform: platform to change to if not None - :raises: LookupError if the calling user or team to edit - could not be found - :raises: RuntimeError if more than one team has the specified - team name - :raises: PermissionError if the calling user does not have sufficient - permissions to edit the specified team - :return: True if the edit was successful, False otherwise - """ - logging.info("Team edit command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - team = db_utils.get_team_by_name(self._db_facade, gh_team_name) - - if not check_permissions(command_user, team): - msg = f"User with ID {caller_id} has insufficient permissions" \ - f" to edit team {gh_team_name}" - logging.error(msg) - raise PermissionError(msg) - - if display_name is not None: - logging.debug(f"Attaching display name {display_name} " - f"to {gh_team_name}") - team.display_name = display_name - - if platform is not None: - logging.debug(f"Attaching platform {platform} to {gh_team_name}") - team.platform = platform - - edited = self._db_facade.store(team) - return edited - - def team_lead(self, - caller_id: str, - lead_id: str, - gh_team_name: str, - remove: bool = False) -> bool: - """ - Add a user as a team lead, and add them to team if not already added. - - :param caller_id: Slack ID of user who called command - :param lead_id: Slack ID of user to declare as team lead - :param gh_team_name: Github team name of team to add a lead to - :param remove: if True, removes the user as team lead of the team - :raises: LookupError if the calling user, the team to add a lead to - could not be found, the user is not on the team, or the user - is not a lead on the team - :raises: RuntimeError if more than one team has the specified - team name - :raises: PermissionError if the calling user does not have sufficient - permissions to add a lead to the specified team - :returns: True if removal was successful, False otherwise - """ - logging.info("Team lead command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - team = db_utils.get_team_by_name(self._db_facade, gh_team_name) - - if not check_permissions(command_user, team): - msg = f"User with ID {caller_id} has insufficient permissions" \ - f" to add lead to team {gh_team_name}" - logging.error(msg) - raise PermissionError(msg) - - lead_user = self._db_facade.retrieve(User, lead_id) - logging.debug(f"User to add as lead: {lead_user.__str__()}") - - if remove: - if not team.has_member(lead_user.github_id): - msg = f"User with Github ID {lead_user.github_id} not a " \ - "member of specified team" - logging.error(msg) - raise LookupError(msg) - if team.has_team_lead(lead_user.github_id): - team.discard_team_lead(lead_user.github_id) - discarded = self._db_facade.store(team) - return discarded - else: - msg = f"User with Github ID {lead_user.github_id} not a " \ - "lead of specified team" - logging.error(msg) - raise LookupError(msg) - else: - if not team.has_member(lead_user.github_id): - team.add_member(lead_user.github_id) - self._gh_interface.add_team_member(lead_user.github_username, - team.github_team_id) - team.add_team_lead(lead_user.github_id) - added = self._db_facade.store(team) - return added - - def team_delete(self, - caller_id: str, - gh_team_name: str) -> None: - """ - Permanently delete a team. - - :param gh_team_name: Github team name of the team to delete - :param caller_id: Slack ID of user who called command - :raises: LookupError if the calling user or the team to delete could - not be found - :raises: RuntimeError if more than one team has the specified - team name - :raises: PermissionError if the calling user does not have sufficient - permissions to delete the specified team - """ - logging.info("Team delete command API called") - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - team = db_utils.get_team_by_name(self._db_facade, gh_team_name) - - if not check_permissions(command_user, team): - msg = f"User with ID {caller_id} has insufficient permissions" \ - f" to delete team {gh_team_name}" - logging.error(msg) - raise PermissionError(msg) - - self._gh_interface.org_delete_team(int(team.github_team_id)) - - self._db_facade.delete(Team, team.github_team_id) - logging.info(f"{gh_team_name} successfully deleted") - - def team_refresh(self, caller_id: str) -> bool: - """ - Ensure that the local team database is the same as Github's. - - In the event that our local team database is outdated compared to - the teams on Github, this command can be called to fix things. - - :param caller_id: Slack ID of the user calling the command - :raises: LookupError if the calling user cannot be found - :raises: PermissionError if the calling user has insufficient - permissions to refresh the local database - :raises: GithubAPIException if there was a failure in fetching - Github team information - :returns: True if synchronization was successful, False otherwise - """ - logging.info("Team refresh command API called") - num_changed = 0 - num_added = 0 - num_deleted = 0 - - command_user = self._db_facade.retrieve(User, caller_id) - logging.debug(f"Calling user: {command_user.__str__()}") - - if not check_permissions(command_user, None): - msg = f"User with ID {caller_id} has insufficient permissions" \ - " to refresh the local team database" - logging.error(msg) - raise PermissionError(msg) - - local_teams: List[Team] = self._db_facade.query(Team) - remote_teams: List[Team] = self._gh_interface.org_get_teams() - local_team_dict = dict((team.github_team_id, team) - for team in local_teams) - remote_team_dict = dict((team.github_team_id, team) - for team in remote_teams) - - # remove teams not in github anymore - for local_id in local_team_dict: - if local_id not in remote_team_dict: - self._db_facade.delete(Team, local_id) - logging.debug(f"Team with Github ID {local_id} deleted") - num_deleted += 1 - - # add teams to db that are in github but not in local database - for remote_id in remote_team_dict: - remote_team = remote_team_dict[remote_id] - if remote_id not in local_team_dict: - stored = self._db_facade.store(remote_team) - if stored: - logging.debug("Created new team with " - f"Github ID {remote_id}") - num_added += 1 - else: - logging.error("Failed to create new team with " - f"Github ID {remote_id}") - return False - else: - # and finally, if a local team differs, update it - local_team = local_team_dict[remote_id] - if local_team.github_team_name != \ - remote_team.github_team_name \ - or local_team.members != remote_team.members: - # update the old team, to retain additional parameters - local_team.github_team_name = remote_team.github_team_name - local_team.members = remote_team.members - edited = self._db_facade.store(local_team) - if edited: - logging.debug("Successfully edited team with " - f"Github ID {remote_id}") - num_changed += 1 - else: - logging.error("Failed to edit team with" - f"Github ID {remote_id}") - return False - - logging.info(f"{num_changed} teams changed, {num_added} added, " - f"{num_deleted} deleted. Wonderful.") - return True diff --git a/api/command/mixins/user.py b/api/command/mixins/user.py deleted file mode 100644 index eb603a0f..00000000 --- a/api/command/mixins/user.py +++ /dev/null @@ -1,170 +0,0 @@ -"""Encapsulate the common business logic of user commands.""" -import logging -from app.model import User, Permissions -from utils.slack_parse import escape_email -from db.facade import DBFacade -from interface.github import GithubInterface - - -class UserCommandApis: - """Encapsulate the various APIs for user command logic.""" - - def __init__(self): - """Declare the interfaces needed.""" - self._db_facade: DBFacade = None - self._gh_interface: GithubInterface = None - - def user_edit(self, - caller_id: str, - member: str = None, - name: str = None, - email: str = None, - pos: str = None, - github: str = None, - major: str = None, - bio: str = None, - permission: Permissions = None) -> bool: - """ - Edit a user in the database. - - If ``member`` is not None, and the calling user is an admin, this - function edits the user with ID ``member``. Otherwise, the calling - user is edited. - - :param caller_id: Slack ID of the user who is calling the API - :param member: Slack ID of the user to edit - :param name: display name to change to for the user - :param email: email to change to for the user - :param pos: position to change to for the user - :param github: Github username to change to for the user - :param major: major to change to for the user - :param bio: bio to change to for the user - :param permission: permission level to change to for the user - :raises: LookupError if the calling user or the desired user to edit - could not be found in the database - :raises: PermissionError if the calling user is attempting to edit - another user and they are not an admin, or are trying to - edit their own permission level without being an admin - :raises: GithubAPIException if setting the Github username fails to - add the corresponding user to the Github organization - :return: True if user was successfully edited in the database, - False otherwise - """ - logging.info("User edit command API called") - - calling_user = self._db_facade.retrieve(User, caller_id) - - is_admin = calling_user.permissions_level == Permissions.admin - edited_user = calling_user - - if member is not None: - if is_admin: - edited_user = self._db_facade.retrieve(User, member) - else: - msg = f"Calling user with Slack ID {caller_id} has" \ - " permission level " \ - f"{str(calling_user.permissions_level)}, insufficient " \ - "for editing another user!" - logging.error(msg) - raise PermissionError(msg) - else: - edited_user = calling_user - - if permission and is_admin: - edited_user.permissions_level = permission - elif permission and not is_admin: - msg = f"Calling user with Slack ID {caller_id} has permission" \ - f" level {str(calling_user.permissions_level)}, " \ - "insufficient for editing own permission level!" - logging.error(msg) - raise PermissionError(msg) - if github: - github_id = self._gh_interface.org_add_member(github) - edited_user.github_username = github - edited_user.github_id = github_id - if name: - edited_user.name = name - if email: - edited_user.email = escape_email(email) - if pos: - edited_user.position = pos - if major: - edited_user.major = major - if bio: - edited_user.biography = bio - - return self._db_facade.store(edited_user) - - def user_delete(self, - caller_id: str, - del_user_id: str) -> None: - """ - Delete a user from the database. - - Delete user with ``slack_id`` if user with ``caller_id`` has admin - permission level. - - **Note**: users can delete themselves. - - :param caller_id: Slack ID of user who is calling the command - :param del_user_id: Slack ID of user who is being deleted - :raises: LookupError if the calling user or the desired user to delete - could not be found in the database - :raises: PermissionError if the calling user is attempting delete edit - another user and they are not an admin - """ - calling_user = self._db_facade.retrieve(User, caller_id) - - if calling_user.permissions_level == Permissions.admin: - self._db_facade.delete(User, del_user_id) - logging.info(f"Deleted user with Slack ID {del_user_id}") - else: - msg = f"Calling user with Slack ID {caller_id} has permission" \ - f" level {str(calling_user.permissions_level)}, " \ - "insufficient for deleting a user!" - logging.error(msg) - raise PermissionError(msg) - - def user_view(self, - caller_id: str, - view_user_id: str = None) -> User: - """ - View user information from the database. - - If ``view_user_id`` is None, return information of ``caller_id``, else - return information of ``view_user_id``. - - :param caller_id: Slack ID of the user who is calling the command - :param view_user_id: Slack ID of user whose info is being retrieved - :raises: LookupError if the calling user or the desired user to view - could not be found in the database - :return: ``User`` object whose information was retrieved - """ - user_to_view_id = caller_id if view_user_id is None else view_user_id - return self._db_facade.retrieve(User, user_to_view_id) - - def user_add(self, - add_user_id: str, - use_force: bool = False) -> bool: - """ - Add a user to the database via user ID. - - :param add_user_id: Slack ID of the user to be added - :param use_force: If this is set, we store the user even iff they are - already added in the database - :raises: RuntimeError if the user has already been added and - ``user_force`` is False - :return: True if the user was successfully added, False otherwise - """ - # Try to look up and avoid overwriting if we are not using force - if not use_force: - try: - self._db_facade.retrieve(User, add_user_id) - msg = f"User with Slack ID {add_user_id} already exists" - logging.error(msg) - raise RuntimeError(msg) - except LookupError: - logging.info(f"User with Slack ID {add_user_id} " - "not in database") - - return self._db_facade.store(User(add_user_id)) diff --git a/app/controller/command/commands/user.py b/app/controller/command/commands/user.py index 126448d0..0e98f7ec 100644 --- a/app/controller/command/commands/user.py +++ b/app/controller/command/commands/user.py @@ -21,7 +21,7 @@ class UserCommand(Command): permission_error = "You do not have the sufficient " \ "permission level for this command!" lookup_error = "Lookup error! User not found!" - noghid_deepdive = 'Specified user does not have a Github account'\ + viewinspect_noghid = 'Specified user does not have a Github account'\ 'registered with Rocket.' delete_text = "Deleted user with Slack ID: " desc = f"for dealing with {command_name}s" @@ -178,7 +178,7 @@ def handle(self, else: return self.get_help(), 200 - def deepdive_helper(self, user: User): + def viewinspect_helper(self, user: User): """ Return an attachment that is the membership info. @@ -207,7 +207,7 @@ def deepdive_helper(self, user: User): ''' else: ret = f''' -{self.noghid_deepdive} +{self.viewinspect_noghid} ''' return { @@ -360,7 +360,7 @@ def view_helper(self, if param_list['inspect']: return {'attachments': [ user.get_attachment(), - self.deepdive_helper(user) + self.viewinspect_helper(user) ]}, 200 else: return {'attachments': [user.get_attachment()]}, 200 diff --git a/tests/api/__init__.py b/tests/api/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/api/command/__init__.py b/tests/api/command/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/api/command/mixins/__init__.py b/tests/api/command/mixins/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/api/command/mixins/team_test.py b/tests/api/command/mixins/team_test.py deleted file mode 100644 index beb4e91b..00000000 --- a/tests/api/command/mixins/team_test.py +++ /dev/null @@ -1,841 +0,0 @@ -"""Test the common business logic for the team command APIs.""" -from api.command import CommandApis -from db import DBFacade -from interface.github import GithubInterface, GithubAPIException -from interface.slack import Bot, SlackAPIError -from app.model import User, Team, Permissions -from unittest import mock, TestCase - - -class TestTeamCommandApis(TestCase): - """Test Case for TeamCommandApi methods.""" - - def setUp(self) -> None: - """Set up the test case environment.""" - self.mock_facade = mock.MagicMock(DBFacade) - self.mock_github = mock.MagicMock(GithubInterface) - self.mock_slack = mock.MagicMock(Bot) - self.testapi = CommandApis(self.mock_facade, - self.mock_github, - self.mock_slack) - - self.regular_user = User("regular") - self.regular_user.permissions_level = Permissions.member - self.regular_user.github_id = "reg_gh_id" - self.regular_user.github_username = "reg_username" - self.lead_user = User("lead") - self.lead_user.permissions_level = Permissions.team_lead - self.lead_user.github_id = "lead_gh_id" - self.lead_user.github_username = "lead_username" - self.admin_user = User("admin") - self.admin_user.permissions_level = Permissions.admin - self.admin_user.github_id = "admin_gh_id" - self.admin_user.github_username = "admin_username" - - self.team1 = Team("1", "gh1", "name1") - self.team2 = Team("2", "gh2", "name2") - self.team3 = Team("3", "gh3", "name3") - self.team3_dup = Team("4", "gh3", "name4") - - def mock_facade_retrieve_side_effect(*args, **kwargs): - """Mock behavior of the retrieve mock facade function.""" - slack_id = args[1] - if slack_id == self.regular_user.slack_id: - return self.regular_user - elif slack_id == self.lead_user.slack_id: - return self.lead_user - elif slack_id == self.admin_user.slack_id: - return self.admin_user - else: - raise LookupError - - self.mock_facade.retrieve.side_effect = \ - mock_facade_retrieve_side_effect - - def mock_facade_query_side_effect(*args, **kwargs): - """Mock behavior of the query mock facade function.""" - query_teams = [] - try: - params = args[1] - except IndexError: - query_teams = [ - self.team1, - self.team2, - self.team3, - self.team3_dup - ] - else: - assert len(params) == 1, \ - "Woops, too many parameters for this mock query!" - attribute, value = params[0] - assert attribute == "github_team_name", \ - "Woops, this mock can only handle `github_team_name`!" - - if value == "gh1": - query_teams = [self.team1] - elif value == "gh2": - query_teams = [self.team2] - elif value == "gh3": - query_teams = [ - self.team3, - self.team3_dup - ] - - return query_teams - - self.mock_facade.query.side_effect = \ - mock_facade_query_side_effect - - # In most cases, store will need to return True for tests - self.mock_facade.store.return_value = True - - def test_list(self) -> None: - """Test list team command API.""" - all_teams = self.testapi.team_list() - self.assertListEqual(all_teams, - [ - self.team1, - self.team2, - self.team3, - self.team3_dup - ]) - - def test_view_missing_team(self) -> None: - """Test view team command API with missing team.""" - self.mock_facade.query.return_value = [] - try: - self.testapi.team_view("no_team") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_view_multiple_teams(self) -> None: - """Test view team command API with multiple matching teams.""" - try: - self.testapi.team_view("gh3") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_view_single_team(self) -> None: - """Test view team command API with singular matching team.""" - team = self.testapi.team_view("gh1") - self.assertEqual(team, self.team1) - - def test_create_missing_creator(self) -> None: - """Test create team command API with missing calling user.""" - try: - self.testapi.team_create("no_user", "team_name") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_create_non_lead_creator(self) -> None: - """Test create team command API with non lead calling user.""" - try: - self.testapi.team_create("regular", "team_name") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_create_gh_team_creation_error(self) -> None: - """Test create team command API with Github team creation error.""" - self.mock_github.org_create_team.side_effect = GithubAPIException("") - try: - self.testapi.team_create("lead", "team_name") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_create_gh_team_add_member_error(self) -> None: - """Test create team command API with Github team member add error.""" - self.mock_github.add_team_member.side_effect = GithubAPIException("") - try: - self.testapi.team_create("lead", "team_name") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_create_success(self) -> None: - """Test create team command API with successful creation.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create("lead", "team_name") - self.assertTrue(created) - stored_team = Team("team_gh_id", "team_name", "") - stored_team.add_member(self.lead_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_github.add_team_member.assert_called_once_with( - self.lead_user.github_username, "team_gh_id") - self.mock_facade.store.assert_called_once_with(stored_team) - - def test_create_with_display_name(self) -> None: - """Test create team command API with display name.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create("lead", - "team_name", - display_name="display_name") - self.assertTrue(created) - stored_team = Team("team_gh_id", "team_name", "display_name") - stored_team.add_member(self.lead_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_facade.store.assert_called_with(stored_team) - - def test_create_with_platform(self) -> None: - """Test create team command API with platform.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create("lead", - "team_name", - platform="platform") - self.assertTrue(created) - stored_team = Team("team_gh_id", "team_name", "") - stored_team.platform = "platform" - stored_team.add_member(self.lead_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_facade.store.assert_called_with(stored_team) - - def test_create_get_channel_members_error(self) -> None: - """Test create team command API with error getting channel users.""" - self.mock_slack.get_channel_users.side_effect = SlackAPIError("") - try: - self.testapi.team_create("lead", "team_name", channel="channel") - except SlackAPIError: - pass - else: - self.assertTrue(False) - - def test_create_missing_slack_user_from_channel(self) -> None: - """Test create team command API with missing channel member.""" - self.mock_slack.get_channel_users.return_value = {"missing": None} - try: - self.testapi.team_create("lead", "team_name", channel="channel") - except LookupError: - self.assertTrue(False) - else: - self.assertEqual(self.mock_github.add_team_member.call_count, 0) - - def test_create_add_channel_member_gh_team_error(self) -> None: - """Test create team command API adding channel member to Github.""" - self.mock_slack.get_channel_users.return_value = \ - {self.regular_user.slack_id: self.regular_user} - self.mock_facade.bulk_retrieve.return_value = [self.regular_user] - - self.mock_github.add_team_member.side_effect = GithubAPIException("") - - try: - self.testapi.team_create("lead", "team_name", channel="channel") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_create_add_channel_members(self) -> None: - """Test create team command API with specified channel.""" - self.mock_slack.get_channel_users.return_value = \ - {self.regular_user.slack_id: self.regular_user} - self.mock_facade.bulk_retrieve.return_value = [self.regular_user] - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create( - "lead", "team_name", channel="channel") - self.assertTrue(created) - stored_team = Team("team_gh_id", "team_name", "") - stored_team.add_member(self.regular_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_github.add_team_member.assert_called_with( - self.regular_user.github_username, "team_gh_id") - self.mock_facade.store.assert_called_once_with(stored_team) - - def test_create_missing_lead(self) -> None: - """Test create team command API with missing team lead.""" - try: - self.testapi.team_create( - "lead", "team_name", lead_id="missing") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_create_with_lead_check_team_error(self) -> None: - """Test create team command API with error from checking team.""" - self.mock_github.has_team_member.side_effect = GithubAPIException("") - try: - self.testapi.team_create( - "admin", "team_name", lead_id="lead") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_create_with_lead_not_in_gh_team(self) -> None: - """Test create team command API with lead not in Github team.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - self.mock_github.has_team_member.return_value = False - self.testapi.team_create( - "admin", "team_name", lead_id="lead") - self.mock_github.add_team_member.assert_called_with( - self.lead_user.github_username, "team_gh_id") - - def test_create_with_lead_in_gh_team(self) -> None: - """Test create team command API with lead in Github team.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - self.mock_github.has_team_member.return_value = True - self.testapi.team_create( - "admin", "team_name", lead_id="lead") - self.assertEqual(self.mock_github.add_team_member.call_count, 1) - - def test_create_with_non_lead_lead(self) -> None: - """Test create team command API with non-lead lead.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - try: - self.testapi.team_create("admin", - "team_name", - lead_id="regular") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_create_with_lead(self) -> None: - """Test create team command API with lead.""" - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create( - "admin", "team_name", lead_id="lead") - self.assertTrue(created) - stored_team = Team("team_gh_id", "team_name", "") - stored_team.add_member(self.admin_user.github_id) - stored_team.add_member(self.lead_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_facade.store.assert_called_once_with(stored_team) - - def test_create_store_fail(self) -> None: - """Test create team command API with failing store.""" - self.mock_facade.store.return_value = False - self.mock_github.org_create_team.return_value = "team_gh_id" - created = self.testapi.team_create("lead", "team_name") - self.assertFalse(created) - stored_team = Team("team_gh_id", "team_name", "") - stored_team.add_member(self.lead_user.github_id) - stored_team.add_team_lead(self.lead_user.github_id) - self.mock_github.add_team_member.assert_called_once_with( - self.lead_user.github_username, "team_gh_id") - self.mock_facade.store.assert_called_once_with(stored_team) - - def test_add_missing_adder(self) -> None: - """Test add team command API with missing calling user.""" - try: - self.testapi.team_add("no_user", "no_user_2", "team_name") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_add_missing_team(self) -> None: - """Test add team command API with missing team to add to.""" - try: - self.testapi.team_add("lead", "regular", "missing_team") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_add_non_unique_team(self) -> None: - """Test add team command API with non unique Github team name.""" - try: - self.testapi.team_add("lead", "regular", "gh3") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_add_permission_error(self) -> None: - """Test add team command API with caller without permissions.""" - try: - self.testapi.team_add("regular", "lead", "gh1") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_add_missing_new_member(self) -> None: - """Test add team command API with missing user.""" - self.team1.add_team_lead(self.lead_user.github_id) - try: - self.testapi.team_add("lead", "no_user", "gh1") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_add_gh_team_add_member_error(self) -> None: - """Test add team command API w/ error adding to Github team.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.add_team_member.side_effect = GithubAPIException("") - try: - self.testapi.team_add("lead", "regular", "gh1") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_add_success(self) -> None: - """Test add team command API successful execution.""" - self.team1.add_team_lead(self.lead_user.github_id) - added = self.testapi.team_add("lead", "regular", "gh1") - self.assertTrue(added) - self.assertTrue(self.team1.has_member(self.regular_user.github_id)) - self.mock_github.add_team_member.assert_called_once_with( - self.regular_user.github_username, - self.team1.github_team_id - ) - - def test_add_fail(self) -> None: - """Test add team command API when store fails.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_facade.store.return_value = False - added = self.testapi.team_add("lead", "regular", "gh1") - self.assertFalse(added) - - def test_remove_missing_remover(self) -> None: - """Test remove team command API with missing remover.""" - try: - self.testapi.team_remove("no_user", "gh1", "regular") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_remove_missing_team(self) -> None: - """Test remove team command API with missing team.""" - try: - self.testapi.team_remove("lead", "no_team", "regular") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_remove_non_unique_team(self) -> None: - """Test remove team command API with non unique Github team name.""" - try: - self.testapi.team_remove("lead", "gh3", "regular") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_remove_permission_error(self) -> None: - """Test remove team command API with caller without permissions.""" - try: - self.testapi.team_remove("regular", "gh1", "lead") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_remove_missing_user_to_remove(self) -> None: - """Test remove team command API with missing member to remove.""" - try: - self.testapi.team_remove("lead", "gh1", "no_user") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_remove_gh_team_not_has_member(self) -> None: - """Test remove team command API when member not in Github team.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.has_team_member.return_value = False - try: - self.testapi.team_remove("lead", "gh1", "regular") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_remove_gh_team_gh_exception(self) -> None: - """Test remove team command API when GithubAPIException is raised.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.has_team_member.side_effect = GithubAPIException("") - try: - self.testapi.team_remove("lead", "gh1", "regular") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_remove_store_fail(self) -> None: - """Test remove team command API when db store fails.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.has_team_member.return_value = True - self.mock_facade.store.return_value = False - removed = self.testapi.team_remove("lead", "gh1", "regular") - self.assertFalse(removed) - - def test_remove_success(self) -> None: - """Test remove team command API successful execution.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.has_team_member.return_value = True - removed = self.testapi.team_remove("lead", "gh1", "regular") - self.assertTrue(removed) - self.assertFalse(self.team1.has_member(self.regular_user.github_id)) - self.mock_github.remove_team_member.assert_called_once_with( - self.regular_user.github_username, - self.team1.github_team_id - ) - - def test_remove_lead_success(self) -> None: - """Test remove team command API on lead successful execution.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.mock_github.has_team_member.return_value = True - removed = self.testapi.team_remove("lead", "gh1", "lead") - self.assertTrue(removed) - self.assertFalse(self.team1.has_member(self.lead_user.github_id)) - self.assertFalse(self.team1.has_team_lead(self.lead_user.github_id)) - self.mock_github.remove_team_member.assert_called_once_with( - self.lead_user.github_username, - self.team1.github_team_id - ) - - def test_edit_missing_editor(self) -> None: - """Test edit team command API with missing calling user.""" - try: - self.testapi.team_edit("no_user", "gh1") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_edit_missing_team(self) -> None: - """Test edit team command API with missing team.""" - try: - self.testapi.team_edit("lead", "no_team") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_edit_non_unique_team(self) -> None: - """Test edit team command API with non unique Github team name.""" - try: - self.testapi.team_edit("lead", "gh3") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_edit_permission_error(self) -> None: - """Test edit team command API with caller without permissions.""" - try: - self.testapi.team_edit("regular", "gh1") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_edit_store_fail(self) -> None: - """Test edit team command API when db store fails.""" - self.mock_facade.store.return_value = False - self.team1.add_team_lead(self.lead_user.github_id) - edited = self.testapi.team_edit("lead", - "gh1", - display_name="tempname") - self.assertFalse(edited) - - def test_edit_display_name(self) -> None: - """Test edit team command API to edit team display name.""" - self.team1.add_team_lead(self.lead_user.github_id) - edited = self.testapi.team_edit("lead", - "gh1", - display_name="tempname") - self.assertTrue(edited) - self.assertEqual("tempname", self.team1.display_name) - - def test_edit_platform(self) -> None: - """Test edit team command API to edit platform.""" - self.team1.add_team_lead(self.lead_user.github_id) - edited = self.testapi.team_edit("lead", - "gh1", - platform="tempplat") - self.assertTrue(edited) - self.assertEqual("tempplat", self.team1.platform) - - def test_lead_missing_lead_assigner(self) -> None: - """Test lead team command API with missing calling user.""" - try: - self.testapi.team_lead("no_user", "lead", "gh1") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_lead_missing_team(self) -> None: - """Test lead team command API with missing team.""" - try: - self.testapi.team_lead("lead", "regular", "no_team") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_lead_non_unique_team(self) -> None: - """Test lead team command API with non unique Github team name.""" - try: - self.testapi.team_lead("lead", "regular", "gh3") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_lead_permission_error(self) -> None: - """Test lead team command API with caller without permissions.""" - try: - self.testapi.team_lead("regular", "lead", "gh1") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_lead_missing_intended_lead(self) -> None: - """Test lead team command API with missing intended lead.""" - self.team1.add_team_lead(self.lead_user.github_id) - try: - self.testapi.team_lead("lead", "no_user", "gh1") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_lead_gh_team_add_member_error(self) -> None: - """Test lead team command API with Github team member add error.""" - self.mock_github.add_team_member.side_effect = GithubAPIException("") - self.team1.add_team_lead(self.lead_user.github_id) - try: - self.testapi.team_lead("lead", "regular", "gh1") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_lead_store_fail(self) -> None: - """Test lead command API failing store.""" - self.mock_facade.store.return_value = False - self.team1.add_team_lead(self.lead_user.github_id) - lead_assigned = self.testapi.team_lead("lead", "regular", "gh1") - self.assertFalse(lead_assigned) - - def test_lead_successful(self) -> None: - """Test lead team command API setting team lead successfully.""" - self.team1.add_team_lead(self.lead_user.github_id) - lead_assigned = self.testapi.team_lead("lead", "regular", "gh1") - self.mock_github.add_team_member.assert_called_once_with( - self.regular_user.github_username, - self.team1.github_team_id - ) - self.assertTrue(lead_assigned) - self.assertTrue(self.team1.has_member(self.regular_user.github_id)) - self.assertTrue(self.team1.has_team_lead(self.regular_user.github_id)) - - def test_lead_remove_not_in_team(self) -> None: - """Test lead team command API remove a member not in a team.""" - self.team1.add_team_lead(self.lead_user.github_id) - try: - self.testapi.team_lead("lead", "regular", "gh1", remove=True) - except LookupError: - pass - else: - self.assertTrue(False) - - def test_lead_remove_not_lead_of_team(self) -> None: - """Test lead team command API remove a member not lead of team.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.team1.add_member(self.regular_user.github_id) - try: - self.testapi.team_lead("lead", "regular", "gh1", remove=True) - except LookupError: - pass - else: - self.assertTrue(False) - - def test_lead_remove_store_fail(self) -> None: - """Test lead command API removal failing store.""" - self.mock_facade.store.return_value = False - self.team1.add_team_lead(self.lead_user.github_id) - self.team1.add_member(self.regular_user.github_id) - self.team1.add_team_lead(self.regular_user.github_id) - lead_removed = self.testapi.team_lead("lead", "regular", "gh1", - remove=True) - self.assertFalse(lead_removed) - - def test_lead_remove_successful(self) -> None: - """Test lead team command API successfully remove lead from team.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.team1.add_member(self.regular_user.github_id) - self.team1.add_team_lead(self.regular_user.github_id) - lead_removed = self.testapi.team_lead("lead", "regular", "gh1", - remove=True) - self.assertTrue(lead_removed) - self.assertTrue(self.team1.has_member(self.regular_user.github_id)) - self.assertFalse(self.team1.has_team_lead(self.regular_user.github_id)) - - def test_delete_missing_deleter(self) -> None: - """Test delete team command API with missing calling user.""" - try: - self.testapi.team_delete("no_user", "gh1") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_delete_missing_team(self) -> None: - """Test delete team command API with missing team.""" - try: - self.testapi.team_delete("lead", "no_team") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_delete_non_unique_team(self) -> None: - """Test delete team command API with non unique Github team name.""" - try: - self.testapi.team_delete("lead", "gh3") - except RuntimeError: - pass - else: - self.assertTrue(False) - - def test_delete_permission_error(self) -> None: - """Test delete team command API with caller without permissions.""" - try: - self.testapi.team_delete("regular", "gh1") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_delete_gh_team_delete_team_error(self) -> None: - """Test delete team command API with Github team delete error.""" - self.mock_github.org_delete_team.side_effect = GithubAPIException("") - self.team1.add_team_lead(self.lead_user.github_id) - try: - self.testapi.team_delete("lead", "gh1") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_delete_successful(self) -> None: - """Test delete team command API deleting team successfully.""" - self.team1.add_team_lead(self.lead_user.github_id) - self.testapi.team_delete("lead", "gh1") - self.mock_github.org_delete_team.assert_called_once_with( - int(self.team1.github_team_id)) - self.mock_facade.delete.assert_called_once_with( - Team, - self.team1.github_team_id) - - def test_refresh_missing_refresher(self) -> None: - """Test refresh team command API with missing calling user.""" - try: - self.testapi.team_refresh("no_user") - except LookupError: - pass - else: - self.assertTrue(False) - - def test_refresh_permission_error(self) -> None: - """Test refresh team command API with caller without permissions.""" - try: - self.testapi.team_refresh("regular") - except PermissionError: - pass - else: - self.assertTrue(False) - - def test_refresh_gh_get_teams_error(self) -> None: - """Test refresh team command API with Github error getting teams.""" - self.mock_github.org_get_teams.side_effect = GithubAPIException("") - try: - self.testapi.team_refresh("admin") - except GithubAPIException: - pass - else: - self.assertTrue(False) - - def test_refresh_github_deleted(self) -> None: - """Test refresh team command API with teams removed from Github.""" - self.mock_github.org_get_teams.return_value = [ - self.team2, - self.team3, - self.team3_dup - ] - refreshed = self.testapi.team_refresh("admin") - self.assertTrue(refreshed) - self.mock_facade.delete.assert_called_once_with( - Team, self.team1.github_team_id) - - def test_refresh_github_added_failed_store(self) -> None: - """Test refresh team command API unable to store new Github teams.""" - team5 = Team("5", "gh5", "name5") - self.mock_github.org_get_teams.return_value = [ - self.team1, - self.team2, - self.team3, - self.team3_dup, - team5 - ] - self.mock_facade.store.return_value = False - refreshed = self.testapi.team_refresh("admin") - self.assertFalse(refreshed) - self.mock_facade.store.assert_called_once_with(team5) - - def test_refresh_github_added_success(self) -> None: - """Test refresh team command API storing new Github teams.""" - team5 = Team("5", "gh5", "name5") - self.mock_github.org_get_teams.return_value = [ - self.team1, - self.team2, - self.team3, - self.team3_dup, - team5 - ] - refreshed = self.testapi.team_refresh("admin") - self.assertTrue(refreshed) - self.mock_facade.store.assert_called_once_with(team5) - - def test_refresh_github_changed_failed_store(self) -> None: - """Test refresh team command API unable to edit teams.""" - new_team1 = Team("1", "newgh1", "name1") - new_team1.add_member(self.regular_user.github_id) - self.mock_github.org_get_teams.return_value = [ - new_team1, - self.team2, - self.team3, - self.team3_dup, - ] - self.mock_facade.store.return_value = False - refreshed = self.testapi.team_refresh("admin") - self.assertFalse(refreshed) - self.team1.github_team_name = "newgh1" - self.team1.add_member(self.regular_user.github_id) - self.mock_facade.store.assert_called_once_with(self.team1) - - def test_refresh_github_changed_success(self) -> None: - """Test refresh team command API to edit teams.""" - new_team1 = Team("1", "newgh1", "name1") - new_team1.add_member(self.regular_user.github_id) - self.mock_github.org_get_teams.return_value = [ - new_team1, - self.team2, - self.team3, - self.team3_dup, - ] - refreshed = self.testapi.team_refresh("admin") - self.assertTrue(refreshed) - self.team1.github_team_name = "newgh1" - self.team1.add_member(self.regular_user.github_id) - self.mock_facade.store.assert_called_once_with(self.team1) diff --git a/tests/api/command/mixins/user_test.py b/tests/api/command/mixins/user_test.py deleted file mode 100644 index bc43f006..00000000 --- a/tests/api/command/mixins/user_test.py +++ /dev/null @@ -1,271 +0,0 @@ -"""Test the common business logic for the user command APIs.""" -from api.command import CommandApis -from db import DBFacade -from interface.github import GithubInterface, GithubAPIException -from interface.slack import Bot -from app.model import User, Permissions -from unittest import mock, TestCase - - -class TestUserCommandApis(TestCase): - """Test Case for UserCommandApi methods.""" - - def setUp(self) -> None: - """Set up the test case environment.""" - self.mock_facade = mock.MagicMock(DBFacade) - self.mock_github = mock.MagicMock(GithubInterface) - self.mock_slack = mock.MagicMock(Bot) - self.testapi = CommandApis(self.mock_facade, - self.mock_github, - self.mock_slack) - - self.test_user_id = "USERID" - self.test_user = User(self.test_user_id) - - def test_edit_missing_calling_user(self) -> None: - """Test edit user command API with missing calling user.""" - self.mock_facade.retrieve.side_effect = LookupError - try: - self.testapi.user_edit(self.test_user_id) - except LookupError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_edit_no_user_properties(self) -> None: - """Test edit user command API while editing no properties.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id) - edited_user = User(self.test_user_id) - self.mock_facade.store.assert_called_with(edited_user) - - def test_edit_user_name(self) -> None: - """Test edit user command API to edit user's name.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id, name="rocketeer") - edited_user = User(self.test_user_id) - edited_user.name = "rocketeer" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_email(self) -> None: - """Test edit user command API to edit user's email.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit( - self.test_user_id, - email="") - edited_user = User(self.test_user_id) - edited_user.email = "test@rocket.com" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_github(self) -> None: - """Test edit user command API to edit user's github.""" - self.mock_facade.retrieve.return_value = self.test_user - self.mock_github.org_add_member.return_value = "GITHUBID" - self.testapi.user_edit(self.test_user_id, github="GITHUBUSERNAME") - edited_user = User(self.test_user_id) - edited_user.github_username = "GITHUBUSERNAME" - edited_user.github_id = "GITHUBID" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_github_with_exception(self) -> None: - """Test edit user command API to edit user's github with exception.""" - self.mock_facade.retrieve.return_value = self.test_user - self.mock_github.org_add_member.side_effect = GithubAPIException(None) - try: - self.testapi.user_edit(self.test_user_id, github="GITHUBUSERNAME") - except GithubAPIException: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_edit_user_position(self) -> None: - """Test edit user command API to edit user's position.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id, pos="dev") - edited_user = User(self.test_user_id) - edited_user.position = "dev" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_major(self) -> None: - """Test edit user command API to edit user's major.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id, major="cpen") - edited_user = User(self.test_user_id) - edited_user.major = "cpen" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_biography(self) -> None: - """Test edit user command API to edit user's biography.""" - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id, bio="I'm testing") - edited_user = User(self.test_user_id) - edited_user.biography = "I'm testing" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_permission(self) -> None: - """Test edit user command API to edit user's permissions.""" - self.test_user.permissions_level = Permissions.admin - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_edit(self.test_user_id, - permission=Permissions.team_lead) - edited_user = User(self.test_user_id) - edited_user.permissions_level = Permissions.team_lead - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_user_permission_insufficient_perm(self) -> None: - """Test edit user command API to edit user's perms w/o permission.""" - self.mock_facade.retrieve.return_value = self.test_user - try: - self.testapi.user_edit(self.test_user_id, - permission=Permissions.team_lead) - except PermissionError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_edit_other_user(self) -> None: - """Test edit user command API to edit other user.""" - self.test_user.permissions_level = Permissions.admin - test_other_id = "OTHERID" - other_user = User(test_other_id) - - def mock_facade_retrieve_side_effect(*args, **kwargs) -> User: - """Mock the behavior of the retrieve mock facade function.""" - user_id = args[1] - if user_id == self.test_user_id: - return self.test_user - elif user_id == test_other_id: - return other_user - else: - raise RuntimeError - - self.mock_facade.retrieve.side_effect \ - = mock_facade_retrieve_side_effect - self.testapi.user_edit(self.test_user_id, - member=test_other_id, - name="rocketeer") - edited_user = User(test_other_id) - edited_user.name = "rocketeer" - self.mock_facade.store.assert_called_once_with(edited_user) - - def test_edit_other_missing_user(self) -> None: - """Test edit user command API to edit a missing other user.""" - self.test_user.permissions_level = Permissions.admin - test_other_id = "OTHERID" - - def mock_facade_retrieve_side_effect(*args, **kwargs) -> User: - """Mock the behavior of the retrieve mock facade function.""" - user_id = args[1] - if user_id == self.test_user_id: - return self.test_user - elif user_id == test_other_id: - raise LookupError - else: - raise RuntimeError - - self.mock_facade.retrieve.side_effect \ - = mock_facade_retrieve_side_effect - try: - self.testapi.user_edit(self.test_user_id, - member=test_other_id, - name="rocketeer") - except LookupError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_edit_other_user_insufficient_perm(self) -> None: - """Test edit user command API to edit other user w/o permisssion.""" - test_other_id = "OTHERID" - other_user = User(test_other_id) - - def mock_facade_retrieve_side_effect(*args, **kwargs) -> User: - """Mock the behavior of the retrieve mock facade function.""" - user_id = args[1] - if user_id == self.test_user_id: - return self.test_user - elif user_id == test_other_id: - return other_user - else: - raise RuntimeError - - self.mock_facade.retrieve.side_effect \ - = mock_facade_retrieve_side_effect - try: - self.testapi.user_edit(self.test_user_id, - member=test_other_id, - name="rocketeer") - except PermissionError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_delete_existing_user(self) -> None: - """Test delete user command API with existing user.""" - self.test_user.permissions_level = Permissions.admin - self.mock_facade.retrieve.return_value = self.test_user - self.testapi.user_delete(self.test_user_id, self.test_user_id) - self.mock_facade.delete.assert_called_once_with(User, - self.test_user_id) - - def test_delete_missing_calling_user(self) -> None: - """Test delete user command API with missing calling user.""" - self.mock_facade.retrieve.side_effect = LookupError - try: - self.testapi.user_delete(self.test_user_id, self.test_user_id) - except LookupError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_delete_insufficient_perm_user(self) -> None: - """Test delete user command API with insufficient permissions.""" - self.test_user.permissions_level = Permissions.member - self.mock_facade.retrieve.return_value = self.test_user - try: - self.testapi.user_delete(self.test_user_id, self.test_user_id) - except PermissionError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_view_existing_user(self) -> None: - """Test view user command API with existing user.""" - self.mock_facade.retrieve.return_value = self.test_user - viewed_user = self.testapi.user_view(self.test_user_id) - self.assertEqual(viewed_user, self.test_user) - - def test_view_non_existant_user(self) -> None: - """Test view user command API with non-existant user.""" - self.mock_facade.retrieve.side_effect = LookupError - try: - self.testapi.user_view(self.test_user_id) - except LookupError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_add_new_user(self) -> None: - """Test add user command API with new user.""" - self.mock_facade.retrieve.side_effect = LookupError - try: - self.testapi.user_add(self.test_user_id) - except RuntimeError: - self.assertTrue(False) - self.mock_facade.store.assert_called_once_with(self.test_user) - - def test_add_existing_user(self) -> None: - """Test add user command API with existing user.""" - try: - self.testapi.user_add(self.test_user_id) - except RuntimeError: - self.assertTrue(True) - else: - self.assertTrue(False) - - def test_force_add_user(self) -> None: - """Test add user command API with force.""" - self.testapi.user_add(self.test_user_id, use_force=True) - self.mock_facade.retrieve.side_effect = LookupError - self.testapi.user_add(self.test_user_id, use_force=True) - self.assertEqual(self.mock_facade.store.call_count, 2) diff --git a/tests/app/controller/command/commands/karma_test.py b/tests/app/controller/command/commands/karma_test.py index d850ae8f..1df96210 100644 --- a/tests/app/controller/command/commands/karma_test.py +++ b/tests/app/controller/command/commands/karma_test.py @@ -31,9 +31,8 @@ def test_handle_bad_args(self): def test_handle_view(self): self.u1.karma = 15 cmd = f'karma view {self.u1.slack_id}' - resp, code = self.testcommand.handle(cmd, self.u0.slack_id) + resp, _ = self.testcommand.handle(cmd, self.u0.slack_id) self.assertIn(str(self.u1.karma), resp) - self.assertEqual(code, 200) def test_handle_view_lookup_error(self): cmd = 'karma view ABCDE8FA9' @@ -44,9 +43,8 @@ def test_handle_reset_all_as_admin(self): self.u0.karma = 2019 self.u1.karma = 2048 with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( 'karma reset --all', self.admin.slack_id) - self.assertEqual(code, 200) self.assertEqual(self.u0.karma, KarmaCommand.karma_default_amount) self.assertEqual(self.u1.karma, KarmaCommand.karma_default_amount) @@ -54,9 +52,8 @@ def test_handle_reset_all_as_admin(self): def test_handle_reset_all_not_as_admin(self): self.u1.karma = 20 with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( 'karma reset --all', self.u0.slack_id) - self.assertEqual(code, 200) self.assertEqual(KarmaCommand.permission_error, resp) self.assertNotEqual(self.u1.karma, KarmaCommand.karma_default_amount) diff --git a/tests/app/controller/command/commands/project_test.py b/tests/app/controller/command/commands/project_test.py index f2d927d3..17b08bb6 100644 --- a/tests/app/controller/command/commands/project_test.py +++ b/tests/app/controller/command/commands/project_test.py @@ -47,16 +47,14 @@ def test_get_invalid_subcommand_help(self): self.testcommand.get_help(subcommand="foo")) def test_handle_help(self): - ret, code = self.testcommand.handle("project help", self.u0.slack_id) + ret, _ = self.testcommand.handle("project help", self.u0.slack_id) self.assertEqual(ret, self.testcommand.get_help()) - self.assertEqual(code, 200) def test_handle_multiple_subcommands(self): """Test handling multiple observed subcommands.""" - ret, code = self.testcommand.handle("project list edit", - self.u0.slack_id) + ret, _ = self.testcommand.handle("project list edit", + self.u0.slack_id) self.assertEqual(ret, self.testcommand.get_help()) - self.assertEqual(code, 200) def test_handle_subcommand_help(self): """Test project subcommand help text.""" @@ -64,17 +62,15 @@ def test_handle_subcommand_help(self): for subcommand in subcommands: for arg in ['--help', '-h', '--invalid argument']: command = f'project {subcommand} {arg}' - ret, code = self.testcommand.handle(command, self.u0.slack_id) + ret, _ = self.testcommand.handle(command, self.u0.slack_id) self.assertEqual(1, ret.count('usage')) - self.assertEqual(code, 200) def test_handle_view(self): with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( f'project view {self.p0.project_id}', self.u0.slack_id) expect = {'attachments': [self.p0.get_attachment()]} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_lookup_error(self): self.assertTupleEqual(self.testcommand.handle( @@ -92,10 +88,9 @@ def test_handle_edit_name(self): other_name = 'other_name' self.assertNotEqual(self.p0.display_name, other_name) with self.app.app_context(): - _, code = self.testcommand.handle( + self.testcommand.handle( f'project edit {self.p0.project_id} --name {other_name}', self.u0.slack_id) - self.assertEqual(code, 200) self.assertEqual(self.p0.display_name, other_name) @mock.patch('app.model.project.uuid') @@ -104,8 +99,7 @@ def test_handle_create_as_team_lead(self, mock_uuid): self.t0.add_team_lead(self.u0.github_id) with self.app.app_context(): cmd = f'project create repo-link {self.t0.github_team_name}' - _, code = self.testcommand.handle(cmd, self.u0.slack_id) - self.assertEqual(code, 200) + self.testcommand.handle(cmd, self.u0.slack_id) proj = self.db.retrieve(Project, '1') self.assertEqual(proj.github_team_id, self.t0.github_team_id) @@ -116,8 +110,7 @@ def test_handle_create_as_admin(self, mock_uuid): mock_uuid.uuid4.return_value = '1' with self.app.app_context(): cmd = f'project create repo-link {self.t0.github_team_name}' - _, code = self.testcommand.handle(cmd, self.admin.slack_id) - self.assertEqual(code, 200) + self.testcommand.handle(cmd, self.admin.slack_id) proj = self.db.retrieve(Project, '1') self.assertEqual(proj.github_team_id, self.t0.github_team_id) @@ -151,12 +144,10 @@ def test_handle_create_user_lookup_error(self): def test_handle_create_with_display_name(self, mock_uuid): mock_uuid.uuid4.return_value = '1' with self.app.app_context(): - _, code = \ - self.testcommand.handle( - f'project create repo-link {self.t0.github_team_name}' - ' --name display-name', - self.admin.slack_id) - self.assertEqual(code, 200) + self.testcommand.handle( + f'project create repo-link {self.t0.github_team_name}' + ' --name display-name', + self.admin.slack_id) proj = self.db.retrieve(Project, '1') self.assertEqual(proj.github_team_id, self.t0.github_team_id) @@ -165,10 +156,9 @@ def test_handle_create_with_display_name(self, mock_uuid): def test_handle_list(self): with self.app.app_context(): - resp, code = self.testcommand.handle('project list', user) + resp, _ = self.testcommand.handle('project list', user) self.assertIn(self.p0.project_id, resp) self.assertIn(self.p1.project_id, resp) - self.assertEqual(code, 200) def test_handle_list_no_projects(self): self.db.projs = {} diff --git a/tests/app/controller/command/commands/team_test.py b/tests/app/controller/command/commands/team_test.py index 11bb8d68..24ae7c25 100644 --- a/tests/app/controller/command/commands/team_test.py +++ b/tests/app/controller/command/commands/team_test.py @@ -50,16 +50,14 @@ def test_get_invalid_subcommand_help(self): self.cmd.get_help(subcommand="foo")) def test_handle_help(self): - ret, code = self.cmd.handle("team help", self.u0.slack_id) + ret, _ = self.cmd.handle("team help", self.u0.slack_id) self.assertEqual(ret, self.cmd.get_help()) - self.assertEqual(code, 200) def test_handle_multiple_subcommands(self): """Test handling multiple observed subcommands.""" - ret, code = self.cmd.handle("team list edit", - self.u0.slack_id) + ret, _ = self.cmd.handle("team list edit", + self.u0.slack_id) self.assertEqual(ret, self.cmd.get_help()) - self.assertEqual(code, 200) def test_handle_subcommand_help(self): """Test team subcommand help text.""" @@ -67,9 +65,8 @@ def test_handle_subcommand_help(self): for subcommand in subcommands: for arg in ['--help', '-h', '--invalid argument']: command = f"team {subcommand} {arg}" - ret, code = self.cmd.handle(command, self.u0.slack_id) + ret, _ = self.cmd.handle(command, self.u0.slack_id) self.assertEqual(1, ret.count("usage")) - self.assertEqual(code, 200) def test_handle_list(self): attachments = [ @@ -79,9 +76,8 @@ def test_handle_list(self): self.t3.get_basic_attachment(), ] with self.app.app_context(): - resp, code = self.cmd.handle('team list', self.u0.slack_id) + resp, _ = self.cmd.handle('team list', self.u0.slack_id) self.assertCountEqual(resp['attachments'], attachments) - self.assertEqual(code, 200) def test_handle_list_no_teams(self): self.db.teams = {} @@ -91,11 +87,10 @@ def test_handle_list_no_teams(self): def test_handle_view(self): with self.app.app_context(): - resp, code = self.cmd.handle('team view brs', - self.u0.slack_id) + resp, _ = self.cmd.handle('team view brs', + self.u0.slack_id) expect = {'attachments': [self.t0.get_attachment()]} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_lookup_error(self): self.assertTupleEqual(self.cmd.handle('team view iesesebrs', @@ -103,10 +98,9 @@ def test_handle_view_lookup_error(self): (self.cmd.lookup_error, 200)) def test_handle_view_noleads(self): - resp, code = self.cmd.handle('team view brs', - self.u0.slack_id) + resp, _ = self.cmd.handle('team view brs', + self.u0.slack_id) self.assertDictEqual(resp['attachments'][0], self.t0.get_attachment()) - self.assertEqual(code, 200) def test_handle_delete_not_admin(self): self.assertTupleEqual(self.cmd.handle('team delete brs', @@ -211,13 +205,12 @@ def test_handle_add(self): self.u0.github_username = 'myuser' self.u0.github_id = 'otherID' with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team add brs {self.u0.slack_id}', self.admin.slack_id) expect = {'attachments': [self.t0.get_attachment()], 'text': 'Added User to brs'} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertTrue(self.t0.has_member("otherID")) self.gh.add_team_member.assert_called_once_with('myuser', 'githubid') @@ -256,14 +249,13 @@ def test_handle_add_promote(self): self.u0.github_id = 'otherID' self.t2.github_team_id = 'githubid' with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team add leads {self.u0.slack_id}', self.admin.slack_id) expect_msg = 'Added User to leads and promoted user to team_lead' expect = {'attachments': [self.t2.get_attachment()], 'text': expect_msg} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertTrue(self.t2.has_member('otherID')) self.assertEqual(self.u0.permissions_level, Permissions.team_lead) self.gh.add_team_member.assert_called_once_with('myuser', 'githubid') @@ -276,14 +268,13 @@ def test_handle_add_promote_current_admin(self): self.u0.permissions_level = Permissions.admin self.t3.add_member(self.u0.github_id) with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team add leads {self.u0.slack_id}', self.admin.slack_id) expect_msg = 'Added User to leads' expect = {'attachments': [self.t2.get_attachment()], 'text': expect_msg} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertTrue(self.t2.has_member('otherID')) self.assertEqual(self.u0.permissions_level, Permissions.admin) self.gh.add_team_member.assert_called_once_with('myuser', 'githubid') @@ -293,13 +284,12 @@ def test_handle_remove(self): self.u0.github_username = 'myuser' self.t0.add_member(self.u0.github_id) with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team remove {self.t0.github_team_name} {self.u0.slack_id}', self.admin.slack_id) expect = {'attachments': [self.t0.get_attachment()], 'text': f'Removed User from {self.t0.github_team_name}'} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertFalse(self.t0.has_member(self.u0.github_id)) self.gh.remove_team_member.assert_called_once_with( self.u0.github_username, @@ -325,14 +315,13 @@ def test_handle_remove_demote_team_lead_to_user(self): self.u0.github_id = 'otherID' self.t2.add_member(self.u0.github_id) with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team remove leads {self.u0.slack_id}', self.admin.slack_id) expect_msg = 'Removed User from leads and demoted user' expect = {'attachments': [self.t2.get_attachment()], 'text': expect_msg} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertEqual(self.u0.permissions_level, Permissions.member) self.gh.remove_team_member.assert_called_once_with( self.u0.github_username, @@ -344,14 +333,13 @@ def test_handle_remove_demote_admin_to_team_lead(self): self.t2.add_member(self.u0.github_id) self.t3.add_member(self.u0.github_id) with self.app.app_context(): - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team remove admin {self.u0.slack_id}', self.admin.slack_id) expect_msg = 'Removed User from admin and demoted user' expect = {'attachments': [self.t3.get_attachment()], 'text': expect_msg} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertEqual(self.u0.permissions_level, Permissions.team_lead) self.gh.remove_team_member.assert_called_once_with( self.u0.github_username, @@ -366,14 +354,13 @@ def test_handle_remove_team_lead_but_user_is_admin(self): with self.app.app_context(): # Leads member should not be demoted if they are also a admin # member - resp, code = self.cmd.handle( + resp, _ = self.cmd.handle( f'team remove leads {self.u0.slack_id}', self.admin.slack_id) expect_msg = 'Removed User from leads' expect = {'attachments': [self.t2.get_attachment()], 'text': expect_msg} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) self.assertEqual(self.u0.permissions_level, Permissions.admin) self.gh.remove_team_member.assert_called_once_with( self.u0.github_username, @@ -409,8 +396,7 @@ def test_handle_lead_add(self): self.u0.github_id = 'githubID' self.u0.github_username = 'myuser' with self.app.app_context(): - _, code = self.cmd.handle(cmdtxt, self.admin.slack_id) - self.assertEqual(code, 200) + self.cmd.handle(cmdtxt, self.admin.slack_id) self.assertTrue(self.t0.has_team_lead(self.u0.github_id)) self.assertTrue(self.t0.has_member(self.u0.github_id)) self.gh.add_team_member.assert_called_once_with( @@ -425,8 +411,7 @@ def test_handle_lead_remove(self): self.t0.add_member(self.u0.github_id) self.t0.add_team_lead(self.u0.github_id) with self.app.app_context(): - _, code = self.cmd.handle(cmdtxt, self.admin.slack_id) - self.assertEqual(code, 200) + self.cmd.handle(cmdtxt, self.admin.slack_id) self.assertFalse(self.t0.has_team_lead(self.u0.github_id)) def test_handle_lead_not_admin(self): @@ -460,10 +445,9 @@ def test_handle_edit(self): cmdtxt = f'team edit {self.t0.github_team_name}' cmdtxt += ' --name brS --platform web' with self.app.app_context(): - _, code = self.cmd.handle(cmdtxt, self.admin.slack_id) + self.cmd.handle(cmdtxt, self.admin.slack_id) self.assertEqual(self.t0.display_name, 'brS') self.assertEqual(self.t0.platform, 'web') - self.assertEqual(code, 200) def test_handle_edit_not_admin(self): cmdtxt = f'team edit {self.t0.github_team_name}' @@ -512,11 +496,10 @@ def test_handle_refresh_team_edited_on_github(self): status = '1 teams changed, 0 added, 0 deleted. Wonderful.' with self.app.app_context(): - resp, code = self.cmd.handle('team refresh', - self.admin.slack_id) + resp, _ = self.cmd.handle('team refresh', + self.admin.slack_id) self.assertCountEqual(resp['attachments'], attachments) self.assertEqual(resp['text'], status) - self.assertEqual(code, 200) self.assertEqual(team, team_update) def test_handle_refresh_addition_and_deletion(self): @@ -534,9 +517,8 @@ def test_handle_refresh_addition_and_deletion(self): status = '0 teams changed, 1 added, 1 deleted. Wonderful.' with self.app.app_context(): - resp, code = self.cmd.handle('team refresh', - self.admin.slack_id) + resp, _ = self.cmd.handle('team refresh', + self.admin.slack_id) self.assertCountEqual(resp['attachments'], attachments) self.assertEqual(resp['text'], status) - self.assertEqual(code, 200) self.assertEqual(len(self.db.teams), 2) diff --git a/tests/app/controller/command/commands/user_test.py b/tests/app/controller/command/commands/user_test.py index 6a2ef5e2..03ddbd70 100644 --- a/tests/app/controller/command/commands/user_test.py +++ b/tests/app/controller/command/commands/user_test.py @@ -77,11 +77,10 @@ def test_handle_view(self): user_attaches = [self.u0.get_attachment()] with self.app.app_context(): # jsonify requires translating the byte-string - resp, code = self.testcommand.handle('user view', - self.u0.slack_id) + resp, _ = self.testcommand.handle('user view', + self.u0.slack_id) expect = {'attachments': user_attaches} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_other_user_by_slack(self): user = User("ABCDE8FA9") @@ -90,10 +89,9 @@ def test_handle_view_other_user_by_slack(self): user_attaches = [user.get_attachment()] with self.app.app_context(): # jsonify requires translating the byte-string - resp, code = self.testcommand.handle(command, self.u0.slack_id) + resp, _ = self.testcommand.handle(command, self.u0.slack_id) expect = {'attachments': user_attaches} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_other_user_by_github(self): user = User("ABCDE8FA9") @@ -103,10 +101,9 @@ def test_handle_view_other_user_by_github(self): user_attaches = [user.get_attachment()] with self.app.app_context(): # jsonify requires translating the byte-string - resp, code = self.testcommand.handle(command, self.u0.slack_id) + resp, _ = self.testcommand.handle(command, self.u0.slack_id) expect = {'attachments': user_attaches} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_other_user_by_email(self): user = User("ABCDE8FA9") @@ -116,10 +113,9 @@ def test_handle_view_other_user_by_email(self): user_attaches = [user.get_attachment()] with self.app.app_context(): # jsonify requires translating the byte-string - resp, code = self.testcommand.handle(command, self.u0.slack_id) + resp, _ = self.testcommand.handle(command, self.u0.slack_id) expect = {'attachments': user_attaches} self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_multiple_users(self): user = User("ABCDE8FA9") @@ -132,13 +128,12 @@ def test_handle_view_multiple_users(self): user_attaches = [user.get_attachment(), user2.get_attachment()] with self.app.app_context(): # jsonify requires translating the byte-string - resp, code = self.testcommand.handle(command, self.u0.slack_id) + resp, _ = self.testcommand.handle(command, self.u0.slack_id) expect = { 'text': 'Warning - multiple users found!', 'attachments': user_attaches, } self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_view_lookup_error(self): command = 'user view --username ABCDE8FA9' @@ -171,40 +166,37 @@ def test_handle_delete_callinguser_lookup_error(self): def test_handle_edit_name(self): with self.app.app_context(): - resp, code = self.testcommand.handle('user edit --name rob', - self.u0.slack_id) + resp, _ = self.testcommand.handle('user edit --name rob', + self.u0.slack_id) expect = {'title': 'Name', 'value': 'rob', 'short': True} self.assertIn(expect, resp['attachments'][0]['fields']) - self.assertEqual(code, 200) def test_handle_edit_github(self): """Test that editing github username sends request to interface.""" self.mock_github.org_add_member.return_value = "123" with self.app.app_context(): - resp, code = self.testcommand.handle("user edit --github rob", - self.u0.slack_id) + resp, _ = self.testcommand.handle("user edit --github rob", + self.u0.slack_id) expect0 = {'title': 'Github Username', 'value': 'rob', 'short': True} expect1 = {'title': 'Github ID', 'value': '123', 'short': True} self.assertIn(expect0, resp['attachments'][0]['fields']) self.assertIn(expect1, resp['attachments'][0]['fields']) - self.assertEqual(code, 200) self.mock_github.org_add_member.assert_called_once_with("rob") def test_handle_edit_github_error(self): self.mock_github.org_add_member.side_effect = GithubAPIException("") with self.app.app_context(): - resp, code = self.testcommand.handle('user edit --github rob', - self.u0.slack_id) + resp, _ = self.testcommand.handle('user edit --github rob', + self.u0.slack_id) expect = { 'attachments': [self.u0.get_attachment()], 'text': '\nError adding user rob to GitHub organization' } self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_edit_all_fields(self): user = User(self.u0.slack_id) @@ -219,7 +211,7 @@ def test_handle_edit_all_fields(self): expect = {'attachments': [user.get_attachment()]} self.mock_github.org_add_member.return_value = "123" with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( "user edit " "--name rob " "--email --pos dev --github" @@ -227,7 +219,6 @@ def test_handle_edit_all_fields(self): " --bio 'Im a human lol'", self.u0.slack_id) self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_edit_not_admin(self): """Test user command with editor user that is not admin.""" @@ -242,7 +233,7 @@ def test_handle_edit_not_admin(self): def test_handle_edit_make_admin(self): with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( f"user edit --username {self.u0.slack_id} " "--permission admin", self.admin.slack_id) @@ -250,18 +241,16 @@ def test_handle_edit_make_admin(self): 'value': 'admin', 'short': True} self.assertIn(expect, resp['attachments'][0]['fields']) - self.assertEqual(code, 200) def test_handle_edit_make_self_admin_no_perms(self): with self.app.app_context(): - resp, code = self.testcommand.handle( + resp, _ = self.testcommand.handle( "user edit --permission admin", self.u0.slack_id) expect = { 'attachments': [self.u0.get_attachment()], 'text': "\nCannot change own permission: user isn't admin." } self.assertDictEqual(resp, expect) - self.assertEqual(code, 200) def test_handle_edit_lookup_error_editee(self): self.assertEqual(self.testcommand.handle( @@ -280,15 +269,13 @@ def test_handle_edit_lookup_error(self): (UserCommand.lookup_error, 200)) def test_handle_command_help(self): - ret, code = self.testcommand.handle('user help', self.u0.slack_id) + ret, _ = self.testcommand.handle('user help', self.u0.slack_id) self.assertEqual(ret, self.testcommand.get_help()) - self.assertEqual(code, 200) def test_handle_multiple_subcommands(self): """Test handling multiple observed subcommands.""" - ret, code = self.testcommand.handle('user edit view', self.u0.slack_id) + ret, _ = self.testcommand.handle('user edit view', self.u0.slack_id) self.assertEqual(ret, self.testcommand.get_help()) - self.assertEqual(code, 200) def test_handle_subcommand_help(self): subcommands = list(self.testcommand.subparser.choices.keys()) @@ -296,12 +283,11 @@ def test_handle_subcommand_help(self): cmd_args = ['--help', '-h', '--invalid argument'] for arg in cmd_args: command = f'user {subcommand} {arg}' - ret, code = self.testcommand.handle(command, self.u0.slack_id) + ret, _ = self.testcommand.handle(command, self.u0.slack_id) self.assertEqual(1, ret.count("usage")) self.assertIn(subcommand, ret) - self.assertEqual(code, 200) - def test_handle_deepdive(self): + def test_handle_view_inspect(self): self.u0.name = 'John Peters' self.u0.email = 'john.peter@hotmail.com' self.u0.github_id = '328593' @@ -312,14 +298,14 @@ def test_handle_deepdive(self): ['- ' + t.github_team_name for t in [self.t0, self.t1]] ) - ret, code = self.testcommand.handle( + ret, _ = self.testcommand.handle( 'user view --inspect --username U0G9QF9C6', self.u1.slack_id) ret = ret['attachments'][1]['text'] self.assertIn(f'*Membership in:*\n{team_names}', ret) self.assertIn(f'*Leading teams:*\n- {self.t1.github_team_name}', ret) - def test_handle_deepdive_with_ghusername(self): + def test_handle_view_inspect_with_ghusername(self): self.u0.name = 'John Peters' self.u0.email = 'john.peter@hotmail.com' self.u0.github_id = '328593' @@ -330,25 +316,25 @@ def test_handle_deepdive_with_ghusername(self): ['- ' + t.github_team_name for t in [self.t0, self.t1]] ) - ret, code = self.testcommand.handle( + ret, _ = self.testcommand.handle( 'user view --inspect --github some_user', self.u1.slack_id) ret = ret['attachments'][1]['text'] self.assertIn(f'*Membership in:*\n{team_names}', ret) self.assertIn(f'*Leading teams:*\n- {self.t1.github_team_name}', ret) - def test_handle_deepdive_user_no_exists(self): - ret, code = self.testcommand.handle( + def test_handle_view_inspect_user_no_exists(self): + ret, _ = self.testcommand.handle( 'user view --inspect --username UXXXXXXXX', self.u1.slack_id) self.assertEqual(UserCommand.lookup_error, ret) - def test_handle_deepdive_no_ghid(self): + def test_handle_view_inspect_no_ghid(self): self.u0.name = 'John Peters' self.u0.email = 'john.peter@hotmail.com' - ret, code = self.testcommand.handle( + ret, _ = self.testcommand.handle( 'user view --inspect --username U0G9QF9C6', self.u1.slack_id) ret = ret['attachments'][1]['text'] - self.assertIn(UserCommand.noghid_deepdive, ret) + self.assertIn(UserCommand.viewinspect_noghid, ret)