Skip to content

Commit

Permalink
updated profile model with id
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Sep 15, 2022
1 parent 2bd2d2c commit 3b1270a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/models-library/src/models_library/basic_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum

from pydantic import conint, constr
from pydantic import PositiveInt, conint, constr

from .basic_regex import UUID_RE, VERSION_RE

Expand All @@ -23,6 +23,9 @@
# e.g. '5c833a78-1af3-43a7-9ed7-6a63b188f4d8'
UUIDStr = constr(regex=UUID_RE)

# auto-incremented primary-key IDs
IdInt = PrimaryKeyInt = PositiveInt


class LogLevel(str, Enum):
DEBUG = "DEBUG"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Literal, Optional
from uuid import UUID

from models_library.basic_types import IdInt
from pydantic import BaseModel, EmailStr, Field, validator
from servicelib.json_serialization import json_dumps
from simcore_postgres_database.models.users import UserRole
Expand Down Expand Up @@ -60,6 +61,7 @@ class ProfileUpdate(_ProfileCommon):


class ProfileGet(_ProfileCommon):
id: IdInt
login: EmailStr
role: Literal["Anonymous", "Guest", "User", "Tester", "Admin"]
groups: Optional[AllUsersGroups] = None
Expand All @@ -79,11 +81,13 @@ class Config:
schema_extra = {
"examples": [
{
"id": 1,
"login": "[email protected]",
"role": "Admin",
"gravatar_id": "205e460b479e2e5b48aec07710c08d50",
},
{
"id": 42,
"login": "[email protected]",
"role": UserRole.ADMIN,
"expirationDate": "2022-09-14",
Expand Down
37 changes: 36 additions & 1 deletion services/web/server/tests/unit/isolated/test_users_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_profile_get_expiration_date(faker: Faker):
fake_expiration = datetime.utcnow()

profile = ProfileGet(
login=faker.email(), role=UserRole.ADMIN, expiration_date=fake_expiration
id=1, login=faker.email(), role=UserRole.ADMIN, expiration_date=fake_expiration
)

assert fake_expiration.date() == profile.expiration_date
Expand All @@ -73,3 +73,38 @@ def test_profile_get_role(user_role: str):
data["role"] = UserRole(user_role)
m2 = ProfileGet(**data)
assert m1 == m2


def test_parsing_output_of_get_user_profile():

result_from_db_query_and_composition = {
"id": 1,
"login": "[email protected]",
"first_name": "PtN5Ab0uv",
"last_name": "",
"role": "Guest",
"gravatar_id": "9d5e02c75fcd4bce1c8861f219f7f8a5",
"groups": {
"me": {
"gid": 2,
"label": "PtN5Ab0uv",
"description": "primary group",
"thumbnail": None,
"inclusionRules": {},
"accessRights": {"read": True, "write": False, "delete": False},
},
"organizations": [],
"all": {
"gid": 1,
"label": "Everyone",
"description": "all users",
"thumbnail": None,
"inclusionRules": {},
"accessRights": {"read": True, "write": False, "delete": False},
},
},
"password": "secret", # should be stripped out
}

profile = ProfileGet.parse_obj(result_from_db_query_and_composition)
assert "password" not in profile.dict(exclude_unset=True)

0 comments on commit 3b1270a

Please sign in to comment.