Пример #1
0
def test_missing_argument_error(api_url,
                                api_key,
                                product_id,
                                endpoint,
                                version=1):
    '''Testing missing argument errors'''

    tomorrow = get_tomorrow()
    warnings: List[str] = []

    # end is missing
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': tomorrow.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=1000,
        error='Missing argument',
        message="Required argument end was not found",
    )
    result = check_api_error(api_error, expected_error)
    if result.is_warning:
        warnings.append(result.message)

    # start is missing
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'end': tomorrow.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=1000,
        error='Missing argument',
        message="Required argument start was not found",
    )
    result = check_api_error(api_error, expected_error)
    if result.is_warning:
        warnings.append(result.message)

    # start and end are missing
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key)
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=1000,
        error='Missing argument',
        message="Required argument start was not found",
    )
    result = check_api_error(api_error, expected_error)
    if result.is_warning:
        warnings.append(result.message)

    if warnings:
        return TestResult(status=1, message='\n '.join(warnings))
    return TestResult()
Пример #2
0
def test_incorrect_api_key(api_url, api_key, product_id, endpoint, version=1):
    '''Request with incorrect API-Key'''
    tomorrow = get_tomorrow()
    raw_response, _ = client(
        f'{api_url}/v{version}/products/{product_id}/{endpoint}',
        api_key, {
            'start': tomorrow.isoformat(),
            'end': tomorrow.isoformat(),
        },
        headers={'API-Key': 'NON-EXISTING-API-KEY'})

    if raw_response.status_code != 403:
        raise FailedTest(
            message=
            f'Incorrect status code "{raw_response.status_code}" when calling the API wihout the API-Key. Expected status code: "403".',
            response=raw_response,
        )

    if raw_response.text != 'Forbidden - Missing or incorrect API key':
        return TestResult(
            status=1,
            message=(
                f'Incorrect text message "{raw_response.text}". '
                'Expected message: "Forbidden - Missing or incorrect API key".'
            ))

    return TestResult()
Пример #3
0
def get_reservation_slot(api_url,
                         api_key,
                         product_id,
                         timeslots: bool,
                         version=1):
    '''Getting day of timeslot with at least one variant available.'''
    if cache.reservation_slot_cache:
        return cache.reservation_slot_cache
    start = datetime.utcnow().date()
    end = start + timedelta(days=30)
    if timeslots:
        response = client(
            f'{api_url}/v{version}/products/{product_id}/timeslots', api_key, {
                'start': start.isoformat(),
                'end': end.isoformat(),
            })
        days = get_timeslots(response)
    else:
        response = client(
            f'{api_url}/v{version}/products/{product_id}/variants', api_key, {
                'start': start.isoformat(),
                'end': end.isoformat(),
            })
        days = get_variants(response)

    days = [day for day in days if day.variants and day.max_tickets > 0]
    single_variant_item = None
    for day in days:
        variants_max_tickets = [
            v.max_tickets for v in day.variants if v.max_tickets > 0
        ]
        if len(variants_max_tickets) > 1:
            # got multi variant slot
            cache.reservation_slot_cache = day
            return day
        if not single_variant_item:
            single_variant_item = day

    if not single_variant_item:
        raise FailedTest(
            'There is no availability in the next 30 days to test a reservation.'
        )
    cache.reservation_slot_cache = single_variant_item
    return single_variant_item
Пример #4
0
def incorrect_date_format(api_url, api_key, product_id, endpoint, version=1):
    '''Checking incorrect date format'''
    tomorrow = get_tomorrow()
    bad_date_format = tomorrow.strftime('%d-%m-%Y')

    # start date in a bad format
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': bad_date_format,
                          'end': tomorrow.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=2000,
        error='Incorrect date format',
        message=
        f'Incorrect date format {bad_date_format}, please use the YYYY-dd-mm format',
    )
    check_api_error(api_error, expected_error)

    # end date in a bad format
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': tomorrow.isoformat(),
                          'end': bad_date_format,
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=2000,
        error='Incorrect date format',
        message=
        f'Incorrect date format {bad_date_format}, please use the YYYY-dd-mm format',
    )
    check_api_error(api_error, expected_error)

    return TestResult()
Пример #5
0
def test_response_format(api_url,
                         api_key,
                         product_id,
                         endpoint,
                         adapter_func,
                         version=1):
    '''Checking response format'''
    today = datetime.utcnow().date()
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': today.isoformat(),
                          'end': today.isoformat(),
                      })
    adapter_func(response)
    return TestResult()
Пример #6
0
def get_availability_timeslots(
    api_url: str,
    api_key: str,
    product_id: str,
    version: int,
    start_date: date,
    end_date: date,
) -> Tuple[List[Timeslot], Response]:
    raw_response, response = client(
        f'{api_url}/v{version}/products/{product_id}/timeslots', api_key, {
            'start': start_date.isoformat(),
            'end': end_date.isoformat(),
        })
    days = parse_availability_timeslots(raw_response, response)
    return days, raw_response
Пример #7
0
def end_before_start_error(api_url, api_key, product_id, endpoint, version=1):
    '''Checking incorrect range error'''
    tomorrow = get_tomorrow()
    next_week = tomorrow + timedelta(days=7)
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': next_week.isoformat(),
                          'end': tomorrow.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=2001,
        error='Incorrect date range',
        message='The end date cannot be earlier then start date',
    )
    return check_api_error(api_error, expected_error)
