"""Unit tests for webfinger.py.""" import copy from unittest.mock import patch import urllib.parse from granary.as2 import CONTENT_TYPE, CONTENT_TYPE_LD_PROFILE from oauth_dropins.webutil import util from oauth_dropins.webutil.testutil import requests_response # import first so that Fake is defined before URL routes are registered from .testutil import ExplicitEnableFake, Fake, TestCase from models import PROTOCOLS import protocol from web import Web from webfinger import fetch, fetch_actor_url from . import test_web WEBFINGER = { 'subject': 'acct:user.com@user.com', 'aliases': [ 'https://user.com/about-me', 'https://user.com/', ], 'links': [{ 'rel': 'http://webfinger.net/rel/profile-page', 'type': 'text/html', 'href': 'https://user.com/about-me', }, { 'rel': 'http://webfinger.net/rel/profile-page', 'type': 'text/html', 'href': 'https://user.com/', }, { 'rel': 'http://webfinger.net/rel/avatar', 'href': 'https://user.com/me.jpg', }, { 'rel': 'canonical_uri', 'type': 'text/html', 'href': 'https://user.com/about-me', }, { 'rel': 'self', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'http://localhost/user.com', }, { 'rel': 'self', 'type': CONTENT_TYPE, 'href': 'http://localhost/user.com', }, { 'rel': 'inbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'http://localhost/user.com/inbox' }, { 'rel': 'sharedInbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'https://web.brid.gy/ap/sharedInbox', }, { 'rel': 'http://ostatus.org/schema/1.0/subscribe', # TODO: genericize 'template': 'https://fed.brid.gy/web/user.com?url={uri}', }], } WEBFINGER_NO_HCARD = { 'subject': 'acct:user.com@user.com', 'aliases': ['https://user.com/'], 'links': [{ 'rel': 'http://webfinger.net/rel/profile-page', 'type': 'text/html', 'href': 'https://user.com/', }, { 'rel': 'canonical_uri', 'type': 'text/html', 'href': 'https://user.com/', }, { 'rel': 'self', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'https://web.brid.gy/user.com', }, { 'rel': 'self', 'type': CONTENT_TYPE, 'href': 'https://web.brid.gy/user.com', }, { 'rel': 'inbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'https://web.brid.gy/user.com/inbox', }, { 'rel': 'sharedInbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'https://web.brid.gy/ap/sharedInbox', }, { 'rel': 'http://ostatus.org/schema/1.0/subscribe', 'template': 'https://fed.brid.gy/web/user.com?url={uri}', }], } WEBFINGER_FAKE = { 'subject': 'acct:fake:handle:user@fa.brid.gy', 'aliases': ['web:fake:user'], 'links': [{ 'rel': 'canonical_uri', 'type': 'text/html', 'href': 'web:fake:user', }, { 'rel': 'self', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'http://localhost/ap/fa/fake:user', }, { 'rel': 'self', 'type': CONTENT_TYPE, 'href': 'http://localhost/ap/fa/fake:user', }, { 'rel': 'inbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'http://localhost/ap/fa/fake:user/inbox', }, { 'rel': 'sharedInbox', 'type': CONTENT_TYPE_LD_PROFILE, 'href': 'https://web.brid.gy/ap/sharedInbox', }, { 'rel': 'http://ostatus.org/schema/1.0/subscribe', 'template': 'https://fed.brid.gy/fa/fake:handle:user?url={uri}', }], } WEBFINGER_FAKE_FA_BRID_GY = copy.deepcopy(WEBFINGER_FAKE) for link in WEBFINGER_FAKE_FA_BRID_GY['links']: if 'href' in link: link['href'] = link['href'].replace('http://localhost/ap/fa', 'https://fa.brid.gy/ap') WEBFINGER_FAKE_FA_BRID_GY['links'][4]['href'] = 'https://fa.brid.gy/ap/sharedInbox' WEBFINGER_FAKE_FA_BRID_GY['links'][5]['template'] = 'https://fed.brid.gy/fa/fake:handle:user?url={uri}' class HostMetaTest(TestCase): def test_host_meta_xrd(self): got = self.client.get('/.well-known/host-meta') self.assertEqual(200, got.status_code) self.assertEqual('application/xrd+xml; charset=utf-8', got.headers['Content-Type']) body = got.get_data(as_text=True) self.assertTrue(body.startswith('