Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom deny responses test #251

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions testsuite/tests/kuadrant/authorino/response/test_deny_with.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Test for custom deny responses."""
from json import loads
import pytest

from testsuite.objects import Value, ValueFrom, Rule

HEADERS = {
"x-string-header": Value("abc"),
"x-int-header": Value(16),
"x-list-header": Value([1, 2, 3]),
"x-dict-header": Value({"anything": "something"}),
"x-dynamic-header": ValueFrom("context.request.http.path"),
}

TESTING_PATH = "/deny"


@pytest.fixture(scope="module")
def authorization(authorization):
"""Set custom deny responses and auth rule with only allowed path '/allow'"""
authorization.responses.set_deny_with(
"unauthenticated",
code=333,
headers=HEADERS,
message=Value("Unauthenticated message"),
body=Value("You are unauthenticated."),
)
authorization.responses.set_deny_with(
"unauthorized",
code=444,
headers=HEADERS,
message=ValueFrom("My path is: " + "{context.request.http.path}"),
body=ValueFrom("You are not authorized to access path: " + "{context.request.http.path}"),
)
# Authorize only when url path is "/allow"
authorization.authorization.add_auth_rules("Whitelist", [Rule("context.request.http.path", "eq", "/allow")])
return authorization


def assert_headers(response):
"""Check deny headers with normalization between HTTP (JSON) strings and Python objects."""
assert response.headers["x-string-header"] == HEADERS["x-string-header"].value
assert loads(response.headers["x-int-header"]) == HEADERS["x-int-header"].value
assert loads(response.headers["x-list-header"]) == HEADERS["x-list-header"].value
assert loads(response.headers["x-dict-header"]) == HEADERS["x-dict-header"].value
assert response.headers["x-dynamic-header"] == TESTING_PATH


def test_unauthenticated(client):
"""Test when no auth is passed results in custom unauthenticated response."""
response = client.get(TESTING_PATH, auth=None)
assert response.status_code == 333
assert_headers(response)
assert response.headers["x-ext-auth-reason"] == "Unauthenticated message"
assert response.content.decode() == "You are unauthenticated."


def test_unauthorized(client, auth):
"""Test when not allowed path is passed results in custom unauthorized response."""
response = client.get(TESTING_PATH, auth=auth)
assert response.status_code == 444
assert_headers(response)
assert response.headers["x-ext-auth-reason"] == f"My path is: {TESTING_PATH}"
assert response.content.decode() == f"You are not authorized to access path: {TESTING_PATH}"