Beispiel #1
0
    def test_it_inits_an_Annotation_model(self, models):
        data = self.annotation_data()

        storage.create_annotation(self.mock_request(), copy.deepcopy(data))

        del data['document']
        models.Annotation.assert_called_once_with(**data)
Beispiel #2
0
    def test_it_inits_an_Annotation_model(self, models):
        data = self.annotation_data()

        storage.create_annotation(self.mock_request(), copy.deepcopy(data))

        del data['document']
        models.Annotation.assert_called_once_with(**data)
Beispiel #3
0
    def test_it_raises_if_user_does_not_have_permissions_for_group(self):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        with pytest.raises(schemas.ValidationError) as err:
            storage.create_annotation(self.mock_request(), data)

        assert str(err.value).startswith('group: ')
Beispiel #4
0
    def test_it_raises_if_user_does_not_have_permissions_for_group(self):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        with pytest.raises(schemas.ValidationError) as exc:
            storage.create_annotation(self.mock_request(), data)

        assert str(exc.value).startswith('group: ')
Beispiel #5
0
    def test_it_calls_first(self, models):
        """If it finds only one document it calls first()."""
        models.Document.find_or_create_by_uris.return_value = mock.Mock(
            count=mock.Mock(return_value=1))

        storage.create_annotation(self.mock_request(), self.annotation_data())

        models.Document.find_or_create_by_uris.return_value\
            .first.assert_called_once_with()
Beispiel #6
0
    def test_it_calls_first(self, models):
        """If it finds only one document it calls first()."""
        models.Document.find_or_create_by_uris.return_value = mock.Mock(
            count=mock.Mock(return_value=1))

        storage.create_annotation(self.mock_request(), self.annotation_data())

        models.Document.find_or_create_by_uris.return_value\
            .first.assert_called_once_with()
Beispiel #7
0
    def test_it_updates_document_updated(self, models):
        yesterday = "yesterday"
        document = models.merge_documents.return_value = mock.Mock(
            updated=yesterday)
        models.Document.find_or_create_by_uris.return_value.first\
            .return_value = document

        storage.create_annotation(self.mock_request(), self.annotation_data())

        assert document.updated == models.Annotation.return_value.updated
Beispiel #8
0
    def test_it_updates_document_updated(self, models):
        yesterday = "yesterday"
        document = models.merge_documents.return_value = mock.Mock(
            updated=yesterday)
        models.Document.find_or_create_by_uris.return_value.first\
            .return_value = document

        storage.create_annotation(self.mock_request(), self.annotation_data())

        assert document.updated == models.Annotation.return_value.updated
Beispiel #9
0
    def test_it_calls_merge_documents(self, models):
        """If it finds more than one document it calls merge_documents()."""
        models.Document.find_or_create_by_uris.return_value = mock.Mock(
            count=mock.Mock(return_value=3))
        request = self.mock_request()

        storage.create_annotation(request, self.annotation_data())

        models.merge_documents.assert_called_once_with(
            request.db,
            models.Document.find_or_create_by_uris.return_value,
            updated=models.Annotation.return_value.updated,
        )
Beispiel #10
0
    def test_it_raises_if_parent_annotation_does_not_exist(self,
                                                           fetch_annotation):
        fetch_annotation.return_value = None

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        with pytest.raises(schemas.ValidationError) as exc:
            storage.create_annotation(self.mock_request(), data)

        assert str(exc.value).startswith('references.0: ')
Beispiel #11
0
    def test_it_raises_if_parent_annotation_does_not_exist(self,
                                                           fetch_annotation):
        fetch_annotation.return_value = None

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        with pytest.raises(schemas.ValidationError) as err:
            storage.create_annotation(self.mock_request(), data)

        assert str(err.value).startswith('references.0: ')
Beispiel #12
0
    def test_it_calls_merge_documents(self, models):
        """If it finds more than one document it calls merge_documents()."""
        models.Document.find_or_create_by_uris.return_value = mock.Mock(
            count=mock.Mock(return_value=3))
        request = self.mock_request()

        storage.create_annotation(request, self.annotation_data())

        models.merge_documents.assert_called_once_with(
            request.db,
            models.Document.find_or_create_by_uris.return_value,
            updated=models.Annotation.return_value.updated,
        )
