-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from jsmolar/opa
Tests for Open Policy Agent (OPA) Rego policies
- Loading branch information
Showing
6 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
33 changes: 33 additions & 0 deletions
33
testsuite/tests/kuadrant/authorino/authorization/test_opa.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Tests for Open Policy Agent (OPA) Rego policies""" | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def header(): | ||
"""Header used by OPA policy""" | ||
return "opa", "opa-test" | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def authorization(authorization, header): | ||
""" | ||
Creates AuthConfig with API key identity and configures it with OPA policy | ||
that accepts only those requests that contain header correct header | ||
""" | ||
key, value = header | ||
rego_inline = f"allow {{ input.context.request.http.headers.{key} == \"{value}\" }}" | ||
authorization.add_opa_policy("opa", rego_inline) | ||
return authorization | ||
|
||
|
||
def test_authorized_by_opa(client, auth, header): | ||
"""Tests a request that should be authorized by OPA""" | ||
key, value = header | ||
response = client.get("/get", auth=auth, headers={key: value}) | ||
assert response.status_code == 200 | ||
|
||
|
||
def test_rejected_by_opa(client, auth): | ||
"""Tests a request that does not have the correct header for OPA policy""" | ||
response = client.get("/get", auth=auth) | ||
assert response.status_code == 403 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters