"""Tests for the ows-account model.""" from unittest.mock import MagicMock, patch import pytest import requests import stringcase from flask.app import AppContext from owsrequest import flask_request from requests.models import Response from permissions.constants import constants from permissions.models import ows_account from permissions.types import Tenant, TenantType ACCOUNT_APPLICATIONS = { 'applications': [ { 'application_id': 'WORKSTATION_ACCOUNTING_APP', 'roles': ['WORKSTATION_ACCOUNTING_BASE_ROLE'], 'url': 'https://workstation.theorchard.com/accounting', }, { 'application_id': 'WORKSTATION_ANALYTICS_APP', 'roles': ['WORKSTATION_ANALYTICS_BASE_ROLE'], 'url': 'https://workstation.theorchard.com/analytics', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE'], 'url': 'https://settings.theorchard.com', }, ] } LABEL_PARTICIPANT_APPLICATIONS = { 'applications': [ { 'application_id': 'INSIGHTS_APP', 'roles': ['INSIGHTS_BASE_ROLE'], 'url': 'https://insights.theorchard.com', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE'], 'url': 'https://settings.theorchard.com', }, ] } SUBACCOUNT_APPLICATIONS = { 'applications': [ { 'application_id': 'WORKSTATION_ACCOUNTING_APP', 'roles': ['WORKSTATION_ACCOUNTING_BASE_ROLE'], 'url': 'https://workstation.theorchard.com/accounting', }, { 'application_id': 'SETTINGS_APP', 'roles': ['SETTINGS_BASE_ROLE'], 'url': 'https://settings.theorchard.com', }, ] } COLLABORATOR_APPLICATIONS = { 'applications': [ { 'application_id': 'CUSTOMER_ACCOUNTING_APP', 'roles': ['CUSTOMER_ACCOUNTING_BASE_ROLE'], 'url': 'https://accounting.theorchard.com', } ] } @pytest.mark.parametrize( ('tenant', 'status_code', 'expected_result'), [ [Tenant(tenant_uuid='abc-123', tenant_type=TenantType.ACCOUNT), 200, ACCOUNT_APPLICATIONS], [ Tenant(tenant_uuid='def-456', tenant_type=TenantType.SUBACCOUNT), 200, SUBACCOUNT_APPLICATIONS, ], [ Tenant(tenant_uuid='ghi-789', tenant_type=TenantType.COLLABORATOR), 200, COLLABORATOR_APPLICATIONS, ], [ Tenant(tenant_uuid='jkl-012', tenant_type=TenantType.LABEL_PARTICIPANT), 200, LABEL_PARTICIPANT_APPLICATIONS, ], [Tenant(tenant_uuid='qwe-189', tenant_type=TenantType.LABEL_PARTICIPANT), 500, None], ], ) @patch('permissions.models.ows_account.g') @patch('owsrequest.flask_request.process') def test_get_enabled_tenant_applications( mock_request_process: MagicMock, mock_g: MagicMock, tenant: Tenant, status_code: int, expected_result: dict[str, list[dict]] | None, app_context: AppContext, ) -> None: """Test get_enabled_tenant_applications.""" tenant_type = stringcase.spinalcase(tenant.tenant_type) tenant_uuid = tenant.tenant_uuid if tenant_type == constants.ACCOUNT_TENANT_TYPE: tenant_type = constants.VENDOR_RESOURCE_TYPE.lower() ows_account_response = Response() ows_account_response.status_code = status_code if status_code == 500: ows_account_response.raise_for_status = MagicMock( side_effect=requests.exceptions.HTTPError('500 Server Error') ) else: ows_account_response.json = MagicMock(return_value=expected_result) mock_request_process.return_value = ows_account_response if status_code == 500: with pytest.raises(requests.exceptions.HTTPError): ows_account.get_enabled_tenant_applications(tenant) else: result = ows_account.get_enabled_tenant_applications(tenant) flask_request.process.assert_called_once_with( application='ows-permissions', environment='test', method='GET', service_name='ows-account', path=f'/{tenant_type}/{tenant_uuid}/applications', uwsgi_cache_enabled=True, headers={ 'content-type': 'application/json', 'Orchard-Profile-Type': mock_g.request_context.profile_type, 'Orchard-Profile-Id': mock_g.request_context.profile_id, 'Orchard-Identity-Id': mock_g.request_context.jwt_identity_id, }, ) if expected_result: assert result == expected_result.get('applications', [])