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()
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()
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)
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()
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)
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()
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)
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()
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()