Skip to content

Commit f1786ce

Browse files
author
Martin Kudlej
committed
Add support for ApplicationAuth CR
1 parent 262a382 commit f1786ce

File tree

6 files changed

+295
-22
lines changed

6 files changed

+295
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ see https://github.com/3scale/3scale-operator/blob/master/doc/backend-reference.
9595
- ProxyConfigPromote
9696
- Applications
9797
- Methods
98+
- ApplicationAuth
9899

99100
Command to run integration unit tests: `pipenv run pytest --log-cli-level=10 -vvvv -s ./tests/integration/ |& tee x`
100101

tests/integration/conftest.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import secrets
3+
import random
4+
import string
35
from distutils.util import strtobool
46

57
import pytest
@@ -9,7 +11,6 @@
911

1012
import threescale_api
1113
import threescale_api_crd
12-
from threescale_api.resources import Application
1314

1415
from threescale_api_crd.resources import (
1516
Service,
@@ -24,6 +25,7 @@
2425
ApplicationPlan,
2526
Proxy,
2627
PricingRule,
28+
Application,
2729
)
2830

2931
load_dotenv()
@@ -210,6 +212,30 @@ def application(service, application_plan, application_params, account) -> Appli
210212
cleanup(resource)
211213

212214

215+
@pytest.fixture(scope="module")
216+
def app_key_params(account, application):
217+
value = "".join(
218+
random.choices(
219+
string.ascii_uppercase
220+
+ string.digits
221+
+ "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
222+
k=100,
223+
)
224+
)
225+
return {
226+
"application_id": application["id"],
227+
"account_id": account["id"],
228+
"key": value,
229+
}
230+
231+
232+
@pytest.fixture(scope="module")
233+
def app_key(application, app_key_params) -> threescale_api.resources.ApplicationKey:
234+
resource = application.keys.create(params=app_key_params)
235+
yield resource
236+
cleanup(resource)
237+
238+
213239
@pytest.fixture(scope="module")
214240
def proxy(service) -> Proxy:
215241
return service.proxy.list()

tests/integration/test_integration_application.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import pytest
21
import secrets
2+
import random
3+
import string
4+
import pytest
35

46
from tests.integration import asserts
57

@@ -36,12 +38,12 @@ def test_application_can_be_read_by_name(api, application_params, application):
3638
@pytest.fixture(scope="module")
3739
def application_plan_params2():
3840
suffix = secrets.token_urlsafe(8)
39-
return dict(
40-
name=f"test-{suffix}",
41-
setup_fee="1.00",
42-
state_event="publish",
43-
cost_per_month="3.00",
44-
)
41+
return {
42+
"name": f"test-{suffix}",
43+
"setup_fee": "1.00",
44+
"state_event": "publish",
45+
"cost_per_month": "3.00",
46+
}
4547

4648

4749
@pytest.fixture(scope="module")
@@ -55,7 +57,7 @@ def application_plan2(service, application_plan_params2):
5557
def update_application_params(application_plan2):
5658
suffix = secrets.token_urlsafe(8)
5759
name = f"updated-{suffix}"
58-
return dict(name=name, description=name, plan_id=application_plan2["id"])
60+
return {"name": name, "description": name, "plan_id": application_plan2["id"]}
5961

6062

6163
def test_application_update(update_application_params, application):
@@ -74,3 +76,38 @@ def test_application_set_state(application):
7476
assert application["state"] == "suspended"
7577
application = application.set_state("resume")
7678
assert application["state"] == "live"
79+
80+
81+
# Application Atuhentication - Application keys
82+
83+
84+
def test_application_key_list(application, app_key):
85+
keys = application.keys.list()
86+
assert len(keys) > 0
87+
88+
89+
def test_application_key_can_be_created(app_key, app_key_params):
90+
asserts.assert_resource(app_key)
91+
asserts.assert_resource_params(app_key, app_key_params)
92+
93+
94+
def test_application_autogenerated_key_can_be_created(application, app_key_params):
95+
keys_len = len(application.keys.list())
96+
key_params = app_key_params.copy()
97+
key_params.pop("key", None)
98+
key_params["generateSecret"] = True
99+
new_key = application.keys.create(params=key_params)
100+
asserts.assert_resource(new_key)
101+
asserts.assert_resource_params(new_key, key_params)
102+
assert new_key["value"]
103+
assert len(application.keys.list()) == keys_len + 1
104+
105+
106+
def test_application_update_userkey(application):
107+
new_key = "".join(
108+
random.choices(string.ascii_letters + string.digits + "-_.", k=100)
109+
)
110+
application.update(params={"user_key": new_key})
111+
application.read()
112+
asserts.assert_resource(application)
113+
assert application["user_key"] == new_key

threescale_api_crd/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ def __init__(self, url, token, ocp_provider_ref=None, ocp_namespace=None, *args,
4545
self._applications = resources.Applications(
4646
parent=self, account=None, instance_klass=resources.Application
4747
)
48+
self._app_auths = resources.AppAuths(
49+
parent=self, instance_klass=resources.AppAuth
50+
)
4851

4952
@classmethod
50-
def get_namespace(_ignore, namespace):
53+
def get_namespace(cls, namespace):
5154
"""
5255
Returns namespace. If there is no valid Openshift 'oc' session, returns "NOT LOGGED IN".
5356
"""
@@ -126,6 +129,13 @@ def applications(self) -> resources.Applications:
126129
"""
127130
return self._applications
128131

132+
@property
133+
def app_auths(self) -> resources.AppAuths:
134+
"""Gets Application Auth client
135+
Returns(resources.Auths): Application Auth client
136+
"""
137+
return self._app_auths
138+
129139
@property
130140
def ocp_provider_ref(self):
131141
"""Gets provider reference"""

threescale_api_crd/constants.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
SERVICE_AUTH_DEFS = {
88
"1": {
99
"userkey": {
10-
"authUserKey": "token",
10+
# "token", see https://issues.redhat.com/browse/THREESCALE-11072
11+
"authUserKey": "user_key",
1112
"credentials": "authorization",
1213
"gatewayResponse": {},
1314
},
@@ -57,7 +58,8 @@
5758
"apicastHosted": {
5859
"authentication": {
5960
"userkey": {
60-
"authUserKey": "token",
61+
# "token", see https://issues.redhat.com/browse/THREESCALE-11072
62+
"authUserKey": "user_key",
6163
"credentials": "query",
6264
"gatewayResponse": {},
6365
},
@@ -309,6 +311,26 @@
309311
},
310312
}
311313

314+
SPEC_APP_AUTH = {
315+
"apiVersion": "capabilities.3scale.net/v1beta1",
316+
"kind": "ApplicationAuth",
317+
"metadata": {
318+
"name": None,
319+
"namespace": None,
320+
"annotations": {"insecure_skip_verify": "true"},
321+
},
322+
"spec": {
323+
"providerAccountRef": {
324+
"name": None,
325+
},
326+
"applicationCRName": None,
327+
"generateSecret": False,
328+
"authSecretRef": {
329+
"name": None,
330+
},
331+
},
332+
}
333+
312334
SPEC_APPLICATION = {
313335
"apiVersion": "capabilities.3scale.net/v1beta1",
314336
"kind": "Application",
@@ -534,6 +556,12 @@
534556
"deleteCR": "deleteCR",
535557
}
536558

559+
KEYS_APP_AUTH = {
560+
"applicationCRName": "applicationCRName",
561+
"generateSecret": "generateSecret",
562+
"authSecretRef": "authSecretRef",
563+
}
564+
537565
KEYS_APPLICATION = {
538566
"description": "description",
539567
"name": "name",

0 commit comments

Comments
 (0)