"""Test POST /ows/profile///has-access-to/resource//.""" # noqa: E501 import pytest import requests from permissions.constants import constants from tests.integration import config, utils def test_add_resource_to_insights_profile(bearer_token_user_with_vendor_star): """Test adding a resource to an Insights profile.""" headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {bearer_token_user_with_vendor_star}', 'Orchard-Profile-Type': constants.SETTINGSPROFILE, 'Orchard-Profile-Id': utils.OWS_ACCOUNT_TEST_USER_SETTINGS_PROFILE_ID, 'Orchard-Identity-Id': utils.OWS_ACCOUNT_TEST_USER_ID, } # Create user first create_data = { 'identity': utils.create_identity_data(), # Test Label 'resource_access': [ { 'resource_type': constants.VENDOR_RESOURCE_TYPE, 'uuid': '573d0372-7f2f-48a6-8deb-c9a6558f9549', 'roles': ['analytics'], } ], 'create_auth0_user': False, } res = requests.post( f'{config.QA_BASE_URL}/identity/add-resources-profiles', json=create_data, headers=headers, ) insights_profile_id = next( profile['profile_id'] for profile in res.json()['profiles_affected'] if profile['profile_type'] == constants.INSIGHTSPROFILE ) # Now add the resource (Platform Test Subaccount) resource_id = 949090 res = requests.post( f'{config.QA_BASE_URL}/ows/profile/{constants.INSIGHTSPROFILE}/{insights_profile_id}/' f'has-access-to/resource/{constants.SUBACCOUNT_RESOURCE_TYPE}/{resource_id}', json={'roles': ['analytics']}, headers=headers, ) assert res.status_code == 201 assert res.json()['resource_type'] == constants.SUBACCOUNT_RESOURCE_TYPE assert res.json()['resource_id'] == resource_id assert res.json()['roles'] == ['analytics'] # Now check that the relationship is really there res = requests.get( f'{config.QA_BASE_URL}/admin/profile-type/{constants.INSIGHTSPROFILE}' f'/profile/{insights_profile_id}/resource/all', headers=headers, ) assert next( item for item in res.json()['items'] if item.get('id') == resource_id and item['type'] == constants.SUBACCOUNT_RESOURCE_TYPE ) @pytest.mark.parametrize( ['profile_id', 'resource_id'], [ # Just some random number that hopefully won't be a profile id, then id of Test Label [500600700, 7123], # Insights profile of ows-permissions-integration-test-no-settings@sonymusic-pde.com, # then some random number that hopefully won't be a vendor id [465771, 700600500], ], ) def test_add_resource_to_profile_fail(profile_id, resource_id, bearer_token_user_with_vendor_star): """Test adding a resource to a profile when one of those doesn't exist.""" res = requests.post( f'{config.QA_BASE_URL}/ows/profile/{constants.INSIGHTSPROFILE}/{profile_id}/' f'has-access-to/resource/{constants.VENDOR_RESOURCE_TYPE}/{resource_id}', json={'roles': ['analytics']}, headers={ 'Content-Type': 'application/json', 'Authorization': f'Bearer {bearer_token_user_with_vendor_star}', 'Orchard-Profile-Type': constants.SETTINGSPROFILE, 'Orchard-Profile-Id': utils.OWS_ACCOUNT_TEST_USER_SETTINGS_PROFILE_ID, 'Orchard-Identity-Id': utils.OWS_ACCOUNT_TEST_USER_ID, }, ) assert res.status_code == 500 error_message = 'Failed to create relationship. Please check if both the nodes exist.' assert res.json()['message'] == error_message