Skip to content

Commit

Permalink
Merge pull request #220 from averevki/response-section
Browse files Browse the repository at this point in the history
Reformat AuthConfig responses section
  • Loading branch information
pehala authored Aug 8, 2023
2 parents 6a659e3 + 29fd00f commit 5bce78a
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 119 deletions.
12 changes: 12 additions & 0 deletions testsuite/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ def asdict(self):
return {"valueFrom": {"authJSON": self.authJSON}}


@dataclass
class Property:
"""Dataclass for static and dynamic values. Property is a Value with name."""

name: str
value: ABCValue

def asdict(self):
"""Override `asdict` function"""
return {"name": self.name, **asdict(self.value)}


@dataclass
class Cache:
"""Dataclass for specifying Cache in Authorization"""
Expand Down
4 changes: 3 additions & 1 deletion testsuite/openshift/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ def commit(self):

def delete(self, ignore_not_found=True, cmd_args=None):
"""Deletes the resource, by default ignored not found"""
return super().delete(ignore_not_found, cmd_args)
deleted = super().delete(ignore_not_found, cmd_args)
self.committed = False
return deleted
61 changes: 52 additions & 9 deletions testsuite/openshift/objects/auth_config/sections.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Rule,
Cache,
ABCValue,
ValueFrom,
Property,
)
from testsuite.openshift.objects import modify

Expand Down Expand Up @@ -175,20 +177,61 @@ def uma_metadata(self, name, endpoint, credentials, **common_features):
class Responses(Section):
"""Section which contains response configuration"""

def add_simple(self, auth_json, name="simple", key="data", **common_features):
"""Adds simple response to AuthConfig"""
self.add(
def _add(
self,
name: str,
value: dict,
wrapper_key: str = None,
wrapper: Literal["httpHeader", "envoyDynamicMetadata"] = None,
**common_features
):
"""Add response to AuthConfig"""
if wrapper:
value["wrapper"] = wrapper
if wrapper_key:
value["wrapperKey"] = wrapper_key

self.add_item(name, value, **common_features)

def add_simple(self, auth_json: str, name="simple", key="data", **common_features):
"""Add simple response to AuthConfig"""
self.json(name, [Property(key, ValueFrom(auth_json))], **common_features)

@modify
def json(self, name: str, properties: list[Property], **common_features):
"""Adds json response to AuthConfig"""
asdict_properties = [asdict(p) for p in properties]
self._add(name, {"json": {"properties": asdict_properties}}, **common_features)

@modify
def plain(self, name: str, value: ABCValue, **common_features):
"""Adds plain response to AuthConfig"""
self._add(name, {"plain": asdict(value)}, **common_features)

@modify
def wristband(self, name: str, issuer: str, secret_name: str, algorithm: str = "RS256", **common_features):
"""Adds wristband response to AuthConfig"""
self._add(
name,
{
"name": name,
"json": {"properties": [{"name": key, "valueFrom": {"authJSON": auth_json}}]},
**common_features,
}
"wristband": {
"issuer": issuer,
"signingKeyRefs": [
{
"name": secret_name,
"algorithm": algorithm,
}
],
},
},
**common_features
)

@modify
def add(self, response, **common_features):
"""Adds response section to AuthConfig."""
self.add_item(response.pop("name"), response, **common_features)
def remove_all(self):
"""Removes all responses from AuthConfig"""
self.section.clear()


class Authorizations(Section):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
"""Test condition to skip the response section of AuthConfig"""
import pytest

from testsuite.objects import Rule
from testsuite.objects import Property, Rule, Value
from testsuite.utils import extract_response


