88
99import contextlib
1010from collections .abc import AsyncIterator
11+ from unittest .mock import MagicMock
1112
1213import pytest
1314from aiohttp .test_utils import TestClient
@@ -44,18 +45,16 @@ def user_role() -> UserRole:
4445
4546
4647@contextlib .asynccontextmanager
47- async def _client_session_with_user (
48+ async def _switch_client_session_to (
4849 client : TestClient , user : UserInfoDict
4950) -> AsyncIterator [TestClient ]:
5051 assert client .app
5152
52- url = client .app .router ["logout" ].url_for ()
53- resp = await client .post (f"{ url } " )
54- await assert_status (resp , status .HTTP_200_OK )
53+ await client .post (f'{ client .app .router ["auth_logout" ].url_for ()} ' )
54+ # sometimes 4xx if user already logged out. Ignore
5555
56- url = client .app .router ["login" ].url_for ()
5756 resp = await client .post (
58- f" { url } " ,
57+ f' { client . app . router [ "auth_login" ]. url_for () } ' ,
5958 json = {
6059 "email" : user ["email" ],
6160 "password" : user ["raw_password" ],
@@ -65,8 +64,7 @@ async def _client_session_with_user(
6564
6665 yield client
6766
68- url = client .app .router ["logout" ].url_for ()
69- resp = await client .post (f"{ url } " )
67+ resp = await client .post (f'{ client .app .router ["auth_logout" ].url_for ()} ' )
7068 await assert_status (resp , status .HTTP_200_OK )
7169
7270
@@ -78,6 +76,7 @@ async def test_trash_service__delete_expired_trash(
7876 other_user_project : ProjectDict ,
7977 mocked_catalog : None ,
8078 mocked_director_v2 : None ,
79+ mocked_dynamic_services_interface : dict [str , MagicMock ],
8180):
8281 assert client .app
8382 assert logged_user ["id" ] != other_user ["id" ]
@@ -117,7 +116,7 @@ async def test_trash_service__delete_expired_trash(
117116 await assert_status (resp , status .HTTP_404_NOT_FOUND )
118117
119118 # ASSERT: other_user tries to get the project and expects 404
120- async with _client_session_with_user (client , other_user ):
119+ async with _switch_client_session_to (client , other_user ):
121120 resp = await client .get (f"/v0/projects/{ other_user_project_id } " )
122121 await assert_status (resp , status .HTTP_404_NOT_FOUND )
123122
@@ -130,64 +129,62 @@ async def test_trash_nested_folders_and_projects(
130129 other_user_project : ProjectDict ,
131130 mocked_catalog : None ,
132131 mocked_director_v2 : None ,
132+ mocked_dynamic_services_interface : dict [str , MagicMock ],
133133):
134134 assert client .app
135135 assert logged_user ["id" ] != other_user ["id" ]
136136
137- # Create folders hierarchy for logged_user
138- resp = await client .post ("/v0/folders" , json = {"name" : "Root Folder" })
139- data , _ = await assert_status (resp , status .HTTP_201_CREATED )
140- logged_user_root_folder = data
137+ async with _switch_client_session_to (client , logged_user ):
138+ # CREATE folders hierarchy for logged_user
139+ resp = await client .post ("/v0/folders" , json = {"name" : "Root Folder" })
140+ data , _ = await assert_status (resp , status .HTTP_201_CREATED )
141+ logged_user_root_folder = data
141142
142- resp = await client .post (
143- "/v0/folders" ,
144- json = {
145- "name" : "Sub Folder" ,
146- "parentFolderId" : logged_user_root_folder ["folderId" ],
147- },
148- )
149- data , _ = await assert_status (resp , status .HTTP_201_CREATED )
150- logged_user_sub_folder = data
143+ resp = await client .post (
144+ "/v0/folders" ,
145+ json = {
146+ "name" : "Sub Folder" ,
147+ "parentFolderId" : logged_user_root_folder ["folderId" ],
148+ },
149+ )
150+ data , _ = await assert_status (resp , status .HTTP_201_CREATED )
151+ logged_user_sub_folder = data
151152
152- # Move project to subfolder
153- resp = await client .put (
154- f"/v0/projects/{ user_project ['uuid' ]} /folders/{ logged_user_sub_folder ['folderId' ]} "
155- )
156- await assert_status (resp , status .HTTP_204_NO_CONTENT )
153+ # MOVE project to subfolder
154+ resp = await client .put (
155+ f"/v0/projects/{ user_project ['uuid' ]} /folders/{ logged_user_sub_folder ['folderId' ]} "
156+ )
157+ await assert_status (resp , status .HTTP_204_NO_CONTENT )
157158
158- # Trash root folders
159- resp = await client .post (f"/v0/folders/{ logged_user_root_folder ['folderId' ]} :trash" )
160- await assert_status (resp , status .HTTP_204_NO_CONTENT )
159+ # TRASH root folders
160+ resp = await client .post (
161+ f"/v0/folders/{ logged_user_root_folder ['folderId' ]} :trash"
162+ )
163+ await assert_status (resp , status .HTTP_204_NO_CONTENT )
161164
162- # Create folders hierarchy for other_user
163- async with _client_session_with_user ( client , other_user ):
165+ async with _switch_client_session_to ( client , other_user ):
166+ # CREATE folders hierarchy for other_user
164167 resp = await client .post ("/v0/folders" , json = {"name" : "Root Folder" })
165168 data , _ = await assert_status (resp , status .HTTP_201_CREATED )
166169 other_user_root_folder = data
167170
168171 resp = await client .post (
169172 "/v0/folders" ,
170173 json = {
171- "name" : "Sub Folder" ,
174+ "name" : "Sub Folder (other) " ,
172175 "parentFolderId" : other_user_root_folder ["folderId" ],
173176 },
174177 )
175178 data , _ = await assert_status (resp , status .HTTP_201_CREATED )
176179 other_user_sub_folder = data
177180
178- # Move project to subfolder
181+ # MOVE project to subfolder
179182 resp = await client .put (
180183 f"/v0/projects/{ other_user_project ['uuid' ]} /folders/{ other_user_sub_folder ['folderId' ]} "
181184 )
182185 await assert_status (resp , status .HTTP_204_NO_CONTENT )
183186
184- # Trash root folders
185- resp = await client .post (
186- f"/v0/folders/{ logged_user_root_folder ['folderId' ]} :trash"
187- )
188- await assert_status (resp , status .HTTP_204_NO_CONTENT )
189-
190- async with _client_session_with_user (client , other_user ):
187+ # TRASH root folders
191188 resp = await client .post (
192189 f"/v0/folders/{ other_user_root_folder ['folderId' ]} :trash"
193190 )
@@ -196,24 +193,24 @@ async def test_trash_nested_folders_and_projects(
196193 # UNDER TEST
197194 await trash_service .safe_delete_expired_trash_as_admin (client .app )
198195
199- async with _client_session_with_user (client , logged_user ):
196+ async with _switch_client_session_to (client , logged_user ):
200197 # Verify logged_user's resources are gone
201198 resp = await client .get (f"/v0/folders/{ logged_user_root_folder ['folderId' ]} " )
202- await assert_status (resp , status .HTTP_404_NOT_FOUND )
199+ await assert_status (resp , status .HTTP_403_FORBIDDEN )
203200
204201 resp = await client .get (f"/v0/folders/{ logged_user_sub_folder ['folderId' ]} " )
205- await assert_status (resp , status .HTTP_404_NOT_FOUND )
202+ await assert_status (resp , status .HTTP_403_FORBIDDEN )
206203
207204 resp = await client .get (f"/v0/projects/{ user_project ['uuid' ]} " )
208205 await assert_status (resp , status .HTTP_404_NOT_FOUND )
209206
210207 # Verify other_user's resources are gone
211- async with _client_session_with_user (client , other_user ):
208+ async with _switch_client_session_to (client , other_user ):
212209 resp = await client .get (f"/v0/folders/{ other_user_root_folder ['folderId' ]} " )
213- await assert_status (resp , status .HTTP_404_NOT_FOUND )
210+ await assert_status (resp , status .HTTP_403_FORBIDDEN )
214211
215212 resp = await client .get (f"/v0/folders/{ other_user_sub_folder ['folderId' ]} " )
216- await assert_status (resp , status .HTTP_404_NOT_FOUND )
213+ await assert_status (resp , status .HTTP_403_FORBIDDEN )
217214
218215 resp = await client .get (f"/v0/projects/{ other_user_project ['uuid' ]} " )
219216 await assert_status (resp , status .HTTP_404_NOT_FOUND )
0 commit comments