From 2166d28a218e78313d6d38064af866fe95e4f0e6 Mon Sep 17 00:00:00 2001 From: Jon Betts Date: Wed, 3 May 2023 18:04:19 +0100 Subject: [PATCH] Use the new annotation create method in the API views --- h/views/api/annotations.py | 5 +- tests/h/views/api/annotations_test.py | 90 +++++++++++---------------- 2 files changed, 41 insertions(+), 54 deletions(-) diff --git a/h/views/api/annotations.py b/h/views/api/annotations.py index 40bf730f359..0cfee9f530b 100644 --- a/h/views/api/annotations.py +++ b/h/views/api/annotations.py @@ -27,6 +27,7 @@ ) from h.schemas.util import validate_query_params from h.security import Permission +from h.services import AnnotationWriteService from h.views.api.config import api_config from h.views.api.exceptions import PayloadError @@ -78,7 +79,9 @@ def create(request): schema = CreateAnnotationSchema(request) appstruct = schema.validate(_json_payload(request)) - annotation = storage.create_annotation(request, appstruct) + annotation = request.find_service(AnnotationWriteService).create_annotation( + data=appstruct + ) _publish_annotation_event(request, annotation, "create") diff --git a/tests/h/views/api/annotations_test.py b/tests/h/views/api/annotations_test.py index 4a7e50ab9de..06c2e571738 100644 --- a/tests/h/views/api/annotations_test.py +++ b/tests/h/views/api/annotations_test.py @@ -8,6 +8,7 @@ from h.services.annotation_delete import AnnotationDeleteService from h.traversal import AnnotationContext from h.views.api import annotations as views +from h.views.api.exceptions import PayloadError @pytest.mark.usefixtures("annotation_json_service", "search_lib") @@ -85,71 +86,44 @@ def search_run(self, search_lib): return search_lib.Search.return_value.run -@pytest.mark.usefixtures( - "AnnotationEvent", - "create_schema", - "links_service", - "annotation_json_service", - "storage", -) class TestCreate: - def test_it(self, pyramid_request, create_schema, storage, annotation_json_service): + def test_it( + self, + pyramid_request, + CreateAnnotationSchema, + annotation_write_service, + annotation_json_service, + AnnotationEvent, + ): result = views.create(pyramid_request) - create_schema.assert_called_once_with(pyramid_request) - create_schema.return_value.validate.assert_called_once_with( - pyramid_request.json_body - ) - storage.create_annotation.assert_called_once_with( - pyramid_request, create_schema.return_value.validate.return_value - ) - - annotation_json_service.present_for_user.assert_called_once_with( - annotation=storage.create_annotation.return_value, user=pyramid_request.user + # Check we validate + CreateAnnotationSchema.assert_called_once_with(pyramid_request) + schema = CreateAnnotationSchema.return_value + schema.validate.assert_called_once_with(pyramid_request.json_body) + # Check we create + annotation_write_service.create_annotation.assert_called_once_with( + data=schema.validate.return_value ) - - assert result == annotation_json_service.present_for_user.return_value - - def test_it_publishes_annotation_event( - self, AnnotationEvent, pyramid_request, storage - ): - views.create(pyramid_request) - - annotation = storage.create_annotation.return_value + annotation = annotation_write_service.create_annotation.return_value + # Check the event is raised AnnotationEvent.assert_called_once_with( pyramid_request, annotation.id, "create" ) pyramid_request.notify_after_commit.assert_called_once_with( AnnotationEvent.return_value ) + # Check we present + annotation_json_service.present_for_user.assert_called_once_with( + annotation=annotation, user=pyramid_request.user + ) + assert result == annotation_json_service.present_for_user.return_value - def test_it_raises_if_json_parsing_fails(self, pyramid_request): - """It raises PayloadError if parsing of the request body fails.""" - # Make accessing the request.json_body property raise ValueError. - type(pyramid_request).json_body = {} - with mock.patch.object( - type(pyramid_request), "json_body", new_callable=mock.PropertyMock - ) as json_body: - json_body.side_effect = ValueError() - with pytest.raises(views.PayloadError): - views.create(pyramid_request) - - def test_it_raises_if_validate_raises(self, pyramid_request, create_schema): - create_schema.return_value.validate.side_effect = ValidationError("asplode") - - with pytest.raises(ValidationError) as exc: - views.create(pyramid_request) - - assert str(exc.value) == "asplode" - - def test_it_raises_if_create_annotation_raises(self, pyramid_request, storage): - storage.create_annotation.side_effect = ValidationError("asplode") - - with pytest.raises(ValidationError) as exc: + @pytest.mark.usefixtures("with_invalid_json_body") + def test_it_raises_for_invalid_json(self, pyramid_request): + with pytest.raises(PayloadError): views.create(pyramid_request) - assert str(exc.value) == "asplode" - @pytest.fixture def pyramid_request(self, pyramid_request): pyramid_request.json_body = {} @@ -157,7 +131,7 @@ def pyramid_request(self, pyramid_request): return pyramid_request @pytest.fixture - def create_schema(self, patch): + def CreateAnnotationSchema(self, patch): return patch("h.views.api.annotations.CreateAnnotationSchema") @@ -339,6 +313,16 @@ def annotation(factories): return factories.Annotation() +@pytest.fixture +def with_invalid_json_body(pyramid_request): + type(pyramid_request).json_body = None + with mock.patch.object( + type(pyramid_request), "json_body", new_callable=mock.PropertyMock + ) as json_body: + json_body.side_effect = ValueError() + yield json_body + + @pytest.fixture def pyramid_request(pyramid_request): pyramid_request.notify_after_commit = mock.Mock(spec_set=[])