"""Test for update_auth0_details.""" import json from unittest.mock import MagicMock, Mock, patch from owsresponse import response import pytest from users.app import app from users.logic import profiles, user_info @pytest.mark.parametrize( ('data', 'expected_response', 'expected_status'), ( ( {'auth0_id': 'new_name', 'user_id': 1232, 'user_type': 'alw'}, {'foo': 'bar', 'active': 'N'}, 200, ), ( {'user_id': 1232, 'user_type': 'alw'}, { 'code': 'not_found_error', 'message': {'missing': 'Missing required params or empty.'}, }, 404, ), ), ) @patch('users.handlers.auth0.Neo4jSession.__enter__') @patch('users.handlers.auth0.Neo4jSession.__exit__') def test_update_auth0_details( neo4j_exit, neo4j_enter, monkeypatch, data, expected_response, expected_status ): """Test update_auth0_details success without contact data.""" app.testing = True monkeypatch.setattr( user_info, 'update_auth0_details', MagicMock(return_value=response.Response(message={'foo': 'bar', 'active': 'N'})), ) result = app.test_client().post( '/users/auth0-update', data=json.dumps(data), content_type='application/json' ) response_data = json.loads(result.data.decode('utf-8')) assert result.status_code == expected_status assert response_data == expected_response if expected_response == 200: user_info.update_auth0_details.assert_called_with(**expected_response) @pytest.mark.parametrize( ( 'post_data', 'roles', 'create_profile', 'new_profile', 'expected_response', ), ( ( # when the role is not supported by Insights Profile. {'auth0_id': 'new_name', 'user_id': 1232, 'user_type': 'alw'}, ['catalog'], False, None, response.Response( { 'foo': 'bar', 'active': 'Y', 'account': {'vendor_id': 20151, 'subaccount_id': 2474}, } ), ), ( # when the role is supported. {'auth0_id': 'new_name', 'user_id': 1232, 'user_type': 'alw'}, ['analytics'], True, {'profile_id': 4567, 'profile_type': 'InsightsProfile'}, response.Response( { 'foo': 'bar', 'active': 'Y', 'account': {'vendor_id': 20151, 'subaccount_id': 2474}, 'InsightsProfile': {'profile_id': 4567, 'profile_type': 'InsightsProfile'}, } ), ), ), ) @patch('users.handlers.auth0.Neo4jSession.__enter__') @patch('users.handlers.auth0.Neo4jSession.__exit__') def test_update_auth0_details_roles_check( neo4j_exit, neo4j_enter, monkeypatch, post_data, roles, create_profile, new_profile, expected_response, ): """Test update_auth0_details.""" app.testing = True db_response = { 'foo': 'bar', 'active': 'Y', 'account': {'vendor_id': 20151, 'subaccount_id': 2474}, } monkeypatch.setattr( user_info, 'update_auth0_details', MagicMock(return_value=response.Response(message=db_response)), ) monkeypatch.setattr( user_info, 'get_roles_for_user', MagicMock(return_value=response.Response(message={'role_names': roles})), ) monkeypatch.setattr( profiles, 'create_profile_if_not_exist', MagicMock(return_value=response.Response(message=new_profile)), ) monkeypatch.setattr(profiles, 'grant_access', MagicMock(return_value=response.Response())) result = app.test_client().post( '/users/auth0-update', data=json.dumps(post_data), content_type='application/json' ) response_data = json.loads(result.data.decode('utf-8')) assert result.status_code == expected_response.status if expected_response: assert response_data == expected_response.message user_info.update_auth0_details.assert_called_with( post_data.get('user_id'), post_data.get('user_type'), post_data.get('auth0_id') ) else: assert response_data == expected_response.errors if create_profile: profiles.create_profile_if_not_exist.assert_called_once() profiles.grant_access.assert_called_once() @patch('users.handlers.auth0.Neo4jSession.__enter__') @patch('users.handlers.auth0.Neo4jSession.__exit__') def test_update_auth0_details_profile_error(neo4j_exit, neo4j_enter, monkeypatch): """Test update_auth0_details.""" app.testing = True post_data = {'auth0_id': 'new_name', 'user_id': 1232, 'user_type': 'alw'} db_response = { 'foo': 'bar', 'active': 'Y', 'account': {'vendor_id': 20151, 'subaccount_id': 2474}, } monkeypatch.setattr( user_info, 'update_auth0_details', MagicMock(return_value=response.Response(message=db_response)), ) monkeypatch.setattr( user_info, 'get_roles_for_user', MagicMock(return_value=response.Response(message={'role_names': ['analytics']})), ) monkeypatch.setattr( profiles, 'create_profile_if_not_exist', Mock(side_effect=Exception('Failed to create profile')), ) monkeypatch.setattr(profiles, 'grant_access', MagicMock(return_value=response.Response())) result = app.test_client().post( '/users/auth0-update', data=json.dumps(post_data), content_type='application/json' ) response_data = json.loads(result.data.decode('utf-8')) assert result.status_code == 200 assert response_data == db_response user_info.update_auth0_details.assert_called_with( post_data.get('user_id'), post_data.get('user_type'), post_data.get('auth0_id') ) profiles.create_profile_if_not_exist.assert_called_once() profiles.grant_access.assert_not_called()