Skip to content

Commit d2fbd04

Browse files
committed
helpers
1 parent e72b5d5 commit d2fbd04

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

packages/pytest-simcore/src/pytest_simcore/helpers/webserver_login.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import contextlib
12
import re
3+
from collections.abc import AsyncIterator
24
from datetime import datetime
35
from typing import Any, TypedDict
46

@@ -186,6 +188,30 @@ async def __aexit__(self, *args):
186188
return await super().__aexit__(*args)
187189

188190

191+
@contextlib.asynccontextmanager
192+
async def switch_client_session_to(
193+
client: TestClient, user: UserInfoDict
194+
) -> AsyncIterator[TestClient]:
195+
assert client.app
196+
197+
await client.post(f'{client.app.router["auth_logout"].url_for()}')
198+
# sometimes 4xx if user already logged out. Ignore
199+
200+
resp = await client.post(
201+
f'{client.app.router["auth_login"].url_for()}',
202+
json={
203+
"email": user["email"],
204+
"password": user["raw_password"],
205+
},
206+
)
207+
await assert_status(resp, status.HTTP_200_OK)
208+
209+
yield client
210+
211+
resp = await client.post(f'{client.app.router["auth_logout"].url_for()}')
212+
await assert_status(resp, status.HTTP_200_OK)
213+
214+
189215
class NewInvitation(NewUser):
190216
def __init__(
191217
self,

services/web/server/tests/unit/with_dbs/03/trash/test_trash_service.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
# pylint: disable=unused-variable
77

88

9-
import contextlib
10-
from collections.abc import AsyncIterator
119
from unittest.mock import MagicMock
1210

1311
import pytest
@@ -16,7 +14,10 @@
1614
from pytest_simcore.helpers.assert_checks import assert_status
1715
from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict
1816
from pytest_simcore.helpers.typing_env import EnvVarsDict
19-
from pytest_simcore.helpers.webserver_login import UserInfoDict
17+
from pytest_simcore.helpers.webserver_login import (
18+
UserInfoDict,
19+
switch_client_session_to,
20+
)
2021
from servicelib.aiohttp import status
2122
from simcore_service_webserver.db.models import UserRole
2223
from simcore_service_webserver.projects import _trash_service
@@ -44,30 +45,6 @@ def user_role() -> UserRole:
4445
return UserRole.USER
4546

4647

47-
@contextlib.asynccontextmanager
48-
async def _switch_client_session_to(
49-
client: TestClient, user: UserInfoDict
50-
) -> AsyncIterator[TestClient]:
51-
assert client.app
52-
53-
await client.post(f'{client.app.router["auth_logout"].url_for()}')
54-
# sometimes 4xx if user already logged out. Ignore
55-
56-
resp = await client.post(
57-
f'{client.app.router["auth_login"].url_for()}',
58-
json={
59-
"email": user["email"],
60-
"password": user["raw_password"],
61-
},
62-
)
63-
await assert_status(resp, status.HTTP_200_OK)
64-
65-
yield client
66-
67-
resp = await client.post(f'{client.app.router["auth_logout"].url_for()}')
68-
await assert_status(resp, status.HTTP_200_OK)
69-
70-
7148
async def test_trash_service__delete_expired_trash(
7249
client: TestClient,
7350
logged_user: UserInfoDict,
@@ -116,7 +93,7 @@ async def test_trash_service__delete_expired_trash(
11693
await assert_status(resp, status.HTTP_404_NOT_FOUND)
11794

11895
# ASSERT: other_user tries to get the project and expects 404
119-
async with _switch_client_session_to(client, other_user):
96+
async with switch_client_session_to(client, other_user):
12097
resp = await client.get(f"/v0/projects/{other_user_project_id}")
12198
await assert_status(resp, status.HTTP_404_NOT_FOUND)
12299

@@ -134,7 +111,7 @@ async def test_trash_nested_folders_and_projects(
134111
assert client.app
135112
assert logged_user["id"] != other_user["id"]
136113

137-
async with _switch_client_session_to(client, logged_user):
114+
async with switch_client_session_to(client, logged_user):
138115
# CREATE folders hierarchy for logged_user
139116
resp = await client.post("/v0/folders", json={"name": "Root Folder"})
140117
data, _ = await assert_status(resp, status.HTTP_201_CREATED)
@@ -162,7 +139,7 @@ async def test_trash_nested_folders_and_projects(
162139
)
163140
await assert_status(resp, status.HTTP_204_NO_CONTENT)
164141

165-
async with _switch_client_session_to(client, other_user):
142+
async with switch_client_session_to(client, other_user):
166143
# CREATE folders hierarchy for other_user
167144
resp = await client.post("/v0/folders", json={"name": "Root Folder"})
168145
data, _ = await assert_status(resp, status.HTTP_201_CREATED)
@@ -193,7 +170,7 @@ async def test_trash_nested_folders_and_projects(
193170
# UNDER TEST
194171
await trash_service.safe_delete_expired_trash_as_admin(client.app)
195172

196-
async with _switch_client_session_to(client, logged_user):
173+
async with switch_client_session_to(client, logged_user):
197174
# Verify logged_user's resources are gone
198175
resp = await client.get(f"/v0/folders/{logged_user_root_folder['folderId']}")
199176
await assert_status(resp, status.HTTP_403_FORBIDDEN)
@@ -205,7 +182,7 @@ async def test_trash_nested_folders_and_projects(
205182
await assert_status(resp, status.HTTP_404_NOT_FOUND)
206183

207184
# Verify other_user's resources are gone
208-
async with _switch_client_session_to(client, other_user):
185+
async with switch_client_session_to(client, other_user):
209186
resp = await client.get(f"/v0/folders/{other_user_root_folder['folderId']}")
210187
await assert_status(resp, status.HTTP_403_FORBIDDEN)
211188

0 commit comments

Comments
 (0)