"""Test get_profiles.""" import json from unittest.mock import MagicMock, patch from owsresponse import response import pytest from users.app import app from users.logic import profiles @pytest.mark.parametrize( 'url_prefix', ['/profile', '/auth0/users/profile'], ) @patch('users.handlers.profile.Neo4jSession.__enter__') @patch('users.handlers.profile.Neo4jSession.__exit__') def test_get_identity_for_profile(neo4j_exit, neo4j_enter, monkeypatch, mocker, url_prefix): """Test get_identity_for_profile.""" profile_id = 12345 profile_type = 'LabelProfile' expected_response = { 'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f', } app.testing = True mocker.patch.object( profiles, 'get_identity_for_profile', return_value=response.Response(expected_response), autospec=True, ) request = app.test_client().get( f'{url_prefix}/profile_id/{profile_id}/profile_type/{profile_type}/identity' ) response_data = json.loads(request.data.decode('utf-8')) assert request.status_code == 200 assert response_data == expected_response profiles.get_identity_for_profile.assert_called_once_with(profile_id, profile_type) @pytest.mark.parametrize( 'url_prefix', ['/profile', '/auth0/users/profile'], ) @pytest.mark.parametrize( ('headers', 'status', 'expected'), [ ( # no profile headers is ok. {}, 200, {'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f'}, ), ( # partial profile headers are invalid. {'Orchard-Profile-Type': 'LabelProfile'}, 403, {'code': 'authorization_error', 'message': 'access denied'}, ), ( {'Orchard-Profile-Id': 12345}, 403, {'code': 'authorization_error', 'message': 'access denied'}, ), ( { 'Grass-Account-Id': '25189', 'Grass-Account-Type': 'vendor', 'Orchard-User-Id': '41128', }, 403, {'code': 'authorization_error', 'message': 'access denied'}, ), ( # only identity is valid. {'Orchard-Identity-Id': 123}, 200, {'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f'}, ), ( # identity header different than profile id is invalid. { 'Orchard-Profile-Type': 'LabelProfile', 'Orchard-Profile-Id': 45466, 'Orchard-Identity-Id': 123, }, 403, {'code': 'authorization_error', 'message': 'user is forbidden'}, ), ( # identity header different than profile type is invalid. { 'Orchard-Profile-Type': 'SettingsProfile', 'Orchard-Profile-Id': 12345, 'Orchard-Identity-Id': 123, }, 403, {'code': 'authorization_error', 'message': 'user is forbidden'}, ), ( # all 3 headers. { 'Orchard-Profile-Type': 'LabelProfile', 'Orchard-Profile-Id': 12345, 'Orchard-Identity-Id': 123, }, 200, {'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f'}, ), ( # when Orchard-Identity-UUID header. { 'Orchard-Profile-Type': 'LabelProfile', 'Orchard-Profile-Id': 12345, 'Orchard-Identity-Id': 'abcd', 'Orchard-Identity-UUID': 123, }, 200, {'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f'}, ), ], ) @patch('users.handlers.profile.Neo4jSession.__enter__') @patch('users.handlers.profile.Neo4jSession.__exit__') def test_get_identity_for_profile_validate_headers( neo4j_exit, neo4j_enter, monkeypatch, headers, status, expected, url_prefix ): """Test get_identity_for_profile.""" app.testing = True profile_id = 12345 profile_type = 'LabelProfile' expected_response = { 'name': 'New User', 'email': 'test@theorchard.com', 'id': '5dd7d78c4ec49c0e2c4e4d1f', } monkeypatch.setattr( profiles, 'get_identity_for_profile', MagicMock(return_value=response.Response(message=expected_response)), ) request = app.test_client().get( f'{url_prefix}/profile_id/{profile_id}/profile_type/{profile_type}/identity', content_type='application/json', headers=headers, ) response_data = json.loads(request.data.decode('utf-8')) assert request.status_code == status assert response_data == expected