Skip to content

Commit

Permalink
LITE-28062 Add app_id as client object __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulmondal committed Jul 13, 2023
1 parent bab42fd commit 9654514
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 381 deletions.
4 changes: 4 additions & 0 deletions connect_ext_ppr/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
Expand All @@ -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
Expand All @@ -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,
Expand Down
128 changes: 15 additions & 113 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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['']

Expand All @@ -149,6 +78,7 @@ def test_client_with_default_headers(
cbc_endpoint,
cbc_oauth_key,
cbc_oauth_secret,
cbc_app_id,
):
headers = {'Custom': 'Value'}

Expand All @@ -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')
Loading

0 comments on commit 9654514

Please sign in to comment.