Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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: ')
Exemplo n.º 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: ')
Exemplo n.º 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()
Exemplo n.º 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()
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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,
        )
Exemplo n.º 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: ')
Exemplo n.º 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: ')
Exemplo n.º 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,
        )
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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()
Exemplo n.º 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'
Exemplo n.º 17
0
Arquivo: views.py Projeto: VanyTang/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(appstruct)

    _publish_annotation_event(request, annotation, 'create')
    return annotation
Exemplo n.º 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'
Exemplo n.º 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
            )
Exemplo n.º 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
            )
Exemplo n.º 21
0
Arquivo: views.py Projeto: bZichett/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')
    annotation_dict = AnnotationJSONPresenter(request, annotation).asdict()
    return annotation_dict
Exemplo n.º 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
        )
Exemplo n.º 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'
Exemplo n.º 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
            )
Exemplo n.º 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'
Exemplo n.º 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
            )
Exemplo n.º 27
0
Arquivo: views.py Projeto: 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()
Exemplo n.º 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')
Exemplo n.º 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')
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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,
        )
Exemplo n.º 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,
        )
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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