"""Unit tests for the ows_pdp module.""" from unittest import mock import marshmallow import pytest from users.models import ows_pdp @mock.patch('users.models.ows_pdp.ows_client') def test_get_tenant_roles_by_identity_error(ows_client_mock: mock.MagicMock) -> None: """Test that an OwsPdpError is raised when the ows-pdp service returns an error.""" identity_id = 'test-identity-id' mock_response = mock.MagicMock(status_code=503, text='Bad Gateway') ows_client_mock.get.return_value = mock_response with pytest.raises(ows_pdp.OwsPdpError) as exc_info: ows_pdp.get_tenant_roles_by_identity(identity_id) assert exc_info.value.status_code == 503 assert str(exc_info.value) == f'Failed to get roles for identity {identity_id}: Bad Gateway' @mock.patch('users.models.ows_pdp.ows_client') def test_get_tenant_roles_by_identity_schema_mismatch(ows_client_mock: mock.MagicMock) -> None: """Test that an OwsPdpError is raised when the response schema does not match expectations.""" identity_id = 'test-identity-id' mock_response = mock.MagicMock( status_code=200, json=lambda: { 'tenants': { 'uuid1': { 'tenant_type': 'account', 'tenant_uuid': 'uuid1', # Items in 'roles' should be dicts, but here it's a string 'roles': ['🥯'], } }, 'cursor': {'cursor': None, 'shorthand': None}, 'errors': {}, }, ) ows_client_mock.get.return_value = mock_response with pytest.raises(marshmallow.exceptions.ValidationError): ows_pdp.get_tenant_roles_by_identity(identity_id) @mock.patch('users.models.ows_pdp.ows_client') def test_get_tenant_roles_by_identity_success(ows_client_mock: mock.MagicMock) -> None: """Test that tenant roles are correctly retrieved from the ows-pdp service.""" identity_id = 'test-identity-id' mock_response = mock.MagicMock( status_code=200, json=lambda: { 'tenants': { 'uuid1': { 'tenant_type': 'account', 'tenant_uuid': 'uuid1', 'roles': [{'role': 'contract_admin'}], }, 'uuid2': { 'tenant_uuid': 'uuid2', 'tenant_type': 'parent_company', 'roles': [{'role': 'seat_can_administer_users'}], }, }, 'cursor': {'cursor': None, 'shorthand': None}, }, ) ows_client_mock.get.return_value = mock_response result = ows_pdp.get_tenant_roles_by_identity(identity_id) assert result == { 'uuid1': { 'tenant_type': 'account', 'tenant_uuid': 'uuid1', 'roles': [{'role': 'contract_admin'}], }, 'uuid2': { 'tenant_uuid': 'uuid2', 'tenant_type': 'parent_company', 'roles': [{'role': 'seat_can_administer_users'}], }, } ows_client_mock.get.assert_called_once_with( service_name='ows-pdp', path='identity/test-identity-id/roles/?cursor=', ) @mock.patch('users.models.ows_pdp.ows_client') def test_get_tenant_roles_by_identity_pagination(ows_client_mock: mock.MagicMock) -> None: """Test that tenant roles are correctly retrieved with pagination.""" identity_id = 'test-identity-id' mock_response_first_page = mock.MagicMock( status_code=200, json=lambda: { 'tenants': { 'uuid1': { 'tenant_type': 'account', 'tenant_uuid': 'uuid1', 'roles': [{'role': 'contract_admin'}], } }, 'cursor': {'cursor': 'some-string-idk', 'shorthand': 'some-other-string'}, }, ) mock_response_second_page = mock.MagicMock( status_code=200, json=lambda: { 'tenants': { 'uuid2': { 'tenant_uuid': 'uuid2', 'tenant_type': 'parent_company', 'roles': [{'role': 'seat_can_administer_users'}], }, }, 'cursor': {'cursor': None, 'shorthand': None}, }, ) ows_client_mock.get.side_effect = [mock_response_first_page, mock_response_second_page] result = ows_pdp.get_tenant_roles_by_identity(identity_id) assert result == { 'uuid1': { 'tenant_type': 'account', 'tenant_uuid': 'uuid1', 'roles': [{'role': 'contract_admin'}], }, 'uuid2': { 'tenant_uuid': 'uuid2', 'tenant_type': 'parent_company', 'roles': [{'role': 'seat_can_administer_users'}], }, } ows_client_mock.get.assert_has_calls( [ mock.call(service_name='ows-pdp', path='identity/test-identity-id/roles/?cursor='), mock.call( service_name='ows-pdp', path='identity/test-identity-id/roles/?cursor=some-string-idk', ), ] )