# coding=utf-8 """Unit tests for webmention.py. TODO: test error handling """ from __future__ import unicode_literals import copy import json import urllib import urllib2 from django_salmon import magicsigs, utils import feedparser from granary import atom, microformats2 from httpsig.sign import HeaderSigner import mf2py import mock from mock import call from oauth_dropins.webutil import util from oauth_dropins.webutil.testutil import requests_response import requests import activitypub from common import ( AS2_PUBLIC_AUDIENCE, CONNEG_HEADERS_AS2, CONNEG_HEADERS_AS2_HTML, CONTENT_TYPE_AS2, CONTENT_TYPE_HTML, CONTENT_TYPE_MAGIC_ENVELOPE, HEADERS, ) from models import Follower, MagicKey, Response import testutil import webmention from webmention import app REPOST_HTML = """\
reposted! Ms. ☕ Baz """ REPOST_AS2 = { '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Announce', 'id': 'http://localhost/r/http://a/repost', 'url': 'http://localhost/r/http://a/repost', 'name': 'reposted!', 'object': 'tag:orig,2017:as2', 'cc': [ AS2_PUBLIC_AUDIENCE, 'http://orig/author', 'http://orig/recipient', 'http://orig/bystander', ], 'actor': { 'type': 'Person', 'id': 'http://localhost/orig', 'url': 'http://localhost/r/http://orig', 'name': 'Ms. ☕ Baz', 'preferredUsername': 'orig', }, } @mock.patch('requests.post') @mock.patch('requests.get') class WebmentionTest(testutil.TestCase): def setUp(self): super(WebmentionTest, self).setUp() self.key = MagicKey.get_or_create('a') self.orig_html_as2 = requests_response("""\ """, url='http://orig/post', content_type=CONTENT_TYPE_HTML) self.orig_html_atom = requests_response("""\ """, url='http://orig/post', content_type=CONTENT_TYPE_HTML) self.orig_atom = requests_response("""\hello i am a post
Ms. ☕ Baz """ self.create = requests_response( self.create_html, content_type=CONTENT_TYPE_HTML) self.create_mf2 = mf2py.parse(self.create_html, url='http://a/create') self.create_as2 = { '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Create', 'object': { '@context': 'https://www.w3.org/ns/activitystreams', 'type': 'Note', 'id': 'http://localhost/r/http://orig/post', 'url': 'http://localhost/r/http://orig/post', 'name': 'hello i am a post', 'content': 'hello i am a post', 'attributedTo': [{ 'type': 'Person', 'id': 'http://localhost/orig', 'url': 'http://localhost/r/https://orig', 'name': 'Ms. ☕ Baz', 'preferredUsername': 'orig', }], 'cc': ['https://www.w3.org/ns/activitystreams#Public'], }, } self.actor = requests_response({ 'objectType' : 'person', 'displayName': 'Mrs. ☕ Foo', 'url': 'https://foo.com/about-me', 'inbox': 'https://foo.com/inbox', }, content_type=CONTENT_TYPE_AS2) self.activitypub_gets = [self.reply, self.orig_as2, self.actor] def verify_salmon(self, mock_post): args, kwargs = mock_post.call_args self.assertEqual(('http://orig/salmon',), args) self.assertEqual(CONTENT_TYPE_MAGIC_ENVELOPE, kwargs['headers']['Content-Type']) env = utils.parse_magic_envelope(kwargs['data']) data = utils.decode(env['data']) assert magicsigs.verify(None, data, env['sig'], key=self.key) return data def test_no_source_entry(self, mock_get, mock_post): mock_get.return_value = requests_response("""nothing to see here except link
""", content_type=CONTENT_TYPE_HTML) got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/post', 'target': 'https://fed.brid.gy/', })) self.assertEquals(400, got.status_int) mock_get.assert_has_calls((self.req('http://a/post'),)) def test_no_targets(self, mock_get, mock_post): mock_get.return_value = requests_response(""" """, content_type=CONTENT_TYPE_HTML) got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/post', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) mock_get.assert_has_calls((self.req('http://a/post'),)) def test_no_backlink(self, mock_get, mock_post): mock_get.return_value = requests_response( self.reply_html.replace('', ''), content_type=CONTENT_TYPE_HTML) got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/post', 'target': 'https://fed.brid.gy/', })) self.assertEquals(400, got.status_int) mock_get.assert_has_calls((self.req('http://a/post'),)) def test_activitypub_create_reply(self, mock_get, mock_post): mock_get.side_effect = self.activitypub_gets mock_post.return_value = requests_response('abc xyz', status=203) got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/reply', 'target': 'https://fed.brid.gy/', })) self.assertEquals(203, got.status_int) mock_get.assert_has_calls(( self.req('http://a/reply'), self.req('http://orig/post', headers=CONNEG_HEADERS_AS2_HTML), self.req('http://orig/author', headers=CONNEG_HEADERS_AS2_HTML), )) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.as2_create, kwargs['json']) headers = kwargs['headers'] self.assertEqual(CONTENT_TYPE_AS2, headers['Content-Type']) rsa_key = kwargs['auth'].header_signer._rsa._key self.assertEqual(self.key.private_pem(), rsa_key.exportKey()) resp = Response.get_by_id('http://a/reply http://orig/as2') self.assertEqual('out', resp.direction) self.assertEqual('activitypub', resp.protocol) self.assertEqual('complete', resp.status) self.assertEqual(self.reply_mf2, json.loads(resp.source_mf2)) # TODO: if i do this, maybe switch to separate HttpRequest model and # foreign key # self.assertEqual([self.as2_create], resp.request_statuses) # self.assertEqual([self.as2_create], resp.requests) # self.assertEqual(['abc xyz'], resp.responses) def test_activitypub_update_reply(self, mock_get, mock_post): Response(id='http://a/reply http://orig/as2', status='complete').put() mock_get.side_effect = self.activitypub_gets mock_post.return_value = requests_response('abc xyz') got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/reply', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.as2_update, kwargs['json']) def test_activitypub_create_reply_attributed_to_id_only(self, mock_get, mock_post): """Based on PeerTube's AS2. https://github.com/snarfed/bridgy-fed/issues/40 """ del self.orig_as2_data['actor'] self.orig_as2_data['attributedTo'] = [{ 'type': 'Person', 'id': 'http://orig/author', }] orig_as2_resp = requests_response( self.orig_as2_data, content_type=CONTENT_TYPE_AS2 + '; charset=utf-8') mock_get.side_effect = [self.reply, orig_as2_resp, self.actor] mock_post.return_value = requests_response('abc xyz', status=203) got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/reply', 'target': 'https://fed.brid.gy/', })) self.assertEquals(203, got.status_int) mock_get.assert_has_calls(( self.req('http://a/reply'), self.req('http://orig/post', headers=CONNEG_HEADERS_AS2_HTML), self.req('http://orig/author', headers=CONNEG_HEADERS_AS2_HTML), )) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.as2_create, kwargs['json']) def test_activitypub_update_reply(self, mock_get, mock_post): Response(id='http://a/reply http://orig/as2', status='complete').put() mock_get.side_effect = self.activitypub_gets mock_post.return_value = requests_response('abc xyz') got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/reply', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.as2_update, kwargs['json']) def test_activitypub_create_repost(self, mock_get, mock_post): mock_get.side_effect = [self.repost, self.orig_as2, self.actor] mock_post.return_value = requests_response('abc xyz') got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/repost', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) mock_get.assert_has_calls(( self.req('http://a/repost'), self.req('http://orig/post', headers=CONNEG_HEADERS_AS2_HTML), self.req('http://orig/author', headers=CONNEG_HEADERS_AS2_HTML), )) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.repost_as2, kwargs['json']) headers = kwargs['headers'] self.assertEqual(CONTENT_TYPE_AS2, headers['Content-Type']) rsa_key = kwargs['auth'].header_signer._rsa._key self.assertEqual(self.key.private_pem(), rsa_key.exportKey()) resp = Response.get_by_id('http://a/repost http://orig/as2') self.assertEqual('out', resp.direction) self.assertEqual('activitypub', resp.protocol) self.assertEqual('complete', resp.status) self.assertEqual(self.repost_mf2, json.loads(resp.source_mf2)) def test_activitypub_link_rel_alternate_as2(self, mock_get, mock_post): mock_get.side_effect = [self.reply, self.orig_html_as2, self.orig_as2, self.actor] mock_post.return_value = requests_response('abc xyz') got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/reply', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) mock_get.assert_has_calls(( self.req('http://a/reply'), self.req('http://orig/post', headers=CONNEG_HEADERS_AS2_HTML), self.req('http://orig/as2', headers=CONNEG_HEADERS_AS2), self.req('http://orig/author', headers=CONNEG_HEADERS_AS2_HTML), )) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assertEqual(self.as2_create, kwargs['json']) def test_activitypub_create_default_url_to_wm_source(self, mock_get, mock_post): """Source post has no u-url. AS2 id should default to webmention source.""" missing_url = requests_response("""\ reposted! Ms. ☕ Baz """, content_type=CONTENT_TYPE_HTML) mock_get.side_effect = [missing_url, self.orig_as2, self.actor] mock_post.return_value = requests_response('abc xyz', status=203) got = app.get_response('/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/repost', 'target': 'https://fed.brid.gy/', })) self.assertEquals(203, got.status_int) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) self.assert_equals(self.repost_as2, kwargs['json']) def test_activitypub_create_author_only_url(self, mock_get, mock_post): """Mf2 author property is just a URL. We should run full authorship. https://indieweb.org/authorship """ repost = requests_response("""\ reposted! """, content_type=CONTENT_TYPE_HTML) author = requests_response("""\ Ms. ☕ Baz """, content_type=CONTENT_TYPE_HTML) mock_get.side_effect = [repost, author, self.orig_as2, self.actor] mock_post.return_value = requests_response('abc xyz', status=201) got = app.get_response('/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://a/repost', 'target': 'https://fed.brid.gy/', })) self.assertEquals(201, got.status_int) args, kwargs = mock_post.call_args self.assertEqual(('https://foo.com/inbox',), args) repost_as2 = copy.deepcopy(self.repost_as2) repost_as2['actor']['image'] = repost_as2['actor']['icon'] = \ {'type': 'Image', 'url': 'http://orig/pic'}, self.assert_equals(repost_as2, kwargs['json']) def test_activitypub_create_post(self, mock_get, mock_post): mock_get.side_effect = [self.create, self.actor] mock_post.return_value = requests_response('abc xyz') Follower.get_or_create('orig', 'https://mastodon/aaa') Follower.get_or_create('orig', 'https://mastodon/bbb', last_follow=json.dumps({'actor': { 'publicInbox': 'https://public/inbox', 'inbox': 'https://unused', }})) Follower.get_or_create('orig', 'https://mastodon/ccc', last_follow=json.dumps({'actor': { 'endpoints': { 'sharedInbox': 'https://shared/inbox', }, }})) Follower.get_or_create('orig', 'https://mastodon/ddd', last_follow=json.dumps({'actor': { 'inbox': 'https://inbox', }})) self.datastore_stub.Flush() got = app.get_response( '/webmention', method='POST', body=urllib.urlencode({ 'source': 'http://orig/post', 'target': 'https://fed.brid.gy/', })) self.assertEquals(200, got.status_int) mock_get.assert_has_calls(( self.req('http://orig/post'), )) inboxes = ('https://public/inbox', 'https://shared/inbox', 'https://inbox') for call, inbox in zip(mock_post.call_args_list, inboxes): self.assertEquals((inbox,), call[0]) self.assertEquals(self.create_as2, call[1]['json']) for inbox in inboxes: resp = Response.get_by_id('http://orig/post %s' % inbox) self.assertEqual('out', resp.direction, inbox) self.assertEqual('activitypub', resp.protocol, inbox) self.assertEqual('complete', resp.status, inbox) self.assertEqual(self.create_mf2, json.loads(resp.source_mf2), inbox) def test_activitypub_create_with_image(self, mock_get, mock_post): create_html = self.create_html.replace( 'foo