def test_feed_title(): feed = atom.feed_from_annotations([], mock.Mock(), mock.Mock(), title="foo") assert feed["title"] == "foo"
def test_feed_updated(): annotations = [ _annotation(updated=datetime(year=2015, month=3, day=11, hour=10, minute=45, second=54, microsecond=537626)), _annotation(updated=datetime(year=2015, month=2, day=11, hour=10, minute=43, second=54, microsecond=537626)), _annotation(updated=datetime(year=2015, month=1, day=11, hour=10, minute=43, second=54, microsecond=537626)) ] feed = atom.feed_from_annotations(annotations, "atom_url", lambda annotation: "annotation url") assert feed['updated'] == '2015-03-11T10:45:54.537626+00:00'
def test_feed_contains_entries(_feed_entry_from_annotation, factories): """The feed should contain an entry for each annotation.""" annotations = [ factories.Annotation(), factories.Annotation(), factories.Annotation() ] annotations_url_function = mock.Mock() annotations_api_url_function = mock.Mock() entries = [ "feed entry for annotation 1", "feed entry for annotation 2", "feed entry for annotation 3" ] def pop(*args, **kwargs): return entries.pop(0) _feed_entry_from_annotation.side_effect = pop feed = atom.feed_from_annotations(annotations, annotations_url_function, annotations_api_url_function) assert feed['entries'] == [ "feed entry for annotation 1", "feed entry for annotation 2", "feed entry for annotation 3" ]
def test_feed_subtitle(): feed = atom.feed_from_annotations([], mock.Mock(), mock.Mock(), subtitle='bar') assert feed['subtitle'] == 'bar'
def test_feed_title(): feed = atom.feed_from_annotations([], mock.Mock(), mock.Mock(), title='foo') assert feed['title'] == 'foo'
def test_feed_contains_entries(_feed_entry_from_annotation, factories): """The feed should contain an entry for each annotation.""" annotations = [ factories.Annotation(), factories.Annotation(), factories.Annotation(), ] annotations_url_function = mock.Mock() annotations_api_url_function = mock.Mock() entries = [ "feed entry for annotation 1", "feed entry for annotation 2", "feed entry for annotation 3", ] def pop(*args, **kwargs): return entries.pop(0) _feed_entry_from_annotation.side_effect = pop feed = atom.feed_from_annotations( annotations, annotations_url_function, annotations_api_url_function ) assert feed["entries"] == [ "feed entry for annotation 1", "feed entry for annotation 2", "feed entry for annotation 3", ]
def test_html_url_link(): """The feed should contain a link to its corresponding HTML page.""" feed = atom.feed_from_annotations( [], mock.Mock(), mock.Mock(), html_url='html_url') assert feed['links'][1] == { 'rel': 'alternate', 'type': 'text/html', 'href': 'html_url'}
def test_feed_subtitle(): feed = atom.feed_from_annotations([], mock.Mock(), mock.Mock(), subtitle="bar") assert feed["subtitle"] == "bar"
def test_feed_updated(_): annotations = [ factories.Annotation(), factories.Annotation(), factories.Annotation()] feed = atom.feed_from_annotations(annotations, mock.Mock(), mock.Mock()) assert feed['updated'] == annotations[0]['updated']
def test_entry_published(): """The published times of entries should come from the annotations.""" annotation = factories.Annotation() feed = atom.feed_from_annotations([annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['published'] == annotation['created']
def test_entry_author(factories): """The authors of entries should come from the annotation usernames.""" annotation = factories.Annotation(userid=u'acct:[email protected]') feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['author']['name'] == 'nobu'
def test_entry_author(factories): """The authors of entries should come from the annotation usernames.""" annotation = factories.Annotation(userid="acct:[email protected]") feed = atom.feed_from_annotations([annotation], "atom_url", lambda annotation: "annotation url") assert feed["entries"][0]["author"]["name"] == "nobu"
def test_entry_published(): """The published times of entries should come from the annotations.""" annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['published'] == annotation['created']
def test_entry_author(): """The authors of entries should come from the annotation usernames.""" annotation = factories.Annotation(user='******') feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['author']['name'] == 'nobu'
def test_feed_updated(factories): annotations = factories.Annotation.build_batch(3) annotations[0].updated = datetime.utcnow() feed = atom.feed_from_annotations(annotations, "atom_url", lambda annotation: "annotation url") assert feed["updated"] == f"utc_iso8601_return:{annotations[0].updated}"
def test_atom_url_link(): """The feed should contain a link to its Atom URL.""" feed = atom.feed_from_annotations([], "atom_url", mock.Mock()) assert feed["links"][0] == { "rel": "self", "type": "application/atom+xml", "href": "atom_url", }
def test_entry_id(util, factories): """The ids of feed entries should come from tag_uri_for_annotation().""" annotation = factories.Annotation() feed = atom.feed_from_annotations([annotation], "atom_url", lambda _: "annotation url") util.tag_uri_for_annotation.assert_called_once() assert feed["entries"][0]["id"] == util.tag_uri_for_annotation.return_value
def test_entry_updated(): """The updated times of entries should come from the annotations.""" annotation = _annotation( updated=datetime(year=2016, month=2, day=29, hour=16, minute=39, second=12, microsecond=537626)) feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['updated'] == '2016-02-29T16:39:12.537626+00:00'
def test_entry_published(): """The published times of entries should come from the annotations.""" annotation = _annotation( created=datetime(year=2016, month=5, day=31, hour=12, minute=15, second=45, microsecond=537626)) feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") assert feed['entries'][0]['published'] == '2016-05-31T12:15:45.537626+00:00'
def test_entry_author(factories): """The authors of entries should come from the annotation usernames.""" annotation = factories.Annotation(userid="acct:[email protected]") feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url" ) assert feed["entries"][0]["author"]["name"] == "nobu"
def test_atom_url_link(): """The feed should contain a link to its Atom URL.""" feed = atom.feed_from_annotations([], 'atom_url', mock.Mock()) assert feed['links'][0] == { 'rel': 'self', 'type': 'application/atom+xml', 'href': 'atom_url' }
def test_html_url_link(): """The feed should contain a link to its corresponding HTML page.""" feed = atom.feed_from_annotations([], mock.Mock(), mock.Mock(), html_url="html_url") assert feed["links"][1] == { "rel": "alternate", "type": "text/html", "href": "html_url", }
def test_feed_updated(_): annotations = [ factories.Annotation(), factories.Annotation(), factories.Annotation() ] feed = atom.feed_from_annotations(annotations, mock.Mock(), mock.Mock()) assert feed['updated'] == annotations[0]['updated']
def test_entry_id(util, factories): """The ids of feed entries should come from tag_uri_for_annotation().""" annotation = factories.Annotation() annotations_url_function = lambda annotation: "annotation url" feed = atom.feed_from_annotations( [annotation], "atom_url", annotations_url_function) util.tag_uri_for_annotation.assert_called_once_with( annotation, annotations_url_function) assert feed['entries'][0]['id'] == util.tag_uri_for_annotation.return_value
def test_entry_title(): """The titles of feed entries should come from annotation.title.""" with mock.patch("h.feeds.atom.presenters.AnnotationHTMLPresenter.title", new_callable=mock.PropertyMock) as mock_title: annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") mock_title.assert_called_once_with() assert feed['entries'][0]['title'] == mock_title.return_value
def test_entry_content(): """The contents of entries come from annotation.description.""" with mock.patch("h.test.factories.api_models.Annotation.description", new_callable=mock.PropertyMock) as mock_description: annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") mock_description.assert_called_once_with() assert feed['entries'][0]['content'] == mock_description.return_value
def test_feed_updated(): annotations = [ _annotation(updated=datetime(year=2015, month=3, day=11, hour=10, minute=45, second=54, microsecond=537626)), _annotation(updated=datetime(year=2015, month=2, day=11, hour=10, minute=43, second=54, microsecond=537626)), _annotation(updated=datetime(year=2015, month=1, day=11, hour=10, minute=43, second=54, microsecond=537626)) ] feed = atom.feed_from_annotations( annotations, "atom_url", lambda annotation: "annotation url") assert feed['updated'] == '2015-03-11T10:45:54.537626+00:00'
def test_entry_title(factories): """The titles of feed entries should come from annotation.title.""" with mock.patch("h.feeds.atom.presenters.AnnotationHTMLPresenter.title", new_callable=mock.PropertyMock) as mock_title: annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") mock_title.assert_called_once_with() assert feed['entries'][0]['title'] == mock_title.return_value
def test_entry_id(util, factories): """The ids of feed entries should come from tag_uri_for_annotation().""" annotation = factories.Annotation() def annotation_url(ann): return "annotation url" feed = atom.feed_from_annotations([annotation], "atom_url", annotation_url) util.tag_uri_for_annotation.assert_called_once_with(annotation, annotation_url) assert feed["entries"][0]["id"] == util.tag_uri_for_annotation.return_value
def test_entry_id(util): """The ids of feed entries should come from tag_uri_for_annotation().""" annotation = factories.Annotation() annotations_url_function = lambda annotation: "annotation url" feed = atom.feed_from_annotations( [annotation], "atom_url", annotations_url_function) util.tag_uri_for_annotation.assert_called_once_with( annotation, annotations_url_function) assert feed['entries'][0]['id'] == util.tag_uri_for_annotation.return_value
def test_entry_id(util, factories): """The ids of feed entries should come from tag_uri_for_annotation().""" annotation = factories.Annotation() def annotation_url(ann): return "annotation url" feed = atom.feed_from_annotations([annotation], "atom_url", annotation_url) util.tag_uri_for_annotation.assert_called_once_with( annotation, annotation_url) assert feed["entries"][0]["id"] == util.tag_uri_for_annotation.return_value
def test_entry_content(factories): """The contents of entries come from annotation.description.""" with mock.patch( "h.feeds.atom.presenters.AnnotationHTMLPresenter.description", new_callable=mock.PropertyMock) as mock_description: annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") mock_description.assert_called_once_with() assert feed['entries'][0]['content'] == mock_description.return_value
def test_entry_dates(factories): annotation = factories.Annotation(created=datetime.utcnow(), updated=datetime.utcnow() + timedelta(hours=1)) feed = atom.feed_from_annotations([annotation], "atom_url", lambda annotation: "annotation url") assert feed["entries"][0][ "published"] == f"utc_iso8601_return:{annotation.created}" assert feed["entries"][0][ "updated"] == f"utc_iso8601_return:{annotation.updated}"
def test_annotation_url_links(_, factories): """Entries should contain links to the HTML pages for the annotations.""" annotation = factories.Annotation() annotation_url = mock.Mock() feed = atom.feed_from_annotations([annotation], "atom_url", annotation_url) annotation_url.assert_called_once_with(annotation) assert feed["entries"][0]["links"][0] == { "rel": "alternate", "type": "text/html", "href": annotation_url.return_value, }
def test_annotation_url_links(_, factories): """Entries should contain links to the HTML pages for the annotations.""" annotation = factories.Annotation() annotation_url = mock.Mock() feed = atom.feed_from_annotations( [annotation], "atom_url", annotation_url) annotation_url.assert_called_once_with(annotation) assert feed['entries'][0]['links'][0] == { 'rel': 'alternate', 'type': 'text/html', 'href': annotation_url.return_value }
def test_annotation_url_links(_): """Entries should contain links to the HTML pages for the annotations.""" annotation = factories.Annotation() annotation_url = mock.Mock() feed = atom.feed_from_annotations( [annotation], "atom_url", annotation_url) annotation_url.assert_called_once_with(annotation) assert feed['entries'][0]['links'][0] == { 'rel': 'alternate', 'type': 'text/html', 'href': annotation_url.return_value }
def test_entry_content(factories): """The contents of entries come from annotation.description.""" with mock.patch( "h.feeds.atom.presenters.AnnotationHTMLPresenter.description", new_callable=mock.PropertyMock, ) as mock_description: annotation = factories.Annotation() feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url" ) mock_description.assert_called_once_with() assert feed["entries"][0]["content"] == mock_description.return_value
def test_annotation_api_url_links(_, factories): """Entries should contain links to the JSON pages for the annotations.""" annotation = factories.Annotation() annotation_api_url = mock.Mock() feed = atom.feed_from_annotations( [annotation], "atom_url", mock.Mock(), annotation_api_url=annotation_api_url) annotation_api_url.assert_called_once_with(annotation) assert feed['entries'][0]['links'][1] == { 'rel': 'alternate', 'type': 'application/json', 'href': annotation_api_url.return_value }
def test_target_links(): """Entries should have links to the annotation's targets.""" with mock.patch("h.test.factories.api_models.Annotation.target_links", new_callable=mock.PropertyMock) as mock_target_links: annotation = factories.Annotation() mock_target_links.return_value = [ "target href 1", "target href 2", "target href 3"] feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") mock_target_links.assert_called_once_with() hrefs = [link['href'] for link in feed['entries'][0]['links']] for href in mock_target_links.return_value: assert href in hrefs
def test_target_links(): """Entries should have links to the annotation's targets.""" annotation = factories.Annotation() annotation['target'] = [ {'source': 'target href 1'}, {'source': 'target href 2'}, {'source': 'target href 3'}, ] feed = atom.feed_from_annotations( [annotation], "atom_url", lambda annotation: "annotation url") hrefs = [link['href'] for link in feed['entries'][0]['links']] for target in annotation['target']: assert target['source'] in hrefs
def test_annotation_api_url_links(_): """Entries should contain links to the JSON pages for the annotations.""" annotation = factories.Annotation() annotation_api_url = mock.Mock() feed = atom.feed_from_annotations([annotation], "atom_url", mock.Mock(), annotation_api_url=annotation_api_url) annotation_api_url.assert_called_once_with(annotation) assert feed['entries'][0]['links'][1] == { 'rel': 'alternate', 'type': 'application/json', 'href': annotation_api_url.return_value }
def test_target_links(): """Entries should have links to the annotation's targets.""" with mock.patch("h.test.factories.api_models.Annotation.target_links", new_callable=mock.PropertyMock) as mock_target_links: annotation = factories.Annotation() mock_target_links.return_value = [ "target href 1", "target href 2", "target href 3" ] feed = atom.feed_from_annotations([annotation], "atom_url", lambda annotation: "annotation url") mock_target_links.assert_called_once_with() hrefs = [link['href'] for link in feed['entries'][0]['links']] for href in mock_target_links.return_value: assert href in hrefs
def render_atom( # pylint:disable=too-many-arguments request, annotations, atom_url, html_url, title, subtitle): """ Return a rendered Atom feed of the given annotations. :param annotations: The list of annotations to render as the feed's entries :type annotations: list of dicts :param atom_url: The URL that this Atom feed will be served at :type atom_url: string :param html_url: The URL of the HTML page that this Atom feed is a feed of :type html_url: string :param title: The title of this Atom feed :type title: unicode :param subtitle: The subtitle of this Atom feed :type subtitle: unicode :rtype: pyramid.response.Response """ def annotation_url(annotation): """Return the HTML permalink URL for the given annotation.""" return request.route_url("annotation", id=annotation.id) def annotation_api_url(annotation): """Return the JSON API URL for the given annotation.""" return request.route_url("api.annotation", id=annotation.id) feed = atom.feed_from_annotations( annotations=annotations, atom_url=atom_url, annotation_url=annotation_url, annotation_api_url=annotation_api_url, html_url=html_url, title=title, subtitle=subtitle, ) response = renderers.render_to_response("h:templates/atom.xml.jinja2", {"feed": feed}, request=request) response.content_type = "application/atom+xml" return response
def render_atom(request, annotations, atom_url, html_url, title, subtitle): """Return a rendered Atom feed of the given annotations. :param annotations: The list of annotations to render as the feed's entries :type annotations: list of dicts :param atom_url: The URL that this Atom feed will be served at :type atom_url: string :param html_url: The URL of the HTML page that this Atom feed is a feed of :type html_url: string :param title: The title of this Atom feed :type title: unicode :param subtitle: The subtitle of this Atom feed :type subtitle: unicode :rtype: pyramid.response.Response """ def annotation_url(annotation): """Return the HTML permalink URL for the given annotation.""" return request.route_url("annotation", id=annotation.id) def annotation_api_url(annotation): """Return the JSON API URL for the given annotation.""" return request.route_url("api.annotation", id=annotation.id) feed = atom.feed_from_annotations( annotations=annotations, atom_url=atom_url, annotation_url=annotation_url, annotation_api_url=annotation_api_url, html_url=html_url, title=title, subtitle=subtitle, ) response = renderers.render_to_response( "h:templates/atom.xml.jinja2", {"feed": feed}, request=request ) response.content_type = "application/atom+xml" return response
def test_target_links(): """Entries should have links to the annotation's targets.""" annotation = factories.Annotation() annotation['target'] = [ { 'source': 'target href 1' }, { 'source': 'target href 2' }, { 'source': 'target href 3' }, ] feed = atom.feed_from_annotations([annotation], "atom_url", lambda annotation: "annotation url") hrefs = [link['href'] for link in feed['entries'][0]['links']] for target in annotation['target']: assert target['source'] in hrefs
def test_atom_url_link(): """The feed should contain a link to its Atom URL.""" feed = atom.feed_from_annotations([], 'atom_url', mock.Mock()) assert feed['links'][0] == { 'rel': 'self', 'type': 'application/atom+xml', 'href': 'atom_url'}
def test_feed_id(): feed = atom.feed_from_annotations([], 'atom_url', mock.Mock()) assert feed['id'] == 'atom_url'
def test_feed_id(): feed = atom.feed_from_annotations([], "atom_url", mock.Mock()) assert feed["id"] == "atom_url"