def test_it_should_be_impossible_to_create_restriction_with_end_time_happening_before_start_time(tables): start_time = datetime.utcnow() + timedelta(hours=5) end_time = datetime.utcnow() + timedelta(minutes=1) restriction = Restriction(name='Test', starts_at=start_time, ends_at=end_time, is_global=False) with pytest.raises(AssertionError): restriction.save()
def test_it_should_be_impossible_to_create_or_edit_restriction_that_already_expired(tables): start_time = datetime.utcnow() - timedelta(hours=5) end_time = start_time + timedelta(hours=1) restriction = Restriction(name='Test', starts_at=start_time, ends_at=end_time, is_global=False) with pytest.raises(AssertionError): restriction.save()
def create(restriction: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: try: new_restriction = Restriction(name=restriction.get('name'), starts_at=restriction['startsAt'], is_global=restriction['isGlobal'], ends_at=DateUtils.try_parse_string( restriction.get('endsAt'))) new_restriction.save() except AssertionError as e: content = { 'msg': RESTRICTION['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': RESTRICTION['create']['success'], 'restriction': new_restriction.as_dict(include_groups=True, include_users=True, include_resources=True) } status = HTTPStatus.CREATED.value finally: return content, status
def test_restriction_creation(tables): starts_at = datetime.utcnow() + timedelta(minutes=5) duration = timedelta(hours=12) new_restriction = Restriction(name='TestRestriction', starts_at=starts_at, ends_at=starts_at + duration, is_global=False) new_restriction.save() assert new_restriction.id is not None
def test_get_global_restrictions_returns_them(tables): starts_at = datetime.utcnow() + timedelta(minutes=5) duration = timedelta(hours=12) new_restriction = Restriction(name='TestRestriction', starts_at=starts_at, ends_at=starts_at + duration, is_global=True) new_restriction.save() assert new_restriction in Restriction.get_global_restrictions()
def test_restriction_with_dates_passed_as_string_gets_added_successfully(tables): new_restriction = Restriction( name='TestRestriction', is_global=False ) new_restriction.starts_at = '2020-09-29T18:07:44.191Z' new_restriction.ends_at = '2120-09-30T18:07:44.191Z' new_restriction.save()
def restriction(): start_time = datetime.datetime.utcnow() + timedelta(minutes=5) end_time = start_time + timedelta(hours=8) restriction = Restriction(name='TestRestriction', starts_at=start_time, ends_at=end_time, is_global=False) restriction.save() return restriction
def permissive_restriction(new_user): start_time = datetime.datetime.utcnow() - timedelta(days=10) end_time = None restriction = Restriction(name='PermissiveRestriction', starts_at=start_time, ends_at=end_time, is_global=True) restriction.apply_to_user(new_user) restriction.save() return restriction
def test_get_all_restrictions_with_data(tables, client): # Create new restriction and save it to the DB start_time = datetime.datetime.utcnow() + datetime.timedelta(minutes=5) end_time = start_time + datetime.timedelta(hours=8) restriction = Restriction(name='TestRestriction', starts_at=start_time, ends_at=end_time, is_global=False) restriction.save() resp = client.get(ENDPOINT, headers=HEADERS) resp_json = json.loads(resp.data.decode('utf-8')) assert resp.status_code == HTTPStatus.OK assert len(resp_json) == 1
def test_restriction_without_schedules_is_active_only_when_between_start_and_end_dates(tables): start_time = datetime.utcnow() - timedelta(hours=5) end_time = datetime.utcnow() + timedelta(hours=5) active_restriction = Restriction(name='ActiveRestriction', starts_at=start_time, ends_at=end_time, is_global=False) active_restriction.save() start_time = datetime.utcnow() + timedelta(hours=1) inactive_restriction = Restriction(name='InactiveRestriction', starts_at=start_time, ends_at=end_time, is_global=False) inactive_restriction.save() assert active_restriction.is_active is True assert inactive_restriction.is_active is False
def test_restriction_with_schedules_is_active_only_when_at_least_one_of_its_schedules_is_active(tables, active_schedule, inactive_schedule): start_time = datetime.utcnow() - timedelta(hours=5) end_time = datetime.utcnow() + timedelta(hours=5) restriction = Restriction(name='ActiveRestriction', starts_at=start_time, ends_at=end_time, is_global=False) restriction.save() restriction.add_schedule(inactive_schedule) assert restriction.is_active is False restriction.add_schedule(active_schedule) assert restriction.is_active is True
def test_indefinite_restriction_creation(tables): starts_at = datetime.utcnow() + timedelta(minutes=5) new_restriction = Restriction(name='TestRestriction', starts_at=starts_at, is_global=False) new_restriction.save() assert new_restriction.id is not None