From d056c0d6f4f82ea606abea8873f233872a6ac807 Mon Sep 17 00:00:00 2001 From: jsmolar Date: Fri, 26 Aug 2022 17:07:35 +0200 Subject: [PATCH] Refactor API key openshift object --- testsuite/httpx/auth.py | 5 +++-- testsuite/openshift/objects/api_key.py | 4 ++++ testsuite/tests/kuadrant/authorino/conftest.py | 4 ++-- .../kuadrant/authorino/identity/api_key/conftest.py | 8 +++----- .../authorino/identity/api_key/test_match_label.py | 10 +++++----- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/testsuite/httpx/auth.py b/testsuite/httpx/auth.py index 8e380c03..84f0a7e3 100644 --- a/testsuite/httpx/auth.py +++ b/testsuite/httpx/auth.py @@ -4,6 +4,7 @@ from httpx import Auth, Request, URL, Response +from testsuite.openshift.objects.api_key import APIKey from testsuite.rhsso import Client @@ -39,9 +40,9 @@ def auth_flow(self, request: Request) -> Generator[Request, Response, None]: class HeaderApiKeyAuth(Auth): """Auth class for authentication with API key""" - def __init__(self, api_key: str, prefix: str = "APIKEY") -> None: + def __init__(self, api_key: APIKey, prefix: str = "APIKEY") -> None: super().__init__() - self.api_key = api_key + self.api_key = str(api_key) self.prefix = prefix def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]: diff --git a/testsuite/openshift/objects/api_key.py b/testsuite/openshift/objects/api_key.py index b35dbfc6..15b20cfe 100644 --- a/testsuite/openshift/objects/api_key.py +++ b/testsuite/openshift/objects/api_key.py @@ -1,4 +1,5 @@ """API Key Secret CR object""" +import base64 from testsuite.openshift.client import OpenShiftClient from testsuite.openshift.objects import OpenShiftObject @@ -7,6 +8,9 @@ class APIKey(OpenShiftObject): """Represents API Key Secret CR for Authorino""" + def __str__(self): + return base64.b64decode(self.model.data["api_key"]).decode("utf-8") + @classmethod def create_instance(cls, openshift: OpenShiftClient, name, label, api_key): """Creates base instance""" diff --git a/testsuite/tests/kuadrant/authorino/conftest.py b/testsuite/tests/kuadrant/authorino/conftest.py index fe8d36e9..c8830abc 100644 --- a/testsuite/tests/kuadrant/authorino/conftest.py +++ b/testsuite/tests/kuadrant/authorino/conftest.py @@ -61,7 +61,7 @@ def create_api_key(blame, request, openshift): def _create_secret(name, label_selector, api_key, ocp: OpenShiftClient = openshift): secret_name = blame(name) secret = APIKey.create_instance(ocp, secret_name, label_selector, api_key) - request.addfinalizer(secret.delete) + request.addfinalizer(lambda: secret.delete(ignore_not_found=True)) secret.commit() - return secret_name + return secret return _create_secret diff --git a/testsuite/tests/kuadrant/authorino/identity/api_key/conftest.py b/testsuite/tests/kuadrant/authorino/identity/api_key/conftest.py index 3ecd1422..6b64b712 100644 --- a/testsuite/tests/kuadrant/authorino/identity/api_key/conftest.py +++ b/testsuite/tests/kuadrant/authorino/identity/api_key/conftest.py @@ -8,8 +8,7 @@ def api_key(create_api_key, module_label): """Creates API key Secret""" api_key = "api_key_value" - create_api_key("api-key", module_label, api_key) - return api_key + return create_api_key("api-key", module_label, api_key) @pytest.fixture(scope="module") @@ -28,11 +27,10 @@ def invalid_label_selector(): def invalid_api_key(create_api_key, invalid_label_selector): """Creates API key Secret with label that does not match any of the labelSelectors defined by AuthConfig""" api_key = "invalid_api_key" - create_api_key("invalid-api-key", invalid_label_selector, api_key) - return api_key + return create_api_key("invalid-api-key", invalid_label_selector, api_key) @pytest.fixture(scope="module") def invalid_auth(invalid_api_key): - """Invalid key Auth""" + """Invalid API key Auth""" return HeaderApiKeyAuth(invalid_api_key) diff --git a/testsuite/tests/kuadrant/authorino/identity/api_key/test_match_label.py b/testsuite/tests/kuadrant/authorino/identity/api_key/test_match_label.py index 19405f81..50706915 100644 --- a/testsuite/tests/kuadrant/authorino/identity/api_key/test_match_label.py +++ b/testsuite/tests/kuadrant/authorino/identity/api_key/test_match_label.py @@ -21,13 +21,13 @@ def test_no_auth(client): assert response.status_code == 401 -def test_invalid_api_key(client, invalid_auth): - """Tests request with wrong API key""" - response = client.get("/get", auth=invalid_auth) +def test_invalid_api_key(client): + """Tests request with wrong invalid API key""" + response = client.get("/get", headers={"Authorization": "APIKEY invalid_api_key_string"}) assert response.status_code == 401 -def test_invalid_api_key_secret(client, invalid_api_key): +def test_invalid_api_key_secret(client, invalid_auth): """Tests request that uses API key secret that is wrongly labeled""" - response = client.get("/get", headers={"Authorization": f"APIKEY {invalid_api_key}"}) + response = client.get("/get", auth=invalid_auth) assert response.status_code == 401