From 965451424ba65ab4735b57c5995c7c0976b2d6ef Mon Sep 17 00:00:00 2001 From: rmondal00 Date: Thu, 13 Jul 2023 14:25:44 +0200 Subject: [PATCH] LITE-28062 Add app_id as client object __init__ --- connect_ext_ppr/client/client.py | 4 + tests/client/test_client.py | 128 +++---------------------- tests/client/test_collection.py | 155 +++---------------------------- tests/client/test_resource.py | 68 ++------------ tests/client/test_service.py | 73 ++------------- tests/conftest.py | 21 +++++ 6 files changed, 68 insertions(+), 381 deletions(-) diff --git a/connect_ext_ppr/client/client.py b/connect_ext_ppr/client/client.py index 9eb471e..5562f5e 100644 --- a/connect_ext_ppr/client/client.py +++ b/connect_ext_ppr/client/client.py @@ -24,6 +24,7 @@ def __init__( endpoint: str, oauth_key: str, oauth_secret: str, + app_id: str, verify_certificate: bool = True, default_headers: Dict[str, str] = None, ): @@ -32,6 +33,7 @@ def __init__( self.endpoint = endpoint self.auth_key = oauth_key self.auth_secret = oauth_secret + self.app_id = app_id self.verify = verify_certificate self.default_headers = default_headers self.path = self.endpoint @@ -41,6 +43,8 @@ def __init__( 'User-Agent': 'Connect-CBC-Client', } + self.default_headers['aps-resource-id'] = self.app_id + self.auth = OAuth1( client_key=oauth_key, client_secret=oauth_secret, diff --git a/tests/client/test_client.py b/tests/client/test_client.py index eff9804..6fe9f5e 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -11,17 +11,10 @@ @responses.activate def test_client_get( + cbc_client, cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, flat_catalog_type_object, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - object_id = flat_catalog_type_object['aps']['id'] responses.add( @@ -36,111 +29,47 @@ def test_client_get( @responses.activate -def test_client_get_empty_identifier( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_get_empty_identifier(cbc_client): with pytest.raises(ValueError): cbc_client.get('') @responses.activate -def test_client_get_invalid_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_get_invalid_type(cbc_client): with pytest.raises(TypeError): cbc_client.get(100.05) def test_client_collection_with_underscore( + cbc_client, cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - collection = cbc_client.flat_catalog assert collection.path == f'{cbc_endpoint}/flat-catalog' def test_client_collection_without_underscore( + cbc_client, cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - collection = cbc_client.flat assert collection.path == f'{cbc_endpoint}/flat' -def test_client_resource( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_resource(cbc_client, cbc_endpoint): resource = cbc_client['identifier'] assert resource.path == f'{cbc_endpoint}/aps/2/resources/identifier' -def test_client_resource_invalid_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_resource_invalid_type(cbc_client): with pytest.raises(TypeError): cbc_client[100.05] -def test_client_resource_empty_value( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_resource_empty_value(cbc_client): with pytest.raises(ValueError): cbc_client[''] @@ -149,6 +78,7 @@ def test_client_with_default_headers( cbc_endpoint, cbc_oauth_key, cbc_oauth_secret, + cbc_app_id, ): headers = {'Custom': 'Value'} @@ -157,53 +87,25 @@ def test_client_with_default_headers( oauth_key=cbc_oauth_key, oauth_secret=cbc_oauth_secret, default_headers=headers, + app_id=cbc_app_id, ) - assert cbc_client.default_headers == headers + headers['aps-resource-id'] = cbc_app_id + assert cbc_client.default_headers == headers -def test_collection_service_discovery_empty_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) +def test_collection_service_discovery_empty_type(cbc_client): with pytest.raises(ValueError): cbc_client('') -def test_collection_service_discovery_invalid_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_service_discovery_invalid_type(cbc_client): with pytest.raises(TypeError): cbc_client(100.05) @patch.object(Session, 'send', side_effect=Exception('Mock error!')) -def test_client_get_connection_error( - mock_send, - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_client_get_connection_error(mock_send, cbc_client): with pytest.raises(ClientError): cbc_client.get('identifier') diff --git a/tests/client/test_collection.py b/tests/client/test_collection.py index 0aad4c5..4854b36 100644 --- a/tests/client/test_collection.py +++ b/tests/client/test_collection.py @@ -4,51 +4,19 @@ import pytest import responses -from connect_ext_ppr.client import CBCClient - - -def test_collection( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) +def test_collection(cbc_endpoint, cbc_client): collection = cbc_client.test_collection assert collection.path == f'{cbc_endpoint}/test-collection' -def test_collection_blank_collection_value( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_blank_collection_value(cbc_client): with pytest.raises(ValueError): cbc_client.collection('') -def test_collection_wrong_collection_value_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_wrong_collection_value_type(cbc_client): with pytest.raises(TypeError): cbc_client.collection(1) @@ -56,16 +24,9 @@ def test_collection_wrong_collection_value_type( @responses.activate def test_collection_get( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_objects, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - responses.add( method='GET', url=f'{cbc_endpoint}/flat-catalog', @@ -77,100 +38,34 @@ def test_collection_get( TestCase().assertListEqual(objs, flat_catalog_type_objects) -@responses.activate -def test_sub_collection( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_sub_collection(cbc_endpoint, cbc_client): sub_collection = cbc_client.flat_catalog.wizard assert sub_collection.path == f'{cbc_endpoint}/flat-catalog/wizard' -@responses.activate -def test_sub_collection_blank( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_sub_collection_blank(cbc_endpoint, cbc_client): with pytest.raises(ValueError): cbc_client.flat_catalog.collection('') -@responses.activate -def test_sub_collection_wrong_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_sub_collection_wrong_type(cbc_client): with pytest.raises(TypeError): cbc_client.flat_catalog.collection(1) -@responses.activate -def test_collection_resource( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_resource(cbc_endpoint, cbc_client): sub_collection = cbc_client.flat_catalog.sub_collection.wizard['identifier'] assert sub_collection.path == f'{cbc_endpoint}/flat-catalog/sub-collection/wizard/identifier' -@responses.activate -def test_collection_resource_blank( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_resource_blank(cbc_client): with pytest.raises(ValueError): cbc_client.flat_catalog.wizard[''] -@responses.activate -def test_collection_resource_wrong_type( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_resource_wrong_type(cbc_client): with pytest.raises(TypeError): cbc_client.flat_catalog.wizard[67567.87] @@ -178,16 +73,9 @@ def test_collection_resource_wrong_type( @responses.activate def test_collection_get_with_identifier( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - object_id = flat_catalog_type_object['aps']['id'] responses.add( @@ -204,16 +92,9 @@ def test_collection_get_with_identifier( @responses.activate def test_collection_action( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - responses.add( method='POST', url=f'{cbc_endpoint}/flat-catalog/wizard/upload', @@ -228,17 +109,7 @@ def test_collection_action( TestCase().assertDictEqual(obj, flat_catalog_type_object) -def test_collection_action_with_both_payload_and_file( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - +def test_collection_action_with_both_payload_and_file(cbc_client): with pytest.raises(ValueError): cbc_client.flat_catalog.wizard.action( name='upload', diff --git a/tests/client/test_resource.py b/tests/client/test_resource.py index 8540007..66466d2 100644 --- a/tests/client/test_resource.py +++ b/tests/client/test_resource.py @@ -3,20 +3,8 @@ import responses -from connect_ext_ppr.client import CBCClient - - -def test_resource( - cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, -): - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) +def test_resource(cbc_endpoint, cbc_client): resource = cbc_client.test_collection['identifier'] assert resource.path == f'{cbc_endpoint}/test-collection/identifier' @@ -25,8 +13,7 @@ def test_resource( @responses.activate def test_resource_get( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -35,12 +22,6 @@ def test_resource_get( json=flat_catalog_type_object, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].get() TestCase().assertDictEqual(obj, flat_catalog_type_object) @@ -49,8 +30,7 @@ def test_resource_get( @responses.activate def test_resource_action_with_payload( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -59,12 +39,6 @@ def test_resource_action_with_payload( json=flat_catalog_type_object, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].action( name='upload', payload={}, @@ -76,8 +50,7 @@ def test_resource_action_with_payload( @responses.activate def test_resource_action_not_json_response( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -86,12 +59,6 @@ def test_resource_action_not_json_response( body='Not a JSON', ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].action( name='upload', payload={}, @@ -103,8 +70,7 @@ def test_resource_action_not_json_response( @responses.activate def test_resource_action_with_extra_headers( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -113,12 +79,6 @@ def test_resource_action_with_extra_headers( json=flat_catalog_type_object, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].action( name='upload', payload={}, @@ -131,8 +91,7 @@ def test_resource_action_with_extra_headers( @responses.activate def test_resource_action_with_file( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -141,12 +100,6 @@ def test_resource_action_with_file( json=flat_catalog_type_object, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].action( name='upload', file=io.StringIO("some initial text data"), @@ -158,8 +111,7 @@ def test_resource_action_with_file( @responses.activate def test_resource_action_with_headers_output( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type_object, ): responses.add( @@ -168,12 +120,6 @@ def test_resource_action_with_headers_output( json=flat_catalog_type_object, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - obj = cbc_client.test_collection['identifier'].action( name='upload', payload={}, diff --git a/tests/client/test_service.py b/tests/client/test_service.py index 0d89a61..be31411 100644 --- a/tests/client/test_service.py +++ b/tests/client/test_service.py @@ -3,15 +3,13 @@ import pytest import responses -from connect_ext_ppr.client import CBCClient from connect_ext_ppr.client.exception import ClientError @responses.activate def test_service_discovery( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, flat_catalog_type_objects, ): @@ -21,12 +19,6 @@ def test_service_discovery( json=flat_catalog_type_objects, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - services = cbc_client(flat_catalog_type).get() TestCase().assertListEqual(services, flat_catalog_type_objects) @@ -34,8 +26,7 @@ def test_service_discovery( @responses.activate def test_service_discovery_client_error( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, ): responses.add( @@ -44,12 +35,6 @@ def test_service_discovery_client_error( status=401, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - with pytest.raises(ClientError): cbc_client(flat_catalog_type).get() @@ -57,8 +42,7 @@ def test_service_discovery_client_error( @responses.activate def test_service_discovery_no_service( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, ): responses.add( @@ -67,12 +51,6 @@ def test_service_discovery_no_service( json=[], ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - with pytest.raises(TypeError): cbc_client(flat_catalog_type).property1 @@ -80,8 +58,7 @@ def test_service_discovery_no_service( @responses.activate def test_service_discovery_more_than_one_service( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, ): responses.add( @@ -90,12 +67,6 @@ def test_service_discovery_more_than_one_service( json=['service1', 'service2'], ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - with pytest.raises(TypeError): cbc_client(flat_catalog_type).property1 @@ -103,8 +74,7 @@ def test_service_discovery_more_than_one_service( @responses.activate def test_service_discovery_collection_with_underscore( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, flat_catalog_type_objects, ): @@ -115,12 +85,6 @@ def test_service_discovery_collection_with_underscore( json=flat_catalog_type_objects, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - collection = cbc_client(flat_catalog_type).flat_catalog assert collection.path == f'{cbc_endpoint}/aps/2/resources/{service_id}/flat-catalog' @@ -129,8 +93,7 @@ def test_service_discovery_collection_with_underscore( @responses.activate def test_service_discovery_collection_without_underscore( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, flat_catalog_type_objects, ): @@ -141,12 +104,6 @@ def test_service_discovery_collection_without_underscore( json=flat_catalog_type_objects, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - collection = cbc_client(flat_catalog_type).flatcatalog assert collection.path == f'{cbc_endpoint}/aps/2/resources/{service_id}/flatcatalog' @@ -155,8 +112,7 @@ def test_service_discovery_collection_without_underscore( @responses.activate def test_service_discovery_collection_wrong_collection_value_type( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, flat_catalog_type_objects, ): @@ -166,12 +122,6 @@ def test_service_discovery_collection_wrong_collection_value_type( json=flat_catalog_type_objects, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - with pytest.raises(TypeError): cbc_client(flat_catalog_type).collection(1) @@ -179,8 +129,7 @@ def test_service_discovery_collection_wrong_collection_value_type( @responses.activate def test_service_discovery_collection_blank_collection_value( cbc_endpoint, - cbc_oauth_key, - cbc_oauth_secret, + cbc_client, flat_catalog_type, flat_catalog_type_objects, ): @@ -190,11 +139,5 @@ def test_service_discovery_collection_blank_collection_value( json=flat_catalog_type_objects, ) - cbc_client = CBCClient( - endpoint=cbc_endpoint, - oauth_key=cbc_oauth_key, - oauth_secret=cbc_oauth_secret, - ) - with pytest.raises(ValueError): cbc_client(flat_catalog_type).collection('') diff --git a/tests/conftest.py b/tests/conftest.py index cb646db..b7a7218 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ from connect.client import AsyncConnectClient, ConnectClient from sqlalchemy.orm import sessionmaker +from connect_ext_ppr.client import CBCClient from connect_ext_ppr.db import create_db, get_db, get_engine, Model, VerboseBaseSession from connect_ext_ppr.models.deployment import Deployment from connect_ext_ppr.models.file import File @@ -304,6 +305,26 @@ def cbc_oauth_secret(): return '7b534af1-4110-4520-abf3-e78503ef78a6' +@pytest.fixture +def cbc_app_id(): + return '30a22e97-c8b0-4e6f-bb6a-87fbcb724830' + + +@pytest.fixture +def cbc_client( + cbc_endpoint, + cbc_oauth_key, + cbc_oauth_secret, + cbc_app_id, +): + return CBCClient( + endpoint=cbc_endpoint, + oauth_key=cbc_oauth_key, + oauth_secret=cbc_oauth_secret, + app_id=cbc_app_id, + ) + + @pytest.fixture def flat_catalog_type_object(): return {