Skip to content

Commit

Permalink
Merge pull request #366 from trepel/plain-identity-test
Browse files Browse the repository at this point in the history
Add plain identity test
  • Loading branch information
azgabur authored Mar 13, 2024
2 parents d012dcf + d7dd431 commit a4bb2ff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Test for plain identity.
Plain identity expects authentication/identity verification to be done in advance and a trusted identity object
being accessible from AuthJson in given path.
If path does not exist or there is a null value there the request is not authorized (401).
If path exists the value is copied over to `auth.identity` of AuthJson and 200 OK is returned
See also https://github.com/Kuadrant/authorino/blob/main/docs/features.md#plain-authenticationplain
There is no authentication/identity verification done, the test just picks HTTP method as if it was an identity object.
"""

import pytest

from testsuite.utils import extract_response

pytestmark = [pytest.mark.authorino]


@pytest.fixture(
scope="module",
params=[
pytest.param(
{"path": "context.nonexistent.path", "code": 401, "identity": None},
id="non-existent AuthJson path to retrieve identity from",
),
pytest.param(
{"path": "context.request.http.method", "code": 200, "identity": "GET"},
id="existing AuthJson path to retrieve identity from",
),
],
)
def plain_identity(request):
"""AuthJson path the identity is retrieved from"""
return request.param


@pytest.fixture(scope="module")
def authorization(authorization, plain_identity):
"""
Setup AuthConfig to retrieve identity from given path
"""
authorization.identity.add_plain("plain", plain_identity["path"])
authorization.responses.add_simple("auth.identity")
return authorization


def test_plain_identity(client, plain_identity):
"""
Setup:
- Create AuthConfig with plain identity object configured to be retrieved from given path from AuthJson
Test:
- Send request
- Assert that response status code is as expected (200 OK / 401 Unauthorized)
- Assert that identity is populated with expected value based on given path (in case of 200 OK)
"""
response = client.get("/get")
assert response.status_code == plain_identity["code"]

identity = extract_response(response)
# identity should be populated with the value retrieved from AuthJson in the specified path
assert identity % None == plain_identity["identity"]
5 changes: 5 additions & 0 deletions testsuite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def extract_response(response, header="Simple", key="data"):
:param response: http response
:return: Extracted value
"""

# Returning None for non-200 responses because the content of such responses might not be a valid JSON
if response.status_code != 200:
return weakget(None)

return weakget(json.loads(response.json()["headers"][header]))[key]


Expand Down

0 comments on commit a4bb2ff

Please sign in to comment.