"""Test for ows_account model.""" from unittest.mock import MagicMock, patch import flexmock import oto import pytest from owsrequest import request from owsrequest.constants import headers as owsrequest_headers from analytics.constants import service as service_constants from analytics.services 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_response(vendor_id): """Return a mock response from ows-assets getting the vendor.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock( return_value={"vendor_id": vendor_id, "more": "info"} ) return mock_response @pytest.fixture def not_found_get_vendor_response(vendor_id): """Return a mock response from ows-assets getting the vendor.""" mock_response = MagicMock(status_code=404) mock_response.json = MagicMock(return_value={"message": "some error"}) return mock_response @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_success(vendor_id, successful_get_vendor_response): """Test that a 200 response is returned with get_vendor.""" resource = "/vendor/{}".format(vendor_id) ( flexmock.flexmock(request) .should_receive("get") .with_args(service_constants.OWS_ACCOUNT, resource) .and_return(successful_get_vendor_response) ) result = ows_account.get_vendor(vendor_id) assert result.status == 200 assert result.message == {"vendor_id": vendor_id, "more": "info"} def test_get_vendor_not_found(vendor_id, not_found_get_vendor_response): """Test that a 200 response is returned with get_vendor.""" resource = "/vendor/{}".format(vendor_id) ( flexmock.flexmock(request) .should_receive("get") .with_args(service_constants.OWS_ACCOUNT, resource) .and_return(not_found_get_vendor_response) ) result = ows_account.get_vendor(vendor_id) assert result.status == 404 assert result.errors.get("message") == "some error" 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.flexmock(request) .should_receive("get") .with_args(service_constants.OWS_ACCOUNT, subaccount_resource) .and_return(successful_get_vendor_id_response) ) result = ows_account.get_vendor_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.flexmock(request) .should_receive("get") .with_args(service_constants.OWS_ACCOUNT, subaccount_resource) .and_return(not_found_get_vendor_id_response) ) result = ows_account.get_vendor_id(subaccount_id) assert result.status == 404 assert result.errors.get("message") == "some error" class TestGetLabelSubaccountWithLabel(object): """Test get_label_subaccount_headers with label.""" account_type = "vendor" account_id = "7123" expected_payload = { "labelid": "7123", "subaccountid": None, "headers": { owsrequest_headers.GRASS_ACCOUNT_TYPE: account_type, owsrequest_headers.GRASS_ACCOUNT_ID: account_id, }, } def test_succeeds(self): """Test successful response.""" response = ows_account.get_label_subaccount_headers( self.account_type, self.account_id ) assert response.status == 200 assert response.message == self.expected_payload class TestGetLabelSubaccountWithSubaccount(object): """Test get_label_subaccount_headers with subaccount.""" account_type = "subaccount" account_id = "345" vendor_id = "7123" expected_payload = { "labelid": "7123", "subaccountid": "345", "headers": { owsrequest_headers.GRASS_ACCOUNT_TYPE: account_type, owsrequest_headers.GRASS_ACCOUNT_ID: account_id, }, } @pytest.fixture def mock_get_vendor(self): """Mock successful response from ows_account.get_vendor_id.""" with patch("analytics.services.ows_account." "get_vendor_id") as get_vendor_id: get_vendor_id.return_value = oto.response.Response(self.vendor_id) yield get_vendor_id @pytest.fixture def mock_get_vendor_failure(self): """Mock failure response from ows_account.get_vendor_id.""" with patch("analytics.services.ows_account." "get_vendor_id") as get_vendor_id: get_vendor_id.return_value = oto.response.Response(status=404) yield get_vendor_id def test_succeeds(self, mock_get_vendor): """Test successful response.""" response = ows_account.get_label_subaccount_headers( self.account_type, self.account_id ) assert response.status == 200 assert response.message == self.expected_payload def test_failure(self, mock_get_vendor_failure): """Test failure response.""" response = ows_account.get_label_subaccount_headers( self.account_type, self.account_id ) assert response.status == 404 class TestOWSAccountIsVendorHasAccessGoodResponse: """Test call to ows-account with good response.""" account_id = "1" response = {"items": [{"feature_name": "Analytics"}]} @pytest.fixture def mock_response(self): """Return mock response of ows-users.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value=self.response) return mock_response @pytest.fixture def mock_request(self, mock_response): """Mock GET request.""" flexmock.flexmock(request).should_receive("get").with_args( service_constants.OWS_ACCOUNT, service_constants.OWS_ACCOUNT_FEATURES_RESOURCE.format( vendor_id=self.account_id ), ).and_return(mock_response) def test_success(self, mock_request): """Test 200 response is returned.""" result = ows_account.vendor_has_access(self.account_id) assert result.message is True class TestOWSUsersIsUserHasAccessBadResponse: """Test call to ows-users with bad response.""" account_type = "vendor" account_id = "1" user_id = "alw:1" response_no_feature = {"items": [{"feature_name": "GreenElefant"}]} @pytest.fixture def mock_response_error(self): """Return mock response of ows-account.""" mock_response = MagicMock(status_code=404) return mock_response @pytest.fixture def mock_response_no_feature(self): """Return mock response of ows-account.""" mock_response = MagicMock(status_code=200) mock_response.json = MagicMock(return_value=self.response_no_feature) return mock_response @pytest.fixture def mock_request_error(self, mock_response_error): """Mock GET request returning extrnal error.""" flexmock.flexmock(request).should_receive("get").with_args( service_constants.OWS_ACCOUNT, service_constants.OWS_ACCOUNT_FEATURES_RESOURCE.format( vendor_id=self.account_id ), ).and_return(mock_response_error) @pytest.fixture def mock_request_no_feature(self, mock_response_no_feature): """Mock GET request returning valid answer with no feature.""" flexmock.flexmock(request).should_receive("get").with_args( service_constants.OWS_ACCOUNT, service_constants.OWS_ACCOUNT_FEATURES_RESOURCE.format( vendor_id=self.account_id ), ).and_return(mock_response_no_feature) def test_failure_of_ows_account(self, mock_request_error): """Test Exception raised.""" with pytest.raises(Exception): ows_account.vendor_has_access(self.account_id) def test_failure_because_of_feature_disabled(self, mock_request_no_feature): """Test False returned.""" assert ows_account.vendor_has_access(self.account_id).message is False