def test_create_reservation_outside_of_schedule(tables, client, new_user,
                                                restriction):
    new_user.save()

    # Create a restriction and assign it to the user
    restriction.starts_at = '2101-01-01T10:00:00.000Z'
    restriction.ends_at = '2101-01-05T10:00:00.000Z'
    restriction.apply_to_user(new_user)

    # Create a schedule and assign it to the restriction
    schedule = RestrictionSchedule(schedule_days='1234567',
                                   hour_start=datetime.time(8, 0, 0),
                                   hour_end=datetime.time(10, 0, 0))
    schedule.save()
    restriction.add_schedule(schedule)

    # Create a resource and assign it to the restriction
    resource = Resource(id='0123456789012345678901234567890123456789')
    resource.save()
    restriction.apply_to_resource(resource)

    # Try to create reservation for a period not covered by the restriction.
    data = {
        'title': 'Test reservation',
        'description': 'Test reservation',
        'resourceId': '0123456789012345678901234567890123456789',
        'userId': new_user.id,
        'start': '2101-01-07T09:00:00.000Z',
        'end': '2101-01-07T10:30:00.000Z'
    }
    resp = client.post(ENDPOINT, headers=HEADERS, data=json.dumps(data))

    assert resp.status_code == HTTPStatus.FORBIDDEN
Exemplo n.º 2
0
def create(schedule: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]:
    try:
        days = [Weekday[day] for day in schedule['scheduleDays']]
        new_schedule = RestrictionSchedule(
            schedule_days=days,
            hour_start=datetime.strptime(schedule['hourStart'], "%H:%M").time(),
            hour_end=datetime.strptime(schedule['hourEnd'], "%H:%M").time()
        )
        new_schedule.save()
    except KeyError:
        # Invalid day
        content = {'msg': GENERAL['bad_request']}
        status = HTTPStatus.UNPROCESSABLE_ENTITY.value
    except AssertionError as e:
        content = {'msg': SCHEDULE['create']['failure']['invalid'].format(reason=e)}
        status = HTTPStatus.UNPROCESSABLE_ENTITY.value
    except Exception as e:
        content = {'msg': GENERAL['internal_error'] + str(e)}
        status = HTTPStatus.INTERNAL_SERVER_ERROR.value
    else:
        content = {
            'msg': SCHEDULE['create']['success'],
            'schedule': new_schedule.as_dict()
        }
        status = HTTPStatus.CREATED.value
    finally:
        return content, status
Exemplo n.º 3
0
def test_schedule_creation(tables):
    schedule_expression = '12345'
    starts_at = datetime.time(8, 0, 0)
    ends_at = datetime.time(15, 0, 0)
    schedule = RestrictionSchedule(schedule_days=schedule_expression,
                                   hour_start=starts_at,
                                   hour_end=ends_at)
    schedule.save()
Exemplo n.º 4
0
def active_schedule():
    schedule_expression = '1234567'
    start_time = datetime.time(0, 0, 0)
    end_time = datetime.time(23, 59, 59)
    schedule = RestrictionSchedule(schedule_days=schedule_expression,
                                   hour_start=start_time,
                                   hour_end=end_time)
    schedule.save()
    return schedule
Exemplo n.º 5
0
def test_schedule_with_schedule_days_as_list_of_enums_gets_saved_successfully(
        tables):
    schedule_expression = [Weekday.Monday, Weekday.Tuesday]
    starts_at = datetime.time(8, 0, 0)
    ends_at = datetime.time(15, 0, 0)
    schedule = RestrictionSchedule(schedule_days=schedule_expression,
                                   hour_start=starts_at,
                                   hour_end=ends_at)
    schedule.save()
Exemplo n.º 6
0
def inactive_schedule():
    today = str(datetime.datetime.utcnow().weekday() + 1)
    schedule_expression = '1234567'.replace(today, '')
    start_time = datetime.time(8, 0, 0)
    end_time = datetime.time(10, 0, 0)
    schedule = RestrictionSchedule(schedule_days=schedule_expression,
                                   hour_start=start_time,
                                   hour_end=end_time)
    schedule.save()
    return schedule
Exemplo n.º 7
0
def test_cannot_create_schedule_with_wrong_schedule_expression(tables):
    starts_at = datetime.time(8, 0, 0)
    ends_at = datetime.time(15, 0, 0)
    wrong_schedule_expression = '1458'
    schedule = RestrictionSchedule(schedule_days=wrong_schedule_expression,
                                   hour_start=starts_at,
                                   hour_end=ends_at)
    with pytest.raises(AssertionError):
        schedule.save()

    schedule.schedule_days = '1123'
    with pytest.raises(AssertionError):
        schedule.save()
Exemplo n.º 8
0
def test_schedule_is_active_method_returns_valid_status(tables, restriction):
    # schedule that runs only on current day of the week
    today_schedule_expression = str(datetime.datetime.utcnow().weekday() + 1)
    hour_start = datetime.time(0, 0, 0)
    hour_end = datetime.time(23, 59, 59)
    active_schedule = RestrictionSchedule(
        schedule_days=today_schedule_expression,
        hour_start=hour_start,
        hour_end=hour_end)
    active_schedule.save()

    # schedule that runs on every day of the week except for today
    not_today_schedule_expression = '1234567'.replace(
        today_schedule_expression, '')
    inactive_schedule = RestrictionSchedule(
        schedule_days=not_today_schedule_expression,
        hour_start=hour_start,
        hour_end=hour_end)
    inactive_schedule.save()

    assert active_schedule.is_active is True
    assert inactive_schedule.is_active is False