"""Functional tests for copy track lyrics. Test endpoint: POST /tracks/copy """ import json from oto import status as http_status import pytest from tests.factories import lyrics_factory from tests.test_utils import db authorization_header = {'Authorization': 'ows-track/ows-lyrics:randomhmac123'} blank_authorization_header = {'Authorization': ''} @db.test_schema @pytest.mark.parametrize( 'headers, response_status, response_data', ((authorization_header, http_status.OK, 'lyrics_data'), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}), (blank_authorization_header, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) def test_copy_lyrics_success( client, client_headers, authorization_mock, headers, response_status, response_data): """Check that copy lyrics works.""" lyrics_list = [ lyrics_factory.LyricsFactory(entity_id=1, description='lalala'), lyrics_factory.LyricsFactory(entity_id=2, description='nanana'), ] db.seed_models(lyrics_list) body = { 'items': [ {'source_tuid': 1, 'destination_tuid': 11}, {'source_tuid': 2, 'destination_tuid': 12}, ] } client_headers.update(headers) response = client.post( '/tracks/copy', headers=client_headers, data=json.dumps(body)) assert response.status_code == response_status if response_data == 'lyrics_data': response = client.get( 'tracks/{}/lyrics'.format(11), headers=client_headers) assert response.status_code == response_status data = json.loads(response.data.decode()) assert data['lyrics'] == lyrics_list[0].description response = client.get( 'tracks/{}/lyrics'.format(12), headers=client_headers) assert response.status_code == response_status data = json.loads(response.data.decode()) assert data['lyrics'] == lyrics_list[1].description else: data = json.loads(response.data.decode()) assert data == response_data @db.test_schema @pytest.mark.parametrize( 'headers, response_status', ((authorization_header, http_status.OK), ({}, http_status.FORBIDDEN))) def test_copy_lyrics_missed( client, client_headers, authorization_mock, headers, response_status): """Check that copy lyrics works even data is missed.""" body = { 'items': [ {'source_tuid': 1, 'destination_tuid': 11}, {'source_tuid': 2, 'destination_tuid': 12}, ] } client_headers.update(headers) response = client.post( '/tracks/copy', headers=client_headers, data=json.dumps(body)) assert response.status_code == response_status @pytest.mark.parametrize( 'headers, response_status, response_message', ((authorization_header, http_status.BAD_REQUEST, 'GRASS requests are forbidden'), ({}, http_status.FORBIDDEN, 'user is forbidden'))) def test_copy_lyrics_grass( client, grass_headers, authorization_mock, headers, response_status, response_message): """Check that endpoint is not accessible via GRASS.""" grass_headers.update(headers) response = client.post('/tracks/copy', headers=grass_headers) assert response.status_code == response_status data = json.loads(response.data.decode()) assert data['message'] == response_message @pytest.mark.parametrize( 'headers, response_status, response_message', ((authorization_header, http_status.BAD_REQUEST, 'validation_message'), ({}, http_status.FORBIDDEN, {'code': 'authorization_error', 'message': 'user is forbidden'}))) def test_copy_tracks_lyrics_validation( client, client_headers, authorization_mock, headers, response_status, response_message): """Check that copy lyrics validation works.""" body = { 'items': [{'src_tuid': 55, 'destination_tuid': 67}] } client_headers.update(headers) response = client.post( '/tracks/copy', headers=client_headers, data=json.dumps(body)) assert response.status_code == response_status data = json.loads(response.data.decode()) if response_message == 'validation_message': assert data['message']['items']['0'] == { 'source_tuid': ['Missing data for required field.']} else: assert data == response_message