# coding=utf-8 """Unit tests for webfinger.py.""" import copy from unittest.mock import patch import urllib.parse from oauth_dropins.webutil.testutil import requests_response # import first so that Fake is defined before URL routes are registered from .testutil import Fake, TestCase 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': 'application/activity+json', 'href': 'http://localhost/user.com', }, { 'rel': 'inbox', 'type': 'application/activity+json', 'href': 'http://localhost/user.com/inbox' }, { 'rel': 'sharedInbox', 'type': 'application/activity+json', '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_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': 'application/activity+json', 'href': 'http://localhost/user.com', }, { 'rel': 'inbox', 'type': 'application/activity+json', 'href': 'http://localhost/user.com/inbox', }, { 'rel': 'sharedInbox', 'type': 'application/activity+json', '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': ['fake:user'], 'links': [{ 'rel': 'canonical_uri', 'type': 'text/html', 'href': 'fake:user', }, { 'rel': 'self', 'type': 'application/activity+json', 'href': 'http://localhost/ap/fa/fake:user', }, { 'rel': 'inbox', 'type': 'application/activity+json', 'href': 'http://localhost/ap/fa/fake:user/inbox', }, { 'rel': 'sharedInbox', 'type': 'application/activity+json', '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'][3]['href'] = 'https://fa.brid.gy/ap/sharedInbox' WEBFINGER_FAKE_FA_BRID_GY['links'][4]['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('