2017-08-13 07:12:16 +00:00
|
|
|
# coding=utf-8
|
|
|
|
"""Unit tests for activitypub.py.
|
2017-08-13 21:49:35 +00:00
|
|
|
|
|
|
|
TODO: test error handling
|
2017-08-13 07:12:16 +00:00
|
|
|
"""
|
2017-08-23 15:14:51 +00:00
|
|
|
import copy
|
2019-12-26 06:20:57 +00:00
|
|
|
from unittest.mock import call, patch
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2017-08-23 15:14:51 +00:00
|
|
|
from oauth_dropins.webutil import util
|
2017-08-24 14:41:46 +00:00
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
2019-12-25 07:26:58 +00:00
|
|
|
from oauth_dropins.webutil.util import json_dumps, json_loads
|
2017-08-13 07:12:16 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
import activitypub
|
2019-12-26 06:20:57 +00:00
|
|
|
from app import application
|
2017-08-15 06:07:24 +00:00
|
|
|
import common
|
2018-10-22 00:37:33 +00:00
|
|
|
from models import Follower, MagicKey, Response
|
2019-12-26 06:20:57 +00:00
|
|
|
from . import testutil
|
2017-08-13 07:12:16 +00:00
|
|
|
|
|
|
|
|
2018-10-15 15:09:36 +00:00
|
|
|
REPLY_OBJECT = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Note',
|
|
|
|
'content': 'A ☕ reply',
|
2018-10-17 13:52:07 +00:00
|
|
|
'id': 'http://this/reply/id',
|
2018-10-15 15:09:36 +00:00
|
|
|
'url': 'http://this/reply',
|
|
|
|
'inReplyTo': 'http://orig/post',
|
|
|
|
'cc': ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
}
|
2018-10-17 14:00:31 +00:00
|
|
|
REPLY_OBJECT_WRAPPED = copy.deepcopy(REPLY_OBJECT)
|
2020-01-31 15:38:58 +00:00
|
|
|
REPLY_OBJECT_WRAPPED['inReplyTo'] = 'http://localhost:80/r/orig/post'
|
2018-10-21 22:28:42 +00:00
|
|
|
REPLY = {
|
2018-10-15 15:09:36 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Create',
|
2018-10-17 14:00:31 +00:00
|
|
|
'id': 'http://this/reply/as2',
|
2018-10-15 15:09:36 +00:00
|
|
|
'object': REPLY_OBJECT,
|
|
|
|
}
|
2019-04-16 14:59:29 +00:00
|
|
|
MENTION_OBJECT = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Note',
|
|
|
|
'content': '☕ mentions of @other @target@target',
|
|
|
|
'id': 'http://this/mention/id',
|
|
|
|
'url': 'http://this/mention',
|
|
|
|
'to': ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
'cc': [
|
|
|
|
'https://this/author/followers',
|
|
|
|
'https://masto.foo/@other',
|
|
|
|
'http://localhost/target', # redirect-wrapped
|
|
|
|
],
|
|
|
|
'tag': [{
|
|
|
|
'type': 'Mention',
|
|
|
|
'href': 'https://masto.foo/@other',
|
|
|
|
'name': '@other@masto.foo',
|
|
|
|
}, {
|
|
|
|
'type': 'Mention',
|
|
|
|
'href': 'http://localhost/target', # redirect-wrapped
|
|
|
|
'name': '@target@target',
|
|
|
|
}],
|
|
|
|
}
|
|
|
|
MENTION = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Create',
|
|
|
|
'id': 'http://this/mention/as2',
|
|
|
|
'object': MENTION_OBJECT,
|
|
|
|
}
|
2018-10-17 14:49:04 +00:00
|
|
|
# based on example Mastodon like:
|
|
|
|
# https://github.com/snarfed/bridgy-fed/issues/4#issuecomment-334212362
|
|
|
|
# (reposts are very similar)
|
2018-10-21 22:28:42 +00:00
|
|
|
LIKE = {
|
2018-10-17 14:49:04 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': 'http://this/like#ok',
|
|
|
|
'type': 'Like',
|
|
|
|
'object': 'http://orig/post',
|
|
|
|
'actor': 'http://orig/actor',
|
|
|
|
}
|
2018-10-21 22:28:42 +00:00
|
|
|
LIKE_WRAPPED = copy.deepcopy(LIKE)
|
2020-01-31 15:38:58 +00:00
|
|
|
LIKE_WRAPPED['object'] = 'http://localhost/r/http://orig/post'
|
2018-10-23 14:52:30 +00:00
|
|
|
LIKE_WITH_ACTOR = copy.deepcopy(LIKE)
|
|
|
|
LIKE_WITH_ACTOR['actor'] = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': 'http://orig/actor',
|
|
|
|
'type': 'Person',
|
|
|
|
'name': 'Ms. Actor',
|
|
|
|
'preferredUsername': 'msactor',
|
|
|
|
'image': {'type': 'Image', 'url': 'http://orig/pic.jpg'},
|
|
|
|
}
|
2018-10-21 22:28:42 +00:00
|
|
|
|
2018-10-23 14:52:30 +00:00
|
|
|
FOLLOW = {
|
2018-10-21 22:28:42 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
2018-10-23 14:11:44 +00:00
|
|
|
'id': 'https://mastodon.social/6d1a',
|
2018-10-21 22:28:42 +00:00
|
|
|
'type': 'Follow',
|
|
|
|
'actor': 'https://mastodon.social/users/swentel',
|
2018-10-23 14:52:30 +00:00
|
|
|
'object': 'https://realize.be/',
|
2018-10-21 22:28:42 +00:00
|
|
|
}
|
2018-10-23 14:52:30 +00:00
|
|
|
FOLLOW_WRAPPED = copy.deepcopy(FOLLOW)
|
|
|
|
FOLLOW_WRAPPED['object'] = 'http://localhost/realize.be'
|
|
|
|
ACTOR = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': FOLLOW['actor'],
|
|
|
|
'type': 'Person',
|
|
|
|
'inbox': 'http://follower/inbox',
|
|
|
|
}
|
|
|
|
FOLLOW_WITH_ACTOR = copy.deepcopy(FOLLOW)
|
|
|
|
FOLLOW_WITH_ACTOR['actor'] = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': FOLLOW['actor'],
|
|
|
|
'type': 'Person',
|
|
|
|
'inbox': 'http://follower/inbox',
|
|
|
|
}
|
|
|
|
FOLLOW_WRAPPED_WITH_ACTOR = copy.deepcopy(FOLLOW_WRAPPED)
|
|
|
|
FOLLOW_WRAPPED_WITH_ACTOR['actor'] = FOLLOW_WITH_ACTOR['actor']
|
|
|
|
|
2018-10-21 22:28:42 +00:00
|
|
|
ACCEPT = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Accept',
|
2020-01-31 15:38:58 +00:00
|
|
|
'id': 'tag:localhost:80:accept/realize.be/https://mastodon.social/6d1a',
|
2018-10-22 01:54:27 +00:00
|
|
|
'actor': 'http://localhost/realize.be',
|
2018-10-21 22:28:42 +00:00
|
|
|
'object': {
|
|
|
|
'type': 'Follow',
|
|
|
|
'actor': 'https://mastodon.social/users/swentel',
|
2018-10-22 01:54:27 +00:00
|
|
|
'object': 'http://localhost/realize.be',
|
2018-10-21 22:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-15 15:09:36 +00:00
|
|
|
|
2019-08-01 14:32:45 +00:00
|
|
|
UNDO_FOLLOW_WRAPPED = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': 'https://mastodon.social/6d1b',
|
|
|
|
'type': 'Undo',
|
|
|
|
'actor': 'https://mastodon.social/users/swentel',
|
|
|
|
'object': FOLLOW_WRAPPED,
|
|
|
|
}
|
|
|
|
|
2020-03-01 04:28:53 +00:00
|
|
|
DELETE = {
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'id': 'https://mastodon.social/users/swentel#delete',
|
|
|
|
'type': 'Delete',
|
|
|
|
'actor': 'https://mastodon.social/users/swentel',
|
|
|
|
'object': 'https://mastodon.social/users/swentel',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-13 06:14:46 +00:00
|
|
|
@patch('requests.post')
|
|
|
|
@patch('requests.get')
|
2018-10-23 14:11:44 +00:00
|
|
|
@patch('requests.head')
|
2017-10-01 14:09:22 +00:00
|
|
|
class ActivityPubTest(testutil.TestCase):
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2019-11-13 05:28:44 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(ActivityPubTest, self).setUp()
|
2019-12-26 06:20:57 +00:00
|
|
|
activitypub.ActorHandler.get.cache_clear()
|
2019-11-13 05:28:44 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_actor_handler(self, _, mock_get, __):
|
2017-08-24 14:41:46 +00:00
|
|
|
mock_get.return_value = requests_response("""
|
2017-08-13 07:12:16 +00:00
|
|
|
<body>
|
2017-10-24 04:49:43 +00:00
|
|
|
<a class="h-card u-url" rel="me" href="/about-me">Mrs. ☕ Foo</a>
|
2017-08-13 07:12:16 +00:00
|
|
|
</body>
|
2019-10-04 04:08:26 +00:00
|
|
|
""", url='https://foo.com/', content_type=common.CONTENT_TYPE_HTML)
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com')
|
2017-08-23 15:14:51 +00:00
|
|
|
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
2019-10-04 04:08:26 +00:00
|
|
|
stream=True, timeout=util.HTTP_TIMEOUT)
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(200, got.status_int)
|
|
|
|
type = got.headers['Content-Type']
|
|
|
|
self.assertTrue(type.startswith(common.CONTENT_TYPE_AS2), type)
|
|
|
|
self.assertEqual({
|
2021-03-23 06:55:14 +00:00
|
|
|
'@context': [
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
],
|
2017-09-28 14:25:21 +00:00
|
|
|
'type' : 'Person',
|
2017-10-13 06:16:32 +00:00
|
|
|
'name': 'Mrs. ☕ Foo',
|
2019-01-04 15:04:45 +00:00
|
|
|
'summary': '',
|
2017-10-26 15:07:29 +00:00
|
|
|
'preferredUsername': 'foo.com',
|
2017-10-24 04:49:43 +00:00
|
|
|
'id': 'http://localhost/foo.com',
|
2018-10-14 14:58:17 +00:00
|
|
|
'url': 'http://localhost/r/https://foo.com/about-me',
|
2017-08-13 07:12:16 +00:00
|
|
|
'inbox': 'http://localhost/foo.com/inbox',
|
2019-01-04 15:04:45 +00:00
|
|
|
'outbox': 'http://localhost/foo.com/outbox',
|
2019-01-14 15:08:04 +00:00
|
|
|
'following': 'http://localhost/foo.com/following',
|
|
|
|
'followers': 'http://localhost/foo.com/followers',
|
2017-10-01 14:09:22 +00:00
|
|
|
'publicKey': {
|
2019-01-04 15:04:45 +00:00
|
|
|
'id': 'foo.com',
|
2019-12-26 06:20:57 +00:00
|
|
|
'publicKeyPem': MagicKey.get_by_id('foo.com').public_pem().decode(),
|
2017-10-01 14:09:22 +00:00
|
|
|
},
|
2019-12-25 07:26:58 +00:00
|
|
|
}, json_loads(got.body))
|
2017-08-13 21:49:35 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_actor_handler_no_hcard(self, _, mock_get, __):
|
2017-08-24 14:41:46 +00:00
|
|
|
mock_get.return_value = requests_response("""
|
2017-08-19 20:31:06 +00:00
|
|
|
<body>
|
|
|
|
<div class="h-entry">
|
|
|
|
<p class="e-content">foo bar</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
2017-08-24 14:41:46 +00:00
|
|
|
""")
|
2017-08-19 20:31:06 +00:00
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com')
|
2017-08-23 15:14:51 +00:00
|
|
|
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
2019-10-04 04:08:26 +00:00
|
|
|
stream=True, timeout=util.HTTP_TIMEOUT)
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(400, got.status_int)
|
|
|
|
self.assertIn('representative h-card', got.body.decode())
|
2017-08-19 20:31:06 +00:00
|
|
|
|
2020-03-02 15:50:37 +00:00
|
|
|
def test_actor_blocked_tld(self, _, __, ___):
|
|
|
|
got = application.get_response('/foo.json')
|
|
|
|
self.assertEqual(404, got.status_int)
|
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_reply_object(self, *mocks):
|
|
|
|
self._test_inbox_reply(REPLY_OBJECT, REPLY_OBJECT, *mocks)
|
2018-10-15 15:09:36 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_reply_object_wrapped(self, *mocks):
|
|
|
|
self._test_inbox_reply(REPLY_OBJECT_WRAPPED, REPLY_OBJECT, *mocks)
|
2018-10-17 14:00:31 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_reply_create_activity(self, *mocks):
|
|
|
|
self._test_inbox_reply(REPLY, REPLY, *mocks)
|
2018-10-15 15:09:36 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def _test_inbox_reply(self, as2, expected_as2, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='http://orig/post')
|
2017-08-24 14:41:46 +00:00
|
|
|
mock_get.return_value = requests_response(
|
|
|
|
'<html><head><link rel="webmention" href="/webmention"></html>')
|
|
|
|
mock_post.return_value = requests_response()
|
2017-08-13 21:49:35 +00:00
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(as2).encode())
|
|
|
|
self.assertEqual(200, got.status_int, got.body)
|
2021-04-25 18:40:19 +00:00
|
|
|
mock_get.assert_called_once_with(
|
|
|
|
'http://orig/post', headers=common.HEADERS, timeout=15, stream=True)
|
2017-08-13 21:49:35 +00:00
|
|
|
|
2017-08-23 15:14:51 +00:00
|
|
|
expected_headers = copy.deepcopy(common.HEADERS)
|
|
|
|
expected_headers['Accept'] = '*/*'
|
2017-08-13 21:49:35 +00:00
|
|
|
mock_post.assert_called_once_with(
|
|
|
|
'http://orig/webmention',
|
|
|
|
data={
|
2018-10-17 14:49:04 +00:00
|
|
|
'source': 'http://localhost/render?source=http%3A%2F%2Fthis%2Freply&target=http%3A%2F%2Forig%2Fpost',
|
2017-08-13 21:49:35 +00:00
|
|
|
'target': 'http://orig/post',
|
|
|
|
},
|
2021-04-25 18:40:19 +00:00
|
|
|
allow_redirects=False, timeout=15, stream=True,
|
2021-04-04 04:15:14 +00:00
|
|
|
headers=expected_headers)
|
2017-10-04 14:07:22 +00:00
|
|
|
|
2017-10-10 00:29:50 +00:00
|
|
|
resp = Response.get_by_id('http://this/reply http://orig/post')
|
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('activitypub', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
2019-12-25 07:26:58 +00:00
|
|
|
self.assertEqual(expected_as2, json_loads(resp.source_as2))
|
2017-10-10 00:29:50 +00:00
|
|
|
|
2019-04-16 14:42:10 +00:00
|
|
|
def test_inbox_reply_drop_self_domain_target(self, mock_head, mock_get, mock_post):
|
|
|
|
reply = copy.deepcopy(REPLY_OBJECT)
|
|
|
|
# same domain as source; should drop
|
|
|
|
reply['inReplyTo'] = 'http://localhost/this',
|
|
|
|
|
|
|
|
mock_head.return_value = requests_response(url='http://this/')
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(reply).encode())
|
|
|
|
self.assertEqual(200, got.status_int, got.body)
|
2019-04-16 14:42:10 +00:00
|
|
|
|
|
|
|
mock_head.assert_called_once_with(
|
2019-10-04 04:08:26 +00:00
|
|
|
'http://this', allow_redirects=True, stream=True, timeout=15)
|
2019-04-16 14:42:10 +00:00
|
|
|
mock_get.assert_not_called()
|
|
|
|
mock_post.assert_not_called()
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(0, Response.query().count())
|
2019-04-16 14:42:10 +00:00
|
|
|
|
2019-04-16 14:59:29 +00:00
|
|
|
def test_inbox_mention_object(self, *mocks):
|
|
|
|
self._test_inbox_mention(MENTION_OBJECT, *mocks)
|
|
|
|
|
|
|
|
def test_inbox_mention_create_activity(self, *mocks):
|
|
|
|
self._test_inbox_mention(MENTION, *mocks)
|
|
|
|
|
|
|
|
def _test_inbox_mention(self, as2, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='http://target')
|
|
|
|
mock_get.return_value = requests_response(
|
|
|
|
'<html><head><link rel="webmention" href="/webmention"></html>')
|
|
|
|
mock_post.return_value = requests_response()
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(as2).encode())
|
|
|
|
self.assertEqual(200, got.status_int, got.body)
|
2021-04-25 18:40:19 +00:00
|
|
|
mock_get.assert_called_once_with(
|
|
|
|
'http://target/', headers=common.HEADERS, timeout=15, stream=True)
|
2019-04-16 14:59:29 +00:00
|
|
|
|
|
|
|
expected_headers = copy.deepcopy(common.HEADERS)
|
|
|
|
expected_headers['Accept'] = '*/*'
|
|
|
|
mock_post.assert_called_once_with(
|
|
|
|
'http://target/webmention',
|
|
|
|
data={
|
|
|
|
'source': 'http://localhost/render?source=http%3A%2F%2Fthis%2Fmention&target=http%3A%2F%2Ftarget%2F',
|
|
|
|
'target': 'http://target/',
|
|
|
|
},
|
2021-04-25 18:40:19 +00:00
|
|
|
allow_redirects=False, timeout=15, stream=True,
|
2021-04-04 04:15:14 +00:00
|
|
|
headers=expected_headers)
|
2019-04-16 14:59:29 +00:00
|
|
|
|
|
|
|
resp = Response.get_by_id('http://this/mention http://target/')
|
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('activitypub', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
2020-01-31 15:38:58 +00:00
|
|
|
self.assertEqual(self.handler.redirect_unwrap(as2), json_loads(resp.source_as2))
|
2019-04-16 14:59:29 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_like(self, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='http://orig/post')
|
2017-10-13 06:14:46 +00:00
|
|
|
mock_get.side_effect = [
|
|
|
|
# source actor
|
2018-10-23 14:52:30 +00:00
|
|
|
requests_response(LIKE_WITH_ACTOR['actor'], headers={'Content-Type': common.CONTENT_TYPE_AS2}),
|
2017-10-13 06:14:46 +00:00
|
|
|
# target post webmention discovery
|
|
|
|
requests_response(
|
|
|
|
'<html><head><link rel="webmention" href="/webmention"></html>'),
|
|
|
|
]
|
2017-10-11 05:42:19 +00:00
|
|
|
mock_post.return_value = requests_response()
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
2020-01-31 15:38:58 +00:00
|
|
|
body=json_dumps(LIKE_WRAPPED).encode())
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(200, got.status_int)
|
2017-10-13 06:14:46 +00:00
|
|
|
|
|
|
|
as2_headers = copy.deepcopy(common.HEADERS)
|
2017-10-20 14:13:04 +00:00
|
|
|
as2_headers.update(common.CONNEG_HEADERS_AS2_HTML)
|
2017-10-13 06:14:46 +00:00
|
|
|
mock_get.assert_has_calls((
|
2019-10-04 04:08:26 +00:00
|
|
|
call('http://orig/actor', headers=as2_headers, stream=True, timeout=15),
|
2021-04-25 18:40:19 +00:00
|
|
|
call('http://orig/post', headers=common.HEADERS, stream=True, timeout=15),
|
2017-10-13 06:14:46 +00:00
|
|
|
))
|
2017-10-10 00:29:50 +00:00
|
|
|
|
2017-10-11 05:42:19 +00:00
|
|
|
args, kwargs = mock_post.call_args
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(('http://orig/webmention',), args)
|
|
|
|
self.assertEqual({
|
2017-10-11 05:42:19 +00:00
|
|
|
# TODO
|
2017-10-12 04:12:39 +00:00
|
|
|
'source': 'http://localhost/render?source=http%3A%2F%2Fthis%2Flike__ok&target=http%3A%2F%2Forig%2Fpost',
|
2017-10-11 05:42:19 +00:00
|
|
|
'target': 'http://orig/post',
|
|
|
|
}, kwargs['data'])
|
|
|
|
|
2017-10-12 04:12:39 +00:00
|
|
|
resp = Response.get_by_id('http://this/like__ok http://orig/post')
|
2017-10-11 05:42:19 +00:00
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('activitypub', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
2019-12-25 07:26:58 +00:00
|
|
|
self.assertEqual(LIKE_WITH_ACTOR, json_loads(resp.source_as2))
|
2017-10-17 05:21:13 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_follow_accept(self, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='https://realize.be/')
|
2018-10-21 22:28:42 +00:00
|
|
|
mock_get.side_effect = [
|
|
|
|
# source actor
|
2018-10-23 14:52:30 +00:00
|
|
|
requests_response(FOLLOW_WITH_ACTOR['actor'],
|
|
|
|
content_type=common.CONTENT_TYPE_AS2),
|
2018-10-23 14:11:44 +00:00
|
|
|
# target post webmention discovery
|
|
|
|
requests_response(
|
|
|
|
'<html><head><link rel="webmention" href="/webmention"></html>'),
|
2018-10-21 22:28:42 +00:00
|
|
|
]
|
|
|
|
mock_post.return_value = requests_response()
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(FOLLOW_WRAPPED).encode())
|
|
|
|
self.assertEqual(200, got.status_int)
|
2018-10-21 22:28:42 +00:00
|
|
|
|
|
|
|
as2_headers = copy.deepcopy(common.HEADERS)
|
|
|
|
as2_headers.update(common.CONNEG_HEADERS_AS2_HTML)
|
|
|
|
mock_get.assert_has_calls((
|
2019-10-04 04:08:26 +00:00
|
|
|
call(FOLLOW['actor'], headers=as2_headers, stream=True, timeout=15),
|
2018-10-21 22:28:42 +00:00
|
|
|
))
|
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
# check AP Accept
|
|
|
|
self.assertEqual(2, len(mock_post.call_args_list))
|
|
|
|
args, kwargs = mock_post.call_args_list[0]
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(('http://follower/inbox',), args)
|
2020-10-31 22:24:56 +00:00
|
|
|
self.assertEqual(ACCEPT, json_loads(kwargs['data']))
|
2018-10-21 22:28:42 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
# check webmention
|
|
|
|
args, kwargs = mock_post.call_args_list[1]
|
2019-12-26 06:20:57 +00:00
|
|
|
self.assertEqual(('https://realize.be/webmention',), args)
|
|
|
|
self.assertEqual({
|
2018-10-23 14:11:44 +00:00
|
|
|
'source': 'http://localhost/render?source=https%3A%2F%2Fmastodon.social%2F6d1a&target=https%3A%2F%2Frealize.be%2F',
|
|
|
|
'target': 'https://realize.be/',
|
|
|
|
}, kwargs['data'])
|
|
|
|
|
|
|
|
resp = Response.get_by_id('https://mastodon.social/6d1a https://realize.be/')
|
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('activitypub', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
2019-12-25 07:26:58 +00:00
|
|
|
self.assertEqual(FOLLOW_WITH_ACTOR, json_loads(resp.source_as2))
|
2018-10-23 14:11:44 +00:00
|
|
|
|
2018-10-22 00:37:33 +00:00
|
|
|
# check that we stored a Follower object
|
2018-10-23 14:52:30 +00:00
|
|
|
follower = Follower.get_by_id('realize.be %s' % (FOLLOW['actor']))
|
2019-08-01 14:32:45 +00:00
|
|
|
self.assertEqual('active', follower.status)
|
2019-12-25 07:26:58 +00:00
|
|
|
self.assertEqual(FOLLOW_WRAPPED_WITH_ACTOR, json_loads(follower.last_follow))
|
2018-10-22 00:37:33 +00:00
|
|
|
|
2019-08-01 14:32:45 +00:00
|
|
|
def test_inbox_undo_follow(self, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='https://realize.be/')
|
|
|
|
|
|
|
|
Follower(id=Follower._id('realize.be', FOLLOW['actor'])).put()
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(UNDO_FOLLOW_WRAPPED).encode())
|
|
|
|
self.assertEqual(200, got.status_int)
|
2019-08-01 14:32:45 +00:00
|
|
|
|
|
|
|
follower = Follower.get_by_id('realize.be %s' % FOLLOW['actor'])
|
|
|
|
self.assertEqual('inactive', follower.status)
|
|
|
|
|
|
|
|
def test_inbox_undo_follow_doesnt_exist(self, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='https://realize.be/')
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(UNDO_FOLLOW_WRAPPED).encode())
|
|
|
|
self.assertEqual(200, got.status_int)
|
2019-08-01 14:32:45 +00:00
|
|
|
|
|
|
|
def test_inbox_undo_follow_inactive(self, mock_head, mock_get, mock_post):
|
|
|
|
mock_head.return_value = requests_response(url='https://realize.be/')
|
|
|
|
Follower(id=Follower._id('realize.be', 'https://mastodon.social/users/swentel'),
|
|
|
|
status='inactive').put()
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json_dumps(UNDO_FOLLOW_WRAPPED).encode())
|
|
|
|
self.assertEqual(200, got.status_int)
|
2019-08-01 14:32:45 +00:00
|
|
|
|
2018-10-23 14:11:44 +00:00
|
|
|
def test_inbox_unsupported_type(self, *_):
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/foo.com/inbox', method='POST', body=json_dumps({
|
2017-10-17 05:21:13 +00:00
|
|
|
'@context': ['https://www.w3.org/ns/activitystreams'],
|
|
|
|
'id': 'https://xoxo.zone/users/aaronpk#follows/40',
|
2018-10-21 22:28:42 +00:00
|
|
|
'type': 'Block',
|
2017-10-17 05:21:13 +00:00
|
|
|
'actor': 'https://xoxo.zone/users/aaronpk',
|
|
|
|
'object': 'http://snarfed.org/',
|
2019-12-26 06:20:57 +00:00
|
|
|
}).encode())
|
|
|
|
self.assertEqual(501, got.status_int)
|
2020-03-01 04:28:53 +00:00
|
|
|
|
|
|
|
def test_inbox_delete_actor(self, mock_head, mock_get, mock_post):
|
|
|
|
follower = Follower.get_or_create('realize.be', DELETE['actor'])
|
|
|
|
Follower.get_or_create('snarfed.org', DELETE['actor'])
|
|
|
|
# other unrelated follower
|
|
|
|
other = Follower.get_or_create('realize.be', 'https://mas.to/users/other')
|
|
|
|
self.assertEqual(3, Follower.query().count())
|
|
|
|
|
|
|
|
got = application.get_response('/realize.be/inbox', method='POST',
|
|
|
|
body=json_dumps(DELETE).encode())
|
|
|
|
self.assertEqual(200, got.status_int)
|
|
|
|
|
2020-10-11 15:32:55 +00:00
|
|
|
# TODO: bring back
|
|
|
|
# self.assertEqual([other], Follower.query().fetch())
|