예제 #1
0
파일: groups.py 프로젝트: hypothesis/h
def create(request):
    """Create a group from the POST payload."""
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request) or request.default_authority,
    ).validate(_json_payload(request))

    group_service = request.find_service(name="group")
    group_create_service = request.find_service(name="group_create")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group:
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid)
            )

    group = group_create_service.create_private_group(
        name=appstruct["name"],
        userid=request.user.userid,
        description=appstruct.get("description", None),
        groupid=groupid,
    )
    return GroupJSONPresenter(GroupContext(group, request)).asdict(
        expand=["organization", "scopes"]
    )
예제 #2
0
파일: groups.py 프로젝트: kaydoh/h
def create(request):
    """Create a group from the POST payload."""
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=request.effective_authority,
    ).validate(_json_payload(request))

    group_service = request.find_service(name="group")
    group_create_service = request.find_service(name="group_create")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group:
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid))

    group = group_create_service.create_private_group(
        name=appstruct["name"],
        userid=request.user.userid,
        description=appstruct.get("description", None),
        groupid=groupid,
    )
    return GroupJSONPresenter(
        group, request).asdict(expand=["organization", "scopes"])
예제 #3
0
파일: group_test.py 프로젝트: pandabb3356/h
 def test_validate_raises_ValidationError_if_no_group_authority(self):
     schema = CreateGroupAPISchema(default_authority='hypothes.is')
     with pytest.raises(ValidationError, match="groupid may only be set on groups oustide of the default authority"):
         schema.validate({
             'name': 'Blustery',
             'groupid': 'group:[email protected]'
         })
예제 #4
0
def create(request):
    """Create a group from the POST payload."""
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request)
        or request.default_authority).validate(_json_payload(request))

    group_service = request.find_service(name='group')
    group_create_service = request.find_service(name='group_create')

    # Check for duplicate group
    groupid = appstruct.get('groupid', None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group:
            raise ConflictError(
                _("group with groupid '{}' already exists").format(groupid))

    group = group_create_service.create_private_group(
        name=appstruct['name'],
        userid=request.user.userid,
        description=appstruct.get('description', None),
        groupid=groupid,
    )
    return GroupJSONPresenter(GroupContext(
        group, request)).asdict(expand=['organization'])
예제 #5
0
파일: group_test.py 프로젝트: hypothesis/h
 def test_validate_raises_ValidationError_if_no_group_authority(self):
     schema = CreateGroupAPISchema(default_authority="hypothes.is")
     with pytest.raises(
         ValidationError,
         match="groupid may only be set on groups oustide of the default authority",
     ):
         schema.validate(
             {"name": "Blustery", "groupid": "group:[email protected]"}
         )
예제 #6
0
 def test_validate_raises_ValidationError_if_no_group_authority(self):
     schema = CreateGroupAPISchema(default_authority="hypothes.is")
     with pytest.raises(
         ValidationError,
         match="groupid may only be set on groups oustide of the default authority",
     ):
         schema.validate(
             {"name": "Blustery", "groupid": "group:[email protected]"}
         )
예제 #7
0
파일: group_test.py 프로젝트: yumatch/h
 def test_it_raises_if_description_too_long(self):
     schema = CreateGroupAPISchema()
     with pytest.raises(ValidationError) as exc:
         schema.validate({
             'name':
             'Name not the Problem',
             'description':
             'o' * (GROUP_DESCRIPTION_MAX_LENGTH + 1)
         })
     assert "description:" in str(exc.value)
     assert "is too long" in str(exc.value)
예제 #8
0
파일: group_test.py 프로젝트: yumatch/h
    def test_it_validates_with_valid_description(self):
        schema = CreateGroupAPISchema()

        appstruct = schema.validate({
            'name':
            'This Seems Fine',
            'description':
            'This description seems adequate'
        })

        assert 'description' in appstruct
예제 #9
0
def upsert(context, request):
    """
    Create or update a group from a PUT payload.

    If no group model is present in the passed ``context`` (on ``context.group``),
    treat this as a create action and delegate to ``create``.

    Otherwise, replace the existing group's resource properties entirely and update
    the object.

    :arg context:
    :type context: h.traversal.GroupUpsertContext
    """
    if context.group is None:
        return create(request)

    group = context.group

    # Because this is a PUT endpoint and not a PATCH, a full replacement of the
    # entire resource is expected. Thus, we're validating against the Create schema
    # here as we want to make sure properties required for a fresh object are present
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request) or request.default_authority,
    ).validate(_json_payload(request))

    group_update_service = request.find_service(name="group_update")
    group_service = request.find_service(name="group")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group and (duplicate_group != group):
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid)
            )

    # Need to make sure every resource-defined property is present, as this
    # is meant as a full-resource-replace operation.
    # TODO: This may be better handled in the schema at some point
    update_properties = {
        "name": appstruct["name"],
        "description": appstruct.get("description", ""),
        "groupid": appstruct.get("groupid", None),
    }

    group = group_update_service.update(group, **update_properties)

    # Note that this view takes a ``GroupUpsertContext`` but uses a ``GroupContext`` here
    return GroupJSONPresenter(GroupContext(group, request)).asdict(
        expand=["organization", "scopes"]
    )
예제 #10
0
파일: groups.py 프로젝트: hypothesis/h
def upsert(context, request):
    """
    Create or update a group from a PUT payload.

    If no group model is present in the passed ``context`` (on ``context.group``),
    treat this as a create action and delegate to ``create``.

    Otherwise, replace the existing group's resource properties entirely and update
    the object.

    :arg context:
    :type context: h.traversal.GroupUpsertContext
    """
    if context.group is None:
        return create(request)

    group = context.group

    # Because this is a PUT endpoint and not a PATCH, a full replacement of the
    # entire resource is expected. Thus, we're validating against the Create schema
    # here as we want to make sure properties required for a fresh object are present
    appstruct = CreateGroupAPISchema(
        default_authority=request.default_authority,
        group_authority=client_authority(request) or request.default_authority,
    ).validate(_json_payload(request))

    group_update_service = request.find_service(name="group_update")
    group_service = request.find_service(name="group")

    # Check for duplicate group
    groupid = appstruct.get("groupid", None)
    if groupid is not None:
        duplicate_group = group_service.fetch(pubid_or_groupid=groupid)
        if duplicate_group and (duplicate_group != group):
            raise HTTPConflict(
                _("group with groupid '{}' already exists").format(groupid)
            )

    # Need to make sure every resource-defined property is present, as this
    # is meant as a full-resource-replace operation.
    # TODO: This may be better handled in the schema at some point
    update_properties = {
        "name": appstruct["name"],
        "description": appstruct.get("description", ""),
        "groupid": appstruct.get("groupid", None),
    }

    group = group_update_service.update(group, **update_properties)

    # Note that this view takes a ``GroupUpsertContext`` but uses a ``GroupContext`` here
    return GroupJSONPresenter(GroupContext(group, request)).asdict(
        expand=["organization", "scopes"]
    )
예제 #11
0
파일: groups.py 프로젝트: afrilya53/h
def create(request):
    """Create a group from the POST payload."""
    schema = CreateGroupAPISchema()

    appstruct = schema.validate(_json_payload(request))
    group_properties = {
        'name': appstruct['name'],
        'description': appstruct.get('description', None),
    }

    group_service = request.find_service(name='group')

    group = group_service.create_private_group(
        group_properties['name'],
        request.user.userid,
        description=group_properties['description'],
    )

    group_context = GroupContext(group, request)
    return GroupJSONPresenter(group_context).asdict(expand=['organization'])
예제 #12
0
파일: groups.py 프로젝트: chinmaygghag/h
def create(request):
    """Create a group from the POST payload."""
    schema = CreateGroupAPISchema()

    appstruct = schema.validate(_json_payload(request))
    group_properties = {
        'name': appstruct['name'],
        'description': appstruct.get('description', None),
    }

    group_service = request.find_service(name='group')

    group = group_service.create_private_group(
        group_properties['name'],
        request.user.userid,
        description=group_properties['description'],
    )

    group_context = GroupContext(group, request)
    return GroupJSONPresenter(group_context).asdict(expand=['organization'])
예제 #13
0
def create(request):
    """Create a group from the POST payload."""
    # @TODO Temporary: Remove this check once private groups supported at other authorities
    if request.authority != request.user.authority:
        raise HTTPBadRequest(
            _('Group creation currently only supported for default authority'))

    schema = CreateGroupAPISchema()

    appstruct = schema.validate(_json_payload(request))
    group_properties = {
        'name': appstruct['name'],
        'description': appstruct.get('description', None),
    }

    group_service = request.find_service(name='group')

    group = group_service.create_private_group(
        group_properties['name'],
        request.user.userid,
        description=group_properties['description'],
    )
    group_context = GroupContext(group, request)
    return GroupJSONPresenter(group_context).asdict(expand=['organization'])
예제 #14
0
파일: group_test.py 프로젝트: yumatch/h
 def test_it_raises_if_name_missing(self):
     schema = CreateGroupAPISchema()
     with pytest.raises(ValidationError,
                        match=".*is a required property.*"):
         schema.validate({})
예제 #15
0
파일: group_test.py 프로젝트: yumatch/h
 def test_it_raises_if_name_too_long(self):
     schema = CreateGroupAPISchema()
     with pytest.raises(ValidationError) as exc:
         schema.validate({'name': 'o' * (GROUP_NAME_MAX_LENGTH + 1)})
     assert "name:" in str(exc.value)
     assert "is too long" in str(exc.value)
예제 #16
0
 def schema(self):
     schema = CreateGroupAPISchema(
         group_authority="hypothes.is", default_authority="hypothes.is"
     )
     return schema
예제 #17
0
파일: group_test.py 프로젝트: yumatch/h
    def test_it_validates_with_valid_name(self):
        schema = CreateGroupAPISchema()

        appstruct = schema.validate({'name': 'Perfectly Fine'})

        assert 'name' in appstruct
예제 #18
0
파일: group_test.py 프로젝트: pandabb3356/h
 def third_party_schema(self):
     schema = CreateGroupAPISchema(group_authority='thirdparty.com',
                                   default_authority='hypothes.is')
     return schema