"""Integration tests.""" import pytest from tests.integration.consts import api from tests.integration.consts import schema from tests.integration.test_helpers import api as api_calls from tests.integration.test_helpers import utils def test_create_podcast(test_network_id_fixture): """Create podcast.""" filename = api_calls.asset.add_without_id('podcast', api.ASSET_TYPE_IMAGE) slug = utils.random_slug() payload = { 'title': 'My Own Podcast', 'description': 'Test podcast created by api_tests', 'network_id': test_network_id_fixture, 'slug': slug, 'host': 'Peter', 'owner': 'Parker', 'copyright': 'Copyright', 'link': 'http://theorchard.com', 'email': 'coolemail@email.com', 'explicit': 'explicit', 'show_type': 'episodic', 'language': 'English', 'categories': [22], 'artwork_filename': filename } response = api_calls.podcast.create(payload) try: response_body = utils.validate_response_status(response) assert isinstance(response_body['id'], int), \ 'Expected "id" to be int, was {}'.format(type(response_body['id'])) assert utils.pattern_matched(api.UUID_REGEX, response_body['uuid']), \ 'Expected uuid to match UUID pattern, was {}'.format(response_body['uuid']) assert response_body['created_date'] is not None, \ 'Expected created_date to be not None' assert response_body['created_date'] == response_body['updated_date'], \ 'Expected created_date to be equal updated_date, was {}!={}'.format( response_body['created_date'], response_body['updated_date']) partial_body = { key: response_body[key] for key in response_body.keys() & payload.keys() } del payload['artwork_filename'] assert partial_body == payload, \ 'Expected response to be equal {}, was {}'.format( payload, partial_body ) finally: api_calls.podcast.delete(response.json()['id']) def test_create_podcast_for_every_network(): """Create podcasts for every network and check megaphone_id.""" networks = utils.validate_response_status( api_calls.podcast.get_networks() ) filtered_networks = [network for network in networks['items'] if not network['name'].startswith('PRODUCTION - ')] created_podcasts_number = 0 for network in filtered_networks: filename = api_calls.asset.add_without_id('podcast', api.ASSET_TYPE_IMAGE) slug = utils.random_slug() payload = { 'title': 'My Own Podcast', 'description': 'Test podcast created by api_tests for network {}'.format(network['name']), 'network_id': network['id'], 'slug': slug, 'host': 'Peter', 'owner': 'Parker', 'copyright': 'Copyright', 'link': 'http://theorchard.com', 'email': 'coolemail@email.com', 'explicit': 'explicit', 'show_type': 'episodic', 'language': 'English', 'categories': [22], 'artwork_filename': filename } response = api_calls.podcast.create(payload) try: response_body = utils.validate_response_status(response) created_podcasts_number += 1 partial_body = { key: response_body[key] for key in response_body.keys() & payload.keys() } del payload['artwork_filename'] assert partial_body == payload, \ 'Expected response to be equal {}, was {}'.format( payload, partial_body ) assert utils.pattern_matched(api.UUID_REGEX, response_body['megaphone_id']), \ 'Expected uuid to match UUID pattern, was {}'.format(response_body['megaphone_id']) finally: api_calls.podcast.delete(response.json()['id']) assert created_podcasts_number == len(filtered_networks), \ 'Expected {} podcasts to be created, was {}'.format(len(networks['items']), created_podcasts_number) def test_update_podcast(podcast_fixture, test_network_id_fixture): """Update podcast.""" podcast_id = podcast_fixture['id'] payload = { 'title': 'Update the title', 'description': 'Update the description', 'network_id': test_network_id_fixture, 'host': 'Peter', 'owner': 'Porker', 'copyright': 'Copyright 2020', 'link': 'http://theorchard.io', 'email': 'bademail@email.com', 'explicit': 'clean', 'show_type': 'episodic', 'language': 'English', 'categories': [21] } response_body = utils.validate_response_status( api_calls.podcast.update(podcast_id, payload) ) partial_body = { key: response_body[key] for key in response_body.keys() & payload.keys() } assert partial_body == payload, \ 'Expected response to be equal {}, was {}'.format( payload, partial_body ) def test_get_podcasts(podcast_fixture): """Get podcasts endpoint.""" response = api_calls.podcast.get() response_body = utils.validate_response_status(response) assert 'items' in response_body, \ 'Expected "items" key to be in {}'.format(response_body) assert 'pagination' in response_body, \ 'Expected "pagination" key to be in {}'.format(response_body) utils.assert_has_keys(response_body['pagination'], ['total_records']) created_by_fixture = None for item in response_body['items']: utils.assert_has_keys(item, schema.PODCAST_FIELDS) if item['id'] == podcast_fixture['id']: created_by_fixture = item if not created_by_fixture: pytest.fail('Podcast id {} not found in response.'.format(podcast_fixture['id'])) omit_keys = ['uuid', 'created_date', 'updated_date'] created_by_fixture = utils.omit_keys(created_by_fixture, omit_keys) podcast_fixture = utils.omit_keys(podcast_fixture, omit_keys) podcast_fixture['num_episodes'] = 0 assert created_by_fixture == podcast_fixture, \ 'Expected to get {}, was {}'.format(podcast_fixture, created_by_fixture) def test_published_podcast(podcast_fixture): """Get podcast with assets.""" podcast_id = podcast_fixture['id'] response = api_calls.podcast.get_by_ids([podcast_id]) response_body_array = utils.validate_response_status(response) response_body = response_body_array['items'][0] omit_fields = [ 'id', 'uuid', 'created_date', 'updated_date', 'num_episodes' ] response_body = utils.omit_keys(response_body, omit_fields) podcast_fixture = utils.omit_keys( podcast_fixture, omit_fields) assert response_body == podcast_fixture, \ 'Expected podcast data to be equal {}, was {}'.format( podcast_fixture, response_body ) assets_response = api_calls.asset.get_by_ids_types([podcast_id], ['podcast']) assets_body = utils.validate_response_status(assets_response) assert len(assets_body['items']) == 1, \ 'Expected to get 1 asset item, got {}'.format(len(assets_body['items'])) asset_item = assets_body['items'][0] utils.assert_has_keys(asset_item, schema.ASSET_FIELDS) assert len(asset_item['asset_finals']) == 1, \ 'Expected to get 1 final asset, got {}'.format(len(asset_item['asset_final'])) asset_final = asset_item['asset_finals'][0] utils.assert_has_keys(asset_final, schema.ASSET_FINAL_FIELDS) expected_asset_item = utils.omit_keys( asset_item, ['id', 'filename', 'asset_finals', 'created_date', 'updated_date']) assert expected_asset_item == { 'user_id': 'podcast-admin', 'asset_type': 'JPG', 'object_id': str(podcast_id), 'object_type': 'podcast', 'original_filename': 'api_tests_filename.ext', 'media_type': 'image', 'status': 'encoding_completed' } asset_final = asset_item['asset_finals'][0] assert asset_final expected_asset_final = utils.omit_keys(asset_final, ['id', 'url']) assert expected_asset_final == { 'asset_type': 'JPG', 'asset_subtype': 'xlarge_cover', 'filename': 'postman_sample_image.jpg', 'duration': 0 } asset_final_url = asset_final['url'] expected_url = utils.asset_url('postman_sample_image.jpg') assert expected_url in asset_final_url assert 'Policy=' in asset_final_url assert 'Signature=' in asset_final_url assert 'Key-Pair-Id=' in asset_final_url def test_delete_podcast(podcast_fixture): """Delete podcast test case.""" response = api_calls.podcast.delete(podcast_fixture['id']) utils.validate_response_status(response) get_response = api_calls.podcast.get_by_ids([podcast_fixture['id']]) assert get_response.json()['items'] == [] def test_podcasts_by_ids(podcast_fixture): """Get podcast by ids endpoint test.""" response = api_calls.podcast.get_by_ids([podcast_fixture['id']]) response_body = utils.validate_response_status(response) assert len(response_body['items']) == 1, \ 'Expected 1 item in response, was {}'.format(len(response_body['items'])) omit_keys = ['updated_date'] response_item = utils.omit_keys(response_body['items'][0], omit_keys) expected_result = utils.omit_keys(podcast_fixture, omit_keys) expected_result['num_episodes'] = 0 assert response_item == expected_result, \ 'Expected response to be {}, was {}'.format( podcast_fixture, response_item ) def test_get_networks(): """Get networks endpoint test.""" response_body = utils.validate_response_status( api_calls.podcast.get_networks() ) assert 'items' in response_body, \ 'Expected items key to be in response' for item in response_body['items']: utils.assert_has_keys(item, ['id', 'name']) def test_get_categories(): """Get categories endpoint test.""" response_body = utils.validate_response_status( api_calls.podcast.get_categories() ) assert 'items' in response_body, \ 'Expected items key to be in response' for item in response_body['items']: utils.assert_has_keys(item, schema.NETWORK_FIELDS) if item['children']: for subitem in item['children']: utils.assert_has_keys(subitem, schema.NETWORK_FIELDS) assert subitem['parent_id'] == item['id']