"""conftest for functional tests.""" import json import pytest from tests.testutils import mocks @pytest.fixture def client_patch_product_tracks_field(client, mocker, client_headers): """Return a function to patch product tracks.""" mocks.head_response(mocker) def func(product_id, data, headers=client_headers): """Call endpoint with product_id and data.""" endpoint = '/product/{}/tracks'.format(product_id) return client.patch( endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_get_tracks( client, mocker, client_headers): """Return a function to get product tracks.""" mocks.head_response(mocker) def func(tuids, localizations=[], exclude=[], headers=client_headers): """Call endpoint.""" mocks.ows_product_get_track_localizations(mocker, localizations) query_string = {'tuids': ','.join([str(tuid) for tuid in tuids])} if exclude: query_string['exclude'] = ','.join(exclude) return client.get('tracks', headers=headers, query_string=query_string) return func @pytest.fixture def client_get_multiple_bulk_tracks( client, mocker, client_headers): """Return a function to get product tracks.""" mocks.head_response(mocker) def func(product_ids, exclude=[], headers=client_headers): """Call endpoint.""" query_string = {'product_ids': ','.join([str(product_id) for product_id in product_ids])} return client.get('tracks-bulk', headers=headers, query_string=query_string) return func @pytest.fixture def client_get_product_tracks( client, mocker, client_headers, track_localization_get_data): """Return a function to get product tracks.""" mocks.head_response(mocker) mocks.ows_product_get_track_localizations( mocker, track_localization_get_data) def func(product_id, headers=client_headers, exclude=[]): """Call endpoint with product_id.""" endpoint = 'product/{}/tracks'.format(product_id) query_string = {} if exclude: query_string['exclude'] = ','.join(exclude) return client.get( endpoint, headers=headers, query_string=query_string) return func @pytest.fixture def client_get_product_tracks_light( client, mocker, client_headers): """Return a function to get product tracks.""" mocks.head_response(mocker) def func(product_id, headers=client_headers): """Call endpoint with product_id.""" endpoint = 'product/{}/tracks/light'.format(product_id) return client.get( endpoint, headers=headers) return func @pytest.fixture def client_patch_product_tracks_roles(client, mocker, client_headers): """Return a function to patch product tracks roles.""" mocks.head_response(mocker) def func(product_id, role_type, data, headers=client_headers): """Call endpoint with product_id and data.""" endpoint = '/product/{}/tracks/{}'.format(product_id, role_type) return client.patch( endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_put_track(client, mocker, client_headers): """Return a function to put tracks.""" mocks.head_response(mocker) def func(tuid, data, headers=client_headers): """Call endpoint with tuid.""" endpoint = 'track/{}'.format(tuid) return client.put(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_patch_track(client, mocker, client_headers): """Return a function to patch tracks.""" mocks.head_response(mocker) def func(tuid, data, headers=client_headers): """Call endpoint with tuid.""" endpoint = 'track/{}'.format(tuid) return client.patch(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_import_tracks(client, mocker, client_headers, request_engine): """Return a function to import tracks.""" mocks.head_response(mocker) request_engine['ows-features'].add_spec('GET', r'/features/user/\w+', {}) def func(product_id, tuids, headers=client_headers): """Call endpoint with tuid.""" endpoint = 'product/{}/tracks/import'.format(product_id) data = {'tuids': tuids} return client.post(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_get_tracks_pub_obl_for_product(client, mocker, client_headers): """Return function to get tracks publishing obligation.""" def func(product_id, headers=client_headers): """Call endpoint with product_id.""" mocks.head_response(mocker) endpoint = 'product/{}/tracks/publishing-obligation'.format(product_id) return client.get(endpoint, headers=headers) return func @pytest.fixture def client_update_product_tracks_pub_obl(client, mocker, client_headers): """Return a function to put tracks publishing obligation.""" def func(product_id, data, is_mech_admin, headers=client_headers): """Call endpoint with product_id.""" mocks.ows_contracts_is_mech_admin(mocker, is_mech_admin=is_mech_admin) endpoint = 'product/{}/tracks/publishing-obligation'.format(product_id) return client.put(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_validate_tracks_pub_obl_for_product( client, mocker, client_headers, microservice_headers, account_query_params): """Return a function to get product tracks.""" def func( product_id, is_mech_admin, headers=client_headers, use_account_query_params=False): """Call endpoint with product_id.""" mocks.ows_contracts_is_mech_admin(mocker, is_mech_admin=is_mech_admin) endpoint = 'product/{}/tracks/validate/publishing-obligation'.format( product_id) if use_account_query_params: return client.get( endpoint, headers=microservice_headers, query_string=account_query_params) else: return client.get(endpoint, headers=headers) return func @pytest.fixture def client_get_spatial_isrc_map_for_product(client, mocker, client_headers): """Return a function to get spatial ISRC map for all tracks in a product.""" def func(product_id, headers=client_headers): """Call endpoint with product_id.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'products/{}/tracks/spatial'.format(product_id) return client.get(endpoint, headers=headers) return func @pytest.fixture def client_get_performers_for_track(client, mocker, client_headers): """Return a function to get performers for a track.""" def func(tuid, headers=client_headers): """Call endpoint with tuid.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'track/{}/performers'.format(tuid) return client.get(endpoint, headers=headers) return func @pytest.fixture def client_get_performers_for_tracks(client, mocker, client_headers): """Return a function to get performers for tracks.""" def func(tracks, headers=client_headers): """Call endpoint with tuids.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'tracks/performers' return client.post(endpoint, data=json.dumps(tracks), headers=headers) return func @pytest.fixture def client_get_performers_for_product(client, mocker, client_headers): """Return a function to get performers for all track in a product.""" def func(product_id, headers=client_headers): """Call endpoint with tuid.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'product/{}/tracks/performers'.format(product_id) return client.get(endpoint, headers=headers) return func @pytest.fixture def client_update_by_performer_type_for_product( client, mocker, client_headers): """Return a function to patch tracks performers by performer type.""" def func(product_id, perfomer_type, data, headers=client_headers): """Call endpoint with product_id.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = '/product/{}/tracks/performers/{}'.format( product_id, perfomer_type) return client.patch(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_update_performers(client, mocker, client_headers): """Return a function to put tracks performers.""" def func(tuid, data, headers=client_headers): """Call endpoint with tuid.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'track/{}/performers'.format(tuid) return client.put(endpoint, data=json.dumps(data), headers=headers) return func @pytest.fixture def client_validate_performers_for_product( client, mocker, client_headers, microservice_headers, account_query_params): """Return a function to validate performers for product.""" def func( product_id, headers=client_headers, use_account_query_params=False): """Call endpoint with product_id.""" mocks.ows_product_verify_product_ownership(mocker) endpoint = 'product/{}/tracks/validate/performers'.format( product_id) if use_account_query_params: return client.get( endpoint, headers=microservice_headers, query_string=account_query_params) else: return client.get(endpoint, headers=headers) return func @pytest.fixture def validate_role_saved( client, mocker, track_localization_get_data): """Validate role was saved.""" def inner(headers, tuid, role, role_id): mocks.ows_product_get_track_localizations( mocker, track_localization_get_data) res = client.get( 'track/{}'.format(tuid), headers=headers) res_body = json.loads(res.data.decode()) entities = res_body[role + 's'] found = False role_id_field = 'track_{}_id'.format(role) for entity in entities: found = found or entity[role_id_field] == role_id assert found, \ 'Created {} should be saved'.format(role) return inner @pytest.fixture def validate_role_deleted( client, mocker, track_localization_get_data): """Validate role was saved.""" def inner(headers, tuid, role, role_id): mocks.ows_product_get_track_localizations( mocker, track_localization_get_data) res = client.get( 'track/{}'.format(tuid), headers=headers) res_body = json.loads(res.data.decode()) entities = res_body[role + 's'] role_id_field = 'track_{}_id'.format(role) for entity in entities: assert entity[role_id_field] != role_id, \ 'Deleted {} role should be deleted'.format(role) return inner @pytest.fixture def client_get_isrc_in_use( client, mocker, client_headers): """Return a function to get isrc in use.""" mocks.head_response(mocker) def func(track_type, isrc, headers=client_headers): """Call endpoint.""" return client.get( '/isrc/{}/{}'.format(track_type, isrc), headers=headers ) return func @pytest.fixture def client_create_tracks_with_metadata( client, mocker, client_headers): """Return a function to create tracks with metadata.""" mocks.head_response(mocker) def func(product_id, data, headers=client_headers): """Call endpoint.""" return client.put( '/product/{}/tracks'.format(product_id), data=json.dumps(data), headers=headers ) return func @pytest.fixture def client_update_artist_role_by_genre(client, mocker, client_headers): """Return a function to update artist roles.""" mocks.head_response(mocker) mocks.ows_product_get_product_by_id(mocker, 123, '123456789012') def func(product_id, data, headers=client_headers): """Call endpoint.""" return client.post( '/product/{}/tracks/change_genre'.format(product_id), data=json.dumps(data), headers=headers ) return func @pytest.fixture def client_update_artist_role_by_genre_with_profile(client, mocker, profile_headers): """Return a function to update artist roles.""" mocks.head_response(mocker) mocks.ows_product_get_product_by_id(mocker, 123, '123456789012') def func(product_id, data, headers=profile_headers): """Call endpoint.""" return client.post( '/product/{}/tracks/change_genre'.format(product_id), data=json.dumps(data), headers=headers ) return func @pytest.fixture def client_get_product_by_id(client, mocker, client_headers): """Return a function to update artist roles.""" mocks.head_response(mocker) def func(product_id, data, headers=client_headers): """Call endpoint.""" return client.post( '/product/{}/tracks/change_genre'.format(product_id), data=json.dumps(data), headers=headers ) return func