diff --git a/foca/models/config.py b/foca/models/config.py index c8ac7fc..6b41bfa 100644 --- a/foca/models/config.py +++ b/foca/models/config.py @@ -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 diff --git a/tests/security/access_control/test_access_control_server.py b/tests/security/access_control/test_access_control_server.py index 6bd8d76..e6cb333 100644 --- a/tests/security/access_control/test_access_control_server.py +++ b/tests/security/access_control/test_access_control_server.py @@ -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 @@ -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, @@ -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) +