Skip to content

Commit

Permalink
do normalization in path and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alohamora committed Dec 3, 2023
1 parent 8a16cca commit db9d205
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion foca/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def validate_model_path(cls, v: Optional[Path]): # pylint: disable=E0213

model_path = Path(v)
if not model_path.is_absolute():
return str(Path.cwd() / model_path)
return str(model_path.resolve())

return v

Expand Down
37 changes: 37 additions & 0 deletions tests/security/access_control/test_access_control_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

from copy import deepcopy
from unittest import TestCase


from flask import Flask
import mongomock
import os
from pkg_resources import resource_filename
from pymongo import MongoClient
import pytest

Expand All @@ -15,8 +19,10 @@
putPermission
)
from foca.security.access_control.foca_casbin_adapter.adapter import Adapter
from foca.security.access_control.constants import ACCESS_CONTROL_BASE_PATH, DEFAULT_MODEL_FILE
from foca.errors.exceptions import BadRequest, InternalServerError, NotFound
from foca.models.config import (AccessControlConfig, Config, MongoConfig)

from tests.mock_data import (
ACCESS_CONTROL_CONFIG,
MOCK_ID,
Expand Down Expand Up @@ -332,3 +338,34 @@ def test_putPermission_BadRequest(self):
with app.test_request_context(json=""):
with pytest.raises(BadRequest):
putPermission.__wrapped__(id=MOCK_ID)


class TestModelPathResolution(TestCase):
"""Test class for checking access control model input resolution"""

def test_no_model_input(self):
config = AccessControlConfig()
default_model_path = str(
resource_filename(
ACCESS_CONTROL_BASE_PATH, DEFAULT_MODEL_FILE
)
)
assert config.model == default_model_path


def test_relative_path_model_input(self):
file_path = "access_model.conf"
config = AccessControlConfig(model=file_path)
assert config.model == os.path.abspath(file_path)

# testing relative paths with relative parent directory notation used
file_path = "../../test/access_model.conf"
config = AccessControlConfig(model=file_path)
assert config.model == os.path.abspath(file_path)

def test_absolute_path_model_input(self):
file_path = "/test/access_model.conf"
config = AccessControlConfig(model=file_path)
assert config.model == file_path
assert config.model == os.path.abspath(file_path)

0 comments on commit db9d205

Please sign in to comment.