@pytest.fixture(scope="module")
def authorization(authorization):
"""Add to the AuthConfig response, which will only trigger on POST requests"""
authorization.responses.add(
{
"name": "simple",
"json": {
"properties": [
{"name": "data", "value": "response"},
]
},
},
when=[Rule("context.request.http.method", "eq", "POST")],
authorization.responses.json(
"simple", [Property("data", Value("response"))], when=[Rule("context.request.http.method", "eq", "POST")]
)
return authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

import pytest

from testsuite.objects import Property, ValueFrom


@pytest.fixture(scope="module")
def authorization(authorization):
"""Setup AuthConfig for test"""
authorization.responses.add(
{
"name": "auth-json",
"json": {
"properties": [
{"name": "auth", "valueFrom": {"authJSON": "auth.identity"}},
{"name": "context", "valueFrom": {"authJSON": "context.request.http.headers.authorization"}},
]
},
}
authorization.responses.json(
"auth-json",
[
Property("auth", ValueFrom("auth.identity")),
Property("context", ValueFrom("context.request.http.headers.authorization")),
],
)
return authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from testsuite.objects import Property, Value
from testsuite.openshift.objects.auth_config import AuthConfig


Expand All @@ -14,7 +15,7 @@ def authorization(authorino, blame, openshift, module_label, envoy, wildcard_dom
auth = AuthConfig.create_instance(
openshift, blame("ac"), None, hostnames=[wildcard_domain], labels={"testRun": module_label}
)
auth.responses.add({"name": "header", "json": {"properties": [{"name": "anything", "value": "one"}]}})
auth.responses.json("header", [Property("anything", Value("one"))])
return auth


Expand All @@ -25,7 +26,7 @@ def authorization2(authorino, blame, openshift2, module_label, envoy, wildcard_d
auth = AuthConfig.create_instance(
openshift2, blame("ac"), None, hostnames=[wildcard_domain], labels={"testRun": module_label}
)
auth.responses.add({"name": "header", "json": {"properties": [{"name": "anything", "value": "two"}]}})
auth.responses.json("header", [Property("anything", Value("two"))])
return auth


Expand Down
10 changes: 4 additions & 6 deletions testsuite/tests/kuadrant/authorino/operator/http/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Conftest for all tests requiring custom deployment of Authorino"""
import pytest

from testsuite.objects import Property, Value
from testsuite.httpx import HttpxBackoffClient
from testsuite.openshift.objects.auth_config import AuthConfig

Expand All @@ -11,12 +12,9 @@ def authorization(authorization, wildcard_domain, openshift, module_label) -> Au
"""In case of Authorino, AuthConfig used for authorization"""
authorization.remove_all_hosts()
authorization.add_host(wildcard_domain)
resp = {
"name": "another-json-returned-in-a-header",
"wrapperKey": "x-ext-auth-other-json",
"json": {"properties": [{"name": "propX", "value": "valueX"}]},
}
authorization.responses.add(response=resp)
authorization.responses.json(
"another-json-returned-in-a-header", [Property("propX", Value("valueX"))], wrapper_key="x-ext-auth-other-json"
)
return authorization


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Conftest for authorino sharding tests"""
import pytest

from testsuite.objects import Property, Value
from testsuite.openshift.envoy import Envoy
from testsuite.openshift.objects.auth_config import AuthConfig

Expand Down Expand Up @@ -31,7 +32,7 @@ def _authorization(hostname=None, sharding_label=None):
hostnames=[hostname],
labels={"testRun": module_label, "sharding": sharding_label},
)
auth.responses.add({"name": "header", "json": {"properties": [{"name": "anything", "value": sharding_label}]}})
auth.responses.json("header", [Property("anything", Value(sharding_label))])
request.addfinalizer(auth.delete)
auth.commit()
return auth
Expand Down
31 changes: 0 additions & 31 deletions testsuite/tests/kuadrant/authorino/response/conftest.py

This file was deleted.

11 changes: 8 additions & 3 deletions testsuite/tests/kuadrant/authorino/response/test_auth_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

from testsuite.objects import Property, ValueFrom


@pytest.fixture(scope="module")
def issuer(oidc_provider):
Expand All @@ -24,10 +26,13 @@ def path_and_value(request):


@pytest.fixture(scope="module")
def responses(path_and_value):
"""Returns response to be added to the AuthConfig"""
def authorization(authorization, path_and_value):
"""Add response to Authorization"""
path, _ = path_and_value
return [{"name": "header", "json": {"properties": [{"name": "anything", "valueFrom": {"authJSON": path}}]}}]

authorization.responses.remove_all() # delete previous responses due to the parametrization
authorization.responses.json("header", [Property("anything", ValueFrom(path))])
return authorization


def test_auth_json_path(auth, client, path_and_value):
Expand Down
23 changes: 8 additions & 15 deletions testsuite/tests/kuadrant/authorino/response/test_base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@

import pytest

from testsuite.objects import Property, ValueFrom


@pytest.fixture(scope="module")
def responses():
"""Returns response to be added to the AuthConfig"""
return [
{
"name": "header",
"json": {
"properties": [
{
"name": "anything",
"valueFrom": {"authJSON": "context.request.http.headers.test|@base64:decode"},
}
]
},
}
]
def authorization(authorization):
"""Add response to Authorization"""
authorization.responses.json(
"header", [Property("anything", ValueFrom("context.request.http.headers.test|@base64:decode"))]
)
return authorization


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

import pytest

from testsuite.objects import Property, 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"}]}},
]
def authorization(authorization):
"""Add response to Authorization"""
authorization.responses.json("header", [Property("anything", Value("one"))])
authorization.responses.json("X-Test", [Property("anything", Value("two"))])
return authorization


def test_multiple_responses(auth, client):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

import pytest

from testsuite.objects import Property, Value


@pytest.fixture(scope="module")
def responses():
"""Returns response to be added to the AuthConfig"""
return [{"name": "header", "json": {"properties": [{"name": "anything", "value": "one"}]}}]
def authorization(authorization):
"""Add response to Authorization"""
authorization.responses.json("header", [Property("anything", Value("one"))])
return authorization


def test_simple_response_with(auth, client):
Expand Down
12 changes: 7 additions & 5 deletions testsuite/tests/kuadrant/authorino/response/test_wrapper_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

from testsuite.objects import Property, Value


@pytest.fixture(scope="module", params=["123456789", "standardCharacters", "specialcharacters+*-."])
def header_name(request):
Expand All @@ -11,11 +13,11 @@ def header_name(request):


@pytest.fixture(scope="module")
def responses(header_name):
"""Returns response to be added to the AuthConfig"""
return [
{"name": "header", "wrapperKey": header_name, "json": {"properties": [{"name": "anything", "value": "one"}]}}
]
def authorization(authorization, header_name):
"""Add response to Authorization"""
authorization.responses.remove_all() # delete previous responses due to the parametrization
authorization.responses.json("header", [Property("anything", Value("one"))], wrapper_key=header_name)
return authorization


def test_wrapper_key_with(auth, client, header_name):
Expand Down
Loading

0 comments on commit 5bce78a

Please sign in to comment.