Пример #8
0
def empty_availability(api_url, api_key, product_id, endpoint, version=1):
    '''Checking availability that is supposed to be empty'''
    today = datetime.utcnow().date()
    start = today + timedelta(days=300)
    end = start + timedelta(days=1)

    raw_response, response = client(
        f'{api_url}/v{version}/products/{product_id}/{endpoint}', api_key, {
            'start': start.isoformat(),
            'end': end.isoformat(),
        })
    if not response:
        return TestResult()
    return TestResult(
        status=1,
        message=("Skipping that test because response is not empty."))
Пример #9
0
def not_allowed_method(api_url, api_key, product_id, endpoint, version=1):
    '''Testing methods that are not allowed'''
    tomorrow = get_tomorrow()
    for method in (requests.post, requests.put, requests.patch,
                   requests.delete):
        response = client(
            f'{api_url}/v{version}/products/{product_id}/{endpoint}',
            api_key, {
                'start': tomorrow.isoformat(),
                'end': tomorrow.isoformat(),
            },
            method=method)
        if response.status_code != 405:
            raise FailedTest(
                f'Incorrect status code ({response.status_code}) when calling the API via method {method}. '
                'Expected status code: 405.')
    return TestResult()
Пример #10
0
def huge_date_range(api_url, api_key, product_id, endpoint, version=1):
    '''Checking availability with huge date range'''
    today = datetime.utcnow().date()
    start = today
    end = today + timedelta(days=365 * 10)

    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': start.isoformat(),
                          'end': end.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=2008,
        error='Date range is too wide',
        message='Maximum date range is',
    )
    return check_api_error(api_error, expected_error)
Пример #11
0
def past_start_date(api_url, api_key, product_id, endpoint, version=1):
    '''Checking availability with start date from the past'''
    today = datetime.utcnow().date()
    start = today - timedelta(days=1)
    end = today

    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': start.isoformat(),
                          'end': end.isoformat(),
                      })
    api_error = get_api_error(response)
    expected_error = ApiError(
        error_code=2007,
        error='Incorrect start date',
        message='Start date cannot be from the past',
    )
    return check_api_error(api_error, expected_error)
Пример #12
0
def test_error_for_timeslot_product(api_url,
                                    api_key,
                                    product_id,
                                    endpoint,
                                    version=1):
    '''Testing variant availability for timeslot product'''
    tomorrow = get_tomorrow()
    raw_response, response = client(
        f'{api_url}/v{version}/products/{product_id}/{endpoint}', api_key, {
            'start': tomorrow.isoformat(),
            'end': tomorrow.isoformat(),
        })
    api_error = get_api_error(raw_response, response)
    expected_error = ApiError(
        error_code=1003,
        error='Non-timeslot product expected',
        message=
        f'Requested non timeslot availability for timeslot product ID {product_id}',
    )
    return check_api_error(raw_response, api_error, expected_error)
Пример #13
0
def not_allowed_method(api_url, api_key, product_id, endpoint, version=1):
    '''Testing methods that are not allowed'''
    tomorrow = get_tomorrow()
    for method in (requests.post, requests.put, requests.patch,
                   requests.delete):
        raw_response, response = client(
            f'{api_url}/v{version}/products/{product_id}/{endpoint}',
            api_key, {
                'start': tomorrow.isoformat(),
                'end': tomorrow.isoformat(),
            },
            method=method)
        status_code = getattr(raw_response, 'status_code', 200)
        if status_code != 405:
            raise FailedTest(
                message=
                f'Incorrect status code "{status_code}" when calling the API via method {method.__name__.upper()}. Expected status code: "405".',
                response=raw_response,
            )
    return TestResult()
Пример #14
0
def test_error_for_non_existing_product(api_url,
                                        api_key,
                                        product_id,
                                        endpoint,
                                        version=1):
    '''Testing availability for non existing product'''
    tomorrow = get_tomorrow()
    raw_response, response = client(
        f'{api_url}/v{version}/products/NON-EXISTING-PRODUCT-ID/{endpoint}',
        api_key, {
            'start': tomorrow.isoformat(),
            'end': tomorrow.isoformat(),
        })
    api_error = get_api_error(raw_response, response)
    expected_error = ApiError(
        error_code=1001,
        error='Missing product',
        message='Product with ID NON-EXISTING-PRODUCT-ID doesn\'t exist',
    )
    return check_api_error(raw_response, api_error, expected_error)
Пример #15
0
def test_next_30_days(api_url,
                      api_key,
                      product_id,
                      endpoint,
                      adapter_func,
                      version=1):
    '''Checking for any availability in the next 30 days'''
    start = datetime.utcnow().date()
    end = start + timedelta(days=30)

    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': start.isoformat(),
                          'end': end.isoformat(),
                      })
    days = adapter_func(response)

    max_tickets_sum = sum(day.max_tickets for day in days)
    if max_tickets_sum <= 0:
        raise FailedTest('There is no availability for next 30 days')

    return TestResult()
Пример #16
0
def test_missing_api_key(api_url, api_key, product_id, endpoint, version=1):
    '''Request without API-Key'''
    tomorrow = get_tomorrow()
    response = client(f'{api_url}/v{version}/products/{product_id}/{endpoint}',
                      api_key, {
                          'start': tomorrow.isoformat(),
                          'end': tomorrow.isoformat(),
                      },
                      headers={})

    if response.status_code != 403:
        raise FailedTest(
            f'Incorrect status code ({response.status_code}) when calling the API wihout the API-Key. '
            'Expected status code: 403.')

    if response.text != 'Forbidden - Missing or incorrect API key':
        raise TestResult(
            status=1,
            message=(
                f'Incorrect text message ({response.text}). '
                'Expected message: Forbidden - Missing or incorrect API key'))

    return TestResult()