diff --git a/testsuite/httpx/auth.py b/testsuite/httpx/auth.py index dad1cb62..6c82e102 100644 --- a/testsuite/httpx/auth.py +++ b/testsuite/httpx/auth.py @@ -4,22 +4,33 @@ from httpx import Auth, Request, URL, Response +from testsuite.oidc.rhsso import User from testsuite.openshift.objects.api_key import APIKey from testsuite.oidc import Token +TokenType = Union[Token, Callable[[], Token]] + class HttpxOidcClientAuth(Auth): """Auth class for Httpx client for product secured by oidc""" - def __init__(self, token: Union[Token, Callable[[], Token]], location="authorization") -> None: + def __init__(self, token: TokenType, location="authorization", + username: str = None, password: str = None) -> None: self.location = location self._token = token + self.username = username + self.password = password + + @classmethod + def from_user(cls, token: TokenType, user: User, location="authorization"): + """Creates Auth from RHSSO User object""" + return cls(token, location, user.username, user.password) @cached_property def token(self): """Lazily retrieves token from OIDC provider""" if callable(self._token): - return self._token() + return self._token(self.username, self.password) return self._token def _add_credentials(self, request: Request, token): diff --git a/testsuite/tests/kuadrant/authorino/identity/rhsso/test_rhsso_roles.py b/testsuite/tests/kuadrant/authorino/identity/rhsso/test_rhsso_roles.py index c41fcfef..ea6cb51e 100644 --- a/testsuite/tests/kuadrant/authorino/identity/rhsso/test_rhsso_roles.py +++ b/testsuite/tests/kuadrant/authorino/identity/rhsso/test_rhsso_roles.py @@ -22,8 +22,7 @@ def authorization(authorization, realm_role, blame): def test_user_with_role(client, user_with_role, rhsso): """Test request when user does have required role using new user with assigned role""" - auth = HttpxOidcClientAuth(rhsso.get_token(user_with_role.username, user_with_role.password), - "authorization") + auth = HttpxOidcClientAuth.from_user(rhsso.get_token, user_with_role, "authorization") response = client.get("/get", auth=auth) assert response.status_code == 200 diff --git a/testsuite/tests/kuadrant/authorino/metadata/test_uma.py b/testsuite/tests/kuadrant/authorino/metadata/test_uma.py index ef2c10ae..ee884545 100644 --- a/testsuite/tests/kuadrant/authorino/metadata/test_uma.py +++ b/testsuite/tests/kuadrant/authorino/metadata/test_uma.py @@ -36,10 +36,8 @@ def client_secret(create_client_secret, rhsso): @pytest.fixture(scope="module") def auth2(rhsso): """Creates new RHSSO User and returns his authentication object for HTTPX""" - new_username = "newTestUser" - new_password = "p" - rhsso.realm.create_user(new_username, new_password) - return HttpxOidcClientAuth(rhsso.get_token(new_username, new_password)) + user = rhsso.realm.create_user("newTestUser", "p") + return HttpxOidcClientAuth.from_user(rhsso.get_token, user=user) @pytest.fixture(scope="module") diff --git a/testsuite/tests/kuadrant/authorino/metadata/test_user_info.py b/testsuite/tests/kuadrant/authorino/metadata/test_user_info.py index b0ff7a56..36a4bead 100644 --- a/testsuite/tests/kuadrant/authorino/metadata/test_user_info.py +++ b/testsuite/tests/kuadrant/authorino/metadata/test_user_info.py @@ -34,6 +34,6 @@ def test_correct_auth(client, auth): def test_incorrect_auth(client, rhsso, user2): """Updates RHSSO user email address and tests incorrect auth""" - auth = HttpxOidcClientAuth(rhsso.get_token(user2.username, user2.password), "authorization") + auth = HttpxOidcClientAuth.from_user(rhsso.get_token, user2, "authorization") response = client.get("get", auth=auth) assert response.status_code == 403