def test_feed_contains_entries(_feed_entry_from_annotation): """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_id(self): """Each test annotation should be created with a unique ID.""" annotation_1 = factories.Annotation() annotation_2 = factories.Annotation() assert annotation_1.get("id") assert annotation_2.get("id") assert annotation_1["id"] != annotation_2["id"]
def test_text(self): """Each annotation should have unique note text.""" annotation_1 = factories.Annotation() annotation_2 = factories.Annotation() assert annotation_1.get("text") assert annotation_2.get("text") assert annotation_1["text"] != annotation_2["text"]
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_feed_from_annotations_with_3_annotations(): """If there are 3 annotations it should return 3 entries.""" annotations = [ factories.Annotation(), factories.Annotation(), factories.Annotation() ] feed = rss.feed_from_annotations(annotations, _annotation_url(), mock.Mock(), '', '', '') assert len(feed['entries']) == 3
def test_feed_from_annotations_pubDate(): """The pubDate should be the updated time of the most recent annotation.""" annotations = [ factories.Annotation(updated='2015-03-11T10:45:54.537626+00:00'), factories.Annotation(updated='2015-02-11T10:43:54.537626+00:00'), factories.Annotation(updated='2015-01-11T10:43:54.537626+00:00') ] feed = rss.feed_from_annotations(annotations, _annotation_url(), mock.Mock(), '', '', '') assert feed['pubDate'] == 'Wed, 11 Mar 2015 10:45:54 UTC'
def test_feed_from_annotations_with_1_annotation(): """If there's 1 annotation it should return 1 entry.""" feed = rss.feed_from_annotations([factories.Annotation()], _annotation_url(), mock.Mock(), '', '', '') assert len(feed['entries']) == 1
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_from_annotations_item_author(): """Feed items should include the annotation's author.""" annotation = factories.Annotation(username="******") feed = rss.feed_from_annotations([annotation], _annotation_url(), mock.Mock(), '', '', '') assert feed['entries'][0]['author'] == {'name': 'janebloggs'}
def test_feed_from_annotations_item_titles(): """Feed items should include the annotation's document's title.""" annotation = factories.Annotation() feed = rss.feed_from_annotations([annotation], _annotation_url(), mock.Mock(), '', '', '') assert feed['entries'][0]['title'] == annotation['document']['title']
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_tags(self): """It should be possible to choose the number of tags with num_tags.""" # If num_tags isn't passed the factory chooses a random number of tags. # Here we choose a num_tags higher than the upper range of this random # choice, so there's no chance of random false positive test passes. annotation = factories.Annotation(num_tags=20) assert len(annotation["tags"]) == 20 assert "num_tags" not in annotation
def test_feed_from_annotations_item_guid(): """Feed items should use the annotation's HTML URL as their GUID.""" feed = rss.feed_from_annotations([ factories.Annotation(id='id', created=datetime.datetime( year=2015, month=3, day=11).isoformat()) ], _annotation_url(), mock.Mock(), '', '', '') assert feed['entries'][0]['guid'] == 'tag:hypothes.is,2015-09:id'
def test_feed_from_annotations_html_links(): """Items should include links to the annotations' HTML pages.""" annotation_url = _annotation_url() feed = rss.feed_from_annotations([factories.Annotation()], annotation_url, mock.Mock(), '', '', '') item = feed['entries'][0] assert item['link'] == annotation_url.return_value
def test_feed_from_annotations_pubDate(): """It should render the pubDates of annotations correctly.""" annotation = factories.Annotation(created=datetime.datetime( year=2015, month=3, day=11, hour=10, minute=43, seconds=54).isoformat()) feed = rss.feed_from_annotations([annotation], _annotation_url(), mock.Mock(), '', '', '') assert feed['entries'][0]['pubDate'] == 'Wed, 11 Mar 2015 10:43:54 +0000'
def test_feed_from_annotations_item_descriptions(): """Feed items should include a description of the annotation.""" with mock.patch( "h.feeds.rss.presenters.AnnotationHTMLPresenter.description", new_callable=mock.PropertyMock) as description: feed = rss.feed_from_annotations([factories.Annotation()], _annotation_url(), mock.Mock(), '', '', '') assert feed['entries'][0]['description'] == (description.return_value)
def test_tag_uri_for_annotation(): """Entry IDs should be tag URIs based on domain, day and annotation ID.""" annotation = factories.Annotation( id="12345", created=datetime.datetime(year=2015, month=3, day=19).isoformat()) tag_uri = util.tag_uri_for_annotation( annotation, annotation_url=mock.Mock(return_value="http://example.com/a/12345")) assert tag_uri == "tag:example.com,2015-09:12345"
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_updated_date(self): """Annotations should have an updated date from the current time.""" before = datetime.datetime.now() annotation = factories.Annotation() after = datetime.datetime.now() updated = datetime.datetime.strptime(annotation["updated"], "%Y-%m-%dT%H:%M:%S.%f") assert before < updated < after
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_content(): """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_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_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_document_link(self): annotation = factories.Annotation(random_number=30) assert annotation["document"]["link"][0]["href"] == ( "http://example.com/document_30")
def test_document_title(self): assert factories.Annotation( random_number=30)["document"]["title"] == ("Example Document 30")
def test_permissions(self): annotation = factories.Annotation(username="******") assert "test_user" in annotation["permissions"]["admin"][0] assert "test_user" in annotation["permissions"]["update"][0] assert "test_user" in annotation["permissions"]["delete"][0]
def test_source(self): annotation = factories.Annotation(random_number=3) assert annotation["target"][0]["source"] == ( "http://example.com/document_3")
def test_uri(self): annotation = factories.Annotation(random_number=3) assert annotation["uri"] == "http://example.com/document_3" assert "random_number" not in annotation
def test_custom_tags(self): assert factories.Annotation(tags=["foo", "bar", "gar"])["tags"] == [ "foo", "bar", "gar" ]