2017-10-20 14:00:42 +00:00
|
|
|
"""Unit tests for common.py."""
|
2019-12-26 06:20:57 +00:00
|
|
|
from unittest import mock
|
2017-10-20 14:00:42 +00:00
|
|
|
|
2023-03-20 21:28:14 +00:00
|
|
|
from flask import g
|
2022-08-24 00:37:50 +00:00
|
|
|
from granary import as2
|
2023-02-07 03:23:25 +00:00
|
|
|
from oauth_dropins.webutil import appengine_config, util
|
2017-10-20 14:00:42 +00:00
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
|
|
|
import requests
|
|
|
|
|
2023-04-19 00:17:48 +00:00
|
|
|
from flask_app import app
|
2017-10-20 14:00:42 +00:00
|
|
|
import common
|
2023-02-14 22:30:00 +00:00
|
|
|
from models import Object, User
|
2023-03-08 21:10:41 +00:00
|
|
|
import protocol
|
2019-12-26 06:20:57 +00:00
|
|
|
from . import testutil
|
2017-10-20 14:00:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CommonTest(testutil.TestCase):
|
2023-02-07 03:23:25 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
with appengine_config.ndb_client.context():
|
|
|
|
# do this in setUpClass since generating RSA keys is slow
|
2023-03-10 23:13:45 +00:00
|
|
|
cls.user = cls.make_user('site')
|
2023-02-07 03:23:25 +00:00
|
|
|
|
2022-11-24 16:20:04 +00:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2023-05-23 06:09:36 +00:00
|
|
|
self.request_context.push()
|
2023-03-20 21:28:14 +00:00
|
|
|
g.user = User(id='site')
|
2022-11-24 16:20:04 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2023-05-23 06:09:36 +00:00
|
|
|
self.request_context.pop()
|
2022-11-24 16:20:04 +00:00
|
|
|
super().tearDown()
|
|
|
|
|
2023-02-07 05:08:52 +00:00
|
|
|
def test_pretty_link(self):
|
|
|
|
for expected, url, text in (
|
2023-03-14 13:54:16 +00:00
|
|
|
('href="http://foo">bar</a>', 'http://foo', 'bar'),
|
|
|
|
('href="http://x.y/@z">@z@x.y</a>', 'http://x.y/@z', None),
|
|
|
|
('href="http://x.y/@z">foo</a>', 'http://x.y/@z', 'foo'),
|
|
|
|
('href="http://x.y/users/z">@z@x.y</a>', 'http://x.y/users/z', None),
|
|
|
|
('href="http://x.y/users/z">foo</a>', 'http://x.y/users/z', 'foo'),
|
|
|
|
('href="http://x.y/@z/123">x.y/@z/123</a>', 'http://x.y/@z/123', None),
|
2023-02-07 05:08:52 +00:00
|
|
|
):
|
2023-03-14 13:54:16 +00:00
|
|
|
self.assertIn(expected, common.pretty_link(url, text=text))
|
|
|
|
|
|
|
|
self.assertEqual('<a href="http://foo">foo</a>',
|
|
|
|
|
|
|
|
common.pretty_link('http://foo'))
|
2023-02-07 05:08:52 +00:00
|
|
|
|
2023-02-07 05:28:40 +00:00
|
|
|
self.assertEqual(
|
2023-03-14 13:54:16 +00:00
|
|
|
'<a class="h-card u-author" href="/user/site"><img src="" class="profile"> site</a>',
|
2023-03-20 21:28:14 +00:00
|
|
|
common.pretty_link('https://site/'))
|
2023-02-07 05:28:40 +00:00
|
|
|
|
2019-04-15 15:14:37 +00:00
|
|
|
def test_redirect_wrap_empty(self):
|
2021-07-10 15:53:37 +00:00
|
|
|
self.assertIsNone(common.redirect_wrap(None))
|
|
|
|
self.assertEqual('', common.redirect_wrap(''))
|
2019-04-15 15:14:37 +00:00
|
|
|
|
2022-12-10 17:01:04 +00:00
|
|
|
def test_redirect_unwrap_empty(self):
|
|
|
|
self.assertIsNone(common.redirect_unwrap(None))
|
|
|
|
for obj in '', {}, []:
|
|
|
|
self.assertEqual(obj, common.redirect_unwrap(obj))
|
|
|
|
|
|
|
|
def test_unwrap_not_web(self):
|
|
|
|
bad = {
|
|
|
|
'type': 'Like',
|
|
|
|
'object': 'http://localhost/r/foo bar',
|
|
|
|
}
|
|
|
|
self.assert_equals(bad, common.redirect_unwrap(bad))
|
|
|
|
|
2023-02-20 16:28:16 +00:00
|
|
|
def test_unwrap_local_actor_urls(self):
|
|
|
|
self.assert_equals(
|
|
|
|
{'object': 'https://foo.com/'},
|
|
|
|
common.redirect_unwrap({'object': 'http://localhost/foo.com'}))
|
|
|
|
|
|
|
|
self.assert_equals(
|
|
|
|
{'object': {'id': 'https://foo.com/'}},
|
|
|
|
common.redirect_unwrap({'object': {'id': 'http://localhost/foo.com'}}))
|
|
|
|
|
2023-01-05 23:03:21 +00:00
|
|
|
def test_host_url(self):
|
|
|
|
with app.test_request_context():
|
|
|
|
self.assertEqual('http://localhost/', common.host_url())
|
|
|
|
self.assertEqual('http://localhost/asdf', common.host_url('asdf'))
|
|
|
|
self.assertEqual('http://localhost/foo/bar', common.host_url('/foo/bar'))
|
|
|
|
|
|
|
|
with app.test_request_context(base_url='https://a.xyz', path='/foo'):
|
|
|
|
self.assertEqual('https://a.xyz/', common.host_url())
|
|
|
|
self.assertEqual('https://a.xyz/asdf', common.host_url('asdf'))
|
|
|
|
self.assertEqual('https://a.xyz/foo/bar', common.host_url('/foo/bar'))
|
|
|
|
|
|
|
|
with app.test_request_context(base_url='http://bridgy-federated.uc.r.appspot.com'):
|
|
|
|
self.assertEqual('https://fed.brid.gy/asdf', common.host_url('asdf'))
|