Skip to content

Commit

Permalink
Add tests for authorino context
Browse files Browse the repository at this point in the history
  • Loading branch information
jakurban committed Sep 26, 2022
1 parent a97f6f6 commit fe83adc
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test for anonymous identity context"""
import json

import pytest


@pytest.fixture(scope="module")
def authorization(authorization, rhsso):
"""Setup AuthConfig for test"""
authorization.add_oidc_identity("rhsso", rhsso.well_known["issuer"])
authorization.add_anonymous_identity("anonymous")
authorization.add_response({"name": "auth-json", "json": {
"properties": [{"name": "auth", "valueFrom": {"authJSON": "auth"}},
{"name": "context", "valueFrom": {"authJSON": "context"}}]}})
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"]["identity"]["anonymous"]
assert response.status_code == 200
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Test for API key identity context"""
import json

import pytest

from testsuite.httpx.auth import HeaderApiKeyAuth


@pytest.fixture(scope="module")
def api_key(create_api_key, module_label):
"""Creates API key Secret"""
api_key = "api_key_value"
return create_api_key("api-key", module_label, api_key)


@pytest.fixture(scope="module")
def auth(api_key):
"""Valid API Key Auth"""
return HeaderApiKeyAuth(api_key)


@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"}}]}})
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"]["identity"]
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Test for RHSSO identity context"""
import json
import time

import pytest


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


@pytest.fixture(scope="module")
def realm_role(rhsso, blame):
"""Add realm role to rhsso user"""
role_name = blame("realm_role")
role = rhsso.realm.create_realm_role(role_name)
rhsso.realm.assign_realm_role(role, rhsso.user)
return 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"]["identity"]
context = auth_json["context"]
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 context["request"]["http"]["headers"]["authorization"] == 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"]

0 comments on commit fe83adc

Please sign in to comment.