-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Enhanced groups/organizations web-api specs and validation 🚨 (#6640)
- Loading branch information
Showing
17 changed files
with
530 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
from contextlib import suppress | ||
from typing import Any, ClassVar | ||
|
||
from pydantic import AnyUrl, BaseModel, Field, ValidationError, parse_obj_as, validator | ||
from pydantic import ( | ||
AnyUrl, | ||
BaseModel, | ||
Field, | ||
ValidationError, | ||
parse_obj_as, | ||
root_validator, | ||
validator, | ||
) | ||
|
||
from ..emails import LowerCaseEmailStr | ||
|
||
# | ||
# GROUPS MODELS defined in OPENAPI specs | ||
# | ||
from ..users import UserID | ||
from ..utils.common_validators import create__check_only_one_is_set__root_validator | ||
from ._base import InputSchema, OutputSchema | ||
|
||
|
||
class GroupAccessRights(BaseModel): | ||
|
@@ -29,7 +36,7 @@ class Config: | |
} | ||
|
||
|
||
class UsersGroup(BaseModel): | ||
class GroupGet(OutputSchema): | ||
gid: int = Field(..., description="the group ID") | ||
label: str = Field(..., description="the group name") | ||
description: str = Field(..., description="the group description") | ||
|
@@ -45,7 +52,7 @@ class UsersGroup(BaseModel): | |
|
||
@validator("thumbnail", pre=True) | ||
@classmethod | ||
def sanitize_legacy_data(cls, v): | ||
def _sanitize_legacy_data(cls, v): | ||
if v: | ||
# Enforces null if thumbnail is not valid URL or empty | ||
with suppress(ValidationError): | ||
|
@@ -86,11 +93,23 @@ class Config: | |
} | ||
|
||
|
||
class AllUsersGroups(BaseModel): | ||
me: UsersGroup | None = None | ||
organizations: list[UsersGroup] | None = None | ||
all: UsersGroup | None = None | ||
product: UsersGroup | None = None | ||
class GroupCreate(InputSchema): | ||
label: str | ||
description: str | ||
thumbnail: AnyUrl | None = None | ||
|
||
|
||
class GroupUpdate(InputSchema): | ||
label: str | None = None | ||
description: str | None = None | ||
thumbnail: AnyUrl | None = None | ||
|
||
|
||
class MyGroupsGet(OutputSchema): | ||
me: GroupGet | ||
organizations: list[GroupGet] | None = None | ||
all: GroupGet | ||
product: GroupGet | None = None | ||
|
||
class Config: | ||
schema_extra: ClassVar[dict[str, Any]] = { | ||
|
@@ -158,3 +177,38 @@ class Config: | |
}, | ||
} | ||
} | ||
|
||
|
||
class GroupUserAdd(InputSchema): | ||
""" | ||
Identify the user with either `email` or `uid` — only one. | ||
""" | ||
|
||
uid: UserID | None = None | ||
email: LowerCaseEmailStr | None = None | ||
|
||
_check_uid_or_email = root_validator(allow_reuse=True)( | ||
create__check_only_one_is_set__root_validator(["uid", "email"]) | ||
) | ||
|
||
class Config: | ||
schema_extra: ClassVar[dict[str, Any]] = { | ||
"examples": [{"uid": 42}, {"email": "[email protected]"}] | ||
} | ||
|
||
|
||
class GroupUserUpdate(InputSchema): | ||
# NOTE: since it is a single item, it is required. Cannot | ||
# update for the moment partial attributes e.g. {read: False} | ||
access_rights: GroupAccessRights | ||
|
||
class Config: | ||
schema_extra: ClassVar[dict[str, Any]] = { | ||
"example": { | ||
"accessRights": { | ||
"read": True, | ||
"write": False, | ||
"delete": False, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/models-library/tests/test_api_schemas_webserver_groups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
from models_library.api_schemas_webserver.groups import GroupUserAdd | ||
from pydantic import ValidationError | ||
|
||
unset = object() | ||
|
||
|
||
@pytest.mark.parametrize("uid", [1, None, unset]) | ||
@pytest.mark.parametrize("email", ["[email protected]", None, unset]) | ||
def test_uid_or_email_are_set(uid: Any, email: Any): | ||
kwargs = {} | ||
if uid != unset: | ||
kwargs["uid"] = uid | ||
if email != unset: | ||
kwargs["email"] = email | ||
|
||
none_are_defined = kwargs.get("uid") is None and kwargs.get("email") is None | ||
both_are_defined = kwargs.get("uid") is not None and kwargs.get("email") is not None | ||
|
||
if none_are_defined or both_are_defined: | ||
with pytest.raises(ValidationError, match="not both"): | ||
GroupUserAdd(**kwargs) | ||
else: | ||
got = GroupUserAdd(**kwargs) | ||
assert bool(got.email) ^ bool(got.uid) |
Oops, something went wrong.