Skip to content

Commit

Permalink
Refactor API key openshift object
Browse files Browse the repository at this point in the history
  • Loading branch information
jsmolar committed Sep 5, 2022
1 parent 0a09533 commit d056c0d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
5 changes: 3 additions & 2 deletions testsuite/httpx/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from httpx import Auth, Request, URL, Response

from testsuite.openshift.objects.api_key import APIKey
from testsuite.rhsso import Client


Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 4 additions & 0 deletions testsuite/openshift/objects/api_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""API Key Secret CR object"""
import base64

from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject
Expand All @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions testsuite/tests/kuadrant/authorino/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d056c0d

Please sign in to comment.