kopia lustrzana https://github.com/snarfed/bridgy-fed
/convert/: require user for converting to AS2
since activitypub.postprocess_as2 currently needs g.user.pull/542/head
rodzic
c8026298b9
commit
0f19654eb2
|
@ -9,7 +9,7 @@ import logging
|
|||
import re
|
||||
import urllib.parse
|
||||
|
||||
from flask import redirect, request
|
||||
from flask import g, redirect, request
|
||||
from granary import as1
|
||||
from oauth_dropins.webutil import flask_util, util
|
||||
from oauth_dropins.webutil.flask_util import error
|
||||
|
@ -52,6 +52,13 @@ def convert(src, dest, _):
|
|||
if not util.is_web(url):
|
||||
error(f'Expected fully qualified URL; got {url}')
|
||||
|
||||
# require g.user for AP since postprocess_as2 currently needs it. ugh
|
||||
if dest == ActivityPub.LABEL:
|
||||
domain = util.domain_from_link(url, minimize=False)
|
||||
g.user = Web.get_by_id(domain)
|
||||
if not g.user:
|
||||
error(f'No web user found for {domain}')
|
||||
|
||||
# load, and maybe fetch. if it's a post/update, redirect to inner object.
|
||||
obj = PROTOCOLS[src].load(url)
|
||||
if not obj.as1:
|
||||
|
|
|
@ -7,6 +7,7 @@ from granary import as2
|
|||
from granary.tests.test_as1 import ACTOR, COMMENT, DELETE_OF_ID, UPDATE
|
||||
from models import Object
|
||||
from oauth_dropins.webutil.testutil import requests_response
|
||||
from oauth_dropins.webutil.util import parse_mf2
|
||||
import requests
|
||||
|
||||
# import first so that Fake is defined before URL routes are registered
|
||||
|
@ -14,12 +15,15 @@ from . import testutil
|
|||
|
||||
from common import CONTENT_TYPE_HTML
|
||||
|
||||
from .test_redirect import (
|
||||
REPOST_AS2,
|
||||
REPOST_HTML,
|
||||
)
|
||||
|
||||
EXPECTED_HTML = """\
|
||||
COMMENT_AS2 = {
|
||||
**as2.to_as1(COMMENT),
|
||||
'type': 'Note',
|
||||
'id': 'http://localhost/r/tag:fake.com:123456',
|
||||
'url': 'http://localhost/r/https://fake.com/123456',
|
||||
'name': 'A ☕ reply',
|
||||
'inReplyTo': 'https://fake.com/123',
|
||||
}
|
||||
HTML = """\
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><meta charset="utf-8">
|
||||
|
@ -37,7 +41,7 @@ EXPECTED_HTML = """\
|
|||
</body>
|
||||
</html>
|
||||
"""
|
||||
EXPECTED_AUTHOR_HTML = """\
|
||||
AUTHOR_HTML = """\
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><meta charset="utf-8">
|
||||
|
@ -87,7 +91,7 @@ class ConvertTest(testutil.TestCase):
|
|||
|
||||
resp = self.client.get('/convert/activitypub/web/https://user.com/bar?baz=baj&biff')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assert_multiline_equals(EXPECTED_HTML, resp.get_data(as_text=True),
|
||||
self.assert_multiline_equals(HTML, resp.get_data(as_text=True),
|
||||
ignore_blanks=True)
|
||||
|
||||
def test_activitypub_to_web_object_empty(self):
|
||||
|
@ -105,7 +109,7 @@ class ConvertTest(testutil.TestCase):
|
|||
resp = self.client.get(f'/convert/activitypub/web/{url}')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(CONTENT_TYPE_HTML, resp.content_type)
|
||||
self.assert_multiline_equals(EXPECTED_HTML, resp.get_data(as_text=True),
|
||||
self.assert_multiline_equals(HTML, resp.get_data(as_text=True),
|
||||
ignore_blanks=True)
|
||||
|
||||
mock_get.assert_has_calls((self.as2_req(url),))
|
||||
|
@ -127,7 +131,7 @@ class ConvertTest(testutil.TestCase):
|
|||
|
||||
resp = self.client.get('/convert/activitypub/web/http://foo')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assert_multiline_equals(EXPECTED_AUTHOR_HTML, resp.get_data(as_text=True),
|
||||
self.assert_multiline_equals(AUTHOR_HTML, resp.get_data(as_text=True),
|
||||
ignore_blanks=True)
|
||||
|
||||
def test_activitypub_to_web_no_url(self):
|
||||
|
@ -138,7 +142,7 @@ class ConvertTest(testutil.TestCase):
|
|||
|
||||
resp = self.client.get('/convert/activitypub/web/http://foo')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
expected = EXPECTED_HTML.replace(
|
||||
expected = HTML.replace(
|
||||
'\n<meta http-equiv="refresh" content="0;url=https://fake.com/123456">', ''
|
||||
).replace('<a class="u-url" href="https://fake.com/123456">fake.com/123456</a>', '')
|
||||
self.assert_multiline_equals(expected, resp.get_data(as_text=True),
|
||||
|
@ -214,3 +218,32 @@ A ☕ reply
|
|||
self.assertEqual(301, resp.status_code)
|
||||
self.assertEqual(f'/convert/activitypub/web/http://foo?bar',
|
||||
resp.headers['Location'])
|
||||
|
||||
def test_web_to_activitypub_object(self):
|
||||
url = 'https://user.com/bar?baz=baj&biff'
|
||||
self.make_user('user.com')
|
||||
|
||||
with self.request_context:
|
||||
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
|
||||
|
||||
resp = self.client.get(f'/convert/web/activitypub/{url}')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assert_equals(COMMENT_AS2, resp.json, ignore=['to'])
|
||||
|
||||
@patch('requests.get')
|
||||
def test_web_to_activitypub_fetch(self, mock_get):
|
||||
mock_get.return_value = requests_response(HTML)
|
||||
url = 'https://user.com/bar?baz=baj&biff'
|
||||
self.make_user('user.com')
|
||||
|
||||
with self.request_context:
|
||||
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
|
||||
|
||||
resp = self.client.get(f'/convert/web/activitypub/{url}')
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assert_equals(COMMENT_AS2, resp.json, ignore=['to'])
|
||||
|
||||
def test_web_to_activitypub_no_user(self):
|
||||
resp = self.client.get(f'/convert/web/activitypub/http://nope.com/post')
|
||||
self.assertEqual(400, resp.status_code)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue