"""Tests for ows-marketing Model.""" from unittest.mock import MagicMock from oto import response from owsrequest import request as requests from ows_product_physical import api from ows_product_physical.constant import account from ows_product_physical.constant import field from ows_product_physical.constant import service_name from ows_product_physical.models import ows_account def raise_no_json_error(_): """Raise error when attempting to parse json body.""" raise ValueError('No json error!') def test_get_vendor_id_from_grass_headers_type_vendor_success(): """Test get vendor by grass header account type vendor success.""" vendor_response = ows_account.get_vendor_id_from_grass_headers( account.VENDOR, '1234') assert vendor_response.message.get('vendor_id') == 1234 def test_get_vendor_id_from_grass_headers_type_subaccount_success( monkeypatch, valid_get_header, context): """Test get vendor by grass header account type subaccount success.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={ field.DESCRIPTION: None, field.SUBACCOUNT_ID: 27921, 'subaccount_name': 'Pelicana', field.VENDOR_ID: 26022}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) vendor_response = ows_account.get_vendor_id_from_grass_headers( account.SUBACCOUNT, '1234') assert vendor_response.message.get('vendor_id') == 26022 def test_get_vendor_id_from_grass_headers_type_subaccount_fail( monkeypatch, valid_get_header, context): """Test get vendor by grass header account type subaccount fail.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value={ 'code': 'not_found_error', 'message': None}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) vendor_response = ows_account.get_vendor_id_from_grass_headers( account.SUBACCOUNT, '1234') assert not vendor_response def test_get_vendor_id_from_grass_headers_type_invalid_account_type( monkeypatch, valid_get_header, context): """Test get vendor by grass header account type subaccount fail.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={ field.DESCRIPTION: None, field.SUBACCOUNT_ID: 27921, 'subaccount_name': 'Pelicana', field.VENDOR_ID: 26022}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) vendor_response = ows_account.get_vendor_id_from_grass_headers( 'banana', '1234') assert vendor_response.status == 400 assert vendor_response.errors.get('message') == 'Invalid account type.' def test_get_subaccount_by_id_success( monkeypatch, valid_get_header, context): """Test get subaccount by id success.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={ field.DESCRIPTION: None, field.SUBACCOUNT_ID: 27921, 'subaccount_name': 'Pelicana', field.VENDOR_ID: 26022}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) subaccount_response = \ ows_account.get_subaccount_by_id(27921) assert requests.get.called assert requests.get.call_args[0][0] == service_name.OWS_ACCOUNT assert requests.get.call_args[0][1] == ( '/subaccount/{}').format(27921) assert subaccount_response.status == 200 assert subaccount_response.message.get(field.VENDOR_ID) == 26022 assert subaccount_response.message.get('subaccount_name') == 'Pelicana' def test_get_subaccount_by_id_fail( monkeypatch, valid_get_header, context): """Test get subaccount by id not found.""" with context, api.app.test_request_context(headers=valid_get_header): mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value={ 'code': 'not_found_error', 'message': None}) monkeypatch.setattr(requests, 'get', MagicMock( return_value=mock_response)) subaccount_response = \ ows_account.get_subaccount_by_id(27921) assert requests.get.called assert requests.get.call_args[0][0] == service_name.OWS_ACCOUNT assert requests.get.call_args[0][1] == ( '/subaccount/{}').format(27921) assert subaccount_response.status == 404 assert subaccount_response.errors.get('code') == 'not_found_error' def test_get_vendor( monkeypatch, valid_get_header, context): """Test getting a vendor.""" account_type = 'vendor' account_id = 123 mock_vendor = {'vendor_id': account_id, 'vendor_name': 'Test'} with context, api.app.test_request_context(headers=valid_get_header): monkeypatch.setattr( ows_account, 'get_vendor_id_from_grass_headers', MagicMock(return_value=response.Response( status=200, message={'vendor_id': account_id}) ) ) monkeypatch.setattr( requests, 'get', MagicMock(return_value=MagicMock( status_code=200, json=MagicMock(return_value=mock_vendor)) ) ) res = ows_account.get_vendor(account_type, account_id) ows_account.get_vendor_id_from_grass_headers.assert_called_once_with( account_type, account_id ) requests.get.assert_called_once_with( service_name.OWS_ACCOUNT, f'/vendor/{account_id}' ) assert res.status == 200 assert res.message.get('vendor_id') == account_id assert res.message.get('vendor_name') == 'Test' # Test when failing to get the vendor_id monkeypatch.setattr( ows_account, 'get_vendor_id_from_grass_headers', MagicMock(return_value=response.Response(status=500)) ) monkeypatch.setattr( requests, 'get', MagicMock() ) res = ows_account.get_vendor(account_type, account_id) ows_account.get_vendor_id_from_grass_headers.assert_called_once_with( account_type, account_id ) requests.get.assert_not_called() assert res.status == 500 # Test when failing to get the vendor details monkeypatch.setattr( ows_account, 'get_vendor_id_from_grass_headers', MagicMock(return_value=response.Response( status=200, message={'vendor_id': account_id}) ) ) monkeypatch.setattr( requests, 'get', MagicMock(return_value=MagicMock( status_code=500, json=MagicMock(return_value={})) ) ) res = ows_account.get_vendor(account_type, account_id) ows_account.get_vendor_id_from_grass_headers.assert_called_once_with( account_type, account_id ) requests.get.assert_called_once_with( service_name.OWS_ACCOUNT, f'/vendor/{account_id}' ) assert res.status == 500