"""Tests for DELETE /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_delete_resource_from_profile(bearer_token_user_with_vendor_star): """Test deleting a relationship with a resource from a 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 delete relationship resource_id = 7123 # Test Label's id res = requests.delete( f'{config.QA_BASE_URL}/ows/profile/{constants.INSIGHTSPROFILE}/{insights_profile_id}/' f'has-access-to/resource/{constants.VENDOR_RESOURCE_TYPE}/{resource_id}', headers=headers, ) assert res.status_code == 204 # Check that the relationship is really gone res = requests.get( f'{config.QA_BASE_URL}/admin/profile-type/{constants.INSIGHTSPROFILE}' f'/profile/{insights_profile_id}/resource/all', headers=headers, ) assert len(res.json()['items']) == 0 @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 [465771, 700600500], ], ) def test_delete_resource_from_profile_fail( profile_id, resource_id, bearer_token_user_with_vendor_star ): """Test deleting a resource from a profile when one of those doesn't exist.""" res = requests.delete( f'{config.QA_BASE_URL}/ows/profile/{constants.INSIGHTSPROFILE}/{profile_id}/' f'has-access-to/resource/{constants.VENDOR_RESOURCE_TYPE}/{resource_id}', 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 delete relationship. ' 'Please check if relationship between both nodes exist.' ) assert res.json()['message'] == error_message