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-19 19:36:36 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-08-23 15:14:51 +00:00
|
|
|
import copy
|
2017-08-13 07:12:16 +00:00
|
|
|
import json
|
|
|
|
import unittest
|
2017-08-13 21:49:35 +00:00
|
|
|
import urllib
|
2017-08-13 07:12:16 +00:00
|
|
|
|
|
|
|
import mock
|
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
|
2017-08-13 07:12:16 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
import activitypub
|
2017-08-13 21:49:35 +00:00
|
|
|
from activitypub import app
|
2017-08-15 06:07:24 +00:00
|
|
|
import common
|
2017-08-13 07:12:16 +00:00
|
|
|
|
|
|
|
|
2017-08-13 21:49:35 +00:00
|
|
|
@mock.patch('requests.post')
|
2017-08-13 07:12:16 +00:00
|
|
|
@mock.patch('requests.get')
|
|
|
|
class ActivityPubTest(unittest.TestCase):
|
|
|
|
|
2017-08-13 21:49:35 +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>
|
|
|
|
<a class="h-card" rel="me" href="/about-me">Mrs. ☕ Foo</a>
|
|
|
|
</body>
|
2017-08-24 14:41:46 +00:00
|
|
|
""", url='https://foo.com/')
|
2017-08-13 07:12:16 +00:00
|
|
|
|
2017-08-13 21:49:35 +00:00
|
|
|
got = app.get_response('/foo.com')
|
2017-08-23 15:14:51 +00:00
|
|
|
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
|
|
|
timeout=util.HTTP_TIMEOUT)
|
2017-08-13 07:12:16 +00:00
|
|
|
self.assertEquals(200, got.status_int)
|
2017-08-15 06:07:24 +00:00
|
|
|
self.assertEquals(activitypub.CONTENT_TYPE_AS2, got.headers['Content-Type'])
|
2017-08-13 07:12:16 +00:00
|
|
|
self.assertEquals({
|
|
|
|
'objectType' : 'person',
|
|
|
|
'displayName': u'Mrs. ☕ Foo',
|
|
|
|
'url': 'https://foo.com/about-me',
|
|
|
|
'inbox': 'http://localhost/foo.com/inbox',
|
|
|
|
}, json.loads(got.body))
|
2017-08-13 21:49:35 +00:00
|
|
|
|
2017-08-19 20:31:06 +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
|
|
|
|
|
|
|
got = app.get_response('/foo.com')
|
2017-08-23 15:14:51 +00:00
|
|
|
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
|
|
|
timeout=util.HTTP_TIMEOUT)
|
2017-08-19 20:31:06 +00:00
|
|
|
self.assertEquals(400, got.status_int)
|
|
|
|
self.assertIn('representative h-card', got.body)
|
|
|
|
# TODO
|
|
|
|
# self.assertEquals('text/html', got.headers['Content-Type'])
|
|
|
|
|
2017-08-13 21:49:35 +00:00
|
|
|
def test_inbox_reply(self, mock_get, mock_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
|
|
|
|
|
|
|
got = app.get_response('/foo.com/inbox', method='POST',
|
|
|
|
body=json.dumps({
|
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Note',
|
|
|
|
'content': u'A ☕ reply',
|
|
|
|
'url': 'http://this/reply',
|
|
|
|
'inReplyTo': 'http://orig/post',
|
|
|
|
'cc': ['https://www.w3.org/ns/activitystreams#Public'],
|
|
|
|
}))
|
2017-08-19 19:29:10 +00:00
|
|
|
mock_get.assert_called_once_with(
|
|
|
|
'http://orig/post', headers=common.HEADERS, verify=False)
|
2017-08-13 21:49:35 +00:00
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
|
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={
|
|
|
|
'source': 'http://this/reply',
|
|
|
|
'target': 'http://orig/post',
|
|
|
|
},
|
|
|
|
allow_redirects=False,
|
2017-08-23 15:14:51 +00:00
|
|
|
headers=expected_headers,
|
2017-08-13 21:49:35 +00:00
|
|
|
verify=False)
|