2018-10-14 14:42:28 +00:00
|
|
|
"""Unit tests for redirect.py.
|
|
|
|
"""
|
2019-01-04 15:17:45 +00:00
|
|
|
import copy
|
2023-05-22 22:27:43 +00:00
|
|
|
from unittest.mock import patch
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2022-08-24 00:37:50 +00:00
|
|
|
from granary import as2
|
2023-05-22 22:27:43 +00:00
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
|
|
|
import requests
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-23 06:09:36 +00:00
|
|
|
from flask_app import app, cache
|
2023-02-12 02:35:34 +00:00
|
|
|
from common import redirect_unwrap
|
|
|
|
from models import Object, User
|
2023-05-23 06:09:36 +00:00
|
|
|
from .test_webmention import (
|
|
|
|
ACTOR_AS2,
|
|
|
|
ACTOR_AS2_FULL,
|
|
|
|
ACTOR_HTML,
|
|
|
|
REPOST_AS2,
|
|
|
|
REPOST_HTML,
|
|
|
|
)
|
2019-12-26 06:20:57 +00:00
|
|
|
from . import testutil
|
2018-10-14 14:42:28 +00:00
|
|
|
|
2023-05-24 22:18:16 +00:00
|
|
|
REPOST_AS2 = {
|
|
|
|
**REPOST_AS2,
|
|
|
|
'actor': ACTOR_AS2,
|
|
|
|
}
|
2023-05-22 22:27:43 +00:00
|
|
|
del REPOST_AS2['cc']
|
2018-10-14 14:42:28 +00:00
|
|
|
|
2023-05-23 06:09:36 +00:00
|
|
|
EXTERNAL_REPOST_AS2 = copy.deepcopy(REPOST_AS2)
|
2023-05-24 22:18:16 +00:00
|
|
|
EXTERNAL_REPOST_AS2['actor']['id'] = 'http://localhost/r/https://user.com/'
|
2023-05-23 06:09:36 +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):
|
2021-08-18 14:59:52 +00:00
|
|
|
super().setUp()
|
2023-05-22 22:27:43 +00:00
|
|
|
self.user = self.make_user('user.com')
|
|
|
|
|
2018-10-14 14:42:28 +00:00
|
|
|
def test_redirect(self):
|
2023-03-17 23:42:45 +00:00
|
|
|
got = self.client.get('/r/https://user.com/bar?baz=baj&biff')
|
2021-07-08 04:02:13 +00:00
|
|
|
self.assertEqual(301, got.status_code)
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual('https://user.com/bar?baz=baj&biff=', got.headers['Location'])
|
2018-10-14 14:42:28 +00:00
|
|
|
|
|
|
|
def test_redirect_scheme_missing(self):
|
2023-03-17 23:42:45 +00:00
|
|
|
got = self.client.get('/r/user.com')
|
2021-07-08 04:02:13 +00:00
|
|
|
self.assertEqual(400, got.status_code)
|
2018-10-14 14:42:28 +00:00
|
|
|
|
|
|
|
def test_redirect_url_missing(self):
|
2021-08-18 14:59:52 +00:00
|
|
|
got = self.client.get('/r/')
|
2021-07-08 04:02:13 +00:00
|
|
|
self.assertEqual(404, got.status_code)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-22 22:27:43 +00:00
|
|
|
def test_redirect_html_no_user(self):
|
2021-08-18 14:59:52 +00:00
|
|
|
got = self.client.get('/r/http://bar.com/baz')
|
2021-07-08 04:02:13 +00:00
|
|
|
self.assertEqual(404, got.status_code)
|
2021-03-07 15:36:34 +00:00
|
|
|
|
2021-06-29 05:52:04 +00:00
|
|
|
def test_redirect_single_slash(self):
|
2023-03-17 23:42:45 +00:00
|
|
|
got = self.client.get('/r/https:/user.com/bar')
|
2021-07-08 04:02:13 +00:00
|
|
|
self.assertEqual(301, got.status_code)
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual('https://user.com/bar', got.headers['Location'])
|
2021-06-29 05:52:04 +00:00
|
|
|
|
2021-09-02 02:48:37 +00:00
|
|
|
def test_redirect_trailing_garbage_chars(self):
|
|
|
|
got = self.client.get(r'/r/https://v2.jacky.wtf\"')
|
|
|
|
self.assertEqual(404, got.status_code)
|
|
|
|
|
2019-01-04 15:17:45 +00:00
|
|
|
def test_as2(self):
|
2023-01-07 05:01:33 +00:00
|
|
|
self._test_as2(as2.CONTENT_TYPE)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
|
|
|
def test_as2_ld(self):
|
2023-01-07 05:01:33 +00:00
|
|
|
self._test_as2(as2.CONTENT_TYPE_LD)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-22 22:27:43 +00:00
|
|
|
def test_as2_no_user(self):
|
2023-05-23 06:09:36 +00:00
|
|
|
with self.request_context:
|
2023-05-23 16:31:09 +00:00
|
|
|
Object(id='https://user.com/repost', as2=EXTERNAL_REPOST_AS2).put()
|
2023-05-23 06:09:36 +00:00
|
|
|
|
2023-05-22 22:27:43 +00:00
|
|
|
self.user.key.delete()
|
2023-05-23 06:09:36 +00:00
|
|
|
|
2023-05-23 16:31:09 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/repost',
|
2023-05-23 06:09:36 +00:00
|
|
|
headers={'Accept': as2.CONTENT_TYPE})
|
|
|
|
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
|
|
|
|
self.assert_equals(EXTERNAL_REPOST_AS2, resp.json)
|
2023-05-22 22:27:43 +00:00
|
|
|
|
|
|
|
@patch('requests.get')
|
|
|
|
def test_as2_fetch_post(self, mock_get):
|
|
|
|
mock_get.return_value = requests_response(REPOST_HTML)
|
2023-05-23 06:09:36 +00:00
|
|
|
|
2023-05-23 16:31:09 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/repost',
|
|
|
|
headers={'Accept': as2.CONTENT_TYPE})
|
|
|
|
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
|
2023-05-24 22:18:16 +00:00
|
|
|
self.assert_equals(REPOST_AS2, resp.json)
|
2023-05-23 16:31:09 +00:00
|
|
|
|
|
|
|
@patch('requests.get')
|
|
|
|
def test_as2_fetch_post_no_backlink(self, mock_get):
|
|
|
|
mock_get.return_value = requests_response(
|
|
|
|
REPOST_HTML.replace('<a href="http://localhost/"></a>', ''))
|
|
|
|
|
|
|
|
resp = self.client.get('/r/https://user.com/repost',
|
2023-05-23 06:09:36 +00:00
|
|
|
headers={'Accept': as2.CONTENT_TYPE})
|
|
|
|
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
|
2023-05-24 22:18:16 +00:00
|
|
|
self.assert_equals(REPOST_AS2, resp.json)
|
2023-05-23 06:09:36 +00:00
|
|
|
|
|
|
|
@patch('requests.get')
|
|
|
|
def test_as2_no_user_fetch_homepage(self, mock_get):
|
|
|
|
mock_get.return_value = requests_response(ACTOR_HTML)
|
|
|
|
self.user.key.delete()
|
|
|
|
|
|
|
|
resp = self.client.get('/r/https://user.com/',
|
|
|
|
headers={'Accept': as2.CONTENT_TYPE})
|
|
|
|
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
|
|
|
|
self.assert_equals({
|
|
|
|
**ACTOR_AS2,
|
|
|
|
'id': 'http://localhost/r/https://user.com/',
|
|
|
|
'summary': '',
|
|
|
|
'attachment': [{
|
|
|
|
'name': 'Ms. ☕ Baz',
|
|
|
|
'type': 'PropertyValue',
|
|
|
|
'value': '<a rel="me" href="https://user.com/">user.com</a>',
|
|
|
|
}],
|
|
|
|
}, resp.json)
|
2023-05-22 22:27:43 +00:00
|
|
|
|
2022-12-26 17:52:08 +00:00
|
|
|
def test_accept_header_cache_key(self):
|
|
|
|
app.config['CACHE_TYPE'] = 'SimpleCache'
|
|
|
|
cache.init_app(app)
|
|
|
|
self.client = app.test_client()
|
|
|
|
|
2023-01-07 05:01:33 +00:00
|
|
|
self._test_as2(as2.CONTENT_TYPE)
|
2022-12-26 17:52:08 +00:00
|
|
|
|
2023-03-17 23:42:45 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/bar')
|
2023-02-12 02:35:34 +00:00
|
|
|
self.assertEqual(301, resp.status_code)
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual('https://user.com/bar', resp.headers['Location'])
|
2022-12-26 17:52:08 +00:00
|
|
|
|
2023-02-12 02:35:34 +00:00
|
|
|
# delete stored Object to make sure we're serving from cache
|
|
|
|
self.obj.delete()
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-02-12 02:35:34 +00:00
|
|
|
self._test_as2(as2.CONTENT_TYPE)
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-03-17 23:42:45 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/bar',
|
2023-02-12 02:35:34 +00:00
|
|
|
headers={'Accept': 'text/html'})
|
|
|
|
self.assertEqual(301, resp.status_code)
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual('https://user.com/bar', resp.headers['Location'])
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-23 06:09:36 +00:00
|
|
|
def _test_as2(self, content_type):
|
|
|
|
with self.request_context:
|
|
|
|
self.obj = Object(id='https://user.com/', as2=REPOST_AS2).put()
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-23 06:09:36 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/', headers={'Accept': content_type})
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
|
2023-02-12 02:35:34 +00:00
|
|
|
self.assertEqual(content_type, resp.content_type)
|
2023-05-23 06:09:36 +00:00
|
|
|
self.assert_equals(REPOST_AS2, resp.json)
|
2023-02-12 05:46:47 +00:00
|
|
|
|
|
|
|
def test_as2_deleted(self):
|
2023-05-23 06:09:36 +00:00
|
|
|
with self.request_context:
|
2023-03-17 23:42:45 +00:00
|
|
|
Object(id='https://user.com/bar', as2={}, deleted=True).put()
|
2023-02-12 05:46:47 +00:00
|
|
|
|
2023-03-17 23:42:45 +00:00
|
|
|
resp = self.client.get('/r/https://user.com/bar',
|
2023-02-12 05:46:47 +00:00
|
|
|
headers={'Accept': as2.CONTENT_TYPE})
|
2023-03-17 23:42:45 +00:00
|
|
|
self.assertEqual(404, resp.status_code, resp.get_data(as_text=True))
|