"""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 @patch('users.handlers.profile.Neo4jSession.__enter__') @patch('users.handlers.profile.Neo4jSession.__exit__') def test_get_profiles_model(neo4j_exit, neo4j_enter, monkeypatch, mocker): """Test get_profiles for not-mocked identity.""" expected = {'foo': 'bar'} orchard_identity = 'auth0id' app_name = 'orchard-go' app.testing = True mocker.patch.object( profiles, 'get_profiles_for_identity', return_value=response.Response(expected), autospec=True, ) request = app.test_client().get( f'/users/identity/{orchard_identity}/application/{app_name}/profiles', content_type='application/json', ) response_data = json.loads(request.data.decode('utf-8')) assert request.status_code == 200 assert response_data == expected profiles.get_profiles_for_identity.assert_called_once_with(orchard_identity, app_name, []) @pytest.mark.parametrize( ('headers', 'status', 'expected'), [ ( # no profile headers is ok. {}, 200, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ( # partial profile headers are invalid. {'Orchard-Profile-Type': 'test'}, 400, { 'code': 'bad_request', 'message': 'invalid combination of profile_type, profile_id, and identity_id', }, # noqa ), ( {'Orchard-Profile-Id': 123}, 400, { 'code': 'bad_request', 'message': 'invalid combination of profile_type, profile_id, and identity_id', }, # noqa ), ( { '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, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ( # identity header different than url is invalid. {'Orchard-Identity-Id': 456}, 400, {'code': 'bad_headers', 'message': "Cannot access other user's profile."}, # noqa ), ( # identity header are completely different { 'Orchard-Profile-Type': 'test', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 'abc', 'Orchard-Identity-Uuid': 'identity-uuid', }, 400, {'code': 'bad_headers', 'message': "Cannot access other user's profile."}, # noqa ), ( # all 4 headers. { 'Orchard-Profile-Type': 'test', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 123, 'Orchard-Identity-Uuid': '123', }, 200, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ( # identity header are completely different { 'Orchard-Profile-Type': 'test', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 123, 'Orchard-Identity-Uuid': 'abc', }, 200, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ( # all 3 headers. {'Orchard-Profile-Type': 'test', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 123}, 200, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ( # when uuid header matches url param. { 'Orchard-Profile-Type': 'test', 'Orchard-Profile-Id': 123, 'Orchard-Identity-Id': 'abcd', 'Orchard-Identity-UUID': 123, }, 200, {'items': [], 'pagination': {'type': 'none', 'total_records': 0}}, ), ], ) @patch('users.handlers.profile.Neo4jSession.__enter__') @patch('users.handlers.profile.Neo4jSession.__exit__') def test_get_profiles_validate_headers( neo4j_exit, neo4j_enter, monkeypatch, headers, status, expected ): """Test get_profiles header verifications.""" app.testing = True profile_data = {'items': [], 'pagination': {'type': 'none', 'total_records': 0}} monkeypatch.setattr( profiles, 'get_profiles_for_identity', MagicMock(return_value=response.Response(message=profile_data)), ) request = app.test_client().get( '/users/identity/123/application/orchard-go/profiles', content_type='application/json', headers=headers, ) response_data = json.loads(request.data.decode('utf-8')) assert request.status_code == status assert response_data == expected