2018-10-14 14:42:28 +00:00
|
|
|
"""Unit tests for redirect.py.
|
|
|
|
"""
|
2019-01-04 15:17:45 +00:00
|
|
|
import copy
|
2019-12-26 06:20:57 +00:00
|
|
|
from unittest.mock import patch
|
2019-01-04 15:17:45 +00:00
|
|
|
|
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
from app import application
|
2019-01-04 15:17:45 +00:00
|
|
|
import common
|
2021-03-07 15:36:34 +00:00
|
|
|
from models import MagicKey
|
2019-12-26 06:20:57 +00:00
|
|
|
from redirect import RedirectHandler
|
|
|
|
from .test_webmention import REPOST_HTML, REPOST_AS2
|
|
|
|
from . import testutil
|
2018-10-14 14:42:28 +00:00
|
|
|
|
|
|
|
|
2019-01-04 15:17:45 +00:00
|
|
|
class RedirectTest(testutil.TestCase):
|
2018-10-14 14:42:28 +00:00
|
|
|
|
2019-11-13 05:28:44 +00:00
|
|
|
def setUp(self):
|
|
|
|
super(RedirectTest, self).setUp()
|
|
|
|
RedirectHandler.get.cache_clear()
|
2021-03-07 15:36:34 +00:00
|
|
|
MagicKey.get_or_create('foo.com')
|
2019-11-13 05:28:44 +00:00
|
|
|
|
2018-10-14 14:42:28 +00:00
|
|
|
def test_redirect(self):
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/r/https://foo.com/bar?baz=baj&biff')
|
2021-03-07 05:47:08 +00:00
|
|
|
self.assertEqual(301, got.status_int)
|
2018-10-14 14:42:28 +00:00
|
|
|
self.assertEqual('https://foo.com/bar?baz=baj&biff', got.headers['Location'])
|
|
|
|
|
|
|
|
def test_redirect_scheme_missing(self):
|
2021-03-07 15:36:34 +00:00
|
|
|
got = application.get_response('/r/foo.com')
|
2018-10-14 14:42:28 +00:00
|
|
|
self.assertEqual(400, got.status_int)
|
|
|
|
|
|
|
|
def test_redirect_url_missing(self):
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/r/')
|
2018-10-14 14:42:28 +00:00
|
|
|
self.assertEqual(404, got.status_int)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2021-06-29 05:52:04 +00:00
|
|
|
def test_redirect_no_magic_key_for_domain(self):
|
2021-03-07 15:36:34 +00:00
|
|
|
got = application.get_response('/r/http://bar.com/baz')
|
|
|
|
self.assertEqual(404, got.status_int)
|
|
|
|
|
2021-06-29 05:52:04 +00:00
|
|
|
def test_redirect_single_slash(self):
|
|
|
|
got = application.get_response('/r/https:/foo.com/bar')
|
|
|
|
self.assertEqual(301, got.status_int)
|
|
|
|
self.assertEqual('https://foo.com/bar', got.headers['Location'])
|
|
|
|
|
2019-01-04 15:17:45 +00:00
|
|
|
def test_as2(self):
|
|
|
|
self._test_as2(common.CONTENT_TYPE_AS2)
|
|
|
|
|
|
|
|
def test_as2_ld(self):
|
|
|
|
self._test_as2(common.CONTENT_TYPE_AS2_LD)
|
|
|
|
|
|
|
|
@patch('requests.get')
|
2019-10-04 04:08:26 +00:00
|
|
|
def _test_as2(self, accept, mock_get):
|
2019-01-04 15:17:45 +00:00
|
|
|
"""Currently mainly for Pixelfed.
|
|
|
|
|
|
|
|
https://github.com/snarfed/bridgy-fed/issues/39
|
|
|
|
"""
|
|
|
|
as2 = copy.deepcopy(REPOST_AS2)
|
|
|
|
as2.update({
|
|
|
|
'cc': [common.AS2_PUBLIC_AUDIENCE],
|
|
|
|
'object': 'http://orig/post',
|
|
|
|
})
|
|
|
|
|
|
|
|
mock_get.return_value = requests_response(
|
2019-10-04 04:08:26 +00:00
|
|
|
REPOST_HTML, content_type=common.CONTENT_TYPE_HTML)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2019-12-26 06:20:57 +00:00
|
|
|
got = application.get_response('/r/https://foo.com/bar', headers={'Accept': accept})
|
2019-01-04 15:17:45 +00:00
|
|
|
|
|
|
|
args, kwargs = mock_get.call_args
|
|
|
|
self.assertEqual(('https://foo.com/bar',), args)
|
|
|
|
|
|
|
|
self.assertEqual(200, got.status_int)
|
|
|
|
self.assertEqual(as2, got.json)
|