"""Integration API tests for ows-salessheets.""" import time def assert_with_timeout( call, call_args, assertions, interval=1, timeout=20, additional_assert_args=None): """Assert expectation until condition is met or timeout is met/exceeded.""" assertions_met = False timer = 0 assertion_error = None while not assertions_met: if timer >= timeout: raise AssertionError( 'Exceeded timeout of {}, assertion failed was {}'.format( timeout, assertion_error)) result_of_call = call(call_args) assertion_error = None try: if additional_assert_args: assertions(result_of_call, additional_assert_args) else: assertions(result_of_call) except AssertionError as ae: assertion_error = ae if 'download_url' in result_of_call.json(): assertions_met = True else: timer += interval time.sleep(interval) return result_of_call def test_download_salessheet(create_product, workstation_api_client): """Ensure successful sales sheet download for new product.""" product_id = create_product['product_id'] post_sales_sheet_response = \ workstation_api_client.post_single_sales_sheet(product_id) assert post_sales_sheet_response.status_code == 200, \ 'Reponse of POST was {}, expected 200.'.format( post_sales_sheet_response.status_code) def get_sales_sheet_assertions(response): assert response.status_code == 200, \ 'Reponse of GET was {}, expected 200.'.format( response.status_code) get_repsonse = assert_with_timeout( workstation_api_client.single_sales_sheet, product_id, get_sales_sheet_assertions) url = get_repsonse.json()['download_url'] assert '.pdf' in url, \ 'Expected url to contain ".pdf", got {}'.format(url) pdf_response = workstation_api_client.simple_get(url) assert pdf_response.status_code == 200, \ 'Reponse of GET was {}, expected 200.'.format( pdf_response.status_code)