"""Test for Auth0.""" from unittest.mock import MagicMock import pytest from auth0.v3 import Auth0Error from owsresponse import response from permissions.models import auth0 @pytest.mark.parametrize( ('is_artist', 'user_metadata', 'app_client_id'), [ [True, {}, 'AUTH0_INSIGHT_APP_CLIENT_ID'], [False, {}, 'AUTH0_SETTINGS_APP_CLIENT_ID'], [False, {'should_send_collaborator_access_email': True}, 'AUTH0_MONEYHUB_APP_CLIENT_ID'], ], ) def test_send_password_reset(mocker, is_artist, user_metadata, app_client_id): """Test send password reset.""" db_mock = MagicMock() db_mock.change_password.side_effect = ['Sent1', 'Sent2'] mocker.patch('permissions.models.auth0.Database', return_value=db_mock) mocker.patch('permissions.config.AUTH0_SETTINGS_APP_CLIENT_ID', 'AUTH0_SETTINGS_APP_CLIENT_ID') mocker.patch('permissions.config.AUTH0_INSIGHT_APP_CLIENT_ID', 'AUTH0_INSIGHT_APP_CLIENT_ID') mocker.patch('permissions.config.AUTH0_MONEYHUB_APP_CLIENT_ID', 'AUTH0_MONEYHUB_APP_CLIENT_ID') actual = auth0.bulk_send_password_reset( ['one@theorchard.io', 'two@foo.com'], is_artist, user_metadata ) assert actual assert actual.message == ['Sent1', 'Sent2'] assert db_mock.change_password.call_count == 2 db_mock.change_password.assert_called_with(app_client_id, 'two@foo.com', 'art-relations') def test_update_user_metadata(mocker): """Test update_user_metadata.""" mgmt_mock = MagicMock() mocker.patch('permissions.models.auth0.get_auth0_management_handle', return_value=mgmt_mock) update_data = {'vend_contac_id': 'value'} data = {'id1': update_data} auth0.update_user_metadata(data) mgmt_mock.users.update.asser_called_with('auth0|id1', update_data) @pytest.mark.freeze_time('2020-05-21') @pytest.mark.parametrize( ('update_result', 'update_data', 'active', 'expected'), [ [ # activate [{'updated': 'user'}], { 'connection': 'art-relations', 'blocked': False, 'user_metadata': {'blocked_at': None}, }, True, response.Response({'updated': 'user'}), ], [ # deactivate [{'updated': 'user'}], { 'connection': 'art-relations', 'blocked': True, 'user_metadata': {'blocked_at': '2020-05-21T00:00:00'}, }, False, response.Response({'updated': 'user'}), ], [ # deactivate but got user not found. Auth0Error(404, 'not-found', 'user not found'), { 'connection': 'art-relations', 'blocked': True, 'user_metadata': {'blocked_at': '2020-05-21T00:00:00'}, }, False, response.Response('User not found in auth0 but can continue.'), ], [ # deactivate but some other error Auth0Error(500, 'some-error', 'some error'), { 'connection': 'art-relations', 'blocked': True, 'user_metadata': {'blocked_at': '2020-05-21T00:00:00'}, }, False, response.create_error_response('update_auth0_user_failed', 'For foo123342: some error'), ], ], ) def test_activate_deactivate_user(mocker, update_result, update_data, active, expected): """Test activate_deactivate_user.""" auth0_id = 'foo123342' mgmt_mock = MagicMock() mgmt_mock.users.update.side_effect = update_result mocker.patch('permissions.models.auth0.get_auth0_management_handle', return_value=mgmt_mock) actual = auth0.activate_deactivate_user(auth0_id, active) assert actual.status == expected.status assert actual.message == expected.message assert mgmt_mock.users.update.call_count == 1 mgmt_mock.users.update.asser_called_with('auth0|auth0_id', update_data) @pytest.mark.parametrize( ('first_name', 'last_name', 'auth0_call'), [ [ None, None, { 'password': 'password123', 'connection': 'art-relations', 'name': 'User 1', 'email': 'one@theorchard.io', 'verify_email': False, 'email_verified': True, 'user_metadata': {}, 'app_metadata': {}, }, ], [ 'Jon', None, { 'password': 'password123', 'connection': 'art-relations', 'name': 'User 1', 'email': 'one@theorchard.io', 'verify_email': False, 'email_verified': True, 'user_metadata': {'first_name': 'Jon'}, 'app_metadata': {}, }, ], [ 'Jon', 'Snow', { 'password': 'password123', 'connection': 'art-relations', 'name': 'User 1', 'email': 'one@theorchard.io', 'verify_email': False, 'email_verified': True, 'user_metadata': {'first_name': 'Jon', 'last_name': 'Snow'}, 'app_metadata': {}, }, ], ], ) def test_create_user(mocker, first_name, last_name, auth0_call): """Test create_user.""" email = 'one@theorchard.io' name = 'User 1' actual = { 'name': 'new name', 'user_metadata': {'orchardIdentityId': 'abcd-234', 'defaultBrand': 'theorchard'}, 'email': email, 'user_id': 'auth0|id', } mgmt_mock = MagicMock() expected = { 'name': 'new name', 'id': 'abcd-234', 'email': email, 'auth0_user_id': 'id', 'first_name': first_name, 'last_name': last_name, 'user_types': None, 'default_brand': 'theorchard', } mgmt_mock.users.create.return_value = actual mocker.patch('permissions.models.auth0.get_auth0_management_handle', return_value=mgmt_mock) db_mock = MagicMock() mocker.patch('permissions.models.auth0.Database', return_value=db_mock) mocker.patch('permissions.models.auth0.gen_password', return_value='password123') actual = auth0.create_user(email, name, True, first_name, last_name) assert actual assert actual.message == expected mgmt_mock.users.create.assert_called_once_with(auth0_call) def test_create_user_with_optional_values(mocker): """Test create_user.""" email = 'one@theorchard.io' name = 'User 2' first_name = 'Test' last_name = 'User 2' user_types = ['artist'] app_metadata = {'foo': 'bar'} user_metadata = {'bar': 'baz'} mock_create = { 'name': 'new name', 'user_metadata': {'orchardIdentityId': 'abcd-234'}, 'email': email, 'user_id': 'auth0|id', } mgmt_mock = MagicMock() expected = { 'name': 'new name', 'id': 'abcd-234', 'email': email, 'auth0_user_id': 'id', 'first_name': first_name, 'last_name': last_name, 'user_types': user_types, 'default_brand': None, } mgmt_mock.users.create.return_value = mock_create mocker.patch('permissions.models.auth0.get_auth0_management_handle', return_value=mgmt_mock) db_mock = MagicMock() mocker.patch('permissions.models.auth0.Database', return_value=db_mock) actual = auth0.create_user( email, name, True, first_name, last_name, user_types, user_metadata, app_metadata ) assert actual assert actual.message == expected mgmt_mock.users.create.assert_called_once() call_args = mgmt_mock.users.create.call_args assert call_args[0][0]['connection'] == 'art-relations' assert call_args[0][0]['name'] == name assert call_args[0][0]['email'] == email assert call_args[0][0]['verify_email'] is False assert call_args[0][0]['email_verified'] is True assert call_args[0][0]['user_metadata'] == { 'bar': 'baz', 'first_name': first_name, 'last_name': last_name, 'user_types': user_types, } assert call_args[0][0]['app_metadata'] == app_metadata