Beispiel #13
0
    def test_it_updates_the_document_metadata_from_the_annotation(
            self, models, update_document_metadata):
        request = self.mock_request()
        annotation_data = self.annotation_data()
        annotation_data['document']['document_meta_dicts'] = (
            mock.sentinel.document_meta_dicts)
        annotation_data['document']['document_uri_dicts'] = (
            mock.sentinel.document_uri_dicts)

        storage.create_annotation(request, annotation_data)

        update_document_metadata.assert_called_once_with(
            request.db, models.Annotation.return_value,
            mock.sentinel.document_meta_dicts,
            mock.sentinel.document_uri_dicts)
Beispiel #14
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'):
        annotation_dict = (
            AnnotationJSONPresenter(request, annotation).asdict())
    else:
        annotation_dict = (
            AnnotationJSONPresenter(request, legacy_annotation).asdict())

    _publish_annotation_event(request, annotation_dict, 'create')

    return annotation_dict
Beispiel #15
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()
Beispiel #16
0
    def test_it_sets_group_for_replies(self, config, fetch_annotation, models):
        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        config.testing_securitypolicy('acct:[email protected]',
                                      groupids=['group:test-group'])

        data = self.annotation_data()
        assert data['groupid'] != 'test-group'

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(self.mock_request(), data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Beispiel #17
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(appstruct)

    _publish_annotation_event(request, annotation, 'create')
    return annotation
Beispiel #18
0
    def test_it_sets_group_for_replies(self, authn_policy, fetch_annotation,
                                       models):
        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        authn_policy.effective_principals.return_value = ['group:test-group']

        data = self.annotation_data()
        assert data['groupid'] != 'test-group'

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(self.mock_request(), data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Beispiel #19
0
    def test_it_calls_create_or_update_document_meta(self, models):
        models.Document.find_or_create_by_uris.return_value.count\
            .return_value = 1

        request = self.mock_request()

        annotation = models.Annotation.return_value

        annotation_data = self.annotation_data()
        annotation_data['document']['document_meta_dicts'] = [
            {
                'claimant': 'http://example.com/claimant',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'type': 'title',
                'value': 'foo',
            },
            {
                'type': 'article title',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'value': 'bar',
                'claimant': 'http://example.com/claimant',
            },
            {
                'type': 'site title',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'value': 'gar',
                'claimant': 'http://example.com/claimant',
            },
        ]

        storage.create_annotation(request, copy.deepcopy(annotation_data))

        assert models.create_or_update_document_meta.call_count == 3
        for document_meta_dict in annotation_data['document'][
                'document_meta_dicts']:
            models.create_or_update_document_meta.assert_any_call(
                session=request.db,
                document=models.Document.find_or_create_by_uris.return_value.first.return_value,
                created=annotation.created,
                updated=annotation.updated,
                **document_meta_dict
            )
Beispiel #20
0
    def test_it_calls_create_or_update_document_meta(self, models):
        models.Document.find_or_create_by_uris.return_value.count\
            .return_value = 1

        request = self.mock_request()

        annotation = models.Annotation.return_value

        annotation_data = self.annotation_data()
        annotation_data['document']['document_meta_dicts'] = [
            {
                'claimant': 'http://example.com/claimant',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'type': 'title',
                'value': 'foo',
            },
            {
                'type': 'article title',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'value': 'bar',
                'claimant': 'http://example.com/claimant',
            },
            {
                'type': 'site title',
                'claimant_normalized':
                    'http://example.com/claimant_normalized',
                'value': 'gar',
                'claimant': 'http://example.com/claimant',
            },
        ]

        storage.create_annotation(request, copy.deepcopy(annotation_data))

        assert models.create_or_update_document_meta.call_count == 3
        for document_meta_dict in annotation_data['document'][
                'document_meta_dicts']:
            models.create_or_update_document_meta.assert_any_call(
                session=request.db,
                document=models.Document.find_or_create_by_uris.return_value.first.return_value,
                created=annotation.created,
                updated=annotation.updated,
                **document_meta_dict
            )
Beispiel #21
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
Beispiel #22
0
    def test_it_updates_the_document_metadata_from_the_annotation(self,
                                                                  models,
                                                                  pyramid_request,
                                                                  update_document_metadata):
        annotation_data = self.annotation_data()
        annotation_data['document']['document_meta_dicts'] = (
            mock.sentinel.document_meta_dicts)
        annotation_data['document']['document_uri_dicts'] = (
            mock.sentinel.document_uri_dicts)

        storage.create_annotation(pyramid_request, annotation_data)

        update_document_metadata.assert_called_once_with(
            pyramid_request.db,
            models.Annotation.return_value,
            mock.sentinel.document_meta_dicts,
            mock.sentinel.document_uri_dicts
        )
Beispiel #23
0
    def test_it_sets_group_for_replies(self,
                                       authn_policy,
                                       fetch_annotation,
                                       models):
        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        authn_policy.effective_principals.return_value = ['group:test-group']

        data = self.annotation_data()
        assert data['groupid'] != 'test-group'

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(self.mock_request(), data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Beispiel #24
0
    def test_it_calls_create_or_update_document_uri(
            self,
            models):
        models.Document.find_or_create_by_uris.return_value.count\
            .return_value = 1

        request = self.mock_request()

        annotation = models.Annotation.return_value

        annotation_data = self.annotation_data()
        annotation_data['document']['document_uri_dicts'] = [
            {
                'uri': 'http://example.com/example_1',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_2',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_3',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
        ]

        storage.create_annotation(request, copy.deepcopy(annotation_data))

        assert models.create_or_update_document_uri.call_count == 3
        for doc_uri_dict in annotation_data['document']['document_uri_dicts']:
            models.create_or_update_document_uri.assert_any_call(
                session=request.db,
                document=models.Document.find_or_create_by_uris.return_value.first.return_value,
                created=annotation.created,
                updated=annotation.updated,
                **doc_uri_dict
            )
Beispiel #25
0
    def test_it_sets_group_for_replies(self,
                                       config,
                                       fetch_annotation,
                                       models):
        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        config.testing_securitypolicy('acct:[email protected]', groupids=['group:test-group'])

        data = self.annotation_data()
        assert data['groupid'] != 'test-group'

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(self.mock_request(), data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Beispiel #26
0
    def test_it_calls_create_or_update_document_uri(
            self,
            models):
        models.Document.find_or_create_by_uris.return_value.count\
            .return_value = 1

        request = self.mock_request()

        annotation = models.Annotation.return_value

        annotation_data = self.annotation_data()
        annotation_data['document']['document_uri_dicts'] = [
            {
                'uri': 'http://example.com/example_1',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_2',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_3',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
        ]

        storage.create_annotation(request, copy.deepcopy(annotation_data))

        assert models.create_or_update_document_uri.call_count == 3
        for doc_uri_dict in annotation_data['document']['document_uri_dicts']:
            models.create_or_update_document_uri.assert_any_call(
                session=request.db,
                document=models.Document.find_or_create_by_uris.return_value.first.return_value,
                created=annotation.created,
                updated=annotation.updated,
                **doc_uri_dict
            )
Beispiel #27
0
Datei: views.py Projekt: hashin/h
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')

    presenter = AnnotationJSONPresenter(annotation)
    return presenter.asdict()
Beispiel #28
0
    def test_it_fetches_parent_annotation_for_replies(self, config,
                                                      fetch_annotation):
        request = self.mock_request()

        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        config.testing_securitypolicy('acct:[email protected]',
                                      groupids=['group:test-group'])

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(request, data)

        fetch_annotation.assert_called_once_with(request.db,
                                                 'parent_annotation_id')
Beispiel #29
0
    def test_it_fetches_parent_annotation_for_replies(self,
                                                      config,
                                                      fetch_annotation):
        request = self.mock_request()

        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        config.testing_securitypolicy('acct:[email protected]', groupids=['group:test-group'])

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(request, data)

        fetch_annotation.assert_called_once_with(request.db,
                                                 'parent_annotation_id')
Beispiel #30
0
    def test_it_fetches_parent_annotation_for_replies(self, authn_policy,
                                                      fetch_annotation):
        request = self.mock_request()

        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        authn_policy.effective_principals.return_value = ['group:test-group']

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(request, data)

        fetch_annotation.assert_called_once_with(request,
                                                 'parent_annotation_id',
                                                 _postgres=True)
Beispiel #31
0
    def test_it_fetches_parent_annotation_for_replies(self,
                                                      authn_policy,
                                                      fetch_annotation):
        request = self.mock_request()

        # Make the annotation's parent belong to 'test-group'.
        fetch_annotation.return_value.groupid = 'test-group'

        # The request will need permission to write to 'test-group'.
        authn_policy.effective_principals.return_value = ['group:test-group']

        data = self.annotation_data()

        # The annotation is a reply.
        data['references'] = ['parent_annotation_id']

        storage.create_annotation(request, data)

        fetch_annotation.assert_called_once_with(request,
                                                 'parent_annotation_id',
                                                 _postgres=True)
Beispiel #32
0
    def test_it_calls_find_or_create_by_uris(self, models):
        request = self.mock_request()
        annotation = models.Annotation.return_value
        annotation_data = self.annotation_data()
        annotation_data['document']['document_uri_dicts'] = [
            {
                'uri': 'http://example.com/example_1',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_2',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_3',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
        ]

        storage.create_annotation(request, annotation_data)

        models.Document.find_or_create_by_uris.assert_called_once_with(
            request.db,
            annotation.target_uri,
            [
                'http://example.com/example_1',
                'http://example.com/example_2',
                'http://example.com/example_3',
            ],
            created=annotation.created,
            updated=annotation.updated,
        )
Beispiel #33
0
    def test_it_calls_find_or_create_by_uris(self, models):
        request = self.mock_request()
        annotation = models.Annotation.return_value
        annotation_data = self.annotation_data()
        annotation_data['document']['document_uri_dicts'] = [
            {
                'uri': 'http://example.com/example_1',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_2',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
            {
                'uri': 'http://example.com/example_3',
                'claimant': 'http://example.com/claimant',
                'type': 'type',
                'content_type': None,
            },
        ]

        storage.create_annotation(request, annotation_data)

        models.Document.find_or_create_by_uris.assert_called_once_with(
            request.db,
            annotation.target_uri,
            [
                'http://example.com/example_1',
                'http://example.com/example_2',
                'http://example.com/example_3',
            ],
            created=annotation.created,
            updated=annotation.updated,
        )
Beispiel #34
0
    def test_it_adds_the_annotation_to_the_database(self, models):
        request = self.mock_request()

        storage.create_annotation(request, self.annotation_data())

        request.db.add.assert_called_once_with(models.Annotation.return_value)
Beispiel #35
0
    def test_it_does_not_crash_if_no_text_or_tags(self):
        # Highlights have no text or tags.
        data = self.annotation_data()
        data['text'] = data['tags'] = ''

        storage.create_annotation(self.mock_request(), data)
Beispiel #36
0
    def test_it_returns_the_annotation(self, models, pyramid_request):
        annotation = storage.create_annotation(pyramid_request,
                                               self.annotation_data())

        assert annotation == models.Annotation.return_value
Beispiel #37
0
    def test_it_returns_the_annotation(self, models):
        annotation = storage.create_annotation(self.mock_request(),
                                               self.annotation_data())

        assert annotation == models.Annotation.return_value
Beispiel #38
0
    def test_it_does_not_crash_if_target_selectors_is_empty(self):
        # Page notes have [] for target_selectors.
        data = self.annotation_data()
        data['target_selectors'] = []

        storage.create_annotation(self.mock_request(), data)
Beispiel #39
0
    def test_it_adds_the_annotation_to_the_database(self, models):
        request = self.mock_request()

        storage.create_annotation(request, self.annotation_data())

        request.db.add.assert_called_once_with(models.Annotation.return_value)
Beispiel #40
0
    def test_it_returns_the_annotation(self, models):
        annotation = storage.create_annotation(self.mock_request(),
                                               self.annotation_data())

        assert annotation == models.Annotation.return_value
Beispiel #41
0
    def test_it_does_not_crash_if_target_selectors_is_empty(self):
        # Page notes have [] for target_selectors.
        data = self.annotation_data()
        data['target_selectors'] = []

        storage.create_annotation(self.mock_request(), data)
Beispiel #42
0
    def test_it_does_not_crash_if_no_text_or_tags(self):
        # Highlights have no text or tags.
        data = self.annotation_data()
        data['text'] = data['tags'] = ''

        storage.create_annotation(self.mock_request(), data)
Beispiel #43
0
    def test_it_adds_the_annotation_to_the_database(self, models, pyramid_request):
        storage.create_annotation(pyramid_request, self.annotation_data())

        assert models.Annotation.return_value in pyramid_request.db.added