"""Functional tests for GET artist by id handler.""" import json from artist import response from artist.constants import errors from artist.constants import headers from artist.models import account from tests.factories.artist import ArtistFactory from tests.testutils import db def test_get_artist_by_id_missing_account_type_vendor( client, valid_headers_for_vendor): """Test error response when missing only account_type from headers.""" modified_header = valid_headers_for_vendor del modified_header[headers.GRASS_ACCOUNT_TYPE] result = client.get( '/artist/1', headers=modified_header) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 400 assert response_json.get('code') == errors.ERROR_CODE_BAD_GRASS_REQUEST def test_get_artist_by_id_missing_account_id_vendor( client, valid_headers_for_vendor): """Test error response when missing only account_id from headers.""" modified_header = valid_headers_for_vendor del modified_header[headers.GRASS_ACCOUNT_ID] result = client.get( '/artist/1', headers=modified_header) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 400 assert response_json.get('code') == errors.ERROR_CODE_BAD_GRASS_REQUEST def test_get_artist_by_id_missing_account_type_subaccount( client, valid_headers_for_subaccount): """Test error response when missing only account_type from headers.""" modified_header = valid_headers_for_subaccount del modified_header[headers.GRASS_ACCOUNT_TYPE] result = client.get( '/artist/1', headers=modified_header) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 400 assert response_json.get('code') == errors.ERROR_CODE_BAD_GRASS_REQUEST def test_get_artist_by_id_missing_account_id_subaccount( client, valid_headers_for_subaccount): """Test error response when missing only account_id from headers.""" modified_header = valid_headers_for_subaccount del modified_header[headers.GRASS_ACCOUNT_ID] result = client.get( '/artist/1', headers=modified_header) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 400 assert response_json.get('code') == errors.ERROR_CODE_BAD_GRASS_REQUEST def test_get_artist_by_id_missing_content_type( client, valid_headers_for_vendor): """Test error response when missing only content-type from headers.""" modified_header = valid_headers_for_vendor del modified_header[headers.CONTENT_TYPE] result = client.get( '/artist/1', headers=modified_header) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 400 assert headers.CONTENT_TYPE in response_json.get('message') @db.test_schema def test_get_artist_by_id_valid_headers_vendor( client, valid_headers_for_vendor ): """Test a successful request to GET /artist/artist_id.""" test_artist = ArtistFactory.build(vendor_id=12345) db.seed_models(test_artist) artist_id = test_artist.artist_id result = client.get( '/artist/{}'.format(artist_id), headers=valid_headers_for_vendor) assert result.status_code == 200 response_json = json.loads(result.data.decode('utf-8')) assert response_json.get('id') == artist_id assert response_json.get('name') assert response_json.get('artist_type') @db.test_schema def test_get_artist_by_id_valid_headers_oa( client, valid_headers_for_oa ): """Test a successful request to GET /artist/artist_id with oa headers.""" test_artist = ArtistFactory.build(vendor_id=5000) db.seed_models(test_artist) artist_id = test_artist.artist_id # OA should be allowed regardless of vendor_id not matching account_id result = client.get( '/artist/{}'.format(artist_id), headers=valid_headers_for_oa) assert result.status_code == 200 response_json = json.loads(result.data.decode('utf-8')) assert response_json.get('id') == artist_id assert response_json.get('name') assert response_json.get('artist_type') @db.test_schema def test_get_artist_by_id_valid_headers_subaccount( client, valid_headers_for_subaccount, mocker ): """Test a successful request to GET /artist/artist_id.""" mocker.patch.object( account, 'get_vendor_id_for_subaccount_id', return_value=response.Response(status=200)) test_artist = ArtistFactory.build(vendor_id=12345) db.seed_models(test_artist) artist_id = test_artist.artist_id result = client.get( '/artist/{}'.format(artist_id), headers=valid_headers_for_subaccount) assert result.status_code == 200 response_json = json.loads(result.data.decode('utf-8')) assert response_json.get('id') == artist_id assert response_json.get('name') assert response_json.get('artist_type')