2017-08-20 14:28:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
"""Unit tests for salmon.py.
|
|
|
|
|
|
|
|
TODO: test error handling
|
|
|
|
"""
|
|
|
|
from __future__ import unicode_literals
|
2017-08-23 15:14:51 +00:00
|
|
|
import copy
|
2017-08-26 22:18:33 +00:00
|
|
|
import datetime
|
2017-08-20 14:28:40 +00:00
|
|
|
import urllib
|
|
|
|
|
|
|
|
from django_salmon import magicsigs
|
|
|
|
import mock
|
2017-08-24 14:41:46 +00:00
|
|
|
from oauth_dropins.webutil.testutil import requests_response, UrlopenResult
|
2017-08-20 14:28:40 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
import common
|
2017-10-10 00:29:50 +00:00
|
|
|
from models import MagicKey, Response
|
2017-08-20 14:28:40 +00:00
|
|
|
from salmon import app
|
2017-08-24 06:24:47 +00:00
|
|
|
import testutil
|
2017-08-20 14:28:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('requests.post')
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
@mock.patch('urllib2.urlopen')
|
2017-08-24 06:24:47 +00:00
|
|
|
class SalmonTest(testutil.TestCase):
|
2017-08-20 14:28:40 +00:00
|
|
|
|
2017-10-17 05:21:13 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(SalmonTest, self).setUp()
|
|
|
|
self.key = MagicKey.get_or_create('alice')
|
|
|
|
|
2017-10-15 23:44:29 +00:00
|
|
|
def send_slap(self, mock_urlopen, mock_get, mock_post, atom_slap):
|
2017-08-20 14:28:40 +00:00
|
|
|
# salmon magic key discovery. first host-meta, then webfinger
|
2017-08-20 18:33:00 +00:00
|
|
|
mock_urlopen.side_effect = [
|
2017-08-24 06:24:47 +00:00
|
|
|
UrlopenResult(200, """\
|
2017-08-20 14:28:40 +00:00
|
|
|
<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
|
|
|
|
<Link rel='lrdd' type='application/xrd+xml' template='http://webfinger/{uri}' />
|
|
|
|
</XRD>"""),
|
2017-08-24 06:24:47 +00:00
|
|
|
UrlopenResult(200, """\
|
2017-08-20 14:28:40 +00:00
|
|
|
<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'>
|
|
|
|
<Subject>alice@fedsoc.net</Subject>
|
|
|
|
<Link rel='magic-public-key' href='%s' />
|
2017-10-15 23:44:29 +00:00
|
|
|
</XRD>""" % self.key.href()),
|
2017-08-20 14:28:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# webmention discovery
|
2017-08-24 14:41:46 +00:00
|
|
|
mock_get.return_value = requests_response(
|
|
|
|
'<html><head><link rel="webmention" href="/webmention"></html>')
|
2017-08-20 14:28:40 +00:00
|
|
|
# webmention post
|
2017-08-24 14:41:46 +00:00
|
|
|
mock_post.return_value = requests_response()
|
2017-08-20 14:28:40 +00:00
|
|
|
|
2017-10-20 14:49:25 +00:00
|
|
|
slap = magicsigs.magic_envelope(atom_slap, common.CONTENT_TYPE_ATOM, self.key)
|
2017-10-26 15:07:29 +00:00
|
|
|
got = app.get_response('/foo.com@foo.com/salmon', method='POST', body=slap)
|
2017-10-15 23:44:29 +00:00
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
|
|
|
|
# check salmon magic key discovery
|
|
|
|
mock_urlopen.assert_has_calls((
|
|
|
|
mock.call('http://fedsoc.net/.well-known/host-meta'),
|
|
|
|
mock.call('http://webfinger/alice@fedsoc.net'),
|
|
|
|
))
|
|
|
|
|
|
|
|
# check webmention discovery
|
|
|
|
self.expected_headers = copy.deepcopy(common.HEADERS)
|
|
|
|
self.expected_headers['Accept'] = '*/*'
|
|
|
|
mock_get.assert_called_once_with(
|
|
|
|
'http://orig/post', headers=common.HEADERS, verify=False)
|
|
|
|
|
|
|
|
def test_reply(self, mock_urlopen, mock_get, mock_post):
|
2017-08-20 14:28:40 +00:00
|
|
|
atom_reply = """\
|
|
|
|
<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<entry xmlns='http://www.w3.org/2005/Atom'>
|
|
|
|
<id>https://my/reply</id>
|
2017-10-15 23:44:29 +00:00
|
|
|
<uri>https://my/reply</uri>
|
2017-08-20 14:28:40 +00:00
|
|
|
<author>
|
|
|
|
<name>Alice</name>
|
|
|
|
<uri>alice@fedsoc.net</uri>
|
|
|
|
</author>
|
2017-10-15 23:44:29 +00:00
|
|
|
<thr:in-reply-to xmlns:thr="http://purl.org/syndication/thread/1.0">
|
2017-08-20 14:28:40 +00:00
|
|
|
http://orig/post
|
|
|
|
</thr:in-reply-to>
|
|
|
|
<content>I hereby reply.</content>
|
|
|
|
<title>My Reply</title>
|
2017-08-26 22:18:33 +00:00
|
|
|
<updated>%s</updated>
|
|
|
|
</entry>""" % datetime.datetime.now().isoformat(b'T')
|
2017-10-15 23:44:29 +00:00
|
|
|
self.send_slap(mock_urlopen, mock_get, mock_post, atom_reply)
|
2017-08-20 14:28:40 +00:00
|
|
|
|
2017-10-15 23:44:29 +00:00
|
|
|
# check webmention post
|
|
|
|
mock_post.assert_called_once_with(
|
|
|
|
'http://orig/webmention',
|
|
|
|
data={'source': 'https://my/reply', 'target': 'http://orig/post'},
|
|
|
|
allow_redirects=False,
|
|
|
|
headers=self.expected_headers,
|
|
|
|
verify=False)
|
2017-08-20 14:28:40 +00:00
|
|
|
|
2017-10-15 23:44:29 +00:00
|
|
|
# check stored response
|
|
|
|
resp = Response.get_by_id('https://my/reply http://orig/post')
|
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('ostatus', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
|
|
|
self.assertEqual(atom_reply, resp.source_atom)
|
|
|
|
|
|
|
|
def test_like(self, mock_urlopen, mock_get, mock_post):
|
|
|
|
atom_like = """\
|
|
|
|
<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<entry xmlns='http://www.w3.org/2005/Atom'
|
|
|
|
xmlns:activity='http://activitystrea.ms/spec/1.0/'>
|
|
|
|
<uri>https://my/like</uri>
|
|
|
|
<author>
|
|
|
|
<name>Alice</name>
|
|
|
|
<uri>alice@fedsoc.net</uri>
|
|
|
|
</author>
|
|
|
|
<activity:verb>http://activitystrea.ms/schema/1.0/like</activity:verb>
|
|
|
|
<activity:object>http://orig/post</activity:object>
|
|
|
|
<updated>%s</updated>
|
|
|
|
</entry>""" % datetime.datetime.now().isoformat(b'T')
|
|
|
|
self.send_slap(mock_urlopen, mock_get, mock_post, atom_like)
|
|
|
|
|
|
|
|
# check webmention post
|
2017-08-20 14:28:40 +00:00
|
|
|
mock_post.assert_called_once_with(
|
|
|
|
'http://orig/webmention',
|
|
|
|
data={
|
2017-10-15 23:44:29 +00:00
|
|
|
'source': 'http://localhost/render?source=https%3A%2F%2Fmy%2Flike&target=http%3A%2F%2Forig%2Fpost',
|
2017-08-20 14:28:40 +00:00
|
|
|
'target': 'http://orig/post',
|
|
|
|
},
|
|
|
|
allow_redirects=False,
|
2017-10-15 23:44:29 +00:00
|
|
|
headers=self.expected_headers,
|
2017-08-20 14:28:40 +00:00
|
|
|
verify=False)
|
2017-10-10 00:29:50 +00:00
|
|
|
|
2017-10-15 23:44:29 +00:00
|
|
|
# check stored response
|
|
|
|
resp = Response.get_by_id('https://my/like http://orig/post')
|
2017-10-10 00:29:50 +00:00
|
|
|
self.assertEqual('in', resp.direction)
|
|
|
|
self.assertEqual('ostatus', resp.protocol)
|
|
|
|
self.assertEqual('complete', resp.status)
|
2017-10-15 23:44:29 +00:00
|
|
|
self.assertEqual(atom_like, resp.source_atom)
|
2017-10-17 05:02:37 +00:00
|
|
|
|
2017-10-17 05:21:13 +00:00
|
|
|
def test_bad_envelope(self, mock_urlopen, mock_get, mock_post):
|
2017-10-17 05:02:37 +00:00
|
|
|
got = app.get_response('/foo.com/salmon', method='POST',
|
|
|
|
body='not xml'.encode('utf-8'))
|
|
|
|
self.assertEquals(400, got.status_int)
|
2017-10-17 05:21:13 +00:00
|
|
|
|
|
|
|
def test_bad_inner_xml(self, mock_urlopen, mock_get, mock_post):
|
2017-10-20 14:49:25 +00:00
|
|
|
slap = magicsigs.magic_envelope('not xml', common.CONTENT_TYPE_ATOM, self.key)
|
2017-10-17 05:21:13 +00:00
|
|
|
got = app.get_response('/foo.com/salmon', method='POST', body=slap)
|
|
|
|
self.assertEquals(400, got.status_int)
|
|
|
|
|
|
|
|
def test_rsvp_not_supported(self, mock_urlopen, mock_get, mock_post):
|
|
|
|
slap = magicsigs.magic_envelope("""\
|
|
|
|
<?xml version='1.0' encoding='UTF-8'?>
|
|
|
|
<entry xmlns='http://www.w3.org/2005/Atom'
|
|
|
|
xmlns:activity='http://activitystrea.ms/spec/1.0/'>
|
|
|
|
<uri>https://my/rsvp</uri>
|
|
|
|
<activity:verb>http://activitystrea.ms/schema/1.0/rsvp</activity:verb>
|
|
|
|
<activity:object>http://orig/event</activity:object>
|
2017-10-20 14:49:25 +00:00
|
|
|
</entry>""", common.CONTENT_TYPE_ATOM, self.key)
|
2017-10-17 05:21:13 +00:00
|
|
|
got = app.get_response('/foo.com/salmon', method='POST', body=slap)
|
|
|
|
self.assertEquals(501, got.status_int)
|