|
| 1 | +import base64 |
| 2 | +import json |
| 3 | +import os |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +import requests |
| 7 | +from dotenv import load_dotenv |
| 8 | +from mdps_ds_lib.lib.aws.aws_s3 import AwsS3 |
| 9 | +from mdps_ds_lib.lib.cognito_login.cognito_login import CognitoLogin |
| 10 | + |
| 11 | + |
| 12 | +class TestGranulesDeletion(TestCase): |
| 13 | + def setUp(self) -> None: |
| 14 | + super().setUp() |
| 15 | + load_dotenv() |
| 16 | + self._url_prefix = f'{os.environ.get("UNITY_URL")}/{os.environ.get("UNITY_STAGE", "sbx-uds-dapa")}' |
| 17 | + self.cognito_login = CognitoLogin() \ |
| 18 | + .with_client_id(os.environ.get('CLIENT_ID', '')) \ |
| 19 | + .with_cognito_url(os.environ.get('COGNITO_URL', '')) \ |
| 20 | + .with_verify_ssl(False) \ |
| 21 | + .start(base64.standard_b64decode(os.environ.get('USERNAME')).decode(), |
| 22 | + base64.standard_b64decode(os.environ.get('PASSWORD')).decode()) |
| 23 | + self.bearer_token = self.cognito_login.token |
| 24 | + self.stage = os.environ.get("UNITY_URL").split('/')[-1] |
| 25 | + self.uds_url = f'{os.environ.get("UNITY_URL")}/{os.environ.get("UNITY_STAGE", "sbx-uds-dapa")}/' |
| 26 | + self.custom_metadata_body = { |
| 27 | + 'tag': {'type': 'keyword'}, |
| 28 | + 'c_data1': {'type': 'long'}, |
| 29 | + 'c_data2': {'type': 'boolean'}, |
| 30 | + 'c_data3': {'type': 'keyword'}, |
| 31 | + } |
| 32 | + |
| 33 | + self.tenant = 'UDS_LOCAL_TEST_3' # 'uds_local_test' # 'uds_sandbox' |
| 34 | + self.tenant_venue = 'DEV' # 'DEV1' # 'dev' |
| 35 | + self.collection_name = 'CCC-04' # 'uds_collection' # 'sbx_collection' |
| 36 | + self.collection_version = '08'.replace('.', '') # '2402011200' |
| 37 | + return |
| 38 | + |
| 39 | + def test_01_setup_permissions(self): |
| 40 | + collection_url = f'{self._url_prefix}/admin/auth' |
| 41 | + admin_add_body = { |
| 42 | + "actions": ["READ", "CREATE", "DELETE"], |
| 43 | + "resources": [f"URN:NASA:UNITY:{self.tenant}:{self.tenant_venue}:.*"], |
| 44 | + "tenant": self.tenant, |
| 45 | + "venue": self.tenant_venue, |
| 46 | + "group_name": "Unity_Viewer" |
| 47 | + } |
| 48 | + s = requests.session() |
| 49 | + s.trust_env = False |
| 50 | + response = s.put(url=collection_url, headers={ |
| 51 | + 'Authorization': f'Bearer {self.cognito_login.token}', |
| 52 | + 'Content-Type': 'application/json', |
| 53 | + }, verify=False, data=json.dumps(admin_add_body)) |
| 54 | + self.assertEqual(response.status_code, 200, f'wrong status code: {response.text}') |
| 55 | + response_json = response.content.decode() |
| 56 | + print(response_json) |
| 57 | + return |
| 58 | + |
| 59 | + def test_delete_all(self): |
| 60 | + collection_id = f'URN:NASA:UNITY:{self.tenant}:{self.tenant_venue}:{self.collection_name}___001' |
| 61 | + post_url = f'{self.uds_url}collections/{collection_id}/items/' # MCP Dev |
| 62 | + headers = { |
| 63 | + 'Authorization': f'Bearer {self.bearer_token}', |
| 64 | + } |
| 65 | + print(post_url) |
| 66 | + query_result = requests.get(url=post_url, |
| 67 | + headers=headers, |
| 68 | + ) |
| 69 | + self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}') |
| 70 | + response_json = json.loads(query_result.text) |
| 71 | + print(json.dumps(response_json, indent=4)) |
| 72 | + self.assertTrue(len(response_json['features']) > 0, f'empty collection :(') |
| 73 | + deleting_granule_id = response_json['features'][0]['id'] |
| 74 | + |
| 75 | + asset_urls = [v['href'] for k, v in response_json['features'][0]['assets'].items()] |
| 76 | + print(asset_urls) |
| 77 | + post_url = f'{self.uds_url}collections/{collection_id}/items/{deleting_granule_id}/' # MCP Dev |
| 78 | + print(post_url) |
| 79 | + query_result = requests.delete(url=post_url, |
| 80 | + headers=headers, |
| 81 | + ) |
| 82 | + self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}') |
| 83 | + response_json = json.loads(query_result.text) |
| 84 | + print(json.dumps(response_json, indent=4)) |
| 85 | + |
| 86 | + post_url = f'{self.uds_url}collections/{collection_id}/items/' # MCP Dev |
| 87 | + query_result = requests.get(url=post_url, headers=headers,) |
| 88 | + self.assertEqual(query_result.status_code, 200, f'wrong status code. {query_result.text}') |
| 89 | + response_json = json.loads(query_result.text) |
| 90 | + print(json.dumps(response_json, indent=4)) |
| 91 | + |
| 92 | + s3 = AwsS3() |
| 93 | + for each_url in asset_urls: |
| 94 | + self.assertFalse(s3.set_s3_url(each_url).exists(s3.target_bucket, s3.target_key), f'file still exists: {each_url}') |
| 95 | + return |
0 commit comments