Exemple #1
0
    def _validate_groupid(self, appstruct):
        """
        Validate the ``groupid`` to make sure it adheres to authority restrictions.

        ``groupid`` is only allowed if the authority of the group associated
        with it is not the default authority—i.e. this is a third-party group.

        :arg appstruct: Data, which may or may not contain a ``groupid`` entry
        :type appstruct: dict
        :raise h.schemas.ValidationError:

        """
        groupid = appstruct.get("groupid", None)
        if groupid is None:  # Nothing to validate
            return None

        if (self.group_authority is None) or (self.group_authority
                                              == self.default_authority):
            # This is a first-party group
            raise ValidationError("{err_msg} '{authority}'".format(
                err_msg=
                _("groupid may only be set on groups oustide of the default authority"
                  ),
                authority=self.default_authority,
            ))

        groupid_parts = split_groupid(groupid)

        if groupid_parts["authority"] != self.group_authority:
            # The authority part of the ``groupid`` doesn't match the
            # group's authority
            raise ValidationError("{err_msg} '{groupid}'".format(
                err_msg=_("Invalid authority specified in groupid"),
                groupid=groupid))
Exemple #2
0
    def groupid(self, value):
        """
        Deconstruct a formatted ``groupid`` and set its constituent properties on the instance.

        If ``groupid`` is set to None, set ``authority_provided_id`` to None
        but leave authority untouched—this allows a caller to nullify the
        ``authority_provided_id`` field.

        :raises ValueError: if ``groupid`` is an invalid format
        """
        if value is None:
            self.authority_provided_id = None
        else:
            groupid_parts = split_groupid(value)
            self.authority_provided_id = groupid_parts["authority_provided_id"]
            self.authority = groupid_parts["authority"]
Exemple #3
0
    def fetch_by_groupid(self, groupid):
        """
        Return a group with the given ``groupid`` or ``None``.

        :arg groupid: String in groupid format, e.g. ``group:[email protected]``.
            See :class:`~h.models.Group`
        :raises ValueError: if ``groupid`` is not a valid groupid.
            See :func:`h.util.group.split_groupid`
        :rtype: :class:`~h.models.Group` or ``None``
        """
        parts = group_util.split_groupid(groupid)
        authority = parts["authority"]
        authority_provided_id = parts["authority_provided_id"]

        return (self.session.query(Group).filter_by(
            authority=authority).filter_by(
                authority_provided_id=authority_provided_id).one_or_none())
Exemple #4
0
    def fetch_by_groupid(self, groupid):
        """
        Return a group with the given ``groupid`` or ``None``.

        :arg groupid: String in groupid format, e.g. ``group:[email protected]``.
            See :class:`~h.models.Group`
        :raises ValueError: if ``groupid`` is not a valid groupid.
            See :func:`h.util.group.split_groupid`
        :rtype: :class:`~h.models.Group` or ``None``
        """
        parts = group_util.split_groupid(groupid)
        authority = parts["authority"]
        authority_provided_id = parts["authority_provided_id"]

        return (
            self.session.query(Group)
            .filter_by(authority=authority)
            .filter_by(authority_provided_id=authority_provided_id)
            .one_or_none()
        )
Exemple #5
0
    def _validate_groupid(self, appstruct):
        """
        Validate the ``groupid`` to make sure it adheres to authority restrictions.

        ``groupid`` is only allowed if the authority of the group associated
        with it is not the default authority—i.e. this is a third-party group.

        :arg appstruct: Data, which may or may not contain a ``groupid`` entry
        :type appstruct: dict
        :raise h.schemas.ValidationError:

        """
        groupid = appstruct.get("groupid", None)
        if groupid is None:  # Nothing to validate
            return None

        if (self.group_authority is None) or (
            self.group_authority == self.default_authority
        ):
            # This is a first-party group
            raise ValidationError(
                "{err_msg} '{authority}'".format(
                    err_msg=_(
                        "groupid may only be set on groups oustide of the default authority"
                    ),
                    authority=self.default_authority,
                )
            )

        groupid_parts = split_groupid(groupid)

        if groupid_parts["authority"] != self.group_authority:
            # The authority part of the ``groupid`` doesn't match the
            # group's authority
            raise ValidationError(
                "{err_msg} '{groupid}'".format(
                    err_msg=_("Invalid authority specified in groupid"), groupid=groupid
                )
            )
Exemple #6
0
 def test_it_raises_ValueError_on_invalid_groupids(self, groupid):
     with pytest.raises(ValueError, match="valid groupid"):
         group_util.split_groupid(groupid)
Exemple #7
0
    def test_it_splits_valid_groupids(self, groupid, authority_provided_id, authority):
        splitgroup = group_util.split_groupid(groupid)

        assert splitgroup["authority_provided_id"] == authority_provided_id
        assert splitgroup["authority"] == authority
 def test_it_raises_ValueError_on_invalid_groupids(self, groupid):
     with pytest.raises(ValueError, match="valid groupid"):
         group_util.split_groupid(groupid)
    def test_it_splits_valid_groupids(self, groupid, authority_provided_id,
                                      authority):
        splitgroup = group_util.split_groupid(groupid)

        assert splitgroup["authority_provided_id"] == authority_provided_id
        assert splitgroup["authority"] == authority