"""Test for ows-account model.""" from http.client import HTTPException from unittest.mock import MagicMock from owsrequest import request import pytest from project_manager.models import ows_account as account def test_is_distributor_true(monkeypatch, vendor_id): """Test correct response when vendor is distributor.""" http_response = MagicMock() http_response.status_code = 200 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_distributor(vendor_id) assert response.status == 200 assert response.message == 'account is a distributor' def test_is_distributor_false(monkeypatch, vendor_id): """Test correct response when vendor is not a distributor.""" http_response = MagicMock() http_response.status_code = 204 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_distributor(vendor_id) assert response.status == 204 assert response.message == 'account is not a distributor' def test_is_distributor_unexpected(monkeypatch, vendor_id, test_request_context): """Test fatal response when enexpected response from ows-account.""" http_response = MagicMock() http_response.status_code = 500 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_distributor(vendor_id) assert response.status == 500 assert response.errors.get('message') == \ 'ows-account returned unexpected status_code of 500' def test_get_vendor_id_for_subaccount_id_success( monkeypatch, subaccount_id, vendor_id): """Test returns vendor information from subaccount_id when found.""" http_response = MagicMock() http_response.status_code = 200 http_response.json = MagicMock(return_value={'vendor_id': vendor_id}) monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = account.get_vendor_id_for_subaccount_id(subaccount_id) assert response == vendor_id def test_is_subaccount_for_vendor_true(monkeypatch, vendor_id, subaccount_id): """Test correct response when subaccount belongs to vendor.""" http_response = MagicMock() http_response.status_code = 200 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_subaccount_for_vendor(vendor_id, subaccount_id) assert response.status == 200 assert response.message == 'subaccount belongs to vendor' def test_is_subaccount_for_vendor_false(monkeypatch, vendor_id, subaccount_id): """Test correct response when subaccount does not belongs to vendor.""" http_response = MagicMock() http_response.status_code = 403 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_subaccount_for_vendor(vendor_id, subaccount_id) assert response.status == 403 assert response.errors.get('message') == \ 'subaccount does not belong to vendor' def test_is_subaccount_for_vendor_unexpected( monkeypatch, vendor_id, subaccount_id, test_request_context): """Test correct response when unexpected response from ows-account.""" http_response = MagicMock() http_response.status_code = 500 monkeypatch.setattr(request, 'head', MagicMock(return_value=http_response)) response = account.is_subaccount_for_vendor(vendor_id, subaccount_id) assert response.status == 500 assert response.errors.get('message') == \ 'ows-account returned unexpected status_code of 500' def test_get_subaccount_for_subaccount_id(monkeypatch, subaccount_id): """Test that subaccount information is returned for a given subaccount.""" http_response = MagicMock() http_response.status_code = 200 http_response.json = MagicMock(return_value={ 'description': 'This is my first subaccount.', 'vendor_id': 16055, 'subaccount_name': 'Label One', 'subaccount_id': 1}) monkeypatch.setattr(request, 'get', MagicMock(return_value=http_response)) response = account.get_subaccount_for_subaccount_id(subaccount_id) assert response == { 'description': 'This is my first subaccount.', 'vendor_id': 16055, 'subaccount_name': 'Label One', 'subaccount_id': 1} def test_vendor_not_found(test_request_context, request_engine): """Test method raises when request fails.""" request_engine['ows-account'].add_spec( 'GET', '/vendor/123', { 'message': 'not found' }, status=404 ) with pytest.raises(HTTPException): account.get_vendor(123)