def test_get_user_by_invalid_id(self): """ Test get user by an invalid id. """ with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): resp = client.get( make_url(USER_BY_ID_URL, user_id=1000) ) has = BaseTestCase.has_permission( permissions, GET_USER_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.NOT_FOUND else: http_status = HTTPStatus.UNAUTHORIZED self.assert_response_status_code(http_status, resp.status_code) self.assertTrue(resp.is_json) self.assert_error_response(resp.get_json(), http_status.value, http_status.phrase, MatchParam.CASE_INSENSITIVE, MatchParam.IN)
def _delete_user(self, expect: Expect, user_id: int, http_status: Union[HTTPStatus, range] = HTTPStatus.OK): """ Create a user :param expect: Expected result; one of Expect.SUCCESS or Expect.FAILURE """ msg = f'user_id {user_id}' with self.client as client: resp = client.delete( make_url(USER_BY_ID_URL, user_id=user_id) ) resp_body = json.loads(resp.data) if expect == Expect.SUCCESS: self.assert_ok(resp.status_code, msg=msg) self.assert_success_response(resp_body, msg=msg) self.assert_body_entry(resp_body, RESULT_DELETED_COUNT, MatchParam.EQUAL, value=1, msg=msg) 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)
def test_get_match_by_id(self): """ Test get matches by id. """ with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): for m in self.matches.values(): std_match = m # std_match = MatchesTestCase.standardise_match(m) with self.subTest(value=std_match): uid = std_match.id \ if isinstance(std_match, EqualDataMixin) \ else std_match[M_ID] resp = client.get( make_url(MATCH_BY_ID_URL, match_id=uid) ) has = BaseTestCase.has_permission( permissions, GET_MATCH_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED expected = std_match.to_dict() \ if isinstance(std_match, EqualDataMixin) \ else std_match self.assert_match_resp(expect, resp, expected, http_status=http_status)
def test_get_user_by_id(self): """ Test get users by id. """ with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): for v in self.users.values(): with self.subTest(value=v): uid = v.id \ if isinstance(v, EqualDataMixin) else v[M_ID] resp = client.get( make_url(USER_BY_ID_URL, user_id=uid) ) has = BaseTestCase.has_permission( permissions, GET_USER_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED expected = v.to_dict() \ if isinstance(v, EqualDataMixin) else v self.assert_user_resp( expect, resp, expected, http_status=http_status)
def test_get_team_by_id(self): """ Test get teams by id. """ with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): for k, v in self.teams.items(): with self.subTest(team=v): resp = client.get( make_url(TEAM_BY_ID_URL, team_id=v[M_ID]) ) has = BaseTestCase.has_permission( permissions, GET_TEAM_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED expected = v.to_dict() \ if isinstance(v, EqualDataMixin) else v self.assert_team_resp( expect, resp, expected, http_status=http_status)
def test_get_all_matches(self): """ Test get all matches. """ expected_list = [v for v in self.matches.values()] with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): resp = client.get( make_url(MATCHES_URL) ) has = BaseTestCase.has_permission( permissions, GET_MATCH_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED self.assert_matches_list_resp(expect, resp, expected_list, http_status=http_status)
def test_setup_user(self): """ Test setup users; success for authorised users, otherwise failure. """ with self.client as client: for _, user in self.users.items(): with self.subTest(user=user): role = get_role_from_id(user.role_id) for user_type in [UserType.PUBLIC, UserType.UNAUTHORISED]: with self.subTest(user_type=user_type): permissions = self.set_permissions( user_type, profile={ M_NAME: user.name, M_AUTH0_ID: user.auth0_id, SETUP_COMPLETE: False }, role=role) resp = client.post(make_url(USER_SETUP_URL), data={ M_NAME: user.name, M_SURNAME: user.surname, M_ROLE_ID: user.role_id }) # Only authenticated users should be able to do # setup, unauthenticated should redirect to login. url = LOGIN_URL if user_type == UserType.PUBLIC: query = NEW_TEAM_QUERY \ if role == MANAGER_ROLE else \ (SET_TEAM_QUERY if role == PLAYER_ROLE else None) if query is not None: url = make_url(DASHBOARD_URL, **{query: YES_ARG}) self.assert_response_status_code( HTTPStatus.FOUND, resp.status_code) soup = BeautifulSoup(resp.data, 'html.parser') UiBaseTestCase.assert_redirect(self, soup, url)
def test_manager_create_team(self): """ Test only managers are able to create teams. """ with self.client as client: for key, user in self.users.items(): role, index, team_name = UsersSetupTestCase.split_user_key(key) # Need to add user to database to perform test. new_user = user.to_dict(ignore=[M_ID]) new_user[M_TEAM_ID] = UNASSIGNED_TEAM.id created = create_user(new_user) with self.subTest(user=user): user_types = [UserType.PUBLIC, UserType.UNAUTHORISED] + \ [UserType.MANAGER if user.role_id == ROLES[MANAGER_ROLE].id else UserType.PLAYER] for user_type in user_types: with self.subTest(user_type=user_type): permissions = self.set_permissions( user_type, profile={ M_NAME: user.name, M_AUTH0_ID: user.auth0_id, SETUP_COMPLETE: False, DB_ID: created[M_ID] }, role=role) resp = client.post(make_url(TEAM_SETUP_URL), json={M_NAME: team_name}) has = BaseTestCase.has_permission( permissions, POST_TEAM_PERMISSION) if has: http_status = HTTPStatus.FOUND url = DASHBOARD_URL elif user_type == UserType.PUBLIC or \ user_type == UserType.PLAYER: http_status = HTTPStatus.UNAUTHORIZED url = None else: http_status = HTTPStatus.FOUND url = LOGIN_URL self.assert_response_status_code( http_status, resp.status_code) soup = BeautifulSoup(resp.data, 'html.parser') if http_status == HTTPStatus.FOUND: UiBaseTestCase.assert_redirect(self, soup, url)
def _update_match(self, expect: Expect, match_id: int, old_match: dict, new_match: dict, tag: str, http_status: Union[HTTPStatus, range] = HTTPStatus.OK): """ Update a match :param expect: Expected result; one of Expect.SUCCESS or Expect.FAILURE """ updates = {} for k, v in old_match.items(): new_v = new_match[k] if new_v != v: updates[k] = new_v msg = f'match_id {match_id} {old_match} -> {new_match} = {updates} ' \ f'{tag if tag is not None else ""}' with self.client as client: resp = client.patch( make_url(MATCH_BY_ID_URL, match_id=match_id), json=updates ) resp_body = json.loads(resp.data) if expect == Expect.SUCCESS: expected = self.standardise_match(new_match) # Set selections to player dicts. selections = expected[M_SELECTIONS] if len(selections) > 0 and isinstance(selections[0], int): expected[M_SELECTIONS] = [ MatchesTestCase.find_user(self, self.users, uid) for uid in selections ] self.assert_ok(resp.status_code, msg=msg) self.assert_success_response(resp_body, msg=msg) self.assert_body_entry(resp_body, RESULT_UPDATED_COUNT, MatchParam.EQUAL, value=1, msg=msg) self.assert_body_entry(resp_body, RESULT_ONE_MATCH, MatchParam.EQUAL, value=expected, msg=msg) 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)
def test_get_all_roles(self): """ Test get all roles. """ for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): with self.client as client: resp = client.get(make_url(ROLES_URL)) if permissions is None or \ GET_ROLE_PERMISSION not in permissions: self.assert_unauthorized_request(resp.status_code) else: self.assert_roles_list_resp([v for k, v in ROLES.items()], resp)
def test_get_role_by_id(self): """ Test get roles by id. """ for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): for k, v in ROLES.items(): with self.subTest(role=v): with self.client as client: resp = client.get( make_url(ROLE_BY_ID_URL, role_id=v.id)) if permissions is None or \ GET_ROLE_PERMISSION not in permissions: self.assert_unauthorized_request(resp.status_code) else: self.assert_role_resp(v.to_dict(), resp)
def _update_user(self, expect: Expect, user_id: int, old_user: dict, new_user: dict, tag: str, http_status: Union[HTTPStatus, range] = HTTPStatus.OK): """ Create a user :param expect: Expected result; one of Expect.SUCCESS or Expect.FAILURE """ updates = {} for k, v in old_user.items(): new_v = new_user[k] if new_v != v: updates[k] = new_v msg = f'user_id {user_id} {old_user} -> {new_user} = {updates} ' \ f'{tag if tag is not None else ""}' with self.client as client: resp = client.patch( make_url(USER_BY_ID_URL, user_id=user_id), json=updates ) resp_body = json.loads(resp.data) if expect == Expect.SUCCESS: self.assert_ok(resp.status_code, msg=msg) self.assert_success_response(resp_body, msg=msg) self.assert_body_entry(resp_body, RESULT_UPDATED_COUNT, MatchParam.EQUAL, value=1, msg=msg) self.assert_body_entry(resp_body, RESULT_ONE_USER, MatchParam.EQUAL, value=new_user, msg=msg) 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)
def test_get_role_by_invalid_id(self): """ Test get role by an invalid id. """ for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): with self.client as client: resp = client.get(make_url(ROLE_BY_ID_URL, role_id=1000)) http_status = HTTPStatus.UNAUTHORIZED \ if permissions is None or \ GET_ROLE_PERMISSION not in permissions \ else HTTPStatus.NOT_FOUND self.assert_response_status_code(http_status, resp.status_code) self.assertTrue(resp.is_json) self.assert_error_response(resp.get_json(), http_status.value, http_status.phrase, MatchParam.CASE_INSENSITIVE, MatchParam.IN)
def test_get_all_teams(self): """ Test get all teams to ensure valid users can access and others cannot. """ with self.client as client: for user in UserType: permissions = self.set_permissions(user) with self.subTest(user=user): resp = client.get( make_url(TEAMS_URL) ) has = BaseTestCase.has_permission( permissions, GET_TEAM_PERMISSION) expect = Expect.SUCCESS if has else Expect.FAILURE if has: http_status = HTTPStatus.OK else: http_status = HTTPStatus.UNAUTHORIZED self.assert_teams_list_resp( expect, resp, [v for v in self.teams.values()], http_status=http_status, msg=f"{user}")