"""Tests for auth0_env.py.""" from oto import response import pytest from sync_env import config from sync_env.logic import auth0_env from sync_env.models import auth0_client @pytest.mark.parametrize( ', '.join([ 'token_response', 'get_users_side_effect', 'delete_user_response' ]), [ ( response.Response( message={'access_token': 'whatever'}), [ response.Response( message=[ {'user_id': 'auth0|1111'}, {'user_id': 'auth0|1112'}, {'user_id': 'auth0|1113'} ]), response.Response( message=[ {'user_id': 'auth0|1114'}, {'user_id': 'auth0|1115'}, {'user_id': 'auth0|1116'}, {'user_id': 'auth0|1117'}, {'user_id': 'auth0|1118'} ]), response.Response( message=[]) ], response.Response() ) ] ) def test_delete_all_users( mocker, token_response, get_users_side_effect, delete_user_response ): """Test delete all users.""" mocker.patch.object( auth0_client, 'get_auth0_management_token', return_value=token_response) mocker.patch.object( auth0_client, 'get_auth0_users_paginated', side_effect=get_users_side_effect) mocker.patch.object( auth0_client, 'delete_auth0_user', return_value=delete_user_response) auth0_env.delete_all_users( 'auth0_domain', 'auth0_api_url', 'auth0_machine_client_id', 'auth0_machine_client_secret', 'auth0_connection', ['1117', '1118']) assert auth0_client.get_auth0_management_token.call_count == 1 assert auth0_client.delete_auth0_user.call_count == 6 assert auth0_client.get_auth0_users_paginated.call_count == 3 @pytest.mark.parametrize( ', '.join([ 'user', 'connection', 'expected_response' ]), [ ( { 'user_id': 'auth0|1111', 'email': 'test1111@test.test' }, 'test-connection', { 'connection': 'test-connection', 'name': config.SYNC_EMAIL_TEMPLATE.format( 'auth0|1111'), 'email': config.SYNC_EMAIL_TEMPLATE.format( 'auth0|1111'), 'password': config.SYNC_PASSWORD, 'user_metadata': { 'source_email': 'test1111@test.test', 'bypass_mfa': True }, 'email_verified': True, 'verify_email': False, 'app_metadata': {} } ), ( { 'user_id': 'auth0|1111', 'name': config.SYNC_EMAIL_TEMPLATE.format( 'auth0|1111'), 'email': 'test1111@test.test', 'user_metadata': { 'pizza': 'party'}, 'app_metadata': { 'hot': 'dogs'} }, 'test-connection', { 'connection': 'test-connection', 'name': config.SYNC_EMAIL_TEMPLATE.format( 'auth0|1111'), 'email': config.SYNC_EMAIL_TEMPLATE.format( 'auth0|1111'), 'password': config.SYNC_PASSWORD, 'user_metadata': { 'pizza': 'party', 'source_email': 'test1111@test.test', 'bypass_mfa': True }, 'email_verified': True, 'verify_email': False, 'app_metadata': { 'hot': 'dogs'} } ) ] ) def test_transform_user( user, connection, expected_response ): """Test user transforms.""" new_user = auth0_env.transform_user( user, connection) assert new_user == expected_response @pytest.mark.parametrize( ', '.join([ 'token_response', 'get_users_side_effect', 'create_user_response' ]), [ ( response.Response( message={'access_token': 'whatever'}), [ response.Response( message=[ {'user_id': 'auth0|1111'}, {'user_id': 'auth0|1112'}, {'user_id': 'auth0|1113'} ]), response.Response( message=[ {'user_id': 'auth0|1114'}, {'user_id': 'auth0|1115'}, {'user_id': 'auth0|1116'} ]), response.Response( message=[]) ], response.Response() ) ] ) def test_sync_users_across_env( mocker, token_response, get_users_side_effect, create_user_response ): """Test sync users from one ENV to another.""" mocker.patch.object( auth0_client, 'get_auth0_management_token', return_value=token_response) mocker.patch.object( auth0_client, 'get_auth0_users_paginated', side_effect=get_users_side_effect) mocker.patch.object( auth0_client, 'create_auth0_user', return_value=create_user_response) auth0_env.sync_users_across_env( 'source_auth0_domain', 'source_auth0_api_url', 'source_auth0_machine_client_id', 'source_auth0_machine_client_secret', 'source_auth0_connection', 'target_auth0_domain', 'target_auth0_api_url', 'target_auth0_machine_client_id', 'target_auth0_machine_client_secret', 'target_auth0_connection') assert auth0_client.get_auth0_management_token.call_count == 2 assert auth0_client.create_auth0_user.call_count == 6 assert auth0_client.get_auth0_users_paginated.call_count == 3 @pytest.mark.parametrize( ', '.join([ 'token_response', 'existing_user_side_effect', 'get_user_side_effect', 'delete_user_side_effect', 'create_user_side_effect' ]), [ ( response.Response( message={'access_token': 'whatever'}), [ response.Response(message=[]), response.Response(message=[{'user_id': 'auth0|7777'}]), response.Response(message=[{'user_id': 'auth0|8888'}]), response.Response(message=[]) ], [ response.Response( message={'user_id': 'auth0|1111'}), response.Response( message={'user_id': 'auth0|2222'}), response.Response( message={'user_id': 'auth0|3333'}), response.Response( message={'user_id': 'auth0|4444'}) ], response.Response(), response.Response() ) ] ) def test_sync_specific_users_across_env( mocker, token_response, existing_user_side_effect, get_user_side_effect, delete_user_side_effect, create_user_side_effect ): """Test sync users from one ENV to another.""" mocker.patch.object( auth0_client, 'get_auth0_management_token', return_value=token_response) mocker.patch.object( auth0_client, 'search_auth0_users_by_email', side_effect=existing_user_side_effect) mocker.patch.object( auth0_client, 'get_auth0_user', side_effect=get_user_side_effect) mocker.patch.object( auth0_client, 'delete_auth0_user', return_value=delete_user_side_effect) mocker.patch.object( auth0_client, 'create_auth0_user', return_value=create_user_side_effect) yaml_obj = [ # user does not exist in target, create it {'id': '1111'}, # user already exists, skip {'id': '2222'}, # user already exists, but we want to force a sync {'id': '3333', 'force_delete': True}, # user does not exist, but we want to force a sync for some reason {'id': '4444', 'force_delete': True}] auth0_env.sync_specific_users_across_env( yaml_obj, 'source_auth0_domain', 'source_auth0_api_url', 'source_auth0_machine_client_id', 'source_auth0_machine_client_secret', 'source_auth0_connection', 'target_auth0_domain', 'target_auth0_api_url', 'target_auth0_machine_client_id', 'target_auth0_machine_client_secret', 'target_auth0_connection') assert auth0_client.get_auth0_management_token.call_count == 2 assert auth0_client.search_auth0_users_by_email.call_count == 4 assert auth0_client.get_auth0_user.call_count == 3 assert auth0_client.delete_auth0_user.call_count == 1 assert auth0_client.create_auth0_user.call_count == 3