def test_set_group_as_a_default(tables, client, new_group): new_group.save() resp = client.put(ENDPOINT + '/{}'.format(new_group.id), data=json.dumps({'isDefault': True}), headers=HEADERS) assert resp.status_code == HTTPStatus.OK assert Group.get(new_group.id).is_default
def update(id: GroupId, newValues: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: new_values = newValues allowed_fields = {'name', 'isDefault'} try: assert set(new_values.keys()).issubset( allowed_fields), 'invalid field is present' group = Group.get(id) for field_name, new_value in new_values.items(): field_name = snakecase(field_name) assert hasattr( group, field_name), 'group has no {} field'.format(field_name) setattr(group, field_name, new_value) group.save() except NoResultFound: content, status = { 'msg': GROUP['not_found'] }, HTTPStatus.NOT_FOUND.value except AssertionError as e: content, status = {'msg': GROUP['update']['failure']['assertions'].format(reason=e)}, \ HTTPStatus.UNPROCESSABLE_ENTITY.value except Exception as e: log.critical(e) content, status = { 'msg': GENERAL['internal_error'] }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content, status = { 'msg': GROUP['update']['success'], 'group': group.as_dict() }, HTTPStatus.OK.value finally: return content, status
def delete(id: GroupId) -> Tuple[Content, HttpStatusCode]: try: group_to_destroy = Group.get(id) users = group_to_destroy.users group_to_destroy.destroy() for user in users: ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=False) except AssertionError as error_message: content, status = { 'msg': str(error_message) }, HTTPStatus.FORBIDDEN.value except NoResultFound: content, status = { 'msg': GROUP['not_found'] }, HTTPStatus.NOT_FOUND.value except Exception as e: content, status = { 'msg': GENERAL['internal_error'] + str(e) }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content, status = { 'msg': GROUP['delete']['success'] }, HTTPStatus.OK.value finally: return content, status
def test_update_group(tables, client, new_group): new_group.save() new_group_name = new_group.name + '111' resp = client.put(ENDPOINT + '/' + str(new_group.id), headers=HEADERS, data=json.dumps({'name': new_group_name})) resp_json = json.loads(resp.data.decode('utf-8')) assert resp.status_code == HTTPStatus.OK assert resp_json['group']['name'] == new_group_name assert Group.get(new_group.id).name == new_group_name
def test_create_group(tables, client): group_name = 'TestGroup' data = {'name': group_name} resp = client.post(ENDPOINT, headers=HEADERS, data=json.dumps(data)) resp_json = json.loads(resp.data.decode('utf-8')) assert resp.status_code == HTTPStatus.CREATED assert resp_json['group']['id'] is not None assert resp_json['group']['name'] == group_name assert Group.get(int(resp_json['group']['id'])) is not None
def get_selected(user_id: Optional[UserId], group_id: Optional[GroupId], resource_id: Optional[ResourceId], schedule_id: Optional[ScheduleId], include_user_groups: Optional[bool] = False) \ -> Tuple[Union[List[Any], Content], HttpStatusCode]: try: # If a specific group is selected then groups are not included in the restriction information in response # The same applies to users and resources include_groups = group_id is None include_users = user_id is None include_resources = schedule_id is None restrictions = [] # type: List[Restriction] if user_id is not None: user = User.get(user_id) restrictions.extend( user.get_restrictions(include_group=include_user_groups)) if group_id is not None: group = Group.get(group_id) restrictions.extend(group.get_restrictions()) if resource_id is not None: resource = Resource.get(resource_id) restrictions.extend(resource.get_restrictions()) if schedule_id is not None: schedule = RestrictionSchedule.get(schedule_id) restrictions.extend(schedule.restrictions) # Take unique restrictions result = set(restrictions) except NoResultFound as e: log.warning(e) content, status = { 'msg': GENERAL['bad_request'] }, HTTPStatus.BAD_REQUEST.value except Exception as e: log.critical(e) content, status = { 'msg': GENERAL['internal_error'] }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content = [ restriction.as_dict( include_groups=include_groups, include_users=include_users, # type: ignore include_resources=include_resources) for restriction in result ] status = HTTPStatus.OK.value finally: return content, status
def remove_from_group(restriction_id: RestrictionId, group_id: GroupId) -> Tuple[Content, HttpStatusCode]: restriction = None try: restriction = Restriction.get(restriction_id) group = Group.get(group_id) restriction.remove_from_group(group) for user in group.users: ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=False) except NoResultFound: if restriction is None: content, status = { 'msg': RESTRICTION['not_found'] }, HTTPStatus.NOT_FOUND.value else: content, status = { 'msg': GENERAL['not_found'] }, HTTPStatus.NOT_FOUND.value except InvalidRequestException: content, status = { 'msg': RESTRICTION['groups']['remove']['failure']['not_found'] }, HTTPStatus.NOT_FOUND.value except AssertionError as e: content, status = {'msg': RESTRICTION['groups']['remove']['failure']['assertions'].format(reason=e)}, \ HTTPStatus.UNPROCESSABLE_ENTITY.value except Exception as e: log.critical(e) content, status = { 'msg': GENERAL['internal_error'] }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content, status = { 'msg': RESTRICTION['groups']['remove']['success'], 'restriction': restriction.as_dict(include_groups=True, include_users=True, include_resources=True) }, HTTPStatus.OK.value finally: return content, status
def get_by_id(id: GroupId) -> Tuple[Content, HttpStatusCode]: try: group = Group.get(id) except NoResultFound as e: log.warning(e) content, status = { 'msg': GROUP['not_found'] }, HTTPStatus.NOT_FOUND.value except Exception as e: log.critical(e) content, status = { 'msg': GENERAL['internal_error'] }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content, status = { 'msg': GROUP['get']['success'], 'group': group.as_dict() }, HTTPStatus.OK.value finally: return content, status
def remove_user(group_id: GroupId, user_id: UserId) -> Tuple[Content, HttpStatusCode]: group = None try: group = Group.get(group_id) user = User.get(user_id) group.remove_user(user) ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=False) except NoResultFound: if group is None: content, status = { 'msg': GROUP['not_found'] }, HTTPStatus.NOT_FOUND.value else: content, status = { 'msg': USER['not_found'] }, HTTPStatus.NOT_FOUND.value except InvalidRequestException: content, status = { 'msg': GROUP['users']['remove']['failure']['not_found'] }, HTTPStatus.NOT_FOUND.value except AssertionError as e: content, status = {'msg': GROUP['users']['remove']['failure']['assertions'].format(reason=e)}, \ HTTPStatus.UNPROCESSABLE_ENTITY.value except Exception as e: log.critical(e) content, status = { 'msg': GENERAL['internal_error'] }, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content, status = { 'msg': GROUP['users']['remove']['success'], 'group': group.as_dict() }, HTTPStatus.OK.value finally: return content, status
def test_marking_group_as_a_default(tables, new_group): new_group.is_default = True new_group.save() assert Group.get(new_group.id).is_default assert new_group in Group.get_default_groups()