"""List Projects Functional Test.""" import json from unittest.mock import patch 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 pagination_const from tests.common_assert import assert_header test_subaccount_obj = { 'subaccount_name': 'Label One', 'description': 'This is my first subaccount.', 'subaccount_id': 1, 'vendor_id': 16055} def test_get_projects_vendor( db_fixture, projects, client, header, monkeypatch, get_artist_info_multiple_artists_ok): """Test getting list of projects using vendor. Assert that project list is the same as expected. """ expected = {'items': [ { '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, 'subaccount': None, 'deletions': 'N'}, { 'project_id': 2, 'project_code': 'DEF', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project 2', 'artist_id': 311, 'created_date_utc': '2016-01-01 02:00:00.000000', 'updated_date_utc': '2016-01-01 02:00:00.000000', 'product_count': 2, 'subaccount': None, 'deletions': 'N'}, { '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 01:00:00.000000', 'updated_date_utc': '2016-01-01 01:00:00.000000', 'product_count': 4, 'subaccount': None, 'deletions': 'N'}], 'pagination': { 'page_offset': 0, 'page_limit': 1000, 'total_records': 3} } expected['items'][0]['subaccount'] = test_subaccount_obj expected['items'][1]['subaccount'] = test_subaccount_obj expected['items'][2]['subaccount'] = test_subaccount_obj get_specs = [{ 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': test_subaccount_obj}, { 'service': 'ows-account', 'path': '/subaccount/2', 'status': 200, 'json': test_subaccount_obj}] + get_artist_info_multiple_artists_ok monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) res = client.get( '/projects?page_offset=0&page_limit=1000', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_projects_subaccount( db_fixture, projects, client, header_subaccount, monkeypatch, get_artist_info_multiple_artists_ok): """Test getting list of projects using subaccount. Assert that project list is the same as expected. """ expected = {'items': [ { 'project_id': 2, 'project_code': 'DEF', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project 2', 'artist_id': 311, 'created_date_utc': '2016-01-01 02:00:00.000000', 'updated_date_utc': '2016-01-01 02:00:00.000000', 'product_count': 2, 'deletions': 'N'}, { '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 01:00:00.000000', 'updated_date_utc': '2016-01-01 01:00:00.000000', 'product_count': 4, 'deletions': 'N'}], 'pagination': { 'page_offset': 0, 'page_limit': 1000, 'total_records': 2} } expected['items'][0]['subaccount'] = test_subaccount_obj expected['items'][1]['subaccount'] = test_subaccount_obj get_specs = [{ 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': test_subaccount_obj}] + get_artist_info_multiple_artists_ok monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) res = client.get( '/projects?page_offset=0&page_limit=1000', headers=header_subaccount) assert expected == json.loads(res.data.decode()) assert_header(header_subaccount, res.headers) @patch('owsrequest.request.head') @patch('owsrequest.request.get') def test_get_projects_distributor( mock_get_request, mock_head_request, db_fixture, projects, client, header, mock_owsrequest): """Test getting list of projects using subaccount in params but not in headers. Assert that project list is the same as expected. """ mock_head_request.return_value = mock_owsrequest(200, None) mock_get_request.return_value = mock_owsrequest(200, test_subaccount_obj) expected = {'items': [ { 'project_id': 2, 'project_code': 'DEF', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project 2', 'artist_id': 311, 'created_date_utc': '2016-01-01 02:00:00.000000', 'updated_date_utc': '2016-01-01 02:00:00.000000', 'product_count': 2, 'deletions': 'N'}, { '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 01:00:00.000000', 'updated_date_utc': '2016-01-01 01:00:00.000000', 'product_count': 4, 'deletions': 'N'}], 'pagination': { 'page_offset': 0, 'page_limit': 1000, 'total_records': 2}} expected['items'][0]['subaccount'] = test_subaccount_obj expected['items'][1]['subaccount'] = test_subaccount_obj result = client.get( '/projects?page_offset=0&page_limit=1000&subaccount_id=1', headers=header) assert expected == json.loads(result.data.decode()) assert_header(header, result.headers) def test_get_projects_distributor_unauthorized( db_fixture, projects, client, header, monkeypatch): """Test getting list of projects when distributor is unauthorized.""" errors = { 'message': 'subaccount does not belong to vendor', 'code': 'authorization_error'} head_specs = [{ 'service': 'ows-account', 'path': '/7123/subaccount/1', 'status': 403, 'json': errors}] monkeypatch.setattr( request, 'head', test_utils.mock_ows_requests(head_specs)) res = client.get( '/projects?page_offset=0&page_limit=1000&subaccount_id=1', headers=header) assert_header(header, res.headers) assert 403 == res.status_code res_json = json.loads(res.data.decode()) assert error_const.AUTHORIZATION_ERROR ==\ res_json[error_const.ERROR_CODE] assert 'subaccount does not belong to vendor' ==\ res_json[error_const.ERROR_DETAIL] def test_get_projects_empty(db_fixture, projects, client, header, monkeypatch): """Test getting empty list of projects. Assert that project list and pagination data is the same as expected. """ expected = {'items': [], 'pagination': { 'page_offset': 0, 'page_limit': 1000, 'total_records': 0}} head_specs = [{ 'service': 'ows-account', 'path': '/7123/subaccount/9999', 'status': 200, 'json': None}] monkeypatch.setattr( request, 'head', test_utils.mock_ows_requests(head_specs)) res = client.get( '/projects?page_offset=0&page_limit=1000&subaccount_id=9999', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_projects_defaults( db_fixture, projects, client, header, monkeypatch): """Test the defaults for the list projects endpoint. Assert that the pagination data is the same as expected. """ expected = {'items': [], 'pagination': { 'page_offset': pagination_const.PAGE_OFFSET_DEFAULT, 'page_limit': pagination_const.PAGE_LIMIT_DEFAULT, 'total_records': 0}} head_specs = [{ 'service': 'ows-account', 'path': '/7123/subaccount/9999', 'status': 200, 'json': None}] monkeypatch.setattr( request, 'head', test_utils.mock_ows_requests(head_specs)) res = client.get('/projects?subaccount_id=9999', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_projects_pagination( db_fixture, projects, client, header, monkeypatch): """Test the pagination values for list projects. Assert that the pagination data is the same as expected. """ expected = {'items': [ { 'project_id': 2, 'project_code': 'DEF', 'vendor_id': 7123, 'subaccount_id': 1, 'project_name': 'new project 2', 'artist_id': 311, 'created_date_utc': '2016-01-01 02:00:00.000000', 'updated_date_utc': '2016-01-01 02:00:00.000000', 'product_count': 2, 'subaccount': test_subaccount_obj, 'deletions': 'N'}], 'pagination': { 'page_offset': 1, 'page_limit': 1, 'total_records': 3} } subaccount_for_vendor = { 'service': 'ows-account', 'path': '/7123/subaccount/9999', 'status': 200, 'json': expected} subaccount_by_id = { 'service': 'ows-account', 'path': '/subaccount/1', 'status': 200, 'json': test_subaccount_obj} get_specs = [subaccount_for_vendor, subaccount_by_id] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) res = client.get( '/projects?page_offset=1&page_limit=1', headers=header) assert expected == json.loads(res.data.decode()) assert_header(header, res.headers) def test_get_projects_pagination_exceed_limit(client, header): """Test exceeding the max page limit. Assert that expected error json is returned. """ expected = { error_const.ERROR_CODE: error_const.VALIDATION_ERROR, error_const.ERROR_DETAIL: 'Page limit is greater than the max acceptable'} url = '/projects?page_limit={}'.format( pagination_const.PAGE_LIMIT_MAX_ACCEPTABLE + 1) res = client.get(url, headers=header) assert 400 == res.status_code assert json.loads(res.data.decode()) == expected def test_get_projects_invalid_account_type(client, header): """Test passing in an invalid account type. Assert that expected error json is returned. """ expected = { error_const.ERROR_CODE: error_const.VALIDATION_ERROR, error_const.ERROR_DETAIL: 'Request header account type incorrect'} header[header_const.GRASS_ACCOUNT_TYPE] = 'invalid' res = client.get('/projects', headers=header) assert 400 == res.status_code assert json.loads(res.data.decode()) == expected def test_get_projects_for_oa( db_fixture, projects_without_subaccount, client, header_user_oa, monkeypatch, orchard_user_id_oa): """Test project endpoint for OA user. Assert that this method should support for OA user. """ expected = {'items': [ { 'project_id': 3, 'artist_id': 4567, 'vendor_id': 7123, 'updated_date_utc': '2016-01-01 03:00:00.000000', 'subaccount_id': None, 'project_name': 'new project 3', 'project_code': 'GHI', 'product_count': 0, 'created_date_utc': '2016-01-01 03:00:00.000000', 'deletions': 'N' }, { 'project_id': 2, 'artist_id': 311, 'vendor_id': 7123, 'updated_date_utc': '2016-01-01 02:00:00.000000', 'subaccount_id': None, 'project_name': 'new project 2', 'project_code': 'DEF', 'product_count': 2, 'created_date_utc': '2016-01-01 02:00:00.000000', 'deletions': 'N' }, { 'project_id': 1, 'artist_id': 1234, 'vendor_id': 7123, 'updated_date_utc': '2016-01-01 01:00:00.000000', 'subaccount_id': None, 'project_name': 'new project', 'project_code': 'ABC', 'product_count': 4, 'created_date_utc': '2016-01-01 01:00:00.000000', 'deletions': 'N' }], 'pagination': { 'page_offset': pagination_const.PAGE_OFFSET_DEFAULT, 'page_limit': pagination_const.PAGE_LIMIT_DEFAULT, 'total_records': 3}} get_specs = [ { 'service': 'ows-features', 'path': '/features/user/{}'.format(orchard_user_id_oa), 'status': 200, 'json': {} }] monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests(get_specs)) res = client.get('/projects?vendor_id=7123', headers=header_user_oa) assert expected == json.loads(res.data.decode()) assert_header(header_user_oa, res.headers)