"""Get product imprints functional test.""" import json from owsrequest import request from owsrequest import test_utils import pytest from project_manager.constant import header_const from project_manager.constant import marketing_const from tests.utils import logger @pytest.mark.parametrize( ('project_id', 'account_type', 'account_id', 'expected_message', 'expected_response_status'), [ # subaccount getting their imprints (1, header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT, 1, [ {'id': 'Apple Records', 'name': 'Apple Records'}, {'id': 'Maple Records', 'name': 'Maple Records'}], 200), # vendor getting their subaccount's imprints (1, header_const.GRASS_ACCOUNT_TYPE_VENDOR, 123, [ {'id': 'Apple Records', 'name': 'Apple Records'}, {'id': 'Maple Records', 'name': 'Maple Records'}], 200), # vendor getting their imprints (2, header_const.GRASS_ACCOUNT_TYPE_VENDOR, 1, [ {'id': 'Best Records', 'name': 'Best Records'}, {'id': 'Second Best Records', 'name': 'Second Best Records'}], 200), # vendor not owner of project (1, header_const.GRASS_ACCOUNT_TYPE_VENDOR, 123456, { 'code': 'authorization_error', 'message': 'Not the owner of this project'}, 403), (2, header_const.GRASS_ACCOUNT_TYPE_VENDOR, 123456, { 'code': 'authorization_error', 'message': 'Not the owner of this project'}, 403), # subaccount not owner of project (1, header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT, 123456, { 'code': 'authorization_error', 'message': 'Not the owner of this project'}, 403), (2, header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT, 123456, { 'code': 'authorization_error', 'message': 'Not the owner of this project'}, 403), # project not found (5, header_const.GRASS_ACCOUNT_TYPE_VENDOR, 123456, { 'code': 'not_found_error', 'message': 'Unable to find project_id 5'}, 404), (5, header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT, 123456, { 'code': 'not_found_error', 'message': 'Unable to find project_id 5'}, 404) ]) def test_get_product_imprints( monkeypatch, product_imprints_db_fixture, project_id, account_type, account_id, expected_message, expected_response_status, client, mocker, highlight_data, get_artist_info_ok): """Test getting product imprint options. Test getting the list of imprint options for products belonging to a given project. Asserts GET /project/{project_id}/product/imprints returns a list of product imprint options. """ get_specs = [{ 'service': 'ows-marketing', 'path': '/highlights/project/{}?mkt_program_id={}'.format( project_id, marketing_const.PROGRAM_MARKETING_HIGHLIGHTS), 'status': 200, 'json': {'items': [highlight_data]}}, get_artist_info_ok] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) logger.patch(mocker) request_url = '/project/{}/product/imprints' request_headers = { header_const.GRASS_ACCOUNT_TYPE: account_type, header_const.GRASS_ACCOUNT_ID: account_id, } server_response = client.get( request_url.format(project_id), headers=request_headers) assert server_response.status_code == expected_response_status response_message = json.loads(server_response.data.decode()) assert response_message == expected_message @pytest.mark.parametrize( ('project_id', 'expected_message', 'expected_response_status'), [ # no grass headers set (1, [ {'id': 'Apple Records', 'name': 'Apple Records'}, {'id': 'Maple Records', 'name': 'Maple Records'}], 200), (2, [ {'id': 'Best Records', 'name': 'Best Records'}, {'id': 'Second Best Records', 'name': 'Second Best Records'}], 200), (5, { 'code': 'not_found_error', 'message': 'Unable to find project_id 5'}, 404) ]) def test_get_product_imprints_with_no_grass_headers( product_imprints_db_fixture, project_id, expected_message, expected_response_status, client, mocker, highlight_data, get_artist_info_ok): """Test getting product imprint options with no grass headers. Test getting the list of imprint options for products belonging to a given project. Asserts GET /project/{project_id}/product/imprints returns a list of product imprint options. """ get_specs = [{ 'service': 'ows-marketing', 'path': '/highlights/project/{}?mkt_program_id={}'.format( project_id, marketing_const.PROGRAM_MARKETING_HIGHLIGHTS), 'status': 200, 'json': {'items': [highlight_data]}}, get_artist_info_ok] mocker.patch( 'owsrequest.request.get', test_utils.mock_ows_requests(get_specs)) request_url = '/project/{}/product/imprints' server_response = client.get(request_url.format(project_id)) assert server_response.status_code == expected_response_status response_message = json.loads(server_response.data.decode()) assert response_message == expected_message @pytest.mark.parametrize( ('request_headers', 'expected_message', 'expected_response_status'), [ # missing header_const.GRASS_ACCOUNT_ID header ({header_const.GRASS_ACCOUNT_TYPE: header_const.GRASS_ACCOUNT_TYPE_SUBACCOUNT}, { 'code': 'authorization_error', 'message': 'Authorization error'}, 403), ({header_const.GRASS_ACCOUNT_TYPE: header_const.GRASS_ACCOUNT_TYPE_VENDOR}, { 'code': 'authorization_error', 'message': 'Authorization error'}, 403), # missing header_const.GRASS_ACCOUNT_TYPE header ({header_const.GRASS_ACCOUNT_ID: 1}, { 'code': 'authorization_error', 'message': 'Authorization error'}, 403) ]) def test_get_product_imprints_with_partial_grass_headers( product_imprints_db_fixture, request_headers, expected_message, expected_response_status, client, mocker): """Test getting product imprint options with request missing headers. Test getting the list of imprint options for products belonging to a given project. Asserts GET /project/{project_id}/product/imprints returns a list of product imprint options. """ project_id = 1 request_url = '/project/{}/product/imprints' server_response = client.get( request_url.format(project_id), headers=request_headers) assert server_response.status_code == expected_response_status response_message = json.loads(server_response.data.decode()) assert response_message == expected_message