Skip to content

Commit

Permalink
i
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaran committed Dec 4, 2023
1 parent 1659012 commit 4947850
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 3 deletions.
4 changes: 2 additions & 2 deletions firetail/decorators/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import functools
import logging

import ipdb
from flask import request
from jsonschema import ValidationError

Expand Down Expand Up @@ -84,9 +85,8 @@ def validate_response_authz(self, response_definition, data):
"No Authz data returned from our app layer - flask must populate IDs to compare " "in Authz"
)
# use spec data to get from the request data.from and compare to the data returned.
auth_data = request_authz_data[request_data_lookup]
resp_obj_data = self.extract_item(data, response_data_loookup)
if auth_data == resp_obj_data:
if request_authz_data == resp_obj_data:
return True
raise AuthzFailed()

Expand Down
14 changes: 14 additions & 0 deletions tests/fakeapi/hello/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,20 @@ def get_user():
return {"user_id": 7, "name": "max"}


def get_user_authz():
request.firetail_authz = 7
return {"user_id": 7, "name": "max"}


def get_user_authz_fails():
request.firetail_authz = 8
return {"user_id": 7, "name": "max"}


def get_user_authz_not_set():
return {"user_id": 7, "name": "max"}


def get_user_with_password():
return {"user_id": 7, "name": "max", "password": "5678"}

Expand Down
41 changes: 41 additions & 0 deletions tests/fixtures/json_validation/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,47 @@ paths:
responses:
200:
description: Success
/authzEnd:
get:
operationId: fakeapi.hello.get_user_authz
responses:
200:
description: Success
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
content:
application/json:
schema:
$ref: '#/components/schemas/User'
/authzEndFails:
get:
operationId: fakeapi.hello.get_user_authz_fails
responses:
200:
description: Success
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
content:
application/json:
schema:
$ref: '#/components/schemas/User'

/authzEndNotSet:
get:
operationId: fakeapi.hello.get_user_authz_not_set
responses:
200:
description: Success
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
content:
application/json:
schema:
$ref: '#/components/schemas/User'


/user:
get:
Expand Down
33 changes: 33 additions & 0 deletions tests/fixtures/json_validation/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ paths:
description: User object
schema:
$ref: '#/definitions/User'
/authzEnd:
get:
operationId: fakeapi.hello.get_user_authz
responses:
200:
description: User object
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
schema:
$ref: '#/definitions/User'
/authzEndFails:
get:
operationId: fakeapi.hello.get_user_authz_fails
responses:
200:
description: User object
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
schema:
$ref: '#/definitions/User'
/authzEndNotSet:
get:
operationId: fakeapi.hello.get_user_authz_not_set
responses:
200:
description: User object
x-firetail-authz:
authenticated-principal-path: "user_id"
resource-authorized-principal-path: "user_id"
schema:
$ref: '#/definitions/User'
/user_with_password:
get:
operationId: fakeapi.hello.get_user_with_password
Expand Down
33 changes: 32 additions & 1 deletion tests/test_json_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

import pytest
from conftest import build_app_from_fixture
from jsonschema.validators import _utils, extend

from firetail import App
from firetail.decorators.validation import RequestBodyValidator
from firetail.json_schema import Draft4RequestValidator
from firetail.spec import Specification
from jsonschema.validators import _utils, extend

SPECS = ["swagger.yaml", "openapi.yaml"]

Expand Down Expand Up @@ -46,6 +47,36 @@ def __init__(self, *args, **kwargs):
assert res.status_code == 400


@pytest.mark.parametrize("spec", SPECS)
def test_validator_map_ft_authz_success(json_validation_spec_dir, spec):
app = App(__name__, specification_dir=json_validation_spec_dir)
app.add_api(spec, validate_responses=True)
app_client = app.app.test_client()

res = app_client.get("/v1.0/authzEnd") # type: flask.Response
assert res.status_code == 200


@pytest.mark.parametrize("spec", SPECS)
def test_validator_map_ft_authz_fail(json_validation_spec_dir, spec):
app = App(__name__, specification_dir=json_validation_spec_dir)
app.add_api(spec, validate_responses=True)
app_client = app.app.test_client()

res = app_client.get("/v1.0/authzEndFails") # type: flask.Response
assert res.status_code == 401 # unauthorized because of authz


@pytest.mark.parametrize("spec", SPECS)
def test_validator_map_ft_authz_not_set(json_validation_spec_dir, spec):
app = App(__name__, specification_dir=json_validation_spec_dir)
app.add_api(spec, validate_responses=True)
app_client = app.app.test_client()

res = app_client.get("/v1.0/authzEndFails") # type: flask.Response
assert res.status_code == 401 # unauthorized because of authz


@pytest.mark.parametrize("spec", SPECS)
def test_readonly(json_validation_spec_dir, spec):
app = build_app_from_fixture(json_validation_spec_dir, spec, validate_responses=True)
Expand Down

0 comments on commit 4947850

Please sign in to comment.