"""Integration API tests for ows-transcoding.""" import time from os import getenv from typing import Any, Callable from requests import Response from tests.testutils.api_client.api_client import APIClient from tests.testutils.api_client.assets_api_client import AssetsAPIClient from tests.testutils.file_helper.file_helper import FileHelper from tests.testutils.s3_helper.s3_helper import S3Helper def create_post_transcoding_body( filename: str | None, container: str = "wave", codec: str = "pcm", sample_rate: int = 44100, bit_rate: int = 2116800, channels: int = 2, bit_depth: int = 24, ) -> dict[str, Any]: """Ows-transcoding post /transcoding body generation.""" return { "input": { "bucket": getenv("INPUT_BUCKET"), "key": filename, "metadata": { "container": container, "codec": codec, "sample_rate": sample_rate, "bit_rate": bit_rate, "channels": channels, "bit_depth": bit_depth, }, }, "status_topic_alias": "default_sns_alias", } def assert_with_timeout( call: Callable[..., Any], assertions: Callable[..., Any], *call_args: dict[str, Any], interval: int = 1, timeout: int = 180, ) -> Any: """Assert until condition is met or timeout is met/exceeded.""" assertions_met = False timer = 0 assertion_error = None result_of_call = 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: assertions(result_of_call) assertions_met = True except AssertionError as ae: assertion_error = ae timer += interval time.sleep(interval) return result_of_call # PLAT-2276 def test_invalid_transcoding_order( api_client: APIClient, headers: dict[str, Any] ) -> None: """ Test creation of an invalid transcoding order. Omit filename, check results in an error. """ order_response = api_client.transcoding_order( headers, create_post_transcoding_body(None) ) assert order_response.status_code == 400, ( "Result of order was {}, expected 400.".format(order_response.status_code) ) code = order_response.json()["code"] assert code == "error_body_validation", ( 'Expected response code to be "error_body_validation", was {}'.format(code) ) def test_track_duration( assets_api_client: AssetsAPIClient, api_client: APIClient, headers: dict[str, Any], wav_file: dict[str, Any], audio_asset_type: str, ) -> None: """Test that duration can be obtained from transcoding status.""" file_data = FileHelper.get_file_data_from_asset(wav_file["file_path"]) upload_token = S3Helper.get_upload_token( assets_api_client, headers, audio_asset_type ) S3Helper.upload_to_s3_check_response( wav_file["file_path"], file_data["file_ext"], file_data["content_type"], upload_token, ) order_response = api_client.transcoding_order( headers, create_post_transcoding_body( "{}.{}".format(upload_token["filename"], file_data["file_ext"]) ), ) assert order_response.status_code == 200, ( "Result of POST was {}, expected 200.".format(order_response.status_code) ) response_content = order_response.json() def order_status_assertions(response: Response) -> None: assert response.status_code == 200, ( "Result of GET was {}, expected 200.".format(response.status_code) ) resp_content = response.json() assert resp_content["status"] == "completed", ( 'Expected status to be "completed", was {}.'.format(resp_content["status"]) ) jobs = [ x for x in resp_content["transcoding_jobs"] if x["container"] in ("flac", "wave") ] assert len(jobs) > 0 for job in jobs: assert job["duration"] == wav_file["duration"], ( "Expected duration to be {}, was {}".format( wav_file["duration"], job["duration"] ) ) assert_with_timeout( api_client.transcoding_status, order_status_assertions, response_content["transcoding_order_id"], )