Skip to content

Commit efb3fce

Browse files
committed
cleanup tests
1 parent fe632bc commit efb3fce

File tree

2 files changed

+90
-65
lines changed
  • services/web/server

2 files changed

+90
-65
lines changed

services/web/server/src/simcore_service_webserver/users/api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async def get_user_profile(
7777
if not user_profile:
7878
user_profile = {
7979
"id": row.users_id,
80-
"user_name": row.name,
80+
"user_name": row.users_name,
8181
"first_name": row.users_first_name,
8282
"last_name": row.users_last_name,
8383
"login": row.users_email,

services/web/server/tests/unit/with_dbs/03/test_users.py

Lines changed: 89 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,55 @@ def app_environment(
5959
"user_role,expected",
6060
[
6161
(UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED),
62-
(UserRole.GUEST, status.HTTP_200_OK),
63-
(UserRole.USER, status.HTTP_200_OK),
64-
(UserRole.TESTER, status.HTTP_200_OK),
62+
*((r, status.HTTP_200_OK) for r in UserRole if r >= UserRole.GUEST),
6563
],
6664
)
67-
async def test_get_profile(
65+
async def test_access_rights_on_get_profile(
66+
user_role: UserRole,
67+
logged_user: UserInfoDict,
68+
client: TestClient,
69+
expected: HTTPStatus,
70+
):
71+
assert client.app
72+
73+
url = client.app.router["get_my_profile"].url_for()
74+
assert url.path == "/v0/me"
75+
76+
resp = await client.get(f"{url}")
77+
await assert_status(resp, expected)
78+
79+
80+
@pytest.mark.parametrize(
81+
"user_role,expected",
82+
[
83+
(UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED),
84+
(UserRole.GUEST, status.HTTP_403_FORBIDDEN),
85+
*((r, status.HTTP_204_NO_CONTENT) for r in UserRole if r >= UserRole.USER),
86+
],
87+
)
88+
async def test_access_update_profile(
6889
logged_user: UserInfoDict,
6990
client: TestClient,
7091
user_role: UserRole,
7192
expected: HTTPStatus,
93+
):
94+
assert client.app
95+
96+
url = client.app.router["update_my_profile"].url_for()
97+
assert url.path == "/v0/me"
98+
99+
resp = await client.put(f"{url}", json={"last_name": "Foo"})
100+
await assert_status(resp, expected)
101+
102+
103+
@pytest.mark.parametrize(
104+
"user_role",
105+
[UserRole.USER],
106+
)
107+
async def test_get_profile(
108+
logged_user: UserInfoDict,
109+
client: TestClient,
110+
user_role: UserRole,
72111
primary_group: dict[str, Any],
73112
standard_groups: list[dict[str, Any]],
74113
all_group: dict[str, str],
@@ -78,84 +117,70 @@ async def test_get_profile(
78117
url = client.app.router["get_my_profile"].url_for()
79118
assert url.path == "/v0/me"
80119

81-
resp = await client.get(url.path)
82-
data, error = await assert_status(resp, expected)
120+
resp = await client.get(f"{url}")
121+
data, error = await assert_status(resp, status.HTTP_200_OK)
83122

84-
# check enveloped
85-
e = Envelope[ProfileGet].model_validate(await resp.json())
86-
assert e.error == error
87-
assert (
88-
e.data.model_dump(**RESPONSE_MODEL_POLICY, mode="json") == data
89-
if e.data
90-
else e.data == data
91-
)
123+
resp_model = Envelope[ProfileGet].model_validate(await resp.json())
92124

93-
if not error:
94-
profile = ProfileGet.model_validate(data)
95-
96-
product_group = {
97-
"accessRights": {"delete": False, "read": False, "write": False},
98-
"description": "osparc product group",
99-
"gid": 2,
100-
"inclusionRules": {},
101-
"label": "osparc",
102-
"thumbnail": None,
103-
}
104-
105-
assert profile.login == logged_user["email"]
106-
assert profile.gravatar_id
107-
assert profile.first_name == logged_user.get("first_name", None)
108-
assert profile.last_name == logged_user.get("last_name", None)
109-
assert profile.role == user_role.name
110-
assert profile.groups
111-
112-
got_profile_groups = profile.groups.model_dump(
113-
**RESPONSE_MODEL_POLICY, mode="json"
114-
)
115-
assert got_profile_groups["me"] == primary_group
116-
assert got_profile_groups["all"] == all_group
125+
assert resp_model.data.model_dump(**RESPONSE_MODEL_POLICY, mode="json") == data
126+
assert resp_model.error is None
117127

118-
sorted_by_group_id = functools.partial(sorted, key=lambda d: d["gid"])
119-
assert sorted_by_group_id(
120-
got_profile_groups["organizations"]
121-
) == sorted_by_group_id([*standard_groups, product_group])
128+
profile = resp_model.data
122129

123-
assert profile.preferences == await get_frontend_user_preferences_aggregation(
124-
client.app, user_id=logged_user["id"], product_name="osparc"
125-
)
130+
product_group = {
131+
"accessRights": {"delete": False, "read": False, "write": False},
132+
"description": "osparc product group",
133+
"gid": 2,
134+
"inclusionRules": {},
135+
"label": "osparc",
136+
"thumbnail": None,
137+
}
138+
139+
assert profile.login == logged_user["email"]
140+
assert profile.gravatar_id
141+
assert profile.first_name == logged_user.get("first_name", None)
142+
assert profile.last_name == logged_user.get("last_name", None)
143+
assert profile.role == user_role.name
144+
assert profile.groups
145+
146+
got_profile_groups = profile.groups.model_dump(**RESPONSE_MODEL_POLICY, mode="json")
147+
assert got_profile_groups["me"] == primary_group
148+
assert got_profile_groups["all"] == all_group
149+
150+
sorted_by_group_id = functools.partial(sorted, key=lambda d: d["gid"])
151+
assert sorted_by_group_id(
152+
got_profile_groups["organizations"]
153+
) == sorted_by_group_id([*standard_groups, product_group])
154+
155+
assert profile.preferences == await get_frontend_user_preferences_aggregation(
156+
client.app, user_id=logged_user["id"], product_name="osparc"
157+
)
126158

127159

128160
@pytest.mark.parametrize(
129-
"user_role,expected",
130-
[
131-
(UserRole.ANONYMOUS, status.HTTP_401_UNAUTHORIZED),
132-
(UserRole.GUEST, status.HTTP_403_FORBIDDEN),
133-
(UserRole.USER, status.HTTP_204_NO_CONTENT),
134-
(UserRole.TESTER, status.HTTP_204_NO_CONTENT),
135-
],
161+
"user_role",
162+
[UserRole.USER],
136163
)
137164
async def test_update_profile(
138165
logged_user: UserInfoDict,
139166
client: TestClient,
140167
user_role: UserRole,
141-
expected: HTTPStatus,
142168
):
143169
assert client.app
144170

145171
url = client.app.router["update_my_profile"].url_for()
146172
assert url.path == "/v0/me"
173+
resp = await client.put(f"{url}", json={"last_name": "Foo"})
174+
_, error = await assert_status(resp, status.HTTP_204_NO_CONTENT)
147175

148-
resp = await client.put(url.path, json={"last_name": "Foo"})
149-
_, error = await assert_status(resp, expected)
150-
151-
if not error:
152-
resp = await client.get(f"{url}")
153-
data, _ = await assert_status(resp, status.HTTP_200_OK)
176+
assert not error
177+
resp = await client.get(f"{url}")
178+
data, _ = await assert_status(resp, status.HTTP_200_OK)
154179

155-
# This is a PUT! i.e. full replace of profile variable fields!
156-
assert data["first_name"] == ProfileUpdate.model_fields["first_name"].default
157-
assert data["last_name"] == "Foo"
158-
assert data["role"] == user_role.name
180+
# This is a PUT! i.e. full replace of profile variable fields!
181+
assert data["first_name"] == ProfileUpdate.model_fields["first_name"].default
182+
assert data["last_name"] == "Foo"
183+
assert data["role"] == user_role.name
159184

160185

161186
@pytest.fixture
@@ -219,7 +244,7 @@ async def test_get_profile_with_failing_db_connection(
219244
(UserRole.PRODUCT_OWNER, status.HTTP_200_OK),
220245
],
221246
)
222-
async def test_only_product_owners_can_access_users_api(
247+
async def test_access_rights_on_search_users_only_product_owners_can_access(
223248
client: TestClient,
224249
logged_user: UserInfoDict,
225250
expected: HTTPStatus,

0 commit comments

Comments
 (0)