88PREFIX = "/auth/current-user/"
99
1010
11- async def test_get_current_user (
12- client , registered_client , registered_superuser_client
13- ):
11+ async def test_get_current_user (client , MockCurrentUser ):
1412 # Anonymous user
1513 res = await client .get (PREFIX )
1614 debug (res .json ())
1715 assert res .status_code == 401
1816
1917 # Registered non-superuser user
20- res = await registered_client .get (PREFIX )
21- debug (res .json ())
22- assert res .status_code == 200
23- assert not res .json ()["is_superuser" ]
24- assert res .json ()["oauth_accounts" ] == []
18+ async with MockCurrentUser ():
19+ res = await client .get (PREFIX )
20+ debug (res .json ())
21+ assert res .status_code == 200
22+ assert not res .json ()["is_superuser" ]
23+ assert res .json ()["oauth_accounts" ] == []
2524
2625 # Registered superuser
27- res = await registered_superuser_client .get (PREFIX )
28- debug (res .json ())
29- assert res .status_code == 200
30- assert res .json ()["is_superuser" ]
26+ async with MockCurrentUser (user_args = dict (is_superuser = True )):
27+ res = await client .get (PREFIX )
28+ debug (res .json ())
29+ assert res .status_code == 200
30+ assert res .json ()["is_superuser" ]
3131
3232
3333async def test_get_current_user_group_ids_names_order (
@@ -83,38 +83,29 @@ async def test_get_current_user_group_ids_names_order(
8383 ]
8484
8585
86- async def test_patch_current_user_response (registered_client ):
87- res = await registered_client .get (f"{ PREFIX } ?group_ids_names=True" )
88- pre_patch_user = res .json ()
89-
90- # Successful API call with empty payload
91- res = await registered_client .patch (PREFIX , json = {})
92- assert res .status_code == 200
93- assert res .json () == pre_patch_user
94-
86+ async def test_patch_current_user_response (client , MockCurrentUser ):
87+ async with MockCurrentUser ():
88+ res = await client .get (f"{ PREFIX } ?group_ids_names=True" )
89+ pre_patch_user = res .json ()
9590
96- async def test_patch_current_user_no_extra (registered_client ):
97- """
98- Test that the PATCH-current-user endpoint fails when extra attributes are
99- provided.
100- """
101- res = await registered_client .patch (PREFIX , json = {})
102- assert res .status_code == 200
103- res = await registered_client .patch (PREFIX , json = {"foo" : "bar" })
104- assert res .status_code == 422
91+ # Successful API call with empty payload
92+ res = await client .patch (PREFIX , json = {})
93+ assert res .status_code == 200
94+ assert res .json () == pre_patch_user
10595
10696
107- async def test_patch_current_user_password_fails (registered_client , client ):
97+ async def test_patch_current_user_password_fails (MockCurrentUser , client ):
10898 """
10999 Users cannot edit their own password.
110100 """
111- res = await registered_client .patch (PREFIX , json = {"password" : "something" })
112- assert res .status_code == 422
101+ async with MockCurrentUser ():
102+ res = await client .patch (PREFIX , json = {"password" : "something" })
103+ assert res .status_code == 422
113104
114105
115106async def test_get_current_user_allowed_viewer_paths (
116- registered_client ,
117- registered_superuser_client ,
107+ MockCurrentUser ,
108+ client ,
118109 override_settings_factory ,
119110 slurm_sudo_resource_profile_db ,
120111):
@@ -124,89 +115,89 @@ async def test_get_current_user_allowed_viewer_paths(
124115 )
125116
126117 # Check that a vanilla user has no viewer_paths
127- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
128- assert res .status_code == 200
129- assert res .json () == [PROJECT_DIR_PLACEHOLDER ]
130-
131- # Find current-user ID
132- res = await registered_client .get (f"{ PREFIX } " )
133- assert res .status_code == 200
134- user_id = res .json ()["id" ]
135-
136- # Add one group to this user
137- res = await registered_superuser_client .post (
138- "/auth/group/" , json = dict (name = "group1" , viewer_paths = ["/a" , "/b" ])
139- )
140- assert res .status_code == 201
141- group1_id = res .json ()["id" ]
142-
143- # Add user to group1
144- res = await registered_superuser_client .post (
145- f"/auth/group/{ group1_id } /add-user/{ user_id } /"
146- )
147- assert res .status_code == 200
148-
149- # Check current-user viewer-paths again
150- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
151- assert res .status_code == 200
152- assert set (res .json ()) == {"/a" , "/b" , PROJECT_DIR_PLACEHOLDER }
118+ async with MockCurrentUser () as user :
119+ user_id = user .id
120+
121+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
122+ assert res .status_code == 200
123+ assert set (res .json ()) == {user .project_dir }
124+
125+ # Add one group to this user
126+ async with MockCurrentUser (
127+ user_kwargs = dict (is_superuser = True )
128+ ) as superuser :
129+ superuser_id = superuser .id
130+ res = await client .post (
131+ "/auth/group/" ,
132+ json = dict (name = "group1" , viewer_paths = ["/a" , "/b" ]),
133+ )
134+ assert res .status_code == 201
135+ group1_id = res .json ()["id" ]
136+
137+ # Add user to group1
138+ res = await client .post (f"/auth/group/{ group1_id } /add-user/{ user_id } /" )
139+ assert res .status_code == 200
140+
141+ # Check current-user viewer-paths again
142+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
143+ assert res .status_code == 200
144+ assert set (res .json ()) == {"/a" , "/b" , PROJECT_DIR_PLACEHOLDER }
153145
154146 # Add one group to this user
155- res = await registered_superuser_client .post (
156- "/auth/group/" , json = dict (name = "group2" , viewer_paths = ["/a" , "/c" ])
157- )
158- assert res .status_code == 201
159- group2_id = res .json ()["id" ]
147+ async with MockCurrentUser (user_kwargs = dict (id = superuser_id )):
148+ res = await client .post (
149+ "/auth/group/" , json = dict (name = "group2" , viewer_paths = ["/a" , "/c" ])
150+ )
151+ assert res .status_code == 201
152+ group2_id = res .json ()["id" ]
160153
161- # Add user to group2
162- res = await registered_superuser_client .post (
163- f"/auth/group/{ group2_id } /add-user/{ user_id } /"
164- )
165- assert res .status_code == 200
154+ # Add user to group2
155+ res = await client .post (f"/auth/group/{ group2_id } /add-user/{ user_id } /" )
156+ assert res .status_code == 200
166157
167- # Update user, defining project_dir
168- res = await registered_superuser_client .patch (
169- f"/auth/users/{ user_id } /" ,
170- json = dict (project_dir = "/path/to/project_dir" ),
171- )
172- assert res .status_code == 200
158+ # Update user, defining project_dir
159+ res = await client .patch (
160+ f"/auth/users/{ user_id } /" ,
161+ json = dict (project_dir = PROJECT_DIR_PLACEHOLDER ),
162+ )
163+ assert res .status_code == 200
173164
174165 # Check that project_dir is used by "viewer-paths" auth scheme
175- override_settings_factory (
176- FRACTAL_VIEWER_AUTHORIZATION_SCHEME = ViewerAuthScheme .VIEWER_PATHS
177- )
178- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
179- assert res .status_code == 200
180- assert set (res .json ()) == {"/path/to/project_dir" , "/a" , "/b" , "/c" }
166+ async with MockCurrentUser (user_kwargs = dict (id = user_id )):
167+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
168+ assert res .status_code == 200
169+ assert set (res .json ()) == {PROJECT_DIR_PLACEHOLDER , "/a" , "/b" , "/c" }
181170
182171 # Test with "users-folders" scheme
183- override_settings_factory (FRACTAL_VIEWER_BASE_FOLDER = "/path/to/base" )
184172 override_settings_factory (
185- FRACTAL_VIEWER_AUTHORIZATION_SCHEME = ViewerAuthScheme .USERS_FOLDERS
173+ FRACTAL_VIEWER_AUTHORIZATION_SCHEME = ViewerAuthScheme .USERS_FOLDERS ,
174+ FRACTAL_VIEWER_BASE_FOLDER = "/path/to/base" ,
186175 )
187- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
188- assert res .status_code == 200
189- assert set (res .json ()) == {"/path/to/project_dir" }
176+ async with MockCurrentUser (user_kwargs = dict (profile_id = None )):
177+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
178+ assert res .status_code == 200
179+ assert set (res .json ()) == {PROJECT_DIR_PLACEHOLDER }
190180
191- # Update user profile adding the slurm_user
181+ # Test that user dir is added when using "users-folders" scheme
192182 resource , profile = slurm_sudo_resource_profile_db
193- res = await registered_superuser_client . patch (
194- f"/auth/users/ { user_id } /" , json = dict (profile_id = profile . id )
195- )
196- assert res . status_code == 200
197-
198- # # Test that user dir is added when using "users-folders" scheme
199- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
200- assert res .status_code == 200
201- assert set (res .json ()) == {
202- "/path/to/project_dir" ,
203- f"/path/to/base/{ profile .username } " ,
204- }
183+ async with MockCurrentUser (
184+ user_kwargs = dict (
185+ id = user_id ,
186+ profile_id = profile . id ,
187+ )
188+ ):
189+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
190+ assert res .status_code == 200
191+ assert set (res .json ()) == {
192+ PROJECT_DIR_PLACEHOLDER ,
193+ f"/path/to/base/{ profile .username } " ,
194+ }
205195
206196 # Verify that scheme "none" returns an empty list
207197 override_settings_factory (
208198 FRACTAL_VIEWER_AUTHORIZATION_SCHEME = ViewerAuthScheme .NONE
209199 )
210- res = await registered_client .get (f"{ PREFIX } allowed-viewer-paths/" )
211- assert res .status_code == 200
212- assert res .json () == []
200+ async with MockCurrentUser (user_kwargs = dict (id = user_id )):
201+ res = await client .get (f"{ PREFIX } allowed-viewer-paths/" )
202+ assert res .status_code == 200
203+ assert res .json () == []
0 commit comments