Example #1
0
def admin_user_output(many: bool = True) -> Schema:
    attributes: MarshmallowSchema = {}

    attributes["uuid"] = fields.UUID()
    # This is because Email is not typed on marshmallow
    attributes["email"] = fields.Email()  # type: ignore
    attributes["name"] = fields.Str()
    attributes["surname"] = fields.Str()
    attributes["first_login"] = fields.DateTime(allow_none=True,
                                                format=ISO8601UTC)
    attributes["last_login"] = fields.DateTime(allow_none=True,
                                               format=ISO8601UTC)
    attributes["last_password_change"] = fields.DateTime(allow_none=True,
                                                         format=ISO8601UTC)
    attributes["is_active"] = fields.Boolean()
    attributes["privacy_accepted"] = fields.Boolean()
    attributes["roles"] = fields.List(fields.Nested(Role))
    attributes["expiration"] = fields.DateTime(allow_none=True,
                                               format=ISO8601UTC)

    if Connector.authentication_service == "neo4j":
        attributes["belongs_to"] = fields.Neo4jRelationshipToSingle(
            Group, data_key="group")
    else:
        attributes["belongs_to"] = fields.Nested(Group, data_key="group")

    if custom_fields := mem.customizer.get_custom_output_fields(None):
        attributes.update(custom_fields)
Example #2
0
class GroupWithMembers(Schema):
    uuid = fields.UUID()
    fullname = fields.Str()
    shortname = fields.Str()

    members = fields.Nested(UserWithUUID(many=True))
    coordinators = fields.Nested(UserWithUUID(many=True))
Example #3
0
class PhenotypeOutputSchema(Schema):
    uuid = fields.Str(required=True)
    name = fields.Str(required=True)
    age = fields.Integer()
    sex = fields.Str(required=True, validate=validate.OneOf(SEX))
    hpo = fields.List(fields.Nested(Hpo), required=False)
    birth_place = fields.Nested(GeoData, required=False)
    relationships = fields.Nested(
        Relationships,
        metadata={"description": "family relationships between phenotypes"},
    )
Example #4
0
def profile_output() -> Schema:
    attributes: MarshmallowSchema = {}

    attributes["uuid"] = fields.UUID(required=True)
    # This is because Email is not typed on marshmallow
    attributes["email"] = fields.Email(required=True)  # type: ignore
    attributes["name"] = fields.Str(required=True)
    attributes["surname"] = fields.Str(required=True)
    attributes["isAdmin"] = fields.Boolean(required=True)
    attributes["isStaff"] = fields.Boolean(required=True)
    attributes["isCoordinator"] = fields.Boolean(required=True)
    attributes["privacy_accepted"] = fields.Boolean(required=True)
    attributes["is_active"] = fields.Boolean(required=True)
    attributes["expiration"] = fields.DateTime(allow_none=True,
                                               format=ISO8601UTC)
    attributes["roles"] = fields.Dict(required=True)
    attributes["last_password_change"] = fields.DateTime(required=True,
                                                         format=ISO8601UTC)
    attributes["first_login"] = fields.DateTime(required=True,
                                                format=ISO8601UTC)
    attributes["last_login"] = fields.DateTime(required=True,
                                               format=ISO8601UTC)

    if Connector.authentication_service == "neo4j":
        attributes["belongs_to"] = fields.Neo4jRelationshipToSingle(
            Group, data_key="group")
    else:
        attributes["belongs_to"] = fields.Nested(Group, data_key="group")

    attributes["two_factor_enabled"] = fields.Boolean(required=True)

    if custom_fields := mem.customizer.get_custom_output_fields(None):
        attributes.update(custom_fields)
Example #5
0
def getProfileData():
    # as defined in Marshmallow.schema.from_dict
    attributes: Dict[str, Union[fields.Field, type]] = {}

    attributes["uuid"] = fields.UUID(required=True)
    attributes["email"] = fields.Email(required=True)
    attributes["name"] = fields.Str(required=True)
    attributes["surname"] = fields.Str(required=True)
    attributes["isAdmin"] = fields.Boolean(required=True)
    attributes["isStaff"] = fields.Boolean(required=True)
    attributes["isCoordinator"] = fields.Boolean(required=True)
    attributes["privacy_accepted"] = fields.Boolean(required=True)
    attributes["is_active"] = fields.Boolean(required=True)
    attributes["expiration"] = fields.DateTime(allow_none=True, format=ISO8601UTC)
    attributes["roles"] = fields.Dict(required=True)
    attributes["last_password_change"] = fields.DateTime(
        required=True, format=ISO8601UTC
    )
    attributes["first_login"] = fields.DateTime(required=True, format=ISO8601UTC)
    attributes["last_login"] = fields.DateTime(required=True, format=ISO8601UTC)

    attributes["group"] = fields.Nested(Group)

    attributes["two_factor_enabled"] = fields.Boolean(required=True)

    if custom_fields := mem.customizer.get_custom_output_fields(None):
        attributes.update(custom_fields)
Example #6
0
class StatsSchema(Schema):
    system = fields.Nested(SystemSchema)
    cpu = fields.Nested(CPUSchema)
    ram = fields.Nested(RAMSchema)
    swap = fields.Nested(SwapSchema)
    disk = fields.Nested(DiskSchema)
    procs = fields.Nested(ProcSchema)
    io = fields.Nested(IOSchema)
    network_latency = fields.Nested(NetworkSchema)
Example #7
0
def group_users_output() -> Schema:
    attributes: MarshmallowSchema = {}

    # This is because Email is not typed on marshmallow
    attributes["email"] = fields.Email()  # type: ignore
    attributes["name"] = fields.Str()
    attributes["surname"] = fields.Str()
    attributes["roles"] = fields.List(fields.Nested(Role))

    if custom_fields := mem.customizer.get_custom_output_fields(None):
        attributes.update(custom_fields)
Example #8
0
def get_output_schema():
    # as defined in Marshmallow.schema.from_dict
    attributes: Dict[str, Union[fields.Field, type]] = {}

    attributes["uuid"] = fields.UUID()
    attributes["email"] = fields.Email()
    attributes["name"] = fields.Str()
    attributes["surname"] = fields.Str()
    attributes["first_login"] = fields.DateTime(allow_none=True,
                                                format=ISO8601UTC)
    attributes["last_login"] = fields.DateTime(allow_none=True,
                                               format=ISO8601UTC)
    attributes["last_password_change"] = fields.DateTime(allow_none=True,
                                                         format=ISO8601UTC)
    attributes["is_active"] = fields.Boolean()
    attributes["privacy_accepted"] = fields.Boolean()
    attributes["roles"] = fields.List(fields.Nested(Roles))
    attributes["expiration"] = fields.DateTime(allow_none=True,
                                               format=ISO8601UTC)

    attributes["belongs_to"] = fields.Nested(Group, data_key="group")

    if custom_fields := mem.customizer.get_custom_output_fields(None):
        attributes.update(custom_fields)
Example #9
0
class TokenAdminSchema(TokenSchema):
    # token_type = fields.Str()
    user = fields.Nested(User)
Example #10
0
    def test_responses(self, faker: Faker) -> None:
        class MySchema(Schema):
            name = fields.Str()

        f = "myfield"
        assert (
            ResponseMaker.get_schema_type(f, fields.Str(metadata={"password": True}))
            == "password"
        )
        assert ResponseMaker.get_schema_type(f, fields.Bool()) == "boolean"
        assert ResponseMaker.get_schema_type(f, fields.Boolean()) == "boolean"
        assert ResponseMaker.get_schema_type(f, fields.Date()) == "date"
        assert ResponseMaker.get_schema_type(f, fields.DateTime()) == "datetime"
        assert ResponseMaker.get_schema_type(f, fields.AwareDateTime()) == "datetime"
        assert ResponseMaker.get_schema_type(f, fields.NaiveDateTime()) == "datetime"
        assert ResponseMaker.get_schema_type(f, fields.Decimal()) == "number"
        # This is because Email is not typed on marshmallow
        assert ResponseMaker.get_schema_type(f, fields.Email()) == "email"  # type: ignore
        assert ResponseMaker.get_schema_type(f, fields.Float()) == "number"
        assert ResponseMaker.get_schema_type(f, fields.Int()) == "int"
        assert ResponseMaker.get_schema_type(f, fields.Integer()) == "int"
        assert ResponseMaker.get_schema_type(f, fields.Number()) == "number"
        assert ResponseMaker.get_schema_type(f, fields.Str()) == "string"
        assert ResponseMaker.get_schema_type(f, fields.String()) == "string"
        assert ResponseMaker.get_schema_type(f, fields.Dict()) == "dictionary"
        assert ResponseMaker.get_schema_type(f, fields.List(fields.Str())) == "string[]"
        assert ResponseMaker.get_schema_type(f, fields.Nested(MySchema())) == "nested"
        # Unsupported types, fallback to string
        assert ResponseMaker.get_schema_type(f, fields.URL()) == "string"
        assert ResponseMaker.get_schema_type(f, fields.Url()) == "string"
        assert ResponseMaker.get_schema_type(f, fields.UUID()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.Constant("x")) == "string"
        assert ResponseMaker.get_schema_type(f, fields.Field()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.Function()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.Mapping()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.Method()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.Raw()) == "string"
        # assert ResponseMaker.get_schema_type(f, fields.TimeDelta()) == "string"

        assert not ResponseMaker.is_binary(None)
        assert not ResponseMaker.is_binary("")
        assert not ResponseMaker.is_binary("application/json")
        assert ResponseMaker.is_binary("application/octet-stream")
        assert ResponseMaker.is_binary("application/x-bzip")
        assert ResponseMaker.is_binary("application/x-bzip2")
        assert ResponseMaker.is_binary("application/pdf")
        assert ResponseMaker.is_binary("application/msword")
        assert ResponseMaker.is_binary("application/rtf")
        assert ResponseMaker.is_binary("application/x-tar")
        assert ResponseMaker.is_binary("application/gzip")
        assert ResponseMaker.is_binary("application/zip")
        assert ResponseMaker.is_binary("application/x-7z-compressed")
        assert not ResponseMaker.is_binary("text/plain")
        assert not ResponseMaker.is_binary("text/css")
        assert not ResponseMaker.is_binary("text/csv")
        assert not ResponseMaker.is_binary("text/html")
        assert not ResponseMaker.is_binary("text/javascript")
        assert not ResponseMaker.is_binary("text/xml")
        assert ResponseMaker.is_binary("image/gif")
        assert ResponseMaker.is_binary("image/jpeg")
        assert ResponseMaker.is_binary("image/png")
        assert ResponseMaker.is_binary("image/svg+xml")
        assert ResponseMaker.is_binary("image/tiff")
        assert ResponseMaker.is_binary("image/webp")
        assert ResponseMaker.is_binary("image/bmp")
        assert ResponseMaker.is_binary("image/aac")
        assert ResponseMaker.is_binary("audio/midi")
        assert ResponseMaker.is_binary("audio/mpeg")
        assert ResponseMaker.is_binary("audio/wav")
        assert ResponseMaker.is_binary("audio/anyother")
        assert ResponseMaker.is_binary("video/mpeg")
        assert ResponseMaker.is_binary("video/ogg")
        assert ResponseMaker.is_binary("video/webm")
        assert ResponseMaker.is_binary("video/anyother")
        assert ResponseMaker.is_binary("video/anyother")
        assert not ResponseMaker.is_binary(faker.pystr())

        response = EndpointResource.response("", code=200)
        assert response[1] == 200  # type: ignore
        response = EndpointResource.response(None, code=200)
        assert response[1] == 204  # type: ignore
        response = EndpointResource.response(None, code=200, head_method=True)
        assert response[1] == 200  # type: ignore
Example #11
0
class Group(Schema):
    uuid = fields.UUID()
    fullname = fields.Str()
    shortname = fields.Str()

    members = fields.Nested(User(many=True))
Example #12
0
class Relationships(Schema):
    mother = fields.Nested(FamilyMembers, required=False)
    father = fields.Nested(FamilyMembers, required=False)
    sons = fields.List(fields.Nested(FamilyMembers, required=False), required=False)
Example #13
0
    class InputSchema(Schema):
        # lowercase key without label defined. label will be key.title() in schema
        mystr = fields.Str(required=True, validate=validate.Length(min=4))
        # non-lowercase key without label defined. label will be == to key in schema
        MYDATE = fields.Date(required=True)
        MYDATETIME = fields.AwareDateTime(
            required=True,
            format=ISO8601UTC,
            default_timezone=pytz.utc,
            validate=validate.Range(
                max=datetime.now(pytz.utc).replace(hour=23, minute=59, second=59),
                min=datetime(1900, 1, 1, tzinfo=pytz.utc),
                max_inclusive=True,
                error="Invalid date",
            ),
        )
        myint_exclusive = fields.Int(
            required=True,
            # Explicit label definition... but missing description
            validate=validate.Range(
                min=1, max=10, min_inclusive=False, max_inclusive=False
            ),
            metadata={
                "label": "Int exclusive field",
            },
        )
        myint_inclusive = fields.Int(
            required=True,
            # Both label and description explicit definition
            validate=validate.Range(min=1, max=10),
            metadata={
                "label": "Int inclusive field",
                "description": "This field accepts values in a defined range",
            },
        )

        myselect = fields.Str(
            required=True,
            validate=validate.OneOf(choices=["a", "b"], labels=["A", "B"]),
        )

        myselect2 = fields.Str(
            required=True,
            # Wrong definition, number labels < number of choices
            # Labels will be ignored and replaced by choices
            validate=validate.OneOf(choices=["a", "b"], labels=["A"]),
        )

        mymaxstr = fields.Str(required=True, validate=validate.Length(max=7))

        myequalstr = fields.Str(required=True, validate=validate.Length(equal=6))

        # Note: requests (from pytest) has to json-dump the arrays and objects,
        # but the normal Marshmallow fields does not json-load the inputs

        # fields.Nested is a replacement of the default Nested field with the ability
        # to receive json dumped data from requests or pytest
        mynested = fields.Nested(Nested, required=True)

        mynullablenested = fields.Nested(Nested, required=True, allow_none=True)

        # fields.List is a replacement of the default List field with the ability
        # to receive json dumped data from requests or pytest

        # In json model the type of this field will be resolved as string[]
        mylist = fields.List(fields.Str(), required=True)
        # In json model the type of this field will be resolved as int[]
        mylist2 = fields.List(CustomInt, required=True)
        # In json model the type of this field will be resolved as mylist3[]
        # The type is key[] ... should be something more explicative like FieldName[]
        mylist3 = fields.List(CustomGenericField, required=True)