Skip to content

Commit 0af4cde

Browse files
author
mkudlej
authored
Merge pull request 3scale-qe#153 from mkudlej/fix_auth_app_keys
fix basic methods for application keys
2 parents d1e479e + 72a8a6b commit 0af4cde

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

tests/integration/conftest.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import secrets
33
import time
4+
import string
5+
import random
46
from distutils.util import strtobool
57

68
import pytest
@@ -11,7 +13,8 @@
1113
from threescale_api.resources import (Service, ApplicationPlan, Application,
1214
Proxy, Backend, Metric, MappingRule,
1315
BackendMappingRule, BackendUsage,
14-
ActiveDoc, Webhooks, InvoiceState)
16+
ActiveDoc, Webhooks, InvoiceState,
17+
ApplicationKey)
1518

1619
load_dotenv()
1720

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

142145

146+
@pytest.fixture(scope='module')
147+
def app_key_params(account, application):
148+
value = ''.join(random.choices(string.ascii_uppercase + string.digits + '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', k=100))
149+
return({"application_id": application["id"], "account_id": account["id"], "key": value})
150+
151+
152+
@pytest.fixture(scope='module')
153+
def app_key(application, app_key_params) -> ApplicationKey:
154+
resource = application.keys.create(params=app_key_params)
155+
yield resource
156+
cleanup(resource)
157+
158+
143159
@pytest.fixture(scope='module')
144160
def proxy(service, application, api_backend) -> Proxy:
145161
params = {

tests/integration/test_integration_application.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ def test_application_update(application, update_application_params):
2525
updated_application = application.update(params=update_application_params)
2626
asserts.assert_resource(updated_application)
2727
asserts.assert_resource_params(updated_application, update_application_params)
28+
29+
30+
def test_application_key_can_be_created(app_key, app_key_params):
31+
asserts.assert_resource(app_key)
32+
asserts.assert_resource_params(app_key, app_key_params)
33+
34+
35+
def test_application_key_list(application, app_key):
36+
keys = application.keys.list()
37+
assert len(keys) > 0

threescale_api/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, app, location=None):
5757
proxy = self.app.service.proxy.list()
5858
self.credentials = {
5959
proxy["auth_app_id"]: self.app["application_id"],
60-
proxy["auth_app_key"]: self.app.keys.list()["keys"][0]["key"]["value"]
60+
proxy["auth_app_key"]: self.app.keys.list()[-1]["value"]
6161
}
6262

6363
def __call__(self, request):

threescale_api/resources.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from enum import Enum
33
from typing import Dict, Union, List, Iterable
4+
from urllib.parse import quote_plus
45

56
from threescale_api import auth
67
from threescale_api import utils
@@ -316,7 +317,7 @@ def url(self) -> str:
316317

317318

318319
class ApplicationKeys(DefaultClient):
319-
def __init__(self, *args, entity_name='application', entity_collection='applications',
320+
def __init__(self, *args, entity_name='key', entity_collection='keys',
320321
**kwargs):
321322
super().__init__(*args, entity_name=entity_name,
322323
entity_collection=entity_collection, **kwargs)
@@ -325,6 +326,30 @@ def __init__(self, *args, entity_name='application', entity_collection='applicat
325326
def url(self) -> str:
326327
return self.parent.url + '/keys'
327328

329+
def create(self, params: dict = None, **kwargs) -> 'ApplicationKey':
330+
"""Create a new instance of ApplicationKey. "keys" POST request
331+
returns Application instead of newly create key.
332+
Returns: Newly created key.
333+
334+
"""
335+
super().create(params=params, **kwargs)
336+
key = sorted(self.list(), key=lambda key: key["created_at"])[-1]
337+
key.entity_id = quote_plus(key["value"])
338+
return key
339+
340+
def list(self, **kwargs) -> List['ApplicationKey']:
341+
"""List all entities of ApplicationKey.
342+
There is no id in list response, so it needs to be assigned the value
343+
to be able to work with key instance.
344+
Args:
345+
**kwargs: Optional parameters
346+
Returns(List['ApplicationKey']): List of ApplicationKey resources
347+
"""
348+
key_list = super().list(**kwargs)
349+
for key in key_list:
350+
key.entity_id = quote_plus(key["value"])
351+
return key_list
352+
328353

329354
class Providers(DefaultClient):
330355
def __init__(self, *args, entity_name='user', entity_collection='users', **kwargs):
@@ -1320,7 +1345,7 @@ def service(self) -> 'Service':
13201345
@property
13211346
def keys(self):
13221347
"Application keys"
1323-
return ApplicationKeys(parent=self, instance_klass=DefaultResource)
1348+
return ApplicationKeys(parent=self, instance_klass=ApplicationKey)
13241349

13251350
def authobj(self, auth_mode=None, location=None):
13261351
"""Returns subclass of requests.auth.BaseAuth to provide authentication
@@ -1396,6 +1421,11 @@ def test_request(self, relpath=None, verify: bool = None):
13961421
return client.get(relpath)
13971422

13981423

1424+
class ApplicationKey(DefaultResource):
1425+
def __init__(self, entity_name='', **kwargs):
1426+
super().__init__(entity_name=entity_name, **kwargs)
1427+
1428+
13991429
class Account(DefaultResource):
14001430
def __init__(self, entity_name='org_name', **kwargs):
14011431
super().__init__(entity_name=entity_name, **kwargs)

0 commit comments

Comments
 (0)