"""Tests for the ows_users model.""" from unittest.mock import MagicMock import pytest from requests.models import Response from notifications.constants import brands as brands_constants, tenant as tenant_constants from notifications.models import ows_users @pytest.mark.parametrize(('status_code', 'expected_status'), [(200, 200), (404, 404)]) def test_get_user(status_code, expected_status, mocker): """Test get user.""" ows_users_response = Response() ows_users_response.status_code = status_code ows_users_response.json = MagicMock(return_value='RESULT') api_get_mock = mocker.patch('notifications.api.ows_client.get', return_value=ows_users_response) user_id = 'alw:123' result = ows_users.get_user(user_id) api_get_mock.assert_called_once_with('ows-users', f'/users/{user_id}/minimum-details') assert result.status == expected_status if expected_status == 200: assert result.message == 'RESULT' @pytest.mark.parametrize(('status_code', 'expected_status'), [(200, 200), (404, 404)]) def test_get_identity(status_code, expected_status, mocker): """Test get identity.""" ows_users_response = Response() ows_users_response.status_code = status_code ows_users_response.json = MagicMock(return_value='RESULT') api_get_mock = mocker.patch('notifications.api.ows_client.get', return_value=ows_users_response) identity_id = '03e83149-3c7f-4587-940b-ce83dec34bc8' result = ows_users.get_identity(identity_id) api_get_mock.assert_called_once_with('ows-users', f'/users/identity/{identity_id}') assert result.status == expected_status if expected_status == 200: assert result.message == 'RESULT' @pytest.mark.parametrize( ('input_kwargs', 'status_code', 'expected_status', 'expected_api_call_params'), [ pytest.param( { 'identity_id': '03e83149-3c7f-4587-940b-ce83dec34bc8', 'resource_type': tenant_constants.VENDOR_RESOURCE_TYPE, 'resource_uuid': '4eb6f1d2-2f4a-4d5a-9f7e-8f3c9e6d7a1b', 'brand': brands_constants.BRAND_ORCHARD, }, 200, 200, { 'resource_type': tenant_constants.VENDOR_RESOURCE_TYPE, 'resource_uuid': '4eb6f1d2-2f4a-4d5a-9f7e-8f3c9e6d7a1b', 'brand': brands_constants.BRAND_ORCHARD, }, id='Success. All arguments passed (all not None).', ), pytest.param( { 'identity_id': '03e83149-3c7f-4587-940b-ce83dec34bc8', 'resource_type': None, 'resource_uuid': None, 'brand': None, }, 200, 200, {}, id='Success. All arguments passed (all None).', ), pytest.param( { 'identity_id': '03e83149-3c7f-4587-940b-ce83dec34bc8', }, 200, 200, {}, id='Success. No arguments passed.', ), pytest.param( { 'identity_id': '03e83149-3c7f-4587-940b-ce83dec34bc8', }, 404, 404, {}, id='Fail.', ), ], ) def test_get_applications( input_kwargs, status_code, expected_status, expected_api_call_params, mocker ): """Test get applications.""" ows_users_response = Response() ows_users_response.status_code = status_code ows_users_response.json = MagicMock(return_value={'items': 'RESULT'}) api_get_mock = mocker.patch('notifications.api.ows_client.get', return_value=ows_users_response) headers = {'foo': 'bar'} mocker.patch('notifications.models.ows_users.get_ows_headers', return_value=headers) identity_id = input_kwargs['identity_id'] result = ows_users.get_applications(**input_kwargs) api_get_mock.assert_called_once_with( 'ows-users', f'/users/identity/{identity_id}/applications', headers=headers, params=expected_api_call_params, ) assert result.status == expected_status if expected_status == 200: assert result.message == 'RESULT' @pytest.mark.parametrize(('status_code', 'expected_status'), [(200, 200), (404, 404)]) def test_get_organization_info(status_code, expected_status, mocker): """Test get organization info.""" ows_users_response = Response() ows_users_response.status_code = status_code ows_users_response.json = MagicMock(return_value='RESULT') api_get_mock = mocker.patch('notifications.api.ows_client.get', return_value=ows_users_response) headers = {'foo': 'bar'} mocker.patch('notifications.models.ows_users.get_ows_headers', return_value=headers) org_name = 'test_org' result = ows_users.get_organization_info(org_name) api_get_mock.assert_called_once_with( 'ows-users', f'/auth0/organizations/{org_name}', headers=headers ) assert result.status == expected_status if expected_status == 200: assert result.message == 'RESULT'