Skip to content

Commit

Permalink
Add ResponseProperties object and example refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
azgabur committed Jun 5, 2023
1 parent b042b30 commit 2acf655
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
35 changes: 33 additions & 2 deletions testsuite/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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"""
Expand Down
3 changes: 3 additions & 0 deletions testsuite/openshift/objects/auth_config/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Cache,
Value,
JsonProperty,
ResponseProperties,
)
from testsuite.openshift.objects import modify

Expand Down Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json

import pytest
from testsuite.objects import ResponseProperties, JsonProperty, Value


@pytest.fixture(scope="module")
Expand All @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]),
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json

import pytest
from testsuite.objects import ResponseProperties, JsonProperty, Value


@pytest.fixture(scope="module", params=["123456789", "standardCharacters", "specialcharacters+*-."])
Expand All @@ -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"))])
]


Expand Down

0 comments on commit 2acf655

Please sign in to comment.