"""Integration tests for auth0 CRUD operations.""" import datetime from random import randrange import pytest import requests from tests.integration.consts import api # module level variabe to keep track of id for diff api calls. test_auth0_id = None random_value = randrange(1000) # TODO: Remove this file entirely. Endpoint is unused. pytestmark = pytest.mark.skip(reason='Deprecated: Auth0 create user endpoint is unused and broken.') @pytest.fixture(scope='module') def auth0_email_fixture(): """Make email fixture.""" return f'integration-test{random_value}@orchard.com' @pytest.mark.dependency() def test_get_auth0_user(auth0_email_fixture): """GET an auth0 user.""" response = requests.get(api.GET_AUTH0_USER_BY_EMAIL.format(email=auth0_email_fixture)) assert response.status_code in [200, 404] if response.status_code == 200: actual = response.json() assert 'result' in actual if len(actual['result']) > 0: # set the module level var. global test_auth0_id test_auth0_id = actual['result'].pop() assert test_auth0_id @pytest.mark.dependency(depends=['test_get_auth0_user']) def test_delete_existing_auth0_user(): """Cleanup: Delete existing auth0 user, in case prev test didn't do it.""" global test_auth0_id if not test_auth0_id: assert True # it is ok if the auth0 user does not exist at first. return response = requests.delete(api.DELETE_AUTH0.format(auth0_id=test_auth0_id)) assert response.status_code == 204 @pytest.mark.dependency(depends=['test_delete_existing_auth0_user']) def test_create_auth0_identity(auth0_email_fixture): """Create Identity in Auth0.""" data = { 'email': auth0_email_fixture, # required 'user_metadata': {}, 'app_metadata': {}, 'email_verified': False, 'verify_email': False, 'reset_password': False, } response = requests.post(api.CREATE_AUTH0_USER, json=data) assert response.status_code in [200, 201] actual = response.json() assert actual.get('email') == auth0_email_fixture assert 'auth0|' in actual.get('user_id') actual_created_date = datetime.datetime.strptime( actual.get('created_at'), '%Y-%m-%dT%H:%M:%S.%fZ' ).date() assert actual_created_date == datetime.date.today() global test_auth0_id test_auth0_id = actual.get('user_id') assert test_auth0_id @pytest.mark.dependency(depends=['test_create_auth0_identity']) def test_edit_auth0_identity(auth0_email_fixture): """Edit Identity data in Auth0.""" global test_auth0_id if not test_auth0_id: raise AssertionError() # edit needs an existing user. data = { 'email': auth0_email_fixture, # required 'email_verified': True, } response = requests.patch(api.EDIT_AUTH0.format(auth0_id=test_auth0_id), json=data) actual = response.json() assert response.status_code in [200, 201] assert actual.get('email') == auth0_email_fixture assert actual.get('user_id') == test_auth0_id assert actual.get('email_verified') @pytest.mark.dependency(depends=['test_edit_auth0_identity']) def test_edit_auth0_identity_with_forbidden_email(auth0_email_fixture): """Edit Identity data in Auth0 with a forbidden email domain.""" global test_auth0_id if not test_auth0_id: raise AssertionError() # edit needs an existing user. forbidden_email = 'test@theorchard.com' data = { 'email': forbidden_email, # required 'email_verified': True, } response = requests.patch(api.EDIT_AUTH0.format(auth0_id=test_auth0_id), json=data) assert response.status_code == 403 @pytest.mark.dependency(depends=['test_edit_auth0_identity']) def test_delete_new_auth0_user(): """Delete new auth0 user.""" global test_auth0_id if not test_auth0_id: raise AssertionError() # edit should have created a user. return response = requests.delete(api.DELETE_AUTH0.format(auth0_id=test_auth0_id)) assert response.status_code == 204 identity_id = test_auth0_id.replace('auth0|', '') # cleanup requests.delete(api.DELETE_IDENTITY.format(identity_id=identity_id))