Example #1
0
    def test_annotation_calls_get(self):
        request = mock_request()
        views.AnnotationController(request).annotation()

        request.es.get.assert_called_once_with(
            index="hypothesis", doc_type="annotation", id="AVLlVTs1f9G3pW-EYc6q"
        )
Example #2
0
    def test_annotation_raises_http_unprocessable_entity_for_file_urls(
            self, parse_document):
        parse_document.return_value["document_uri"] = (
            "file:///home/seanh/Foo.pdf")

        with pytest.raises(httpexceptions.HTTPUnprocessableEntity):
            views.AnnotationController(mock_request()).annotation()
Example #3
0
    def test_annotation_calls_get(self, elasticsearch_client):
        views.AnnotationController(mock_request()).annotation()

        elasticsearch_client.return_value.get.assert_called_once_with(
            index="hypothesis",
            doc_type="annotation",
            id="AVLlVTs1f9G3pW-EYc6q")
Example #4
0
    def test_annotation_truncates_pretty_url(self, parse_document):
        parse_document.return_value[1] = (
            "http://www.abcdefghijklmnopqrst.com/example.html")

        template_data = views.AnnotationController(mock_request()).annotation()

        assert template_data["pretty_url"] == "www.abcdefghijklmnop…"
Example #5
0
    def test_annotation_omits_via_url_for_third_party_annotations(self, parse_document):
        parse_document.return_value["authority"] = "partner.org"
        template_data = views.AnnotationController(mock_request()).annotation()

        data = json.loads(template_data["data"])

        assert data["viaUrl"] is None
Example #6
0
    def test_annotation_raises_if_parse_document_raises(self, parse_document):
        parse_document.side_effect = util.InvalidAnnotationError(
            "error message", "the_reason")

        with pytest.raises(httpexceptions.HTTPUnprocessableEntity) as exc:
            views.AnnotationController(mock_request()).annotation()
        assert str(exc.value) == "error message"
Example #7
0
    def test_annotation_raises_http_not_found_if_annotation_deleted(
        self, parse_document
    ):
        parse_document.side_effect = util.DeletedAnnotationError()

        with pytest.raises(httpexceptions.HTTPNotFound):
            views.AnnotationController(mock_request()).annotation()
Example #8
0
    def test_annotation_returns_extension_url(self):
        template_data = views.AnnotationController(mock_request()).annotation()

        data = json.loads(template_data["data"])
        assert data["extensionUrl"] == (
            "http://www.example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q"
        )
Example #9
0
    def test_omits_via_url_if_url_embeds_client(self, url_embeds_client):
        url_embeds_client.return_value = True

        template_data = views.AnnotationController(mock_request()).annotation()
        data = json.loads(template_data["data"])

        url_embeds_client.assert_called_with("http://www.example.com/example.html")
        assert data["viaUrl"] is None
Example #10
0
    def test_annotation_strips_bare_fragment_identifiers(self, parse_document):
        parse_document.return_value[1] = "http://example.com/example.html#"
        template_data = views.AnnotationController(mock_request()).annotation()

        data = json.loads(template_data["data"])

        assert data["extensionUrl"] == (
            "http://example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q")
        assert data["viaUrl"] == (
            "https://via.hypothes.is/http://example.com/example.html#annotations:AVLlVTs1f9G3pW-EYc6q"
        )
Example #11
0
    def test_annotation_increments_stat_for_file_URLs(self, parse_document,
                                                      statsd):
        parse_document.return_value[1] = "file:///home/seanh/Foo.pdf"

        try:
            views.AnnotationController(mock_request()).annotation()
        except:
            pass

        statsd.incr.assert_called_once_with(
            "views.annotation.422.not_an_http_or_https_document")
Example #12
0
    def test_annotation_increments_stat_if_parse_document_raises(
            self, parse_document, statsd):
        parse_document.side_effect = util.InvalidAnnotationError(
            "error message", "the_reason")

        try:
            views.AnnotationController(mock_request()).annotation()
        except:
            pass

        statsd.incr.assert_called_once_with("views.annotation.422.the_reason")
Example #13
0
    def test_annotation_increments_stat_if_get_raises_not_found_error(self,
                                                                      statsd):
        request = mock_request()
        request.es.get.side_effect = es_exceptions.NotFoundError

        try:
            views.AnnotationController(request).annotation()
        except:
            pass

        statsd.incr.assert_called_once_with(
            "views.annotation.404.annotation_not_found")
Example #14
0
    def test_annotation_increments_stat_if_get_raises_NotFoundError(
            self, elasticsearch_client, statsd):
        elasticsearch_client.return_value.get.side_effect = (
            exceptions.NotFoundError)

        try:
            views.AnnotationController(mock_request()).annotation()
        except:
            pass

        statsd.incr.assert_called_once_with(
            "views.annotation.404.annotation_not_found")
Example #15
0
    def test_annotation_increments_stat_when_annotation_found(self, statsd):
        views.AnnotationController(mock_request()).annotation()

        statsd.incr.assert_called_once_with(
            "views.annotation.200.annotation_found")
Example #16
0
 def test_annotation_returns_quote(self):
     template_data = views.AnnotationController(mock_request()).annotation()
     quote = template_data["quote"]
     assert quote == "Hypothesis annotation for www.example.com"
Example #17
0
    def test_annotation_calls_parse_document(self, elasticsearch_client,
                                             parse_document):
        views.AnnotationController(mock_request()).annotation()

        parse_document.assert_called_once_with(
            elasticsearch_client.return_value.get.return_value)
Example #18
0
 def test_annotation_raises_HTTPNotFound_if_get_raises_NotFoundError(
         self, elasticsearch_client):
     elasticsearch_client.return_value.get.side_effect = (
         exceptions.NotFoundError)
     with pytest.raises(httpexceptions.HTTPNotFound):
         views.AnnotationController(mock_request()).annotation()
Example #19
0
    def test_annotation_calls_elasticsearch_client(self, elasticsearch_client):
        request = mock_request()
        views.AnnotationController(request).annotation()

        elasticsearch_client.assert_called_once_with(request.registry.settings)
Example #20
0
    def test_annotation_raises_http_not_found_if_get_raises_not_found(self):
        request = mock_request()
        request.es.get.side_effect = es_exceptions.NotFoundError

        with pytest.raises(httpexceptions.HTTPNotFound):
            views.AnnotationController(request).annotation()
Example #21
0
    def test_annotation_returns_pretty_url(self):
        template_data = views.AnnotationController(mock_request()).annotation()

        assert template_data["pretty_url"] == "www.example.com"
Example #22
0
    def test_annotation_calls_parse_document(self, parse_document):
        request = mock_request()

        views.AnnotationController(request).annotation()

        parse_document.assert_called_once_with(request.es.get.return_value)
Example #23
0
    def test_annotation_returns_chrome_extension_id(self):
        template_data = views.AnnotationController(mock_request()).annotation()

        data = json.loads(template_data["data"])
        assert data["chromeExtensionId"] == "test-extension-id"