|
3 | 3 | # pylint: disable=unused-variable |
4 | 4 | # pylint: disable=too-many-arguments |
5 | 5 |
|
6 | | -import itertools |
7 | | -from copy import deepcopy |
8 | 6 | from datetime import UTC, datetime |
9 | 7 | from typing import Any |
10 | 8 |
|
|
16 | 14 | MyProfilePrivacyGet, |
17 | 15 | ) |
18 | 16 | from models_library.generics import Envelope |
19 | | -from models_library.users import UserThirdPartyToken |
20 | 17 | from models_library.utils.fastapi_encoders import jsonable_encoder |
21 | | -from pydantic import BaseModel, ValidationError |
22 | | -from pytest_simcore.pydantic_models import ( |
23 | | - assert_validation_model, |
24 | | - iter_model_examples_in_class, |
25 | | -) |
26 | 18 | from servicelib.rest_constants import RESPONSE_MODEL_POLICY |
27 | | -from simcore_postgres_database.models.users import UserRole |
28 | 19 | from simcore_service_webserver.users._common.models import ToUserUpdateDB |
29 | 20 |
|
30 | 21 |
|
31 | | -@pytest.mark.parametrize( |
32 | | - "model_cls, example_name, example_data", |
33 | | - itertools.chain( |
34 | | - iter_model_examples_in_class(MyProfileGet), |
35 | | - iter_model_examples_in_class(UserThirdPartyToken), |
36 | | - ), |
37 | | -) |
38 | | -def test_user_models_examples( |
39 | | - model_cls: type[BaseModel], example_name: str, example_data: Any |
40 | | -): |
41 | | - model_instance = assert_validation_model( |
42 | | - model_cls, example_name=example_name, example_data=example_data |
43 | | - ) |
44 | | - |
45 | | - model_enveloped = Envelope[model_cls].from_data( |
46 | | - model_instance.model_dump(by_alias=True) |
47 | | - ) |
48 | | - model_array_enveloped = Envelope[list[model_cls]].from_data( |
49 | | - [ |
50 | | - model_instance.model_dump(by_alias=True), |
51 | | - model_instance.model_dump(by_alias=True), |
52 | | - ] |
53 | | - ) |
54 | | - |
55 | | - assert model_enveloped.error is None |
56 | | - assert model_array_enveloped.error is None |
57 | | - |
58 | | - |
59 | 22 | @pytest.fixture |
60 | 23 | def fake_profile_get(faker: Faker) -> MyProfileGet: |
61 | 24 | fake_profile: dict[str, Any] = faker.simple_profile() |
@@ -104,18 +67,6 @@ def test_auto_compute_gravatar__deprecated(fake_profile_get: MyProfileGet): |
104 | 67 | assert data["preferences"] == profile.preferences |
105 | 68 |
|
106 | 69 |
|
107 | | -@pytest.mark.parametrize("user_role", [u.name for u in UserRole]) |
108 | | -def test_profile_get_role(user_role: str): |
109 | | - for example in MyProfileGet.model_json_schema()["examples"]: |
110 | | - data = deepcopy(example) |
111 | | - data["role"] = user_role |
112 | | - m1 = MyProfileGet(**data) |
113 | | - |
114 | | - data["role"] = UserRole(user_role) |
115 | | - m2 = MyProfileGet(**data) |
116 | | - assert m1 == m2 |
117 | | - |
118 | | - |
119 | 70 | def test_parsing_output_of_get_user_profile(): |
120 | 71 | result_from_db_query_and_composition = { |
121 | 72 | "id": 1, |
@@ -185,58 +136,3 @@ def test_mapping_update_models_from_rest_to_db(): |
185 | 136 | "name": "foo1234", |
186 | 137 | "privacy_hide_fullname": False, |
187 | 138 | } |
188 | | - |
189 | | - |
190 | | -def test_my_profile_patch_username_min_len(): |
191 | | - # minimum length username is 4 |
192 | | - with pytest.raises(ValidationError) as err_info: |
193 | | - MyProfilePatch.model_validate({"userName": "abc"}) |
194 | | - |
195 | | - assert err_info.value.error_count() == 1 |
196 | | - assert err_info.value.errors()[0]["type"] == "too_short" |
197 | | - |
198 | | - MyProfilePatch.model_validate({"userName": "abcd"}) # OK |
199 | | - |
200 | | - |
201 | | -def test_my_profile_patch_username_valid_characters(): |
202 | | - # Ensure valid characters (alphanumeric + . _ -) |
203 | | - with pytest.raises(ValidationError, match="start with a letter") as err_info: |
204 | | - MyProfilePatch.model_validate({"userName": "1234"}) |
205 | | - |
206 | | - assert err_info.value.error_count() == 1 |
207 | | - assert err_info.value.errors()[0]["type"] == "value_error" |
208 | | - |
209 | | - MyProfilePatch.model_validate({"userName": "u1234"}) # OK |
210 | | - |
211 | | - |
212 | | -def test_my_profile_patch_username_special_characters(): |
213 | | - # Ensure no consecutive special characters |
214 | | - with pytest.raises( |
215 | | - ValidationError, match="consecutive special characters" |
216 | | - ) as err_info: |
217 | | - MyProfilePatch.model_validate({"userName": "u1__234"}) |
218 | | - |
219 | | - assert err_info.value.error_count() == 1 |
220 | | - assert err_info.value.errors()[0]["type"] == "value_error" |
221 | | - |
222 | | - MyProfilePatch.model_validate({"userName": "u1_234"}) # OK |
223 | | - |
224 | | - # Ensure it doesn't end with a special character |
225 | | - with pytest.raises(ValidationError, match="end with") as err_info: |
226 | | - MyProfilePatch.model_validate({"userName": "u1234_"}) |
227 | | - |
228 | | - assert err_info.value.error_count() == 1 |
229 | | - assert err_info.value.errors()[0]["type"] == "value_error" |
230 | | - |
231 | | - MyProfilePatch.model_validate({"userName": "u1_234"}) # OK |
232 | | - |
233 | | - |
234 | | -def test_my_profile_patch_username_reserved_words(): |
235 | | - # Check reserved words (example list; extend as needed) |
236 | | - with pytest.raises(ValidationError, match="cannot be used") as err_info: |
237 | | - MyProfilePatch.model_validate({"userName": "admin"}) |
238 | | - |
239 | | - assert err_info.value.error_count() == 1 |
240 | | - assert err_info.value.errors()[0]["type"] == "value_error" |
241 | | - |
242 | | - MyProfilePatch.model_validate({"userName": "midas"}) # OK |
0 commit comments