"""Fixtures, etc.""" import time from typing import Callable from jwtauth.testing import JwtAuthSecretsManager, SecretLookupInfo import pytest import requests from secrets_manager.python_ext import PythonSecretsManager from tests.integration import config, constants pytest_plugins = ['jwtauth.testing.pytest_plugin'] @pytest.fixture(scope='session') def grass_headers(): """Return grass headers for tester@settingstesting.com.""" return create_grass_headers( identity_id=constants.GRASS_HEADERS_IDENTITY_ID, identity_uuid=constants.GRASS_HEADERS_IDENTITY_ID, profile_type='SettingsProfile', profile_id='71799', ) @pytest.fixture(scope='session') def settings_admin_with_ff_grass_headers(): """Return grass headers for pde-service-user@sonymusic-pde.com. This user has SettingsProfile 508401 and the edit_super_admins feature flag enabled, so is_settings_admin returns True and all identities (including employees) are visible. """ return create_grass_headers( identity_id=constants.SETTINGS_ADMIN_WITH_FF_IDENTITY_ID, identity_uuid=constants.SETTINGS_ADMIN_WITH_FF_IDENTITY_ID, profile_type='SettingsProfile', profile_id='508401', ) @pytest.fixture(scope='session') def oa_profile_grass_headers(): """Return grass headers for cypress_qa@theorchard.io with OA Profile.""" return create_grass_headers( identity_id='33fafa64-b64f-4390-a98e-137c02c2c1ae', identity_uuid='33fafa64-b64f-4390-a98e-137c02c2c1ae', profile_type='OrchAdminProfile', profile_id='100002', ) @pytest.fixture(scope='session') def suite_admin_profile_grass_headers(): """Return grass headers for suite admin profile.""" return create_grass_headers( identity_id='82a57fc8-0ab8-4a75-855c-d13f0b3e5765', identity_uuid='82a57fc8-0ab8-4a75-855c-d13f0b3e5765', profile_type='SettingsProfile', profile_id='677544', ) def create_grass_headers(identity_id, identity_uuid, profile_type, profile_id): """Return grass headers for the given id and profile values.""" return { 'Orchard-Identity-Id': identity_id, 'Orchard-Identity-UUID': identity_uuid, 'Orchard-Profile-Type': profile_type, 'Orchard-Profile-Id': profile_id, } @pytest.fixture def identity_payload(): """Return a valid payload for creating an identity.""" now = str(time.time()).replace('.', '_') return { 'name': 'ows-users identity integration test', 'email': f'ows-users-identity-integration+{now}@theorchard.com', 'identity_id': f'identity_0000000{now}', 'default_brand': 'theorchard', 'localization': 'en', 'number_format': 'us', 'active': 'Y', 'user_types': [], } @pytest.fixture def new_identity(grass_headers, identity_payload): """Create an identity for use in a test.""" res = requests.post( f'{config.QA_BASE_URL}/users/identity', headers=grass_headers, json=identity_payload, ) if res.status_code != 200: pytest.fail(f'Failed to create identity: {res.status_code} - {res.text}', pytrace=False) identity_data = res.json() yield identity_data requests.delete( f"{config.QA_BASE_URL}/users/identity/{identity_data['id']}", headers=grass_headers ) @pytest.fixture def new_profile(grass_headers, new_identity): """Create a profile (and identity) for use in a test.""" profile_payload = { 'profile_name': 'Test InsightsProfile crud', 'profile_type': 'InsightsProfile', 'roles': ['catalog'], } return create_profile_for_identity_id( headers=grass_headers, profile_payload=profile_payload, identity_id=new_identity['id'], ) def create_profile_for_identity_id(headers, profile_payload, identity_id): """Create a profile for the given identity.""" res = requests.post( f'{config.QA_BASE_URL}/profile/identity/{identity_id}', headers=headers, json=profile_payload, ) return res.json() @pytest.fixture def authorized_user_jwt( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """Return a JWT for the ows-users integration test user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.OWS_USERS_INTEGRATION_TEST_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def seat_admin_jwt( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """Return a JWT for the seat admin integration test user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.SEAT_ADMIN_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def auth0_action_test_machine_jwt() -> str: """Return a JWT for the auth0-action-test-machine.""" secrets_manager_client = PythonSecretsManager( environment='qa', service_name=config.AUTH0_ACTION_TEST_MACHINE_SERVICE_NAME ) secret = secrets_manager_client.get_cred(constants.M2M_JWT_ACCESS_TOKEN) return secret.get('token')