88from fastapi .testclient import TestClient
99from fmu .settings ._init import init_fmu_directory
1010from fmu .settings .resources .config import Config
11+ from pytest import MonkeyPatch
1112
1213from fmu_settings_api .__main__ import app
1314from fmu_settings_api .config import settings
@@ -32,6 +33,94 @@ def test_get_fmu_invalid_token() -> None:
3233 assert response .json () == {"detail" : "Not authorized" }
3334
3435
36+ def test_get_cwd_fmu_directory_no_permissions (
37+ mock_token : str , fmu_dir_no_permissions : Path , monkeypatch : MonkeyPatch
38+ ) -> None :
39+ """Test 403 returns when lacking permissions somewhere in the path tree."""
40+ ert_model_path = fmu_dir_no_permissions / "project/24.0.3/ert/model"
41+ ert_model_path .mkdir (parents = True )
42+ monkeypatch .chdir (ert_model_path )
43+ response = client .get (
44+ ROUTE ,
45+ headers = {settings .TOKEN_HEADER_NAME : mock_token },
46+ )
47+ assert response .status_code == status .HTTP_403_FORBIDDEN
48+ assert response .json () == {"detail" : "Permission denied locating .fmu" }
49+
50+
51+ def test_get_cwd_fmu_directory_does_not_exist (
52+ mock_token : str , tmp_path : Path , monkeypatch : MonkeyPatch
53+ ) -> None :
54+ """Test 404 returns when .fmu or directory does not exist from the cwd."""
55+ ert_model_path = tmp_path / "project/24.0.3/ert/model"
56+ ert_model_path .mkdir (parents = True )
57+ monkeypatch .chdir (ert_model_path )
58+ response = client .get (
59+ ROUTE ,
60+ headers = {settings .TOKEN_HEADER_NAME : mock_token },
61+ )
62+ assert response .status_code == status .HTTP_404_NOT_FOUND
63+ assert response .json () == {
64+ "detail" : f"No .fmu directory found from { ert_model_path } "
65+ }
66+
67+
68+ def test_get_cwd_fmu_directory_is_not_directory (
69+ mock_token : str , tmp_path : Path , monkeypatch : MonkeyPatch
70+ ) -> None :
71+ """Test 404 returns when .fmu exists but is not a directory.
72+
73+ Although a .fmu file exists, because a .fmu _directory_ is not, it is
74+ treated as a 404.
75+ """
76+ path = tmp_path / ".fmu"
77+ path .touch ()
78+ ert_model_path = tmp_path / "project/24.0.3/ert/model"
79+ ert_model_path .mkdir (parents = True )
80+ monkeypatch .chdir (ert_model_path )
81+
82+ response = client .get (
83+ ROUTE ,
84+ headers = {settings .TOKEN_HEADER_NAME : mock_token },
85+ )
86+ assert response .status_code == status .HTTP_404_NOT_FOUND
87+ assert response .json () == {
88+ "detail" : f"No .fmu directory found from { ert_model_path } "
89+ }
90+
91+
92+ def test_get_cwd_fmu_directory_raises_other_exceptions (mock_token : str ) -> None :
93+ """Test 500 returns if other exceptions are raised."""
94+ with patch (
95+ "fmu_settings_api.v1.routes.fmu.find_nearest_fmu_directory" ,
96+ side_effect = Exception ("foo" ),
97+ ):
98+ response = client .get (
99+ ROUTE ,
100+ headers = {settings .TOKEN_HEADER_NAME : mock_token },
101+ )
102+ assert response .status_code == status .HTTP_500_INTERNAL_SERVER_ERROR
103+ assert response .json () == {"detail" : "foo" }
104+
105+
106+ def test_get_cwd_fmu_directory_exists (
107+ mock_token : str , tmp_path : Path , monkeypatch : MonkeyPatch
108+ ) -> None :
109+ """Test 200 and config returns when .fmu exists."""
110+ fmu_dir = init_fmu_directory (tmp_path )
111+ ert_model_path = tmp_path / "project/24.0.3/ert/model"
112+ ert_model_path .mkdir (parents = True )
113+ monkeypatch .chdir (ert_model_path )
114+
115+ response = client .get (
116+ ROUTE ,
117+ headers = {settings .TOKEN_HEADER_NAME : mock_token },
118+ )
119+ assert response .status_code == status .HTTP_200_OK
120+ config = Config .model_validate (response .json ())
121+ assert fmu_dir .config .load () == config
122+
123+
35124def test_get_fmu_directory_no_permissions (
36125 mock_token : str , fmu_dir_no_permissions : Path
37126) -> None :
0 commit comments