Exemplo n.º 1
0
    def test_it_inits_an_Annotation_model(self, models, pyramid_request):
        data = self.annotation_data()

        storage.create_annotation(pyramid_request, copy.deepcopy(data))

        del data['document']
        models.Annotation.assert_called_once_with(**data)
Exemplo n.º 2
0
    def test_it_does_not_crash_if_target_selectors_is_empty(
            self, pyramid_request):
        # Page notes have [] for target_selectors.
        data = self.annotation_data()
        data['target_selectors'] = []

        storage.create_annotation(pyramid_request, data)
Exemplo n.º 3
0
    def test_it_finds_the_group(self, pyramid_request, pyramid_config, groups_find):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        storage.create_annotation(pyramid_request, data)

        groups_find.assert_called_once_with(pyramid_request, 'foo-group')
Exemplo n.º 4
0
    def test_it_inits_an_Annotation_model(self, models, pyramid_request):
        data = self.annotation_data()

        storage.create_annotation(pyramid_request, copy.deepcopy(data))

        del data['document']
        models.Annotation.assert_called_once_with(**data)
Exemplo n.º 5
0
    def test_it_finds_the_group(self, pyramid_request, pyramid_config, group_service):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        storage.create_annotation(pyramid_request, data, group_service)

        group_service.find.assert_called_once_with('foo-group')
Exemplo n.º 6
0
    def test_it_raises_if_user_does_not_have_permissions_for_group(self, pyramid_request):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

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

        assert str(exc.value).startswith('group: ')
Exemplo n.º 7
0
    def test_it_raises_if_user_does_not_have_permissions_for_group(
            self, pyramid_request):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

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

        assert str(exc.value).startswith('group: ')
Exemplo n.º 8
0
    def test_it_raises_when_group_could_not_be_found(self, pyramid_request, pyramid_config, groups_find):
        pyramid_config.testing_securitypolicy('userid', permissive=True)
        groups_find.return_value = None

        data = self.annotation_data()
        data['groupid'] = 'missing-group'

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

        assert str(exc.value).startswith('group: ')
Exemplo n.º 9
0
    def test_it_raises_when_user_is_missing_write_permission(self, pyramid_request, pyramid_config, group_service):
        pyramid_config.testing_securitypolicy('userid', permissive=False)
        group_service.find.return_value = FakeGroup()

        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        with pytest.raises(schemas.ValidationError) as exc:
            storage.create_annotation(pyramid_request, data, group_service)

        assert str(exc.value).startswith('group: ')
Exemplo n.º 10
0
    def test_it_raises_when_user_is_missing_write_permission(self, pyramid_request, pyramid_config, groups_find):
        pyramid_config.testing_securitypolicy('userid', permissive=False)
        groups_find.return_value = FakeGroup()

        data = self.annotation_data()
        data['groupid'] = 'foo-group'

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

        assert str(exc.value).startswith('group: ')
Exemplo n.º 11
0
    def test_it_raises_when_group_could_not_be_found(self, pyramid_request, pyramid_config, group_service):
        pyramid_config.testing_securitypolicy('userid', permissive=True)
        group_service.find.return_value = None

        data = self.annotation_data()
        data['groupid'] = 'missing-group'

        with pytest.raises(schemas.ValidationError) as exc:
            storage.create_annotation(pyramid_request, data, group_service)

        assert str(exc.value).startswith('group: ')
Exemplo n.º 12
0
    def test_it_raises_if_parent_annotation_does_not_exist(
            self, fetch_annotation, pyramid_request):
        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(pyramid_request, data)

        assert str(exc.value).startswith('references.0: ')
Exemplo n.º 13
0
    def test_it_raises_if_parent_annotation_does_not_exist(self,
                                                           fetch_annotation,
                                                           pyramid_request):
        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(pyramid_request, data)

        assert str(exc.value).startswith('references.0: ')
Exemplo n.º 14
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.º 15
0
    def test_it_allows_when_no_groupfinder_is_configured(self, pyramid_request, models):
        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        # this should not raise
        result = storage.create_annotation(pyramid_request, data)

        assert result == models.Annotation.return_value
Exemplo n.º 16
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.º 17
0
    def test_it_sets_group_for_replies(self, fetch_annotation, models,
                                       pyramid_config, pyramid_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'.
        pyramid_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(pyramid_request, data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Exemplo n.º 18
0
    def test_it_sets_the_annotations_document_id(self, models,
                                                 pyramid_request):
        annotation_data = self.annotation_data()

        document = mock.Mock()
        models.update_document_metadata.return_value = document

        ann = storage.create_annotation(pyramid_request, annotation_data)

        assert ann.document == document
Exemplo n.º 19
0
    def test_it_allows_when_user_has_write_permission(self, pyramid_request, pyramid_config, models, groups_find):
        pyramid_config.testing_securitypolicy('userid', permissive=True)
        groups_find.return_value = FakeGroup()

        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        # this should not raise
        result = storage.create_annotation(pyramid_request, data)

        assert result == models.Annotation.return_value
Exemplo n.º 20
0
    def test_it_allows_when_user_has_write_permission(self, pyramid_request, pyramid_config, models, group_service):
        pyramid_config.testing_securitypolicy('userid', permissive=True)
        group_service.find.return_value = FakeGroup()

        data = self.annotation_data()
        data['groupid'] = 'foo-group'

        # this should not raise
        result = storage.create_annotation(pyramid_request, data, group_service)

        assert result == models.Annotation.return_value
Exemplo n.º 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')

    links_service = request.find_service(name='links')
    presenter = AnnotationJSONPresenter(annotation, links_service)
    return presenter.asdict()
Exemplo n.º 22
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')

    links_service = request.find_service(name='links')
    presenter = AnnotationJSONPresenter(annotation, links_service)
    return presenter.asdict()
Exemplo n.º 23
0
    def test_it_fetches_parent_annotation_for_replies(self, fetch_annotation,
                                                      pyramid_config,
                                                      pyramid_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'.
        pyramid_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(pyramid_request, data)

        fetch_annotation.assert_called_once_with(pyramid_request.db,
                                                 'parent_annotation_id')
Exemplo n.º 24
0
    def test_it_sets_the_annotations_document_id(self,
                                                 models,
                                                 pyramid_request):
        annotation_data = self.annotation_data()

        document = mock.Mock()
        models.update_document_metadata.return_value = document

        ann = storage.create_annotation(pyramid_request, annotation_data)

        assert ann.document == document
Exemplo n.º 25
0
    def test_it_fetches_parent_annotation_for_replies(self,
                                                      fetch_annotation,
                                                      pyramid_config,
                                                      pyramid_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'.
        pyramid_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(pyramid_request, data)

        fetch_annotation.assert_called_once_with(pyramid_request.db,
                                                 'parent_annotation_id')
Exemplo n.º 26
0
    def test_it_updates_the_document_metadata_from_the_annotation(self,
                                                                  models,
                                                                  pyramid_request,
                                                                  datetime,
                                                                  group_service):
        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, group_service)

        models.update_document_metadata.assert_called_once_with(
            pyramid_request.db,
            models.Annotation.return_value.target_uri,
            mock.sentinel.document_meta_dicts,
            mock.sentinel.document_uri_dicts,
            created=datetime.utcnow(),
            updated=datetime.utcnow(),
        )
Exemplo n.º 27
0
    def test_it_sets_group_for_replies(self,
                                       fetch_annotation,
                                       models,
                                       pyramid_config,
                                       pyramid_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'.
        pyramid_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(pyramid_request, data)

        assert models.Annotation.call_args[1]['groupid'] == 'test-group'
Exemplo n.º 28
0
    def test_it_does_not_crash_if_no_text_or_tags(self, pyramid_request):
        # Highlights have no text or tags.
        data = self.annotation_data()
        data['text'] = data['tags'] = ''

        storage.create_annotation(pyramid_request, data)
Exemplo n.º 29
0
    def test_it_does_not_crash_if_target_selectors_is_empty(self, pyramid_request):
        # Page notes have [] for target_selectors.
        data = self.annotation_data()
        data['target_selectors'] = []

        storage.create_annotation(pyramid_request, data)
Exemplo n.º 30
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.º 31
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
Exemplo n.º 32
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
Exemplo n.º 33
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.º 34
0
    def test_it_does_not_crash_if_no_text_or_tags(self, pyramid_request):
        # Highlights have no text or tags.
        data = self.annotation_data()
        data['text'] = data['tags'] = ''

        storage.create_annotation(pyramid_request, data)