"""Functional tests for retrieving multiple track lyrics. Test endpoint: GET /tracks/lyrics """ import json from oto import status as http_status import pytest from lyrics.constants import error, header 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_bulk_track_lyrics_success( client, authorization_mock, headers, response_status, response_data): """Check that bulk track lyrics works.""" lyrics_sample = [(42, 'abc'), (43, 'pqr')] lyrics_obj_list = [lyrics_factory.LyricsFactory( entity_id=lyrics_row[0], description=lyrics_row[1]) for lyrics_row in lyrics_sample] db.seed_models(lyrics_obj_list) response = client.get('tracks/lyrics?tuids=42,43', headers=headers) assert response.status_code == response_status data = json.loads(response.data.decode()) if response_data == 'lyrics_data': response_data = { 'items': [row.to_dict() for row in lyrics_obj_list] } assert data == response_data @db.test_schema @pytest.mark.parametrize( 'headers, response_status', ((authorization_header, http_status.NOT_FOUND), ({}, http_status.FORBIDDEN)) ) def test_bulk_track_lyrics_failure( client, authorization_mock, headers, response_status): """Check bulk track lyrics when data is missing.""" response = client.get( 'tracks/lyrics?tuids=42,43', headers=headers) assert response.status_code == response_status @db.test_schema @pytest.mark.parametrize( 'headers, response_status, response_code', ((authorization_header, http_status.BAD_REQUEST, error.VALIDATION_ERROR_CODE), ({}, http_status.FORBIDDEN, 'authorization_error')) ) def test_bulk_track_lyrics_invalid( client, authorization_mock, headers, response_status, response_code): """Check bulk track lyrics if request is invalid.""" response = client.get('tracks/lyrics?tuids=abc,,', headers=headers) assert response.status_code == response_status response_message = json.loads(response.data.decode()) assert response_message['code'] == response_code @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'})) ) def test_bulk_track_with_grass_oa_headers( client, grass_oa_headers, authorization_mock, headers, response_status, response_data): """Check bulk track lyrics works with oa grass headers.""" lyrics_sample = [(42, 'abc'), (43, 'pqr')] lyrics_obj_list = [lyrics_factory.LyricsFactory( entity_id=lyrics_row[0], description=lyrics_row[1]) for lyrics_row in lyrics_sample] db.seed_models(lyrics_obj_list) grass_oa_headers.update(headers) response = client.get('tracks/lyrics?tuids=42,43', headers=grass_oa_headers) assert response.status_code == response_status data = json.loads(response.data.decode()) if response_data == 'lyrics_data': response_data = { 'items': [row.to_dict() for row in lyrics_obj_list] } assert data == response_data @db.test_schema @pytest.mark.parametrize( 'headers, response_status', ((authorization_header, http_status.FORBIDDEN), ({}, http_status.FORBIDDEN)) ) def test_bulk_track_with_grass_vendor_headers( client, grass_headers, authorization_mock, headers, response_status): """Check bulk track lyrics throws 403 when used with vendor headers.""" grass_headers.update(headers) response = client.get('tracks/lyrics?tuids=42,43', headers=grass_headers) assert response.status_code == response_status @db.test_schema @pytest.mark.parametrize( 'headers, response_status', ((authorization_header, http_status.FORBIDDEN), ({}, http_status.FORBIDDEN)) ) def test_bulk_track_with_grass_subaccount_headers( client, grass_headers, authorization_mock, headers, response_status): """Check bulk track lyrics throws 403 when used with subaccount headers.""" grass_headers.update(headers) grass_headers[header.GRASS_ACCOUNT_TYPE] = \ header.GRASS_ACCOUNT_TYPE_SUBACCOUNT response = client.get('tracks/lyrics?tuids=42,43', headers=grass_headers) assert response.status_code == response_status @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_bulk_track_lyrics_success_using_post( client, authorization_mock, headers, response_status, response_data): """Check that bulk track lyrics works using POST method.""" tuids = {'tuids': '42,43'} lyrics_sample = [(42, 'abc'), (43, 'pqr')] lyrics_obj_list = [lyrics_factory.LyricsFactory( entity_id=lyrics_row[0], description=lyrics_row[1]) for lyrics_row in lyrics_sample] db.seed_models(lyrics_obj_list) response = client.post( 'tracks/lyrics', headers=headers, data=json.dumps(tuids)) assert response.status_code == response_status data = json.loads(response.data.decode()) if response_data == 'lyrics_data': response_data = { 'items': [row.to_dict() for row in lyrics_obj_list] } assert data == response_data