diff --git a/testsuite/objects/__init__.py b/testsuite/objects/__init__.py index 9b93a2e1..b01df8cd 100644 --- a/testsuite/objects/__init__.py +++ b/testsuite/objects/__init__.py @@ -2,7 +2,7 @@ import abc from dataclasses import dataclass from functools import cached_property -from typing import Literal, List +from typing import Literal, List, Optional from testsuite.objects.sections import Metadata, Identities, Authorizations, Responses @@ -67,7 +67,7 @@ def to_dict(self): class JsonProperty: """ https://github.com/kuadrant/authorino/blob/v0.13.0/api/v1beta1/auth_config_types.go#L89 - For use in identity extended_property functionality + For use in identity extended_property and dynamic response functions. """ name: str @@ -78,6 +78,37 @@ def to_dict(self): return {"name": self.name, **self.value.to_dict()} +@dataclass +class ResponseProperties: + """ + Dataclass for dynamic response definition. + Args: + :param properties: List of JsonProperty objects + :param name: Name of the property and by default name of the header, by default it is 'Auth-Json' + :param wrapper: Optional `httpHeader` or `envoyDynamicMetadata` value + :param wrapperKey: Optional header name + :param wristband: Optional wristband functionality + """ + + name: str + properties: List[JsonProperty] + + wrapper: Optional[Literal["httpHeader", "envoyDynamicMetadata"]] = None + wrapperKey: Optional[str] = None # pylint: disable=invalid-name + wristband: Optional[dict] = None # new class for wristband? + + def to_dict(self): + """Returns dict representation of itself (shallow copy only)""" + ret = {"name": self.name, "json": {"properties": [prop.to_dict() for prop in self.properties]}} + if self.wrapper is not None: + ret.update({"wrapper": self.wrapper}) + if self.wrapperKey is not None: + ret.update({"wrapperKey": self.wrapperKey}) + if self.wristband is not None: + ret.update({"wristband": self.wristband}) + return ret + + @dataclass class PatternRef: """Dataclass for specifying Pattern reference in Authorization""" diff --git a/testsuite/openshift/objects/auth_config/sections.py b/testsuite/openshift/objects/auth_config/sections.py index 1300e1a8..3a57742c 100644 --- a/testsuite/openshift/objects/auth_config/sections.py +++ b/testsuite/openshift/objects/auth_config/sections.py @@ -12,6 +12,7 @@ Cache, Value, JsonProperty, + ResponseProperties, ) from testsuite.openshift.objects import modify @@ -191,6 +192,8 @@ class ResponsesSection(Section, Responses): @modify def add(self, response, **common_features): """Adds response section to AuthConfig.""" + if isinstance(response, ResponseProperties): + response = response.to_dict() self.add_item(response.pop("name"), response, **common_features) diff --git a/testsuite/tests/kuadrant/authorino/response/test_auth_json.py b/testsuite/tests/kuadrant/authorino/response/test_auth_json.py index a820ceb9..3093bcfc 100644 --- a/testsuite/tests/kuadrant/authorino/response/test_auth_json.py +++ b/testsuite/tests/kuadrant/authorino/response/test_auth_json.py @@ -3,6 +3,7 @@ import json import pytest +from testsuite.objects import ResponseProperties, JsonProperty, Value @pytest.fixture(scope="module") @@ -27,7 +28,7 @@ def path_and_value(request): def responses(path_and_value): """Returns response to be added to the AuthConfig""" path, _ = path_and_value - return [{"name": "header", "json": {"properties": [{"name": "anything", "valueFrom": {"authJSON": path}}]}}] + return [ResponseProperties(name="header", properties=[JsonProperty("anything", Value(jsonPath=path))])] def test_auth_json_path(auth, client, path_and_value): diff --git a/testsuite/tests/kuadrant/authorino/response/test_multiple_responses.py b/testsuite/tests/kuadrant/authorino/response/test_multiple_responses.py index 59fdfd74..db07102c 100644 --- a/testsuite/tests/kuadrant/authorino/response/test_multiple_responses.py +++ b/testsuite/tests/kuadrant/authorino/response/test_multiple_responses.py @@ -2,14 +2,15 @@ import json import pytest +from testsuite.objects import ResponseProperties, JsonProperty, Value @pytest.fixture(scope="module") def responses(): """Returns response to be added to the AuthConfig""" return [ - {"name": "Header", "json": {"properties": [{"name": "anything", "value": "one"}]}}, - {"name": "X-Test", "json": {"properties": [{"name": "anything", "value": "two"}]}}, + ResponseProperties(name="Header", properties=[JsonProperty("anything", Value("one"))]), + ResponseProperties(name="X-Test", properties=[JsonProperty("anything", Value("two"))]), ] diff --git a/testsuite/tests/kuadrant/authorino/response/test_simple_response.py b/testsuite/tests/kuadrant/authorino/response/test_simple_response.py index ec2fb33f..33263b43 100644 --- a/testsuite/tests/kuadrant/authorino/response/test_simple_response.py +++ b/testsuite/tests/kuadrant/authorino/response/test_simple_response.py @@ -2,12 +2,13 @@ import json import pytest +from testsuite.objects import ResponseProperties, JsonProperty, Value @pytest.fixture(scope="module") def responses(): """Returns response to be added to the AuthConfig""" - return [{"name": "header", "json": {"properties": [{"name": "anything", "value": "one"}]}}] + return [ResponseProperties(name="header", properties=[JsonProperty("anything", Value("one"))])] def test_simple_response_with(auth, client): diff --git a/testsuite/tests/kuadrant/authorino/response/test_wrapper_key.py b/testsuite/tests/kuadrant/authorino/response/test_wrapper_key.py index 289d5fd4..02c4284b 100644 --- a/testsuite/tests/kuadrant/authorino/response/test_wrapper_key.py +++ b/testsuite/tests/kuadrant/authorino/response/test_wrapper_key.py @@ -2,6 +2,7 @@ import json import pytest +from testsuite.objects import ResponseProperties, JsonProperty, Value @pytest.fixture(scope="module", params=["123456789", "standardCharacters", "specialcharacters+*-."]) @@ -14,7 +15,7 @@ def header_name(request): def responses(header_name): """Returns response to be added to the AuthConfig""" return [ - {"name": "header", "wrapperKey": header_name, "json": {"properties": [{"name": "anything", "value": "one"}]}} + ResponseProperties(name="header", wrapperKey=header_name, properties=[JsonProperty("anything", Value("one"))]) ]