"""Test for ows_account model.""" from unittest.mock import MagicMock import flexmock from owsrequest import request import pytest from analytics.consts import services from analytics.models import ows_account @pytest.fixture def subaccount_id(): """Return a subaccount_id for testing.""" return 25537 @pytest.fixture def vendor_id(): """Return a subaccount_id for testing.""" return 25537 @pytest.fixture def successful_get_vendor_id_response(vendor_id): """Return a mock response from ows-assets getting the vendor_id.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value={'vendor_id': vendor_id}) return mock_response @pytest.fixture def not_found_get_vendor_id_response(vendor_id): """Return a mock response from ows-assets getting the vendor_id.""" mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value={'message': 'some error'}) return mock_response def test_get_vendor_id_success( subaccount_id, vendor_id, successful_get_vendor_id_response): """Test that a 200 response is returned with vendor_id.""" subaccount_resource = '/subaccount/{}'.format(subaccount_id) (flexmock(request).should_receive('get').with_args( services.OWS_ACCOUNT, subaccount_resource).and_return( successful_get_vendor_id_response)) result = ows_account.get_vendor_id_by_subaccount_id(subaccount_id) assert result.status == 200 assert result.message == vendor_id def test_get_vendor_id_not_found( subaccount_id, vendor_id, not_found_get_vendor_id_response): """Test that a 200 response is returned with vendor_id.""" subaccount_resource = '/subaccount/{}'.format(subaccount_id) (flexmock(request).should_receive('get').with_args( services.OWS_ACCOUNT, subaccount_resource).and_return( not_found_get_vendor_id_response)) result = ows_account.get_vendor_id_by_subaccount_id(subaccount_id) assert result.status == 404 assert result.errors.get('message') == 'some error'