def test_delete_restriction(tables, client, restriction, new_user): new_user.save() restriction.apply_to_user(new_user) resp = client.delete(ENDPOINT + '/' + str(restriction.id), headers=HEADERS) assert resp.status_code == HTTPStatus.OK assert restriction not in new_user.get_restrictions() with pytest.raises(NoResultFound): Restriction.get(restriction.id)
def delete(id: RestrictionId) -> Tuple[Content, HttpStatusCode]: try: restriction_to_destroy = Restriction.get(id) users = restriction_to_destroy.get_all_affected_users() restriction_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': RESTRICTION['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': RESTRICTION['delete']['success'] }, HTTPStatus.OK.value finally: return content, status
def test_create_indefinte_retriction(tables, client): data = { 'name': 'Test restriction', 'startsAt': '2100-01-01T10:00:00.000Z', 'isGlobal': False } 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 Restriction.get(resp_json['restriction']['id']) is not None
def test_update_restriction_incorrect_data(tables, client, restriction): old_start_date = restriction.starts_at old_end_date = restriction.ends_at data = { 'startsAt': '2200-01-01T10:00:00.000Z', # start date is after the end date, this request shouldn't be accepted 'endsAt': '2199-02-01T10:00:00.000Z', } resp = client.put(ENDPOINT + '/' + str(restriction.id), headers=HEADERS, data=json.dumps(data)) db_session.remove( ) # make sure we'll get the restriction from the DB, and not from memory restriction = Restriction.get(restriction.id) assert resp.status_code == HTTPStatus.UNPROCESSABLE_ENTITY assert restriction.starts_at == old_start_date assert restriction.ends_at == old_end_date
def add_schedule(restriction_id: RestrictionId, schedule_id: ScheduleId) -> Tuple[Content, HttpStatusCode]: restriction = None try: restriction = Restriction.get(restriction_id) schedule = RestrictionSchedule.get(schedule_id) restriction.add_schedule(schedule) have_users_permissions_increased = len( restriction.schedules) > 1 # if added another schedule for user in restriction.get_all_affected_users(): ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased) except NoResultFound: if restriction is None: content, status = { 'msg': RESTRICTION['not_found'] }, HTTPStatus.NOT_FOUND.value else: content, status = { 'msg': SCHEDULE['not_found'] }, HTTPStatus.NOT_FOUND.value except InvalidRequestException: content, status = { 'msg': RESTRICTION['schedules']['add']['failure']['duplicate'] }, HTTPStatus.CONFLICT.value except AssertionError as e: content, status = {'msg': RESTRICTION['schedules']['add']['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['schedules']['add']['success'], 'restriction': restriction.as_dict(include_groups=True, include_users=True, include_resources=True) }, HTTPStatus.OK.value finally: return content, status
def update(id: RestrictionId, newValues: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: new_values = newValues allowed_fields = {'name', 'startsAt', 'endsAt', 'isGlobal'} try: assert set(new_values.keys()).issubset( allowed_fields), 'invalid field is present' restriction = Restriction.get(id) for field_name, new_value in new_values.items(): field_name = snakecase(field_name) assert (field_name is not None) and hasattr(restriction, field_name), \ 'restriction has no {} field'.format(field_name) setattr(restriction, field_name, new_value) restriction.save() for user in restriction.get_all_affected_users(): ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=True) ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=False) except NoResultFound: content, status = { 'msg': RESTRICTION['not_found'] }, HTTPStatus.NOT_FOUND.value except AssertionError as e: content, status = {'msg': RESTRICTION['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': RESTRICTION['update']['success'], 'restriction': restriction.as_dict(include_groups=True, include_users=True, include_resources=True) }, HTTPStatus.OK.value finally: return content, status
def remove_from_resources_by_hostname( restriction_id: RestrictionId, hostname: str) -> Tuple[Content, HttpStatusCode]: restriction = None try: restriction = Restriction.get(restriction_id) resources = Resource.get_by_hostname(hostname) if resources: restriction.remove_from_resources(resources) for user in restriction.get_all_affected_users(): ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=False) else: raise NoResultFound except NoResultFound: if restriction is None: content, status = { 'msg': RESTRICTION['not_found'] }, HTTPStatus.NOT_FOUND.value else: content, status = { 'msg': NODES['hostname']['not_found'] }, HTTPStatus.NOT_FOUND.value except AssertionError as e: content, status = {'msg': RESTRICTION['hosts']['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['hosts']['remove']['success'], 'restriction': restriction.as_dict(include_groups=True, include_users=True, include_resources=True) }, 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 apply_to_user(restriction_id: RestrictionId, user_id: UserId) -> Tuple[Content, HttpStatusCode]: restriction = None try: restriction = Restriction.get(restriction_id) user = User.get(user_id) restriction.apply_to_user(user) ReservationVerifier.update_user_reservations_statuses( user, have_users_permissions_increased=True) except NoResultFound: if restriction is None: content, status = { 'msg': RESTRICTION['not_found'] }, HTTPStatus.NOT_FOUND.value else: content, status = { 'msg': USER['not_found'] }, HTTPStatus.NOT_FOUND.value except InvalidRequestException: content, status = { 'msg': RESTRICTION['users']['apply']['failure']['duplicate'] }, HTTPStatus.CONFLICT.value except AssertionError as e: content, status = {'msg': RESTRICTION['users']['apply']['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['users']['apply']['success'], 'restriction': restriction.as_dict(include_groups=True, include_users=True, include_resources=True) }, HTTPStatus.OK.value finally: return content, status