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
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
# import first so that Fake is defined before URL routes are registered
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from flask_app import app, cache
|
2023-06-20 18:22:54 +00:00
|
|
|
from models import Object
|
2023-06-05 03:58:21 +00:00
|
|
|
from web import Web
|
|
|
|
|
|
|
|
from .test_activitypub import ACTOR_BASE_FULL
|
2023-05-27 00:40:29 +00:00
|
|
|
from .test_web import (
|
2023-05-23 06:09:36 +00:00
|
|
|
ACTOR_AS2,
|
|
|
|
ACTOR_HTML,
|
|
|
|
REPOST_AS2,
|
|
|
|
REPOST_HTML,
|
|
|
|
)
|
2018-10-14 14:42:28 +00:00
|
|
|
|
2023-05-24 22:18:16 +00:00
|
|
|
REPOST_AS2 = {
|
|
|
|
**REPOST_AS2,
|
2023-10-11 23:17:43 +00:00
|
|
|
'actor': 'http://localhost/user.com',
|
2023-05-24 22:18:16 +00:00
|
|
|
}
|
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
|
|
|
|
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
|
|
|
|
2023-09-23 20:50:19 +00:00
|
|
|
def test_redirect_html_domain_allowlist(self):
|
|
|
|
got = self.client.get('/r/http://bsky.app/baz')
|
|
|
|
self.assertEqual(301, got.status_code)
|
|
|
|
self.assertEqual('http://bsky.app/baz', got.headers['Location'])
|
|
|
|
|
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-30 02:37:35 +00:00
|
|
|
def test_as2_creates_user(self):
|
2023-06-15 22:09:03 +00:00
|
|
|
Object(id='https://user.com/repost', as2=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))
|
2023-06-05 03:58:21 +00:00
|
|
|
self.assert_equals(REPOST_AS2, resp.json)
|
|
|
|
|
|
|
|
self.assert_user(Web, 'user.com', direct=False)
|
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))
|
2023-06-05 03:58:21 +00:00
|
|
|
|
|
|
|
expected = copy.deepcopy(ACTOR_BASE_FULL)
|
|
|
|
del expected['endpoints']
|
|
|
|
del expected['followers']
|
|
|
|
del expected['following']
|
|
|
|
self.assert_equals(expected, resp.json, ignore=['publicKeyPem'])
|
|
|
|
|
2023-06-16 04:22:20 +00:00
|
|
|
self.assert_user(Web, 'user.com', direct=False, obj_as2={
|
2023-06-05 03:58:21 +00:00
|
|
|
'@context': 'https://www.w3.org/ns/activitystreams',
|
|
|
|
'type': 'Person',
|
2023-07-10 19:23:00 +00:00
|
|
|
'id': 'https://user.com/',
|
2023-06-05 03:58:21 +00:00
|
|
|
'url': 'https://user.com/',
|
|
|
|
'name': 'Ms. ☕ Baz',
|
2023-05-23 06:09:36 +00:00
|
|
|
'attachment': [{
|
|
|
|
'type': 'PropertyValue',
|
2023-06-05 03:58:21 +00:00
|
|
|
'name': 'Ms. ☕ Baz',
|
2023-06-23 05:32:50 +00:00
|
|
|
'value': '<a rel="me" href="https://user.com/"><span class="invisible">https://</span>user.com<span class="invisible">/</span></a>',
|
2023-05-23 06:09:36 +00:00
|
|
|
}],
|
2023-06-05 03:58:21 +00:00
|
|
|
})
|
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-06-20 18:22:54 +00:00
|
|
|
headers={'Accept': 'text/html'})
|
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'])
|
2019-01-04 15:17:45 +00:00
|
|
|
|
2023-05-23 06:09:36 +00:00
|
|
|
def _test_as2(self, content_type):
|
2023-06-15 22:09:03 +00:00
|
|
|
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-06-15 22:09:03 +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-06-20 18:22:54 +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))
|