"""Fixtures for integration tests using jwtauth[testing].""" import random import uuid from dataclasses import dataclass from typing import Callable import pytest import requests from jwtauth.testing import JwtAuthSecretsManager, SecretLookupInfo from permissions.constants import application, constants, parent_companies from tests.integration import config # Register the jwtauth pytest plugin for JWT token generation pytest_plugins = ['jwtauth.testing.pytest_plugin'] # Service name for AWS Secrets Manager lookups SERVICE_NAME = 'ows-permissions-integration-test' # Secret names in AWS Secrets Manager for user credentials # Format: {"username": "...", "password": "..."} PDP_TEST_USER_CREDENTIALS = 'PDP_TEST_USER_CREDENTIALS' OWS_ACCOUNT_TEST_USER_CREDENTIALS = 'OWS_ACCOUNT_TEST_USER_CREDENTIALS' OWS_PERMISSIONS_TEST_USER_CREDENTIALS = 'OWS_PERMISSIONS_TEST_USER_CREDENTIALS' OWS_PERMISSIONS_VENDOR_STAR_USER_CREDENTIALS = 'OWS_PERMISSIONS_VENDOR_STAR_USER_CREDENTIALS' OWS_PERMISSIONS_NO_SETTINGS_USER_CREDENTIALS = 'OWS_PERMISSIONS_NO_SETTINGS_USER_CREDENTIALS' SEAT_ADMIN_CREDENTIALS = 'INTEGRATION_TEST_SEAT_USER_CREDENTIALS' # Secret names in AWS Secrets Manager for Auth0 app credentials # Format: {"client_id": "...", "client_secret": "..."} SETTINGS_AUTH0_APP_CREDENTIALS = 'SETTINGS_AUTH0_APP_CREDENTIALS' SEAT_AUTH0_APP_CREDENTIALS = 'SEAT_AUTH0_APP_CREDENTIALS' # Environment QA_ENVIRONMENT = 'qa' @dataclass class ProfileInfo: """Holds profile info.""" identity_id: str profile_id: int profile_type: str # ===================================== # JWT Token Fixtures (using jwtauth) # ===================================== @pytest.fixture(scope='session') def bearer_token( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """JWT for the PDP Test User.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=PDP_TEST_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_user_without_settings( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """JWT for the ows-permissions user without settings profile.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=OWS_PERMISSIONS_NO_SETTINGS_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_user_with_vendor_star( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """JWT for the ows-account user with settings profile and vendor star access.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=OWS_ACCOUNT_TEST_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_user_without_vendor_star( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """JWT for the ows-permissions user with settings profile and no vendor star access.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=OWS_PERMISSIONS_TEST_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SETTINGS_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) @pytest.fixture(scope='session') def bearer_token_user_vendor_star_not_superadmin( generate_bearer_token: Callable, jwtauth_secrets_manager: JwtAuthSecretsManager, ) -> str: """JWT for the ows-permissions user with settings profile and vendor star access.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=OWS_PERMISSIONS_VENDOR_STAR_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=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: """JWT for the seat admin user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SEAT_ADMIN_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment=QA_ENVIRONMENT, service_name=SERVICE_NAME, secret_name=SEAT_AUTH0_APP_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, ) # ===================================== # Profile Info Fixtures # ===================================== @pytest.fixture(scope='session') def pdp_test_user_profile() -> ProfileInfo: """SettingsProfile for PDP test user.""" return ProfileInfo( identity_id='27d2fd9c-a3c5-4183-8f0f-e552962cf855', profile_id=182392550, profile_type='SettingsProfile', ) @pytest.fixture(scope='session') def ows_account_test_user_settings_profile() -> ProfileInfo: """SettingsProfile for ows-account-integration-test with Vendor * access.""" return ProfileInfo( identity_id='40210960-c0e6-47c7-bb49-71f2373260dd', profile_id=465788, profile_type='SettingsProfile', ) @pytest.fixture(scope='session') def ows_permissions_test_user_settings_profile() -> ProfileInfo: """SettingsProfile for ows-permissions-integration-test without Vendor * access.""" return ProfileInfo( identity_id='d0e55c65-9dae-4d91-b409-677854806272', profile_id=467615, profile_type='SettingsProfile', ) @pytest.fixture(scope='session') def ows_permissions_vendor_star_user_settings_profile() -> ProfileInfo: """SettingsProfile for ows-permissions-test-vendor-star (vendor star but not superadmin).""" return ProfileInfo( identity_id='e1ed2677-c057-4275-ac45-37271eecbda7', profile_id=99000022, profile_type='SettingsProfile', ) @pytest.fixture(scope='session') def ows_permissions_no_settings_user_profile() -> ProfileInfo: """Profile info for ows-permissions-integration-test-no-settings (no settings profile).""" return ProfileInfo( identity_id='3b805209-1e1a-4d3d-afdb-cb8c079e2c3c', profile_id=0, # No settings profile profile_type='', ) # ===================================== # Test Data Fixtures # ===================================== @pytest.fixture() def create_identity_json() -> dict: """Return a valid body for the POST /v2/identities endpoint.""" return { 'first_name': 'Foo', 'last_name': 'Bar', 'email': 'test_idt+' + str(random.randint(24, 4142355)) + '@sonymusic-pde.com', 'roles_to_attach': [ 'COLLABORATORS_BASE_ROLE', 'INSIGHTS_BASE_ROLE', 'CUSTOMER_ACCOUNTING_BASE_ROLE', 'WORKSTATION_ANALYTICS_BASE_ROLE', 'WORKSTATION_CATALOG_ROLE', 'WORKSTATION_MARKETING_ROLE', 'WORKSTATION_ADVERTISING_ROLE', ], 'tenant': {'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6'}, } @pytest.fixture() def create_identity_with_subaccount() -> dict: """Return a valid body for the POST /v2/identities endpoint.""" return { 'first_name': 'Foo', 'last_name': 'Bar', 'email': 'test_idt+' + str(random.randint(24, 4142355)) + '@sonymusic-pde.com', 'roles_to_attach': [ 'INSIGHTS_BASE_ROLE', 'CUSTOMER_ACCOUNTING_BASE_ROLE', 'WORKSTATION_CATALOG_ROLE', ], 'tenant': { 'tenant_type': 'subaccount', 'tenant_uuid': '1fab3fe8-c9a6-488a-9a96-959734a93997', }, } @pytest.fixture() def create_identity_json_no_workstation() -> dict: """Return a valid body for the POST v2/identities endpoint.""" return { 'first_name': 'Foo', 'last_name': 'Bar', 'email': 'test_idt+' + str(random.randint(24, 4142355)) + '@sonymusic-pde.com', 'roles_to_attach': [ 'COLLABORATORS_BASE_ROLE', 'INSIGHTS_BASE_ROLE', ], 'tenant': {'tenant_type': 'account', 'tenant_uuid': 'dffedd4d-b88d-444d-a9eb-6ce89aa4d2f6'}, } EMPLOYEE_CREATE_BODY = { 'first_name': '🐶', 'last_name': '🦴', 'email': 'nugget@theorchard.com', 'brand': constants.THEORCHARD_BRAND, 'tenant': { 'tenant_type': constants.PARENT_COMPANY_TENANT_TYPE, 'tenant_uuid': parent_companies.ORCHARD_PARENT_COMPANY_UUID, }, 'roles_to_attach': [application.SETTINGS_BASE_ROLE, application.INSIGHTS_BASE_ROLE], } EMPLOYEE_CREATE_BODY_ACCOUNT_TENANT = { 'first_name': '🐶', 'last_name': '🦴', 'email': 'nugget@theorchard.com', 'brand': constants.THEORCHARD_BRAND, 'tenant': { 'tenant_type': constants.ACCOUNT_TENANT_TYPE, # Platform Test Vendor 'tenant_uuid': '2e79b9b8-29ed-44e1-832f-2aa3b47f13c8', }, 'roles_to_attach': [application.FANSIFTER_BASE_ROLE, application.SONGWHIP_READ_ROLE], } @pytest.fixture() def valid_create_body() -> dict: """Return a valid request body for creating an employee identity.""" randomness = str(uuid.uuid4()).replace('-', '')[:12] return { **EMPLOYEE_CREATE_BODY, 'email': f'ows-permissions-integration-test-employee_{randomness}@theorchard.com', } @pytest.fixture() def new_employee_id(seat_admin_jwt: str, valid_create_body: dict) -> str: """Create a new employee identity and return the id.""" res = requests.post( f'{config.QA_BASE_URL}/internal/v2/identities', json=valid_create_body, headers={'Authorization': f'Bearer {seat_admin_jwt}', 'Content-Type': 'application/json'}, ) return res.json()['id']