"""Integration tests.""" from tests.integration.consts import api from tests.integration.test_helpers import api as api_calls from tests.integration.test_helpers import utils def test_assets_by_ids_and_types_with_finals( asset_episode_jpg_fixture, asset_podcast_jpg_fixture): """Get assets by ids and types endpoint.""" utils.create_asset_final_from_asset_upload(asset_episode_jpg_fixture) utils.create_asset_final_from_asset_upload(asset_podcast_jpg_fixture) object_ids = [ asset_episode_jpg_fixture['object_id'], asset_podcast_jpg_fixture['object_id'] ] object_types = ['episode', 'podcast'] response = api_calls.asset.get_by_ids_and_types(object_ids, object_types, False) response_body = utils.validate_response_status(response) assert 'items' in response_body, \ 'Expected response body to have items key' assert len(response_body['items']) == 2, \ 'Expected 2 items in response, was {}'.format(len(response_body['items'])) podcast_item = [x for x in response_body['items'] if x['object_type'] == 'podcast'][0] episode_item = [x for x in response_body['items'] if x['object_type'] == 'episode'][0] assert len(podcast_item['asset_finals']) == 1, \ 'Expected 1 final in podcast asset, was {}'.format(len(podcast_item['asset_finals'])) assert len(episode_item['asset_finals']) == 1, \ 'Expected 3 final in episode asset, was {}'.format(len(episode_item['asset_finals'])) podcast_item = utils.omit_keys(podcast_item, ['id', 'created_date', 'updated_date']) episode_item = utils.omit_keys(episode_item, ['id', 'created_date', 'updated_date']) episode_item['asset_finals'][0].pop('id') podcast_item['asset_finals'][0].pop('id') assert episode_item == { 'asset_finals': [{ 'asset_subtype': 'xlarge_cover', 'asset_type': 'JPG', 'duration': 900, 'filename': 'sample_image.jpg', 'url': 'https://qa-podcast-assets.theorchard.io/sample_image.jpg' }], 'asset_type': 'JPG', 'filename': asset_episode_jpg_fixture['filename'], 'media_type': 'image', 'object_id': '5030', 'object_type': 'episode', 'original_filename': 'test-original-filename', 'status': 'encoding_completed', 'user_id': 'podcast-admin'} assert podcast_item == { 'user_id': api.REQUEST_USER_ID, 'asset_type': 'JPG', 'object_id': asset_podcast_jpg_fixture['object_id'], 'object_type': 'podcast', 'filename': asset_podcast_jpg_fixture['filename'], 'media_type': 'image', 'original_filename': 'test-original-filename', 'asset_finals': [ { 'asset_type': 'JPG', 'asset_subtype': 'xlarge_cover', 'filename': api.TEST_ASSET_JPG_FILENAME, 'url': utils.asset_podcast_url(api.TEST_ASSET_JPG_FILENAME), 'duration': 900 } ], 'status': 'encoding_completed' } def test_assets_by_ids_and_types_with_finals_and_signed_urls( asset_episode_jpg_fixture, asset_podcast_jpg_fixture): """Get assets by ids and types endpoint.""" utils.create_asset_final_from_asset_upload(asset_episode_jpg_fixture) utils.create_asset_final_from_asset_upload(asset_podcast_jpg_fixture) object_ids = [ asset_episode_jpg_fixture['object_id'], asset_podcast_jpg_fixture['object_id'] ] object_types = ['episode', 'podcast'] response = api_calls.asset.get_by_ids_and_types(object_ids, object_types) response_body = utils.validate_response_status(response) podcast_item = [x for x in response_body['items'] if x['object_type'] == 'podcast'][0] podcast_image_url = podcast_item['asset_finals'][0]['url'] assert utils.asset_podcast_url(api.TEST_ASSET_JPG_FILENAME) in podcast_image_url assert 'Policy=' in podcast_image_url assert 'Key-Pair-Id' in podcast_image_url assert 'Signature=' in podcast_image_url episode_item = [x for x in response_body['items'] if x['object_type'] == 'episode'][0] episode_image_url = episode_item['asset_finals'][0]['url'] assert utils.asset_podcast_url(api.TEST_ASSET_JPG_FILENAME) in episode_image_url assert 'Policy=' in episode_image_url assert 'Key-Pair-Id' in episode_image_url assert 'Signature=' in episode_image_url def test_assets_by_ids_and_types_without_finals( asset_episode_jpg_fixture, asset_podcast_jpg_fixture): """Get assets by ids and types that have not finals but status.""" status = 'encoding_processing' utils.send_asset_status(asset_episode_jpg_fixture['filename'], status) utils.send_asset_status(asset_podcast_jpg_fixture['filename'], status) object_ids = [ asset_episode_jpg_fixture['object_id'], asset_podcast_jpg_fixture['object_id'] ] object_types = ['episode', 'podcast'] response = api_calls.asset.get_by_ids_and_types(object_ids, object_types) response_body = utils.validate_response_status(response) assert 'items' in response_body, \ 'Expected response body to have items key' assert len(response_body['items']) == 2, \ 'Expected 2 items in response, was {}'.format(len(response_body['items'])) podcast_item = [x for x in response_body['items'] if x['object_type'] == 'podcast'][0] episode_item = [x for x in response_body['items'] if x['object_type'] == 'episode'][0] assert len(podcast_item['asset_finals']) == 0, \ 'Expected 0 final in podcast asset, was {}'.format(len(podcast_item['asset_finals'])) assert len(episode_item['asset_finals']) == 0, \ 'Expected 0 final in episode asset, was {}'.format(len(episode_item['asset_finals'])) podcast_item = utils.omit_keys(podcast_item, ['id', 'created_date', 'updated_date']) episode_item = utils.omit_keys(episode_item, ['id', 'created_date', 'updated_date']) assert episode_item == { 'user_id': api.REQUEST_USER_ID, 'asset_type': 'JPG', 'object_id': asset_episode_jpg_fixture['object_id'], 'object_type': 'episode', 'filename': asset_episode_jpg_fixture['filename'], 'original_filename': 'test-original-filename', 'media_type': 'image', 'asset_finals': [], 'status': status } assert podcast_item == { 'user_id': api.REQUEST_USER_ID, 'asset_type': 'JPG', 'object_id': asset_podcast_jpg_fixture['object_id'], 'object_type': 'podcast', 'filename': asset_podcast_jpg_fixture['filename'], 'original_filename': 'test-original-filename', 'media_type': 'image', 'asset_finals': [], 'status': status } def test_assets_by_ids_and_type(asset_episode_jpg_fixture, asset_episode_wav_fixture): """Get assets by ids and type endpoint test.""" utils.create_asset_final_from_asset_upload(asset_episode_jpg_fixture) utils.create_asset_final_from_asset_upload(asset_episode_wav_fixture) response = api_calls.asset.get_by_ids_and_type( [asset_episode_jpg_fixture['object_id'], asset_episode_wav_fixture['object_id']], 'episode', False ) response_body = utils.validate_response_status(response) assert 'items' in response_body, \ 'Expected response body to have items key' assert len(response_body['items']) == 2, \ 'Expected 2 items in response, was {}'.format(len(response_body['items'])) artwork_item = [x for x in response_body['items'] if x['asset_type'] == api.ASSET_TYPE_JPG][0] audio_item = [x for x in response_body['items'] if x['asset_type'] == api.ASSET_TYPE_WAV][0] assert len(artwork_item['asset_finals']) == 1, \ 'Expected 1 final in episode asset, was {}'.format(len(artwork_item['asset_finals'])) assert len(audio_item['asset_finals']) == 1, \ 'Expected 1 final in episode asset, was {}'.format(len(audio_item['asset_finals'])) artwork_item = utils.omit_keys(artwork_item, ['id', 'created_date', 'updated_date']) audio_item = utils.omit_keys(audio_item, ['id', 'created_date', 'updated_date']) audio_item['asset_finals'][0].pop('id') assert audio_item == { 'user_id': api.REQUEST_USER_ID, 'asset_type': 'WAV', 'object_id': asset_episode_wav_fixture['object_id'], 'object_type': 'episode', 'filename': asset_episode_wav_fixture['filename'], 'original_filename': 'test-original-filename', 'media_type': 'audio', 'asset_finals': [ { 'asset_type': 'WAV', 'asset_subtype': 'none', 'filename': api.TEST_ASSET_MP3_FILENAME, 'url': utils.asset_podcast_url(api.TEST_ASSET_MP3_FILENAME), 'duration': 900 } ], 'status': 'encoding_completed' } artwork_item['asset_finals'][0].pop('id') assert artwork_item == { 'user_id': api.REQUEST_USER_ID, 'asset_type': 'JPG', 'object_id': asset_episode_jpg_fixture['object_id'], 'object_type': 'episode', 'filename': asset_episode_jpg_fixture['filename'], 'original_filename': 'test-original-filename', 'media_type': 'image', 'asset_finals': [ { 'asset_type': 'JPG', 'asset_subtype': 'xlarge_cover', 'filename': api.TEST_ASSET_JPG_FILENAME, 'url': utils.asset_podcast_url(api.TEST_ASSET_JPG_FILENAME), 'duration': 900 } ], 'status': 'encoding_completed' } def test_upload_token(): """Get upload-token endpoint test.""" response_body = utils.validate_response_status( api_calls.asset.get_upload_token() ) utils.assert_has_keys(response_body, ['filename', 'credentials', 'asset_upload_id', 'bucket']) utils.assert_has_keys(response_body['credentials'], ['token', 'aws_access_key_id', 'aws_secret_access_key', 'expiration']) def test_post_asset(): """POST upload endpoint test.""" upload_token = utils.validate_response_status(api_calls.asset.get_upload_token()) object_id = '5030' object_type = 'episode' filename = upload_token['filename'] asset_type = api.ASSET_TYPE_JPG original_filename = 'test-original-filename' response = api_calls.asset.post( object_id, object_type, filename, original_filename, asset_type ) response_body = utils.validate_response_status(response) try: utils.assert_has_keys( response_body['config'], ['output_bucket_name', 'preview_bucket_name', 'elastic_transcoder_id'] ) response_body = utils.omit_keys(response_body, ['id', 'config', 'created_date', 'updated_date']) assert response_body == { 'user_id': api.REQUEST_USER_ID, 'asset_type': asset_type, 'filename': upload_token['filename'], 'object_id': object_id, 'object_type': object_type, 'media_type': 'image', 'original_filename': original_filename } finally: api_calls.asset.delete(object_id, object_type, 'artwork') def test_delete_asset(asset_test_jpg_fixture): """Delete /upload endpoint test.""" response = api_calls.asset.delete( asset_test_jpg_fixture['object_id'], asset_test_jpg_fixture['object_type'], 'artwork' ) response_body = utils.validate_response_status(response) response_body['items'][0] = utils.omit_keys(response_body['items'][0], ['created_date', 'updated_date']) assert response_body == { 'items': [ { 'id': asset_test_jpg_fixture['id'], 'user_id': api.REQUEST_USER_ID, 'asset_type': asset_test_jpg_fixture['asset_type'], 'filename': asset_test_jpg_fixture['filename'], 'object_id': asset_test_jpg_fixture['object_id'], 'object_type': asset_test_jpg_fixture['object_type'], 'media_type': 'image', 'original_filename': asset_test_jpg_fixture['original_filename'] } ] } response_body = utils.validate_response_status( api_calls.asset.get_by_ids_and_type( [asset_test_jpg_fixture['object_id']], asset_test_jpg_fixture['object_type']) ) assert response_body['items'] == [], \ 'Expected to get empty items, was {}'.format(response_body['items']) def test_post_asset_general_status(asset_episode_jpg_fixture): """POST /status endpoint test.""" payload = dict( filename=asset_episode_jpg_fixture['filename'], status='encoding_error', errors={'image_too_small_dimensions_error': 'Image dimensions are too small'}, timestamp='2020-01-20T10:00:01.000Z' ) response = api_calls.asset.post_status(**payload) response_body = utils.validate_response_status(response) status_time = response_body.pop('status_time') assert status_time == '2020-01-20T10:00:01.000Z' response_body = utils.omit_keys(response_body, ['id']) assert response_body == { 'asset_upload_id': asset_episode_jpg_fixture['id'], 'status': payload['status'], 'errors': 'image_too_small_dimensions_error', 'created_date': payload['timestamp'], 'assets': [] } def test_post_asset_general_status_with_results(asset_episode_jpg_fixture): """POST /status endpoint test with additional result keys.""" payload = dict( filename=asset_episode_jpg_fixture['filename'], status='encoding_error', errors={'image_invalid_mode': 'Image has invalid color mode: LA'}, timestamp='2020-01-20T10:00:01.000Z' ) response_body = utils.validate_response_status( api_calls.asset.post_status(**payload)) response_body = utils.omit_keys(response_body, ['id', 'status_time']) assert response_body == { 'asset_upload_id': asset_episode_jpg_fixture['id'], 'status': payload['status'], 'errors': 'image_invalid_mode', 'created_date': payload['timestamp'], 'assets': [] } def test_post_asset_final(asset_episode_jpg_fixture): """POST /final endpoint test.""" asset_type = 'JPG' asset_subtype = 'xlarge_cover' asset_key = api.TEST_ASSET_JPG_FILENAME final_assets = [ { 'asset_type': asset_type, 'asset_subtype': asset_subtype, 'key': asset_key, 'duration': 0 } ] response = api_calls.asset.post_final( filename=asset_episode_jpg_fixture['filename'], final_assets=final_assets, status='encoding_completed', timestamp='2020-02-01T11:00:11.000Z' ) response_body = utils.validate_response_status(response) response_body = utils.omit_keys(response_body, ['id', 'status_time', 'updated_date']) assert response_body == { 'asset_upload_id': asset_episode_jpg_fixture['id'], 'status': 'encoding_completed', 'errors': None, 'created_date': '2020-02-01T11:00:11.000Z', 'assets': [] } response = api_calls.asset.get_by_ids_and_types( [asset_episode_jpg_fixture['object_id']], [asset_episode_jpg_fixture['object_type']] ) response_body = utils.validate_response_status(response) assert len(response_body['items']) == 1 asset_finals = response_body['items'][0]['asset_finals'] for asset_final in asset_finals: url = asset_final['url'] assert utils.asset_podcast_url(api.TEST_ASSET_JPG_FILENAME) in url assert 'Policy=' in url assert 'Key-Pair-Id=' in url assert 'Signature=' in url def test_get_asset_status(asset_episode_jpg_fixture): """Get /status/ endpoint test.""" filename = asset_episode_jpg_fixture['filename'] api_calls.asset.post_status( filename=filename, status='encoding_processing', timestamp='2020-02-02T02:02:02.000Z' ) api_calls.asset.post_status( filename=filename, status='encoding_warning', timestamp='2020-02-02T20:20:20.000Z' ) response_body = utils.validate_response_status( api_calls.asset.get_status(filename) ) response_body = utils.omit_keys(response_body, ['id', 'status_time']) assert response_body == { 'asset_upload_id': asset_episode_jpg_fixture['id'], 'status': 'encoding_warning', 'errors': None, 'created_date': '2020-02-02T20:20:20.000Z', 'assets': [] } def test_get_asset_status_not_started(): """Get /status/ endpoint test for just uploaded.""" upload_token = utils.validate_response_status( api_calls.asset.get_upload_token() ) response_body = utils.validate_response_status( api_calls.asset.get_status(upload_token['filename']) ) assert response_body == { 'status': 'not_started', 'description': '', 'assets': [] }