"""Functional tests for PUT 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 @db.test_schema def test_put_artist_by_id( client, valid_headers_for_vendor, artist_update_data): """Test success response.""" test_artist = ArtistFactory.build(artist_id=12345, vendor_id=12345) db.seed_models(test_artist) artist_id = test_artist.artist_id result = client.put( '/artist/12345', headers=valid_headers_for_vendor, data=json.dumps(artist_update_data)) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 200 assert response_json.get('id') == artist_id def test_put_artist_by_id_missing_account_id_vendor( client, valid_headers_for_vendor, artist_update_data): """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.put( '/artist/12345', headers=modified_header, data=json.dumps(artist_update_data)) 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_put_artist_by_id_missing_account_type( client, valid_headers_for_vendor, artist_update_data): """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.put( '/artist/12345', headers=modified_header, data=json.dumps(artist_update_data)) 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 @db.test_schema def test_put_artist_by_id_by_subaccount( client, mocker, valid_headers_for_subaccount, artist_update_data): """Test success response when account_type is subaccount.""" mocker.patch.object( account, 'get_vendor_id_for_subaccount_id', return_value=response.Response(status=200)) test_artist = ArtistFactory.build(artist_id=12345) db.seed_models(test_artist) artist_id = test_artist.artist_id result = client.put( '/artist/12345', headers=valid_headers_for_subaccount, data=json.dumps(artist_update_data)) response_json = json.loads(result.data.decode('utf-8')) assert result.status_code == 200 assert response_json.get('id') == artist_id def test_put_artist_by_id_missing_account_id_subaccount( client, mocker, valid_headers_for_subaccount, artist_update_data): """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.put( '/artist/12345', headers=modified_header, data=json.dumps(artist_update_data)) 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_put_artist_by_id_missing_account_type_subaccount( client, valid_headers_for_subaccount, artist_update_data): """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, data=json.dumps(artist_update_data)) 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