"""Get Project Functional Test.""" import json from unittest.mock import MagicMock from flexmock import flexmock from owsrequest import request from owsrequest import test_utils from project_manager.constant import error_const from project_manager.constant import header_const from project_manager.constant import marketing_const from tests.common_assert import assert_header def test_get_empty_response(db_fixture, project, client, header): """Assert valid response from GET /project. It checks to see if an empty json is returned for non existing project. It tests for correlation-id and content-type to be in the response header. """ mock_response = MagicMock() (flexmock(request).should_receive('get').and_return(mock_response)) res = client.get('/project/0', headers=header) res_json = json.loads(res.data.decode()) assert 'Unable to find project_id 0' == res_json[error_const.ERROR_DETAIL] assert error_const.ERROR_CODE_NOT_FOUND == res_json[error_const.ERROR_CODE] assert 404 == res.status_code assert_header(header, res.headers) def test_get_project_by_id( db_fixture, highlight_data, project_highlight_ok, monkeypatch, project, client, header, get_artist_info_ok): """Assert that project inserted is the same as expected.""" monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([ project_highlight_ok, get_artist_info_ok])) expected = { 'project_id': 1, 'project_code': 'ABC', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project', 'artist_id': 123, 'created_date_utc': '2016-01-01 00:00:00.000000', 'updated_date_utc': '2016-01-01 00:00:00.000000', 'product_count': 4, 'description': None, 'project_highlights': highlight_data.get('description'), 'vendor_name': 'Awesome Vendor', 'subaccount_name': 'subaccount_test_value', 'deletions': 'N'} res = client.get('/project/1', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_project_by_id_with_deletions( db_fixture, highlight_data, project_highlight_ok, monkeypatch, deleted_project, client, header, get_artist_info_ok): """Assert that deleted project is not returned.""" monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([ project_highlight_ok, get_artist_info_ok])) expected = { 'code': 'not_found_error', 'message': 'Unable to find project_id 1'} res = client.get('/project/1', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_project_with_many_products( db_fixture, highlight_data, project_highlight_ok, monkeypatch, project, client, header, get_artist_info_ok): """Assert that project inserted is the same as expected with many prod.""" monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([ project_highlight_ok, get_artist_info_ok])) expected = { 'project_id': 1, 'project_code': 'ABC', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project', 'artist_id': 123, 'created_date_utc': '2016-01-01 00:00:00.000000', 'updated_date_utc': '2016-01-01 00:00:00.000000', 'product_count': 4, 'description': None, 'project_highlights': highlight_data.get('description'), 'vendor_name': 'Awesome Vendor', 'subaccount_name': 'subaccount_test_value', 'deletions': 'N' } res = client.get('/project/1', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_project_with_no_product( db_fixture, highlight_data, monkeypatch, projects, client, header, get_artist_info_ok): """Assert that project inserted does not have products.""" project_id = 3 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]} }, { 'service': 'ows-artist', 'path': '/artist/4567', 'status': 200, 'json': { 'id': 4567, 'name': 'Solange Knowles', 'artist_type': 'artist'}}] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) expected = { 'project_id': 3, 'project_code': 'GHI', 'vendor_id': 7123, 'subaccount_id': 2, 'project_name': 'new project 3', 'artist_id': 4567, 'created_date_utc': '2016-01-01 03:00:00.000000', 'updated_date_utc': '2016-01-01 03:00:00.000000', 'product_count': 0, 'description': None, 'project_highlights': highlight_data.get('description'), 'vendor_name': 'Awesome Vendor', 'subaccount_name': 'subaccount_test_value', 'deletions': 'N' } res = client.get('/project/{}'.format(project_id), headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_not_auth( db_fixture, project, client, header, monkeypatch): """Assert that project not owned will give 403.""" mock_response = MagicMock() (flexmock(request).should_receive('get').and_return(mock_response)) header[header_const.GRASS_ACCOUNT_ID] = 7555 res = client.get('/project/1', headers=header) data = json.loads(res.data.decode()) assert 403 == res.status_code assert error_const.AUTHORIZATION_ERROR == data[error_const.ERROR_CODE] assert_header(header, res.headers) def test_get_project_by_id_as_orchard_admin( db_fixture, highlight_data, project_highlight_ok, monkeypatch, project, client, header_user_oa, get_artist_info_ok ): """Assert that an Orchard Admin user can get a project by id.""" monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([ project_highlight_ok, get_artist_info_ok])) expected = { 'project_id': 1, 'project_code': 'ABC', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project', 'artist_id': 123, 'created_date_utc': '2016-01-01 00:00:00.000000', 'updated_date_utc': '2016-01-01 00:00:00.000000', 'product_count': 4, 'description': None, 'project_highlights': highlight_data.get('description'), 'vendor_name': 'Awesome Vendor', 'subaccount_name': 'subaccount_test_value', 'deletions': 'N' } res = client.get('/project/1', headers=header_user_oa) assert expected == json.loads(res.data.decode())