Exemple #1
0
def update(context, request):
    """Update the fields we received and store the updated version."""
    annotation = context.model

    # Read the new fields for the annotation
    try:
        fields = request.json_body
    except ValueError:
        return _api_error(request,
                          'No JSON payload sent. Annotation not created.',
                          status_code=400)  # Client Error: Bad Request

    try:
        appstruct = schemas.AnnotationSchema().validate(fields)
    except schemas.ValidationError as err:
        return _api_error(request, err.message, status_code=400)

    # Update and store the annotation
    try:
        logic.update_annotation(annotation,
                                appstruct,
                                userid=request.authenticated_userid)
    except RuntimeError as err:
        return _api_error(request, err.args[0], status_code=err.args[1])

    # Notify any subscribers
    _publish_annotation_event(request, annotation, 'update')

    # Return the updated version that was just stored.
    return search_lib.render(annotation)
Exemple #2
0
def test_annotationschema_removes_protected_fields(field):
    data = {}
    data[field] = 'something forbidden'

    result = schemas.AnnotationSchema().validate(data)

    assert field not in result
Exemple #3
0
    def test_it_raises_if_document_is_not_a_dict(self):
        schema = schemas.AnnotationSchema()

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(document=False, ))

        assert str(err.value) == "document: False is not of type 'object'"
Exemple #4
0
    def test_it_raises_if_text_is_not_a_string(self):
        schema = schemas.AnnotationSchema()

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(text=False))

        assert str(err.value) == "text: False is not of type 'string'"
Exemple #5
0
    def test_it_raises_if_document_link_is_not_a_list(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(document={'link': False}))

        assert str(err.value) == "document.link: False is not of type 'array'"
Exemple #6
0
    def test_it_raises_if_uri_is_not_a_string(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(uri=False))

        assert str(err.value) == "uri: False is not of type 'string'"
Exemple #7
0
    def test_it_raises_if_document_dc_is_not_a_dict(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(document={'dc': False}))

        assert str(err.value) == "document.dc: False is not of type 'object'"
Exemple #8
0
    def test_it_raises_if_permissions_has_no_read(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(permissions={}))

        assert str(err.value) == "permissions: 'read' is a required property"
Exemple #9
0
    def test_it_does_not_pass_modified_dict_to_document_metas_from_data(
            self, parse_document_claims):
        """

        If document_uris_from_data() modifies the document dict that it's
        given, the original dict (or one with the same values as it) should be
        passed t document_metas_from_data(), not the modified copy.

        """
        document = {
            'top_level_key': 'original_value',
            'sub_dict': {
                'key': 'original_value'
            }
        }

        def document_uris_from_data(document, claimant):
            document['new_key'] = 'new_value'
            document['top_level_key'] = 'new_value'
            document['sub_dict']['key'] = 'new_value'

        parse_document_claims.document_uris_from_data.side_effect = (
            document_uris_from_data)
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        schema.validate(annotation_data(document=document))

        assert (parse_document_claims.document_metas_from_data.call_args[0][0]
                == document)
Exemple #10
0
    def test_it_inserts_empty_string_if_data_has_no_uri(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'uri' not in data

        assert schema.validate(data)['target_uri'] == ''
Exemple #11
0
    def test_it_puts_document_metas_in_appstruct(self, parse_document_claims):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        appstruct = schema.validate(annotation_data())

        assert appstruct['document']['document_meta_dicts'] == (
            parse_document_claims.document_metas_from_data.return_value)
Exemple #12
0
    def test_it_keeps_references(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        result = schema.validate(
            annotation_data(references=['parent id', 'parent id 2']))

        assert result['references'] == ['parent id', 'parent id 2']
Exemple #13
0
    def test_it_does_not_crash_if_data_contains_no_target(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'target' not in data

        schema.validate(data)
Exemple #14
0
    def test_it_renames_group_to_groupid(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        result = schema.validate(annotation_data(group='foo'))

        assert result['groupid'] == 'foo'
        assert 'group' not in result
Exemple #15
0
    def test_it_inserts_empty_string_if_data_contains_no_text(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'text' not in data

        assert schema.validate(data)['text'] == ''
Exemple #16
0
    def test_it_inserts_empty_list_if_data_contains_no_tags(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'tags' not in data

        assert schema.validate(data)['tags'] == []
Exemple #17
0
    def test_it_raises_if_references_is_not_a_list(self):
        schema = schemas.AnnotationSchema()

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(references=False))

        assert str(err.value) == "references: False is not of type 'array'"
Exemple #18
0
    def test_it_raises_if_permissions_is_not_a_dict(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(permissions=False))

        assert str(err.value) == "permissions: False is not of type 'object'"
Exemple #19
0
    def test_it_moves_extra_data_into_extra_sub_dict(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        result = schema.validate({
            # Throw in all the fields, just to make sure that none of them get
            # into extra.
            'created': 'created',
            'updated': 'updated',
            'user': '******',
            'id': 'id',
            'uri': 'uri',
            'text': 'text',
            'tags': ['gar', 'har'],
            'permissions': {
                'read': ['group:__world__']
            },
            'target': [],
            'group': '__world__',
            'references': ['parent'],

            # These should end up in extra.
            'foo': 1,
            'bar': 2,
        })

        assert result['extra'] == {'foo': 1, 'bar': 2}
Exemple #20
0
    def test_it_raises_if_target_has_no_selector(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(target=[{}]))

        assert str(err.value) == "target.0: 'selector' is a required property"
Exemple #21
0
    def test_it_raises_if_target_item_is_not_a_dict(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(target=[False]))

        assert str(err.value) == "target.0: False is not of type 'object'"
Exemple #22
0
    def test_it_raises_if_target_is_not_a_list(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(target=False))

        assert str(err.value) == "target: False is not of type 'array'"
Exemple #23
0
    def test_it_raises_if_references_item_is_not_a_string(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(references=[False]))

        assert str(err.value) == "references.0: False is not of type 'string'"
Exemple #24
0
    def test_it_sets_userid(self, authn_policy):
        authn_policy.authenticated_userid.return_value = (
            'acct:[email protected]')
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        result = schema.validate(annotation_data())

        assert result['userid'] == 'acct:[email protected]'
Exemple #25
0
    def test_it_renames_uri_to_target_uri(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        result = schema.validate(
            annotation_data(uri='http://example.com/example'), )

        assert result['target_uri'] == 'http://example.com/example'
        assert 'uri' not in result
Exemple #26
0
    def test_it_raises_if_permissions_read_is_not_a_list(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(permissions={'read': False}))

        assert str(
            err.value) == ("permissions.read: False is not of type 'array'")
Exemple #27
0
    def test_it_raises_if_document_link_item_is_not_a_dict(self):
        schema = schemas.AnnotationSchema()

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(document={'link': [False]}))

        assert str(
            err.value) == ("document.link.0: False is not of type 'object'")
Exemple #28
0
    def test_it_raises_if_document_link_item_has_no_href(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as err:
            schema.validate(self.valid_input_data(document={'link': [{}]}))

        assert str(
            err.value) == ("document.link.0: 'href' is a required property")
Exemple #29
0
    def test_it_inserts_default_groupid_if_no_group(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'group' not in data

        result = schema.validate(data)

        assert result['groupid'] == '__world__'
Exemple #30
0
    def test_it_inserts_empty_list_if_no_references(self):
        schema = schemas.AnnotationSchema(testing.DummyRequest())

        data = annotation_data()
        assert 'references' not in data

        result = schema.validate(data)

        assert result['references'] == []