Exemple #1
0
    def test_it_deletes_groupid_for_replies(self):
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        appstruct = schema.validate(
            self.valid_data(group='foo', references=['parent annotation id']))

        assert 'groupid' not in appstruct
Exemple #2
0
    def test_it_keeps_references(self):
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        appstruct = schema.validate(
            self.valid_data(references=['parent id', 'parent id 2']))

        assert appstruct['references'] == ['parent id', 'parent id 2']
Exemple #3
0
    def test_it_renames_group_to_groupid(self):
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        appstruct = schema.validate(self.valid_data(group='foo'))

        assert appstruct['groupid'] == 'foo'
        assert 'group' not in appstruct
Exemple #4
0
    def test_it_sets_userid(self, config):
        config.testing_securitypolicy('acct:[email protected]')
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        appstruct = schema.validate(self.valid_data())

        assert appstruct['userid'] == 'acct:[email protected]'
Exemple #5
0
    def test_it_removes_protected_fields(self, field):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        result = schema.validate(
            self.annotation_data(field='something forbidden'), )

        assert field not in result
Exemple #6
0
    def test_it_keeps_references(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

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

        appstruct = schema.validate(mock.sentinel.input_data)

        assert appstruct == AnnotationSchema.return_value.validate.return_value
Exemple #8
0
    def test_it_renames_group_to_groupid(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        assert result['groupid'] == 'foo'
        assert 'group' not in result
Exemple #9
0
    def test_it_inserts_empty_list_if_data_contains_no_tags(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        assert schema.validate(data)['tags'] == []
Exemple #10
0
    def test_it_does_not_crash_if_data_contains_no_target(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        schema.validate(data)
Exemple #11
0
    def test_it_inserts_empty_string_if_data_contains_no_text(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        assert schema.validate(data)['text'] == ''
Exemple #12
0
    def test_it_keeps_text(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        result = schema.validate(
            self.annotation_data(text='some annotation text'))

        assert result['text'] == 'some annotation text'
Exemple #13
0
    def test_it_inserts_empty_string_if_data_has_no_uri(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        assert schema.validate(data)['target_uri'] == ''
Exemple #14
0
    def test_it_raises_if_uri_is_empty_string(self):
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError) as exc:
            schema.validate(self.valid_data(uri=''))

        assert exc.value.message == "uri: 'uri' is a required property"
Exemple #15
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.CreateAnnotationSchema(self.mock_request())

        schema.validate(self.annotation_data(document=document))

        assert (parse_document_claims.document_metas_from_data.call_args[0][0]
                == document)
Exemple #16
0
def create_annotation_schema_validate(data):
    # 'uri' is required when creating new annotations.
    if 'uri' not in data:
        data['uri'] = 'http://example.com/example'

    schema = schemas.CreateAnnotationSchema(testing.DummyRequest())
    return schema.validate(data)
Exemple #17
0
    def test_it_puts_document_metas_in_appstruct(self, parse_document_claims):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        appstruct = schema.validate(self.annotation_data())

        assert appstruct['document']['document_meta_dicts'] == (
            parse_document_claims.document_metas_from_data.return_value)
Exemple #18
0
def create(request):
    """Create an annotation from the POST payload."""
    json_payload = _json_payload(request)

    # Validate the annotation for, and create the annotation in, PostgreSQL.
    if request.feature('postgres'):
        schema = schemas.CreateAnnotationSchema(request)
        appstruct = schema.validate(copy.deepcopy(json_payload))
        annotation = storage.create_annotation(request, appstruct)

    # Validate the annotation for, and create the annotation in, Elasticsearch.
    legacy_schema = schemas.LegacyCreateAnnotationSchema(request)
    legacy_appstruct = legacy_schema.validate(copy.deepcopy(json_payload))

    # When 'postgres' is on make sure that annotations in the legacy
    # Elasticsearch database use the same IDs as the PostgreSQL ones.
    if request.feature('postgres'):
        assert annotation.id
        legacy_appstruct['id'] = annotation.id

    legacy_annotation = storage.legacy_create_annotation(
        request, legacy_appstruct)

    if request.feature('postgres'):
        _publish_annotation_event(request, annotation, 'create')
        return AnnotationJSONPresenter(request, annotation).asdict()

    _publish_annotation_event(request, legacy_annotation, 'create')
    return AnnotationJSONPresenter(request, legacy_annotation).asdict()
Exemple #19
0
    def test_it_moves_extra_data_into_extra_sub_dict(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        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_sets_userid(self, authn_policy):
        authn_policy.authenticated_userid.return_value = (
            'acct:[email protected]')
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        result = schema.validate(self.annotation_data())

        assert result['userid'] == 'acct:[email protected]'
Exemple #21
0
    def test_it_raises_if_AnnotationSchema_validate_raises(
            self, AnnotationSchema):
        AnnotationSchema.return_value.validate.side_effect = (
            schemas.ValidationError('asplode'))
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        with pytest.raises(schemas.ValidationError):
            schema.validate({'foo': 'bar'})
Exemple #22
0
    def test_it_passes_input_to_AnnotationSchema_validator(
            self, AnnotationSchema):
        schema = schemas.CreateAnnotationSchema(testing.DummyRequest())

        schema.validate(mock.sentinel.input_data)

        schema.structure.validate.assert_called_once_with(
            mock.sentinel.input_data)
Exemple #23
0
def test_createannotationschema_raises_if_structure_validator_raises():
    request = testing.DummyRequest()
    schema = schemas.CreateAnnotationSchema(request)
    schema.structure = mock.Mock()
    schema.structure.validate.side_effect = schemas.ValidationError('asplode')

    with pytest.raises(schemas.ValidationError):
        schema.validate({'foo': 'bar'})
Exemple #24
0
def test_createannotationschema_permits_all_other_changes(data):
    request = testing.DummyRequest()
    schema = schemas.CreateAnnotationSchema(request)

    result = schema.validate(data)

    for k in data:
        assert result[k] == data[k]
Exemple #25
0
    def test_it_renames_uri_to_target_uri(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

        result = schema.validate(
            self.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_createannotationschema_ignores_input_user(data, authn_policy):
    """Any user field sent in the payload should be ignored."""
    authn_policy.authenticated_userid.return_value = 'acct:[email protected]'
    request = testing.DummyRequest()
    schema = schemas.CreateAnnotationSchema(request)

    result = schema.validate(data)

    assert result == {'user': '******'}
Exemple #27
0
def test_createannotationschema_removes_protected_fields(field):
    request = testing.DummyRequest()
    schema = schemas.CreateAnnotationSchema(request)
    data = {}
    data[field] = 'something forbidden'

    result = schema.validate(data)

    assert field not in result
Exemple #28
0
def test_createannotationschema_passes_input_to_structure_validator():
    request = testing.DummyRequest()
    schema = schemas.CreateAnnotationSchema(request)
    schema.structure = mock.Mock()
    schema.structure.validate.return_value = {}

    schema.validate({'foo': 'bar'})

    schema.structure.validate.assert_called_once_with({'foo': 'bar'})
Exemple #29
0
def create(request):
    """Create an annotation from the POST payload."""
    schema = schemas.CreateAnnotationSchema(request)
    appstruct = schema.validate(_json_payload(request))
    annotation = storage.create_annotation(request, appstruct)

    _publish_annotation_event(request, annotation, 'create')
    annotation_dict = AnnotationJSONPresenter(request, annotation).asdict()
    return annotation_dict
Exemple #30
0
    def test_it_inserts_empty_list_if_no_references(self):
        schema = schemas.CreateAnnotationSchema(self.mock_request())

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

        result = schema.validate(data)

        assert result['references'] == []