Skip to content

Commit

Permalink
fix basic methods for application keys
Browse files Browse the repository at this point in the history
  • Loading branch information
mkudlej committed May 15, 2024
1 parent d1e479e commit 2729f51
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
18 changes: 17 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import secrets
import time
import string
import random
from distutils.util import strtobool

import pytest
Expand All @@ -11,7 +13,8 @@
from threescale_api.resources import (Service, ApplicationPlan, Application,
Proxy, Backend, Metric, MappingRule,
BackendMappingRule, BackendUsage,
ActiveDoc, Webhooks, InvoiceState)
ActiveDoc, Webhooks, InvoiceState,
ApplicationKey)

load_dotenv()

Expand Down Expand Up @@ -140,6 +143,19 @@ def application(account, application_plan, application_params) -> Application:
cleanup(resource)


@pytest.fixture(scope='module')
def app_key_params(account, application):
value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=100))
return({"application_id": application["id"], "account_id": account["id"], "key": value})


@pytest.fixture(scope='module')
def app_key(application, app_key_params) -> ApplicationKey:
resource = application.keys.create(params=app_key_params)
yield resource
cleanup(resource)


@pytest.fixture(scope='module')
def proxy(service, application, api_backend) -> Proxy:
params = {
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_integration_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ def test_application_update(application, update_application_params):
updated_application = application.update(params=update_application_params)
asserts.assert_resource(updated_application)
asserts.assert_resource_params(updated_application, update_application_params)


def test_application_key_can_be_created(app_key, app_key_params):
asserts.assert_resource(app_key)
asserts.assert_resource_params(app_key, app_key_params)


def test_application_key_list(application, app_key):
keys = application.keys.list()
assert len(keys) > 0
2 changes: 1 addition & 1 deletion threescale_api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(self, app, location=None):
proxy = self.app.service.proxy.list()
self.credentials = {
proxy["auth_app_id"]: self.app["application_id"],
proxy["auth_app_key"]: self.app.keys.list()["keys"][0]["key"]["value"]
proxy["auth_app_key"]: self.app.keys.list()[-1]["value"]
}

def __call__(self, request):
Expand Down
31 changes: 30 additions & 1 deletion threescale_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def url(self) -> str:


class ApplicationKeys(DefaultClient):
def __init__(self, *args, entity_name='application', entity_collection='applications',
def __init__(self, *args, entity_name='key', entity_collection='keys',
**kwargs):
super().__init__(*args, entity_name=entity_name,
entity_collection=entity_collection, **kwargs)
Expand All @@ -325,6 +325,30 @@ def __init__(self, *args, entity_name='application', entity_collection='applicat
def url(self) -> str:
return self.parent.url + '/keys'

def create(self, params: dict = None, **kwargs) -> 'ApplicationKey':
"""Create a new instance of ApplicationKey. "keys" POST request
returns Application instead of newly create key.
Returns: Newly created key.
"""
super().create(params=params, **kwargs)
key = sorted(self.list(), key=lambda key: key["created_at"])[-1]
key.entity_id = key["value"]
return key

def list(self, **kwargs) -> List['ApplicationKey']:
"""List all entities of ApplicationKey.
There is no id in list response, so it needs to be assigned the value
to be able to work with key instance.
Args:
**kwargs: Optional parameters
Returns(List['ApplicationKey']): List of ApplicationKey resources
"""
key_list = super().list(**kwargs)
for key in key_list:
key.entity_id = key["value"]
return key_list


class Providers(DefaultClient):
def __init__(self, *args, entity_name='user', entity_collection='users', **kwargs):
Expand Down Expand Up @@ -1396,6 +1420,11 @@ def test_request(self, relpath=None, verify: bool = None):
return client.get(relpath)


class ApplicationKey(DefaultResource):
def __init__(self, entity_name='', **kwargs):
super().__init__(entity_name=entity_name, **kwargs)


class Account(DefaultResource):
def __init__(self, entity_name='org_name', **kwargs):
super().__init__(entity_name=entity_name, **kwargs)
Expand Down

0 comments on commit 2729f51

Please sign in to comment.