2017-08-19 15:21:05 +00:00
|
|
|
# coding=utf-8
|
|
|
|
"""Unit tests for webfinger.py.
|
|
|
|
|
|
|
|
TODO: test error handling
|
|
|
|
"""
|
2017-08-19 20:10:18 +00:00
|
|
|
from __future__ import unicode_literals
|
2017-08-19 15:21:05 +00:00
|
|
|
import json
|
2017-09-12 14:31:50 +00:00
|
|
|
import urllib
|
2017-08-19 15:21:05 +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-19 15:21:05 +00:00
|
|
|
import requests
|
|
|
|
|
2017-08-19 20:10:18 +00:00
|
|
|
import common
|
|
|
|
import models
|
2017-08-24 06:24:47 +00:00
|
|
|
import testutil
|
2017-08-19 15:21:05 +00:00
|
|
|
from webfinger import app
|
|
|
|
|
2017-09-12 14:31:50 +00:00
|
|
|
USER = '%s@foo.com' % common.USERNAME
|
|
|
|
|
2017-08-19 15:21:05 +00:00
|
|
|
|
2017-08-24 06:24:47 +00:00
|
|
|
class WebFingerTest(testutil.TestCase):
|
2017-08-19 16:24:00 +00:00
|
|
|
|
2017-09-03 22:44:01 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(WebFingerTest, self).setUp()
|
|
|
|
self.html = """
|
2017-08-19 16:24:00 +00:00
|
|
|
<body>
|
|
|
|
<a class="h-card" rel="me" href="/about-me">
|
|
|
|
<img class="u-photo" src="/me.jpg" />
|
|
|
|
Mrs. ☕ Foo
|
|
|
|
</a>
|
|
|
|
</body>
|
2017-09-03 22:44:01 +00:00
|
|
|
"""
|
2017-09-12 14:31:50 +00:00
|
|
|
self.key = models.MagicKey.get_or_create('foo.com')
|
2017-09-03 22:44:01 +00:00
|
|
|
self.expected_webfinger = {
|
2017-09-12 14:31:50 +00:00
|
|
|
'subject': 'acct:' + USER,
|
2017-08-19 16:24:00 +00:00
|
|
|
'aliases': [
|
2017-08-19 20:10:18 +00:00
|
|
|
'https://foo.com/about-me',
|
2017-08-19 20:34:06 +00:00
|
|
|
'https://foo.com/',
|
2017-08-19 16:24:00 +00:00
|
|
|
],
|
2017-09-03 22:44:01 +00:00
|
|
|
'magic_keys': [{'value': self.key.href()}],
|
2017-08-19 16:24:00 +00:00
|
|
|
'links': [{
|
|
|
|
'rel': 'http://webfinger.net/rel/profile-page',
|
|
|
|
'type': 'text/html',
|
2017-08-19 20:34:06 +00:00
|
|
|
'href': 'https://foo.com/about-me'
|
2017-08-19 16:24:00 +00:00
|
|
|
}, {
|
2017-08-19 20:10:18 +00:00
|
|
|
'rel': 'http://webfinger.net/rel/profile-page',
|
|
|
|
'type': 'text/html',
|
2017-08-19 20:34:06 +00:00
|
|
|
'href': 'https://foo.com/'
|
2017-08-20 02:46:53 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'http://webfinger.net/rel/avatar',
|
|
|
|
'href': 'https://foo.com/me.jpg'
|
2017-09-03 19:35:18 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'canonical_uri',
|
|
|
|
'type': 'text/html',
|
|
|
|
'href': 'https://foo.com/about-me'
|
2017-09-28 14:25:21 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'self',
|
|
|
|
'type': 'application/activity+json',
|
|
|
|
'href': 'http://localhost/foo.com'
|
|
|
|
}, {
|
|
|
|
'rel': 'inbox',
|
|
|
|
'type': 'application/activity+json',
|
|
|
|
'href': 'http://localhost/foo.com/inbox'
|
2017-09-03 19:35:18 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'http://schemas.google.com/g/2010#updates-from',
|
|
|
|
'type': 'application/atom+xml',
|
2017-09-13 14:05:30 +00:00
|
|
|
'href': 'https://granary-demo.appspot.com/url?url=https%3A%2F%2Ffoo.com%2F&input=html&hub=https%3A%2F%2Ffoo.com%2F&output=atom',
|
2017-09-13 14:48:32 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'hub',
|
|
|
|
'href': 'https://bridgy-fed.superfeedr.com/'
|
2017-08-19 16:24:00 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'magic-public-key',
|
2017-09-03 22:44:01 +00:00
|
|
|
'href': self.key.href(),
|
2017-08-20 02:46:53 +00:00
|
|
|
}, {
|
|
|
|
'rel': 'salmon',
|
2017-09-12 14:31:50 +00:00
|
|
|
'href': 'http://localhost/foo.com/salmon'
|
2017-08-19 16:24:00 +00:00
|
|
|
# }, {
|
|
|
|
# 'rel': 'http://ostatus.org/schema/1.0/subscribe',
|
|
|
|
# 'template': 'https://mastodon.technology/authorize_follow?acct={uri}'
|
|
|
|
}]
|
2017-09-03 22:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def test_host_meta_handler_xrd(self):
|
|
|
|
got = app.get_response('/.well-known/host-meta')
|
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
self.assertEquals('application/xrd+xml; charset=utf-8',
|
|
|
|
got.headers['Content-Type'])
|
|
|
|
self.assertTrue(got.body.startswith('<?xml'), got.body)
|
|
|
|
|
|
|
|
def test_host_meta_handler_xrds(self):
|
|
|
|
got = app.get_response('/.well-known/host-meta.xrds')
|
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
self.assertEquals('application/xrds+xml; charset=utf-8',
|
|
|
|
got.headers['Content-Type'])
|
|
|
|
self.assertTrue(got.body.startswith('<XRDS'), got.body)
|
|
|
|
|
|
|
|
def test_host_meta_handler_jrd(self):
|
|
|
|
got = app.get_response('/.well-known/host-meta.json')
|
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
self.assertEquals('application/json; charset=utf-8',
|
|
|
|
got.headers['Content-Type'])
|
|
|
|
self.assertTrue(got.body.startswith('{'), got.body)
|
|
|
|
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
def test_user_handler(self, mock_get):
|
|
|
|
mock_get.return_value = requests_response(self.html, url = 'https://foo.com/')
|
|
|
|
|
2017-09-12 14:31:50 +00:00
|
|
|
got = app.get_response('/foo.com', headers={'Accept': 'application/json'})
|
2017-09-03 22:44:01 +00:00
|
|
|
self.assertEquals(200, got.status_int)
|
|
|
|
self.assertEquals('application/json; charset=utf-8',
|
|
|
|
got.headers['Content-Type'])
|
|
|
|
mock_get.assert_called_once_with('http://foo.com/', headers=common.HEADERS,
|
|
|
|
timeout=util.HTTP_TIMEOUT)
|
|
|
|
|
|
|
|
self.assertEquals(self.expected_webfinger, json.loads(got.body))
|
2017-08-19 20:31:06 +00:00
|
|
|
|
2017-08-19 20:36:52 +00:00
|
|
|
# check that magic key is persistent
|
|
|
|
again = json.loads(app.get_response(
|
2017-09-12 14:31:50 +00:00
|
|
|
'/foo.com', headers={'Accept': 'application/json'}).body)
|
2017-09-03 22:44:01 +00:00
|
|
|
self.assertEquals(self.key.href(), again['magic_keys'][0]['value'])
|
2017-08-20 02:46:53 +00:00
|
|
|
|
|
|
|
links = {l['rel']: l['href'] for l in again['links']}
|
2017-09-03 22:44:01 +00:00
|
|
|
self.assertEquals(self.key.href(), links['magic-public-key'])
|
2017-08-19 20:36:52 +00:00
|
|
|
|
2017-09-13 14:05:30 +00:00
|
|
|
@mock.patch('requests.get')
|
|
|
|
def test_user_handler_with_atom_feed(self, mock_get):
|
|
|
|
html = """\
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<link rel="feed" href="/dont-use">
|
|
|
|
<link rel="alternate" type="application/rss+xml" href="/dont-use-either">
|
|
|
|
<link rel="alternate" type="application/atom+xml" href="/use-this">
|
|
|
|
</head>
|
|
|
|
""" + self.html
|
|
|
|
mock_get.return_value = requests_response(html, url = 'https://foo.com/')
|
|
|
|
|
|
|
|
got = app.get_response('/foo.com', headers={'Accept': 'application/json'})
|
|
|
|
self.assertEquals(200, got.status_int)
|
2017-09-28 14:25:21 +00:00
|
|
|
self.assertIn({
|
2017-09-13 14:48:32 +00:00
|
|
|
'rel': 'http://schemas.google.com/g/2010#updates-from',
|
|
|
|
'type': 'application/atom+xml',
|
|
|
|
'href': 'https://foo.com/use-this',
|
2017-09-28 14:25:21 +00:00
|
|
|
}, json.loads(got.body)['links'])
|
2017-09-13 14:48:32 +00:00
|
|
|
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
def test_user_handler_with_push_header(self, mock_get):
|
|
|
|
mock_get.return_value = requests_response(
|
|
|
|
self.html, url = 'https://foo.com/', headers={
|
|
|
|
'Link': 'badly formatted, '
|
|
|
|
"<xyz>; rel='foo',"
|
|
|
|
'<http://a.custom.hub/>; rel="hub"',
|
|
|
|
})
|
|
|
|
|
|
|
|
got = app.get_response('/foo.com', headers={'Accept': 'application/json'})
|
|
|
|
self.assertEquals(200, got.status_int)
|
2017-09-28 14:25:21 +00:00
|
|
|
self.assertIn({
|
2017-09-13 14:48:32 +00:00
|
|
|
'rel': 'hub',
|
|
|
|
'href': 'http://a.custom.hub/',
|
2017-09-28 14:25:21 +00:00
|
|
|
}, json.loads(got.body)['links'])
|
2017-09-13 14:05:30 +00:00
|
|
|
|
2017-08-19 20:31:06 +00:00
|
|
|
@mock.patch('requests.get')
|
|
|
|
def test_user_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-09-12 14:31:50 +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)
|
2017-09-03 22:44:01 +00:00
|
|
|
|
|
|
|
@mock.patch('requests.get')
|
|
|
|
def test_webfinger_handler(self, mock_get):
|
2017-09-12 14:31:50 +00:00
|
|
|
mock_get.return_value = requests_response(self.html, url='https://foo.com/')
|
|
|
|
|
|
|
|
for resource in ('me@foo.com', 'acct:me@foo.com', 'xyz@foo.com',
|
|
|
|
'foo.com', 'http://foo.com/', 'https://foo.com/'):
|
|
|
|
url = '/.well-known/webfinger?%s' % urllib.urlencode(
|
|
|
|
{'resource': resource})
|
|
|
|
got = app.get_response(url, headers={'Accept': 'application/json'})
|
|
|
|
self.assertEquals(200, got.status_int, got.body)
|
|
|
|
self.assertEquals('application/json; charset=utf-8',
|
|
|
|
got.headers['Content-Type'])
|
|
|
|
self.assertEquals(self.expected_webfinger, json.loads(got.body))
|