Skip to content

Commit

Permalink
Merge pull request #114 from JaurbanRH/context
Browse files Browse the repository at this point in the history
Add tests for authorino context
  • Loading branch information
pehala authored Oct 5, 2022
2 parents 0560b8b + f24c8d8 commit 026b4fb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Test for anonymous identity context"""
import json

import pytest


@pytest.fixture(scope="module")
def authorization(authorization):
"""Setup AuthConfig for test"""
authorization.add_anonymous_identity("anonymous")
authorization.add_response({"name": "auth-json", "json": {
"properties": [{"name": "auth", "valueFrom": {"authJSON": "auth.identity.anonymous"}}]}})
return authorization


def test_anonymous_context(client):
"""
Test:
- Make request without authentication
- Assert that response has the right information in context
"""
response = client.get("/get")
assert json.loads(response.json()["headers"]["Auth-Json"])["auth"]
assert response.status_code == 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Test for API key identity context"""
import json

import pytest


@pytest.fixture(scope="module")
def authorization(authorization, module_label):
"""Setup AuthConfig for test"""
authorization.add_api_key_identity("api_key", match_label=module_label)
authorization.add_response({"name": "auth-json", "json": {
"properties": [{"name": "auth", "valueFrom": {"authJSON": "auth.identity"}}]}})
return authorization


def tests_api_key_context(client, auth, api_key, module_label, testconfig):
"""
Test:
- Make request with API key authentication
- Assert that response has the right information in context
"""
response = client.get("get", auth=auth)
assert response.status_code == 200
identity = json.loads(response.json()["headers"]["Auth-Json"])["auth"]
assert identity['data']['api_key'] == api_key.model.data.api_key
assert identity["metadata"]["namespace"] == testconfig["openshift"].project
assert identity["metadata"]["labels"]["group"] == module_label
6 changes: 6 additions & 0 deletions testsuite/tests/kuadrant/authorino/identity/rhsso/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ def authorization(authorization, rhsso):
"""Add RHSSO identity to AuthConfig"""
authorization.add_oidc_identity("rhsso", rhsso.well_known["issuer"])
return authorization


@pytest.fixture(scope="module")
def realm_role(rhsso, blame):
"""Creates new realm role"""
return rhsso.realm.create_realm_role(blame("role"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Test for RHSSO identity context"""
import json
import time

import pytest


@pytest.fixture(scope="module")
def authorization(authorization):
"""Setup AuthConfig for test"""
authorization.add_response({"name": "auth-json", "json": {
"properties": [{"name": "auth", "valueFrom": {"authJSON": "auth.identity"}},
{"name": "context", "valueFrom": {"authJSON": "context.request.http.headers.authorization"}}]}})
return authorization


@pytest.fixture(scope="module")
def realm_role(rhsso, realm_role):
"""Add realm role to rhsso user"""
rhsso.realm.assign_realm_role(realm_role, rhsso.user)
return realm_role


def tests_rhsso_context(client, auth, rhsso, realm_role):
"""
Test:
- Make request with RHSSO authentication
- Assert that response has the right information in context
"""
response = client.get("get", auth=auth)
assert response.status_code == 200
auth_json = json.loads(response.json()["headers"]["Auth-Json"])
identity = auth_json["auth"]
now = time.time()
assert rhsso.well_known["issuer"] == identity["iss"]
assert identity["azp"] == rhsso.client_name
assert float(identity["exp"]) > now
assert float(identity["iat"]) <= now
assert auth_json["context"] == f"Bearer {auth.token.access_token}"
assert realm_role["name"] in identity["realm_access"]["roles"]
assert identity['email'] == rhsso.client.admin.get_user(rhsso.user)["email"]
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ def user_with_role(rhsso, realm_role, blame):
return {"id": user_id, "username": username, "password": password}


@pytest.fixture(scope="module")
def realm_role(rhsso, blame):
"""Creates new realm role"""
return rhsso.realm.create_realm_role(blame("role"))


@pytest.fixture(scope="module")
def authorization(authorization, realm_role, blame):
"""Adds rule, that requires user to be part of realm_role to be allowed access."""
Expand Down

0 comments on commit 026b4fb

Please sign in to comment.