"""Test get_applications.""" import json from unittest.mock import patch from owsresponse import response import pytest from users import constants # noqa from users.app import app from users.logic import profiles @pytest.mark.parametrize( ('headers', 'admin_response', 'status', 'expected'), [ ( { 'Orchard-Profile-Type': 'SettingsProfile', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 'admin-uuid', 'Orchard-Identity-UUID': 'admin-uuid', }, {'identity_id': 'admin-uuid'}, 200, {'success': 'from get_applications_for_identity'}, ), ( # no headers {}, {'identity_id': 'admin-uuid'}, 400, { 'code': 'bad_request', 'message': 'invalid combination of profile_type, profile_id, and identity_id', }, ), ( # admin with awal brand { 'Orchard-Profile-Type': 'SettingsProfile', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 'admin-uuid', 'Orchard-Identity-UUID': 'admin-uuid', }, {'identity_id': 'admin-uuid', 'default_brand': 'awal'}, 200, {'success': 'from get_applications_for_identity'}, ), ], ) @patch('users.handlers.profile.Neo4jSession.__enter__') @patch('users.handlers.profile.Neo4jSession.__exit__') def test_get_applications( neo4j_exit, neo4j_enter, headers, admin_response, status, expected, mocker, ): """Test get_applications.""" identity_id = 'uuid' get_response = {'success': 'from get_applications_for_identity'} app.testing = True mocker.patch.object( profiles, 'get_applications_for_identity_tx', return_value=response.Response(get_response), autospec=True, ) mocker.patch.object( profiles, 'get_identity_from_graph_tx', return_value=response.Response(admin_response), autospec=True, ) request = app.test_client().get( f'/users/identity/{identity_id}/applications', headers=headers, content_type='application/json', ) response_data = json.loads(request.data.decode('utf-8')) assert request.status_code == status assert response_data == expected if status == 200: profiles.get_identity_from_graph_tx.assert_called_once_with(identity_id) profiles.get_applications_for_identity_tx.assert_called_once_with( identity_id, None, admin_response.get('default_brand', constants.ORCHARD_BRAND), None, None, )