bridgy-fed/tests/test_common.py

90 wiersze
3.3 KiB
Python
Czysty Zwykły widok Historia

2017-10-20 14:00:42 +00:00
"""Unit tests for common.py."""
from unittest import mock
2017-10-20 14:00:42 +00:00
from flask import g
from granary import as2
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
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
import protocol
from . import testutil
2017-10-20 14:00:42 +00:00
class CommonTest(testutil.TestCase):
@classmethod
def setUpClass(cls):
with appengine_config.ndb_client.context():
# do this in setUpClass since generating RSA keys is slow
cls.user = cls.make_user('site')
def setUp(self):
super().setUp()
self.request_context.push()
g.user = User(id='site')
def tearDown(self):
self.request_context.pop()
super().tearDown()
def test_pretty_link(self):
for expected, url, text in (
('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),
):
self.assertIn(expected, common.pretty_link(url, text=text))
self.assertEqual('<a href="http://foo">foo</a>',
common.pretty_link('http://foo'))
self.assertEqual(
'<a class="h-card u-author" href="/user/site"><img src="" class="profile"> site</a>',
common.pretty_link('https://site/'))
def test_redirect_wrap_empty(self):
self.assertIsNone(common.redirect_wrap(None))
self.assertEqual('', common.redirect_wrap(''))
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))
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'}}))
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'))