def _create_team(self, expect: Expect, new_team: TeamData, http_status: Union[HTTPStatus, range] = HTTPStatus.OK, tag: str = None): """ Create a team :param expect: Expected result; one of Expect.SUCCESS or Expect.FAILURE :param new_team: team to create """ result = None new_team_dict = new_team.to_dict(ignore=[M_ID]) msg = f'{new_team} {tag if tag is not None else ""}' with self.client as client: resp = client.post(TEAMS_URL, json=new_team_dict) resp_body = json.loads(resp.data) if expect == Expect.SUCCESS: self.assert_created(resp.status_code, msg=msg) self.assert_success_response(resp_body, msg=msg) self.assert_body_entry(resp_body, RESULT_CREATED_COUNT, MatchParam.EQUAL, value=1, msg=msg) self.assertTrue(RESULT_ONE_TEAM in resp_body.keys(), msg=msg) self.assert_equal_dict( new_team_dict, resp_body[RESULT_ONE_TEAM], ignore=[M_ID]) result = resp_body[RESULT_ONE_TEAM] else: if isinstance(http_status, HTTPStatus): self.assert_response_status_code(http_status, resp.status_code, msg=msg) self.assertTrue(resp.is_json) self.assert_error_response(resp_body, http_status, '', MatchParam.IGNORE, msg=msg) return result
def setup_test_teams(test_case: BaseTestCase) -> dict: """ Setup teams for tests. :param test_case: BaseTestCase instance :return teams { # key is '{team number}' '1': {<team dict>}, ... } """ teams = { UNASSIGNED_TEAM.name: TeamData(UNASSIGNED_TEAM.id, UNASSIGNED_TEAM.name).to_dict() } with test_case.app.app_context(): # Add required teams. for k, team_data in TEAMS.items(): team = team_data.to_model(ignore=[M_ID]) test_case.db.session.add(team) test_case.db.session.flush() team_data.id = team.id teams[k] = team_data.to_dict() test_case.db.session.commit() return teams
def test_create_duplicate_team(self): """ Test creating a duplicate team """ # Only managers can create teams. self.set_permissions(UserType.MANAGER) new_team = TeamData(0, "The one and only") self._create_team(Expect.SUCCESS, new_team, http_status=HTTPStatus.OK) self._create_team(Expect.FAILURE, new_team, http_status=range(400, 500))
def test_create_invalid_team(self): """ Test creating an invalid team """ # Only managers can create teams. self.set_permissions(UserType.MANAGER) bad_team = TeamData(0, "") for index in range(1): with self.subTest(index=index): if index == 0: bad_team.name = " " info = "empty name" else: self.fail(f"Unexpected index {index}") self._create_team(Expect.FAILURE, bad_team, http_status=range(400, 500), tag=f'index {index} {info}')
def test_create_team(self): """ Test create a team """ for user in UserType: with self.subTest(user=user): permissions = self.set_permissions(user) new_team = TeamData(0, f"Bueno {user} FC") has = BaseTestCase.has_permission( permissions, POST_TEAM_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED self._create_team(expect, new_team, http_status=http_status)
def read_teams(file): """ Read teams from file (team's have only one manager, so create team for each manager). :param file: file to load :return a dict of the form { '<team_name>': TeamData(..), .. } """ with file.open() as f: data = json.load(f) teams = { team["team_name"]: TeamData(0, team["team_name"]) for team in data } return teams
def test_update_team(self): """ Test updating a team """ # Only managers can create teams. self.set_permissions(UserType.MANAGER) new_team = TeamData(0, "Change can be swift") created_team = self._create_team(Expect.SUCCESS, new_team, http_status=HTTPStatus.OK) for index in range(1): with self.subTest(index=index): if index == 0: # Change name old_team = created_team new_team = deepcopy(old_team) new_team[M_NAME] = f"{new_team[M_NAME]} and sudden" tag = "name" else: self.fail(f"Unexpected index {index}") for user in UserType: if index > 0 and user != UserType.MANAGER: # Only manager's can update, so only need to test # others once. continue with self.subTest(user=user): permissions = self.set_permissions(user) has = BaseTestCase.has_permission( permissions, POST_TEAM_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED self._update_team(expect, created_team[M_ID], old_team, new_team, tag, http_status=http_status)
def test_update_team_invalid(self): """ Test updating a team """ # Only managers can create and update teams. self.set_permissions(UserType.MANAGER) new_team = TeamData(0, "Change can be hard") created_team = self._create_team(Expect.SUCCESS, new_team, http_status=HTTPStatus.OK) for index in range(1): with self.subTest(index=index): if index == 0: # Empty name old_team = created_team new_team = deepcopy(old_team) new_team[M_NAME] = " " tag = "empty name" else: self.fail(f"Unexpected index {index}") self._update_team(Expect.FAILURE, created_team[M_ID], old_team, new_team, tag, http_status=range(400, 500))
def test_delete_team(self): """ Test deleting a team """ # Only managers can create teams. self.set_permissions(UserType.MANAGER) new_team = TeamData(0, "Short lived") created_team = self._create_team(Expect.SUCCESS, new_team, http_status=HTTPStatus.OK) for user in UserType: with self.subTest(user=user): permissions = self.set_permissions(user) has = BaseTestCase.has_permission( permissions, POST_TEAM_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED self._delete_team(expect, created_team[M_ID], http_status=http_status)
POST_TEAM_PERMISSION, GET_TEAM_PERMISSION, ) from team_picker.models import M_ID, M_NAME, Team from base_test import BaseTestCase from misc import make_url, MatchParam, UserType, Expect from test_data import EqualDataMixin, TeamData, UNASSIGNED_TEAM TEAM_1 = '1' TEAM_2 = '2' TEAM_3 = '3' TEAM_4 = '4' TEAM_5 = '5' TEAMS_KEY_LIST = [TEAM_1, TEAM_2, TEAM_3, TEAM_4, TEAM_5] TEAMS = { k: TeamData(0, f"Team {k}") for k in TEAMS_KEY_LIST } class TeamsTestCase(BaseTestCase): """This class represents the test case for teams.""" teams = {} def setUp(self): """ Method called to prepare the test fixture. This is called immediately before calling the test method. """ super().setUp()