2022-11-17 15:38:52 +00:00
|
|
|
"""Unit tests for pages.py."""
|
2024-03-13 20:53:30 +00:00
|
|
|
from unittest import skip
|
2023-09-28 21:42:18 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import arroba.server
|
|
|
|
from flask import get_flashed_messages
|
2023-07-20 05:39:22 +00:00
|
|
|
from google.cloud import ndb
|
2023-09-28 21:42:18 +00:00
|
|
|
from google.cloud.tasks_v2.types import Task
|
2023-06-20 18:22:54 +00:00
|
|
|
from granary import atom, microformats2, rss
|
2023-02-09 16:23:31 +00:00
|
|
|
from oauth_dropins.webutil import util
|
2023-09-28 21:42:18 +00:00
|
|
|
from oauth_dropins.webutil.appengine_config import tasks_client
|
|
|
|
from oauth_dropins.webutil.testutil import requests_response
|
2024-05-06 02:22:02 +00:00
|
|
|
from requests import ConnectionError
|
2023-01-28 23:07:05 +00:00
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
# import first so that Fake is defined before URL routes are registered
|
2023-07-28 22:49:29 +00:00
|
|
|
from .testutil import Fake, TestCase, ACTOR, COMMENT, MENTION, NOTE
|
2023-05-30 23:36:18 +00:00
|
|
|
|
2023-06-02 05:00:47 +00:00
|
|
|
from activitypub import ActivityPub
|
2023-11-16 03:08:06 +00:00
|
|
|
from atproto import ATProto
|
2023-10-31 19:49:15 +00:00
|
|
|
import common
|
2023-09-28 21:42:18 +00:00
|
|
|
from models import Object, Follower, Target
|
2023-07-20 05:39:22 +00:00
|
|
|
from web import Web
|
2023-05-26 23:07:36 +00:00
|
|
|
|
2023-10-03 23:52:21 +00:00
|
|
|
from granary.tests.test_bluesky import ACTOR_AS, ACTOR_PROFILE_BSKY
|
2023-06-20 18:22:54 +00:00
|
|
|
from .test_web import ACTOR_AS2, REPOST_AS2
|
2023-02-09 04:22:16 +00:00
|
|
|
|
2023-06-02 05:00:47 +00:00
|
|
|
ACTOR_WITH_PREFERRED_USERNAME = {
|
2024-06-23 15:38:00 +00:00
|
|
|
'objectType': 'person',
|
2024-05-13 01:45:51 +00:00
|
|
|
'displayName': 'Me',
|
2024-06-23 15:38:00 +00:00
|
|
|
'username': 'me',
|
2024-01-13 03:52:49 +00:00
|
|
|
'url': 'https://plus.google.com/bob',
|
2024-05-13 01:45:51 +00:00
|
|
|
'image': 'http://pic',
|
2023-06-02 05:00:47 +00:00
|
|
|
}
|
2022-11-17 15:38:52 +00:00
|
|
|
|
2023-06-20 18:22:54 +00:00
|
|
|
|
2022-11-17 15:38:52 +00:00
|
|
|
def contents(activities):
|
2024-04-29 19:26:54 +00:00
|
|
|
return [
|
|
|
|
' '.join(util.parse_html((a.get('object') or a)['content']).get_text().split())
|
|
|
|
for a in activities]
|
2022-11-17 15:38:52 +00:00
|
|
|
|
|
|
|
|
2023-05-26 23:07:36 +00:00
|
|
|
class PagesTest(TestCase):
|
2023-10-10 22:43:29 +00:00
|
|
|
EXPECTED = contents([COMMENT, MENTION, NOTE])
|
2023-10-12 17:37:44 +00:00
|
|
|
EXPECTED_SNIPPETS = [
|
|
|
|
'Dr. Eve replied a comment',
|
|
|
|
'tag:fake.com:44... posted a mention',
|
2024-04-29 19:26:54 +00:00
|
|
|
'🌐 user.com posted my note',
|
2023-10-12 17:37:44 +00:00
|
|
|
]
|
2022-11-17 15:38:52 +00:00
|
|
|
|
2022-11-22 23:21:53 +00:00
|
|
|
def setUp(self):
|
|
|
|
super().setUp()
|
2023-11-30 05:06:55 +00:00
|
|
|
self.user = self.make_user('user.com', cls=Web, has_redirects=True)
|
2022-11-22 23:21:53 +00:00
|
|
|
|
2022-12-02 19:08:24 +00:00
|
|
|
def test_user(self):
|
2023-09-26 23:43:48 +00:00
|
|
|
got = self.client.get('/web/user.com', base_url='https://fed.brid.gy/')
|
2022-12-02 19:08:24 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
def test_user_fake(self):
|
2023-10-10 21:55:27 +00:00
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo')
|
2023-05-30 23:36:18 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-09-25 22:08:14 +00:00
|
|
|
def test_user_page_handle(self):
|
2024-01-13 03:52:49 +00:00
|
|
|
user = self.make_user('http://fo/o', cls=ActivityPub,
|
2024-06-23 15:38:00 +00:00
|
|
|
obj_as1=ACTOR_WITH_PREFERRED_USERNAME)
|
|
|
|
self.assertEqual('@me@fo', user.handle_as(ActivityPub))
|
2023-06-02 05:00:47 +00:00
|
|
|
|
2024-06-23 15:38:00 +00:00
|
|
|
got = self.client.get('/ap/@me@fo')
|
2023-06-02 05:00:47 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-06-23 19:22:37 +00:00
|
|
|
# TODO: can't handle slashes in id segment of path. is that ok?
|
|
|
|
# got = self.client.get('/ap/http%3A//foo')
|
|
|
|
# self.assert_equals(302, got.status_code)
|
|
|
|
# self.assert_equals('/ap/@me@plus.google.com', got.headers['Location'])
|
2023-06-02 05:00:47 +00:00
|
|
|
|
2023-06-07 18:51:31 +00:00
|
|
|
def test_user_web_custom_username_doesnt_redirect(self):
|
|
|
|
"""https://github.com/snarfed/bridgy-fed/issues/534"""
|
2023-06-27 03:22:06 +00:00
|
|
|
self.user.obj = Object(id='a', as2={
|
2023-06-07 18:51:31 +00:00
|
|
|
**ACTOR_AS2,
|
|
|
|
'url': 'acct:baz@user.com',
|
2023-06-27 03:22:06 +00:00
|
|
|
})
|
|
|
|
self.user.obj.put()
|
2023-06-07 18:51:31 +00:00
|
|
|
self.user.put()
|
|
|
|
self.assertEqual('baz', self.user.username())
|
|
|
|
|
|
|
|
got = self.client.get('/web/@baz@user.com')
|
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
|
|
|
got = self.client.get('/web/baz')
|
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
|
|
|
got = self.client.get('/web/user.com')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assertIn('@baz@user.com', got.get_data(as_text=True))
|
|
|
|
|
2023-01-28 23:07:05 +00:00
|
|
|
def test_user_objects(self):
|
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com')
|
2023-01-24 03:20:31 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2022-12-02 19:08:24 +00:00
|
|
|
def test_user_not_found(self):
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/bar.com')
|
2022-11-22 23:21:53 +00:00
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-05-30 03:16:15 +00:00
|
|
|
def test_user_not_direct(self):
|
2024-04-27 16:53:21 +00:00
|
|
|
got = self.client.get('/web/user.com')
|
2024-04-11 21:24:18 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
fake = self.make_user('http://fo/o', cls=ActivityPub, direct=False)
|
|
|
|
got = self.client.get('/ap/@o@fo')
|
2023-05-30 03:16:15 +00:00
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-10-13 19:36:31 +00:00
|
|
|
def test_user_opted_out(self):
|
|
|
|
self.user.obj.our_as1 = {'summary': '#nobridge'}
|
|
|
|
self.user.obj.put()
|
|
|
|
got = self.client.get('/web/user.com')
|
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2024-06-23 15:03:22 +00:00
|
|
|
def test_user_handle_opted_out(self):
|
|
|
|
user = self.make_user('fake:user', cls=Fake, manual_opt_out=True)
|
|
|
|
got = self.client.get('/fa/fake:handle:user')
|
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-06-04 04:48:15 +00:00
|
|
|
def test_user_web_redirect(self):
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/user/user.com')
|
|
|
|
self.assert_equals(301, got.status_code)
|
|
|
|
self.assert_equals('/web/user.com', got.headers['Location'])
|
|
|
|
|
2022-12-02 19:08:24 +00:00
|
|
|
def test_user_use_instead(self):
|
2023-11-15 22:23:08 +00:00
|
|
|
self.make_user('bar.com', cls=Web, use_instead=self.user.key)
|
2022-12-02 19:08:24 +00:00
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/bar.com')
|
2023-06-07 18:51:31 +00:00
|
|
|
self.assert_equals(302, got.status_code)
|
2023-05-30 21:08:13 +00:00
|
|
|
self.assert_equals('/web/user.com', got.headers['Location'])
|
2022-12-02 19:08:24 +00:00
|
|
|
|
2023-06-07 18:51:31 +00:00
|
|
|
got = self.client.get('/web/user.com')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-03-03 18:07:54 +00:00
|
|
|
def test_user_object_bare_string_id(self):
|
2023-06-15 22:09:03 +00:00
|
|
|
Object(id='a', users=[self.user.key], labels=['notification'],
|
|
|
|
as2=REPOST_AS2).put()
|
2023-03-03 18:07:54 +00:00
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com')
|
2023-03-03 18:07:54 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-03-24 05:04:09 +00:00
|
|
|
def test_user_object_url_object(self):
|
2023-05-23 06:09:36 +00:00
|
|
|
with self.request_context:
|
2023-06-09 19:56:45 +00:00
|
|
|
Object(id='a', users=[self.user.key], labels=['notification'], our_as1={
|
2023-03-24 05:04:09 +00:00
|
|
|
**REPOST_AS2,
|
|
|
|
'object': {
|
|
|
|
'id': 'https://mas.to/toot/id',
|
|
|
|
'url': {'value': 'http://foo', 'displayName': 'bar'},
|
|
|
|
},
|
|
|
|
}).put()
|
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com')
|
2023-03-24 05:04:09 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-05-24 13:49:54 +00:00
|
|
|
def test_user_before(self):
|
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get(f'/web/user.com?before={util.now().isoformat()}')
|
2023-05-24 13:49:54 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_user_after(self):
|
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get(f'/web/user.com?after={util.now().isoformat()}')
|
2023-05-24 13:49:54 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_user_before_bad(self):
|
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com?before=nope')
|
2023-05-24 13:49:54 +00:00
|
|
|
self.assert_equals(400, got.status_code)
|
|
|
|
|
|
|
|
def test_user_before_and_after(self):
|
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com?before=2024-01-01+01:01:01&after=2023-01-01+01:01:01')
|
2023-05-24 13:49:54 +00:00
|
|
|
self.assert_equals(400, got.status_code)
|
|
|
|
|
2024-04-23 03:21:56 +00:00
|
|
|
def test_update_profile(self):
|
|
|
|
self.make_user('fake:user', cls=Fake)
|
|
|
|
|
|
|
|
actor = {
|
|
|
|
'objectType': 'person',
|
|
|
|
'id': 'fake:user',
|
|
|
|
'displayName': 'Ms User',
|
|
|
|
}
|
2024-05-29 23:18:15 +00:00
|
|
|
Fake.fetchable = {'fake:profile:user': actor}
|
2024-04-23 03:21:56 +00:00
|
|
|
got = self.client.post('/fa/fake:user/update-profile')
|
|
|
|
self.assert_equals(302, got.status_code)
|
|
|
|
self.assert_equals('/fa/fake:handle:user', got.headers['Location'])
|
2024-05-29 23:46:16 +00:00
|
|
|
self.assertEqual(
|
|
|
|
['Updating profile from <a href="web:fake:user">fake:handle:user</a>...'],
|
|
|
|
get_flashed_messages())
|
2024-04-23 03:21:56 +00:00
|
|
|
|
2024-05-29 23:18:15 +00:00
|
|
|
self.assertEqual(['fake:profile:user'], Fake.fetched)
|
2024-04-25 20:45:27 +00:00
|
|
|
|
|
|
|
actor['updated'] = '2022-01-02T03:04:05+00:00'
|
2024-04-25 00:06:25 +00:00
|
|
|
self.assert_object('fake:user', source_protocol='fake', our_as1=actor)
|
2024-04-23 03:21:56 +00:00
|
|
|
|
2024-05-06 02:22:02 +00:00
|
|
|
@patch.object(Fake, 'fetch', side_effect=ConnectionError('foo'))
|
|
|
|
def test_update_profile_load_fails(self, _):
|
|
|
|
self.make_user('fake:user', cls=Fake)
|
|
|
|
|
|
|
|
got = self.client.post('/fa/fake:user/update-profile')
|
|
|
|
self.assert_equals(302, got.status_code)
|
|
|
|
self.assert_equals('/fa/fake:handle:user', got.headers['Location'])
|
|
|
|
self.assertEqual(
|
2024-05-29 23:46:16 +00:00
|
|
|
['Couldn\'t update profile for <a href="web:fake:user">fake:handle:user</a>: foo'],
|
2024-05-06 02:22:02 +00:00
|
|
|
get_flashed_messages())
|
|
|
|
|
2024-05-25 17:55:48 +00:00
|
|
|
@patch.object(tasks_client, 'create_task', return_value=Task(name='my task'))
|
|
|
|
def test_update_profile_receive_task(self, mock_create_task):
|
|
|
|
common.RUN_TASKS_INLINE = False
|
|
|
|
|
|
|
|
user = self.make_user('fake:user', cls=Fake)
|
|
|
|
|
2024-05-29 23:18:15 +00:00
|
|
|
Fake.fetchable = {'fake:profile:user': {
|
2024-05-25 17:55:48 +00:00
|
|
|
'objectType': 'person',
|
|
|
|
'id': 'fake:user',
|
|
|
|
'displayName': 'Ms User',
|
|
|
|
}}
|
|
|
|
|
2024-05-28 21:54:31 +00:00
|
|
|
# use handle in URL to check that we use key id as authed_as below
|
|
|
|
got = self.client.post('/fa/fake:handle:user/update-profile')
|
2024-05-25 17:55:48 +00:00
|
|
|
self.assert_equals(302, got.status_code)
|
|
|
|
self.assert_equals('/fa/fake:handle:user', got.headers['Location'])
|
|
|
|
|
2024-05-29 23:18:15 +00:00
|
|
|
self.assert_equals(['fake:profile:user'], Fake.fetched)
|
|
|
|
self.assert_task(mock_create_task, 'receive',
|
|
|
|
obj=Object(id='fake:profile:user').key.urlsafe(),
|
2024-05-25 17:55:48 +00:00
|
|
|
authed_as='fake:user')
|
|
|
|
|
2023-01-19 23:25:56 +00:00
|
|
|
def test_followers(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
Follower.get_or_create(
|
|
|
|
to=self.user,
|
2024-05-13 01:45:51 +00:00
|
|
|
from_=self.make_user('http://un/used', cls=ActivityPub, obj_as1={
|
2023-06-12 22:50:47 +00:00
|
|
|
**ACTOR,
|
2024-01-13 03:52:49 +00:00
|
|
|
'id': 'http://un/used',
|
2023-06-12 22:50:47 +00:00
|
|
|
'url': 'http://stored/users/follow',
|
|
|
|
}))
|
2023-06-06 21:50:20 +00:00
|
|
|
Follower.get_or_create(
|
|
|
|
to=self.user,
|
2023-11-28 05:40:01 +00:00
|
|
|
from_=self.make_user('http://masto/user', cls=ActivityPub,
|
2024-06-23 15:38:00 +00:00
|
|
|
obj_as1=ACTOR_WITH_PREFERRED_USERNAME))
|
2023-06-06 21:50:20 +00:00
|
|
|
|
2024-02-11 22:35:10 +00:00
|
|
|
from models import PROTOCOLS
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/user.com/followers')
|
2023-01-19 23:25:56 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
body = got.get_data(as_text=True)
|
2023-06-12 22:50:47 +00:00
|
|
|
self.assertIn('@follow@stored', body)
|
2024-06-23 15:38:00 +00:00
|
|
|
self.assertIn('@me@masto', body)
|
2023-01-19 23:25:56 +00:00
|
|
|
|
2023-10-10 21:55:27 +00:00
|
|
|
def test_home_fake(self):
|
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo/home')
|
2023-10-10 21:55:27 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_home_objects(self):
|
|
|
|
self.add_objects()
|
|
|
|
got = self.client.get('/web/user.com/home')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-10-11 18:28:39 +00:00
|
|
|
def test_notifications_fake(self):
|
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo/notifications')
|
2023-10-11 18:28:39 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_notifications_objects(self):
|
|
|
|
self.add_objects()
|
|
|
|
got = self.client.get('/web/user.com/notifications')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_notifications_rss(self):
|
|
|
|
self.add_objects()
|
|
|
|
got = self.client.get('/web/user.com/notifications?format=rss')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assert_equals(rss.CONTENT_TYPE, got.headers['Content-Type'])
|
2023-10-12 17:37:44 +00:00
|
|
|
self.assert_equals(self.EXPECTED_SNIPPETS,
|
|
|
|
contents(rss.to_activities(got.text)))
|
2023-10-11 18:28:39 +00:00
|
|
|
|
|
|
|
def test_notifications_atom(self):
|
|
|
|
self.add_objects()
|
|
|
|
got = self.client.get('/web/user.com/notifications?format=atom')
|
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assert_equals(atom.CONTENT_TYPE, got.headers['Content-Type'])
|
2023-10-12 17:37:44 +00:00
|
|
|
self.assert_equals(self.EXPECTED_SNIPPETS,
|
|
|
|
contents(atom.atom_to_activities(got.text)))
|
2023-10-11 18:28:39 +00:00
|
|
|
|
|
|
|
def test_notifications_html(self):
|
|
|
|
self.add_objects()
|
|
|
|
got = self.client.get('/web/user.com/notifications?format=html')
|
|
|
|
self.assert_equals(200, got.status_code)
|
2023-10-12 17:37:44 +00:00
|
|
|
self.assert_equals(self.EXPECTED_SNIPPETS,
|
2023-10-11 18:28:39 +00:00
|
|
|
contents(microformats2.html_to_activities(got.text)))
|
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
def test_followers_fake(self):
|
2023-10-10 21:55:27 +00:00
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo/followers')
|
2023-05-30 23:36:18 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-11-28 05:01:02 +00:00
|
|
|
def test_followers_activitypub(self):
|
2024-06-23 15:38:00 +00:00
|
|
|
user = self.make_user('https://inst/user', cls=ActivityPub,
|
|
|
|
obj_as1=ACTOR_WITH_PREFERRED_USERNAME)
|
2023-11-28 05:01:02 +00:00
|
|
|
|
2024-06-23 15:38:00 +00:00
|
|
|
got = self.client.get('/ap/@me@inst/followers')
|
2023-11-28 05:01:02 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assert_equals('text/html', got.headers['Content-Type'].split(';')[0])
|
|
|
|
|
2023-01-19 23:25:56 +00:00
|
|
|
def test_followers_empty(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/user.com/followers')
|
2023-01-19 23:25:56 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assertNotIn('class="follower', got.get_data(as_text=True))
|
|
|
|
|
2022-11-22 23:21:53 +00:00
|
|
|
def test_followers_user_not_found(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/nope.com/followers')
|
2022-11-22 23:21:53 +00:00
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
def test_followers_redirect(self):
|
|
|
|
got = self.client.get('/user/user.com/followers')
|
|
|
|
self.assert_equals(301, got.status_code)
|
|
|
|
self.assert_equals('/web/user.com/followers', got.headers['Location'])
|
|
|
|
|
2023-01-19 23:25:56 +00:00
|
|
|
def test_following(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
Follower.get_or_create(
|
|
|
|
from_=self.user,
|
2024-05-13 01:45:51 +00:00
|
|
|
to=self.make_user('http://un/used', cls=ActivityPub, obj_as1={
|
2023-06-12 22:50:47 +00:00
|
|
|
**ACTOR,
|
2024-01-13 03:52:49 +00:00
|
|
|
'id': 'http://un/used',
|
2023-06-12 22:50:47 +00:00
|
|
|
'url': 'http://stored/users/follow',
|
|
|
|
}))
|
2023-06-06 21:50:20 +00:00
|
|
|
Follower.get_or_create(
|
|
|
|
from_=self.user,
|
2023-11-28 05:40:01 +00:00
|
|
|
to=self.make_user('http://masto/user', cls=ActivityPub,
|
2024-06-23 15:38:00 +00:00
|
|
|
obj_as1=ACTOR_WITH_PREFERRED_USERNAME))
|
2023-06-06 21:50:20 +00:00
|
|
|
|
|
|
|
got = self.client.get('/web/user.com/following')
|
2023-01-19 23:25:56 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
body = got.get_data(as_text=True)
|
2023-06-12 22:50:47 +00:00
|
|
|
self.assertIn('@follow@stored', body)
|
2024-06-23 15:38:00 +00:00
|
|
|
self.assertIn('@me@masto', body)
|
2023-01-19 23:25:56 +00:00
|
|
|
|
|
|
|
def test_following_empty(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/user.com/following')
|
2023-01-19 23:25:56 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assertNotIn('class="follower', got.get_data(as_text=True))
|
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
def test_following_fake(self):
|
2023-10-10 21:55:27 +00:00
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo/following')
|
2023-05-30 23:36:18 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2022-11-22 23:21:53 +00:00
|
|
|
def test_following_user_not_found(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/nope.com/following')
|
2022-11-22 23:21:53 +00:00
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
def test_following_redirect(self):
|
|
|
|
got = self.client.get('/user/user.com/following')
|
|
|
|
self.assert_equals(301, got.status_code)
|
|
|
|
self.assert_equals('/web/user.com/following', got.headers['Location'])
|
|
|
|
|
2023-02-09 16:23:31 +00:00
|
|
|
def test_following_before_empty(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get(f'/web/user.com/following?before={util.now().isoformat()}')
|
2023-02-09 16:23:31 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
|
|
|
def test_following_after_empty(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get(f'/web/user.com/following?after={util.now().isoformat()}')
|
2023-02-09 16:23:31 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2022-11-22 23:21:53 +00:00
|
|
|
def test_feed_user_not_found(self):
|
2023-06-06 21:50:20 +00:00
|
|
|
got = self.client.get('/web/nope.com/feed')
|
2022-11-22 23:21:53 +00:00
|
|
|
self.assert_equals(404, got.status_code)
|
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
def test_feed_web_redirect(self):
|
2023-03-19 22:43:55 +00:00
|
|
|
got = self.client.get('/user/user.com/feed')
|
2023-05-30 21:08:13 +00:00
|
|
|
self.assert_equals(301, got.status_code)
|
|
|
|
self.assert_equals('/web/user.com/feed', got.headers['Location'])
|
|
|
|
|
2023-05-30 23:36:18 +00:00
|
|
|
def test_feed_fake(self):
|
2023-10-10 21:55:27 +00:00
|
|
|
self.make_user('fake:foo', cls=Fake)
|
2023-11-24 06:06:08 +00:00
|
|
|
got = self.client.get('/fake/fake:foo/feed')
|
2023-05-30 23:36:18 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
def test_feed_html_empty(self):
|
|
|
|
got = self.client.get('/web/user.com/feed')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
|
|
|
self.assert_equals([], microformats2.html_to_activities(got.text))
|
|
|
|
|
|
|
|
def test_feed_html(self):
|
2023-01-28 23:07:05 +00:00
|
|
|
self.add_objects()
|
2023-07-20 05:39:22 +00:00
|
|
|
|
2023-07-20 17:24:58 +00:00
|
|
|
# repost with object (original post) in separate Object
|
|
|
|
repost = {
|
|
|
|
'objectType': 'activity',
|
|
|
|
'verb': 'share',
|
|
|
|
'object': 'fake:orig',
|
|
|
|
}
|
|
|
|
orig = {
|
|
|
|
'objectType': 'note',
|
|
|
|
'content': 'biff',
|
|
|
|
}
|
2023-07-28 22:49:29 +00:00
|
|
|
self.store_object(id='fake:repost', feed=[self.user.key], our_as1=repost)
|
2023-07-20 17:24:58 +00:00
|
|
|
self.store_object(id='fake:orig', our_as1=orig)
|
|
|
|
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com/feed')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
2023-07-28 22:49:29 +00:00
|
|
|
self.assert_equals(['biff'] + self.EXPECTED,
|
2022-11-17 15:38:52 +00:00
|
|
|
contents(microformats2.html_to_activities(got.text)))
|
2023-07-28 22:49:29 +00:00
|
|
|
|
|
|
|
# NOTE's and MENTION's authors; check for two instances
|
|
|
|
bob = '<a class="p-name u-url" href="https://plus.google.com/bob">Bob</a>'
|
|
|
|
assert got.text.index(bob) != got.text.rindex(bob)
|
2022-11-17 15:38:52 +00:00
|
|
|
|
|
|
|
def test_feed_atom_empty(self):
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com/feed?format=atom')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
2023-10-11 18:28:39 +00:00
|
|
|
self.assert_equals(atom.CONTENT_TYPE, got.headers['Content-Type'])
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals([], atom.atom_to_activities(got.text))
|
|
|
|
|
2023-10-23 20:10:27 +00:00
|
|
|
def test_feed_atom_empty_g_user_without_obj(self):
|
|
|
|
self.user.obj_key = None
|
|
|
|
self.user.put()
|
|
|
|
self.test_feed_atom_empty()
|
|
|
|
|
2022-11-17 15:38:52 +00:00
|
|
|
def test_feed_atom(self):
|
2023-01-28 23:07:05 +00:00
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com/feed?format=atom')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
2023-10-11 18:28:39 +00:00
|
|
|
self.assert_equals(atom.CONTENT_TYPE, got.headers['Content-Type'])
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(self.EXPECTED, contents(atom.atom_to_activities(got.text)))
|
|
|
|
|
2023-07-28 22:49:29 +00:00
|
|
|
# NOTE's and MENTION's authors; check for two instances
|
|
|
|
bob = """
|
|
|
|
<uri>https://plus.google.com/bob</uri>
|
|
|
|
|
|
|
|
<name>Bob</name>
|
|
|
|
"""
|
|
|
|
assert got.text.index(bob) != got.text.rindex(bob)
|
|
|
|
# COMMENT's author
|
|
|
|
self.assertIn('Dr. Eve', got.text)
|
|
|
|
|
2022-11-17 15:38:52 +00:00
|
|
|
def test_feed_rss_empty(self):
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com/feed?format=rss')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
2023-10-11 18:28:39 +00:00
|
|
|
self.assert_equals(rss.CONTENT_TYPE, got.headers['Content-Type'])
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals([], rss.to_activities(got.text))
|
|
|
|
|
|
|
|
def test_feed_rss(self):
|
2023-01-28 23:07:05 +00:00
|
|
|
self.add_objects()
|
2023-05-30 21:08:13 +00:00
|
|
|
got = self.client.get('/web/user.com/feed?format=rss')
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(200, got.status_code)
|
2023-10-11 18:28:39 +00:00
|
|
|
self.assert_equals(rss.CONTENT_TYPE, got.headers['Content-Type'])
|
2022-11-17 15:38:52 +00:00
|
|
|
self.assert_equals(self.EXPECTED, contents(rss.to_activities(got.text)))
|
2023-05-26 23:36:45 +00:00
|
|
|
|
2023-07-28 22:49:29 +00:00
|
|
|
# NOTE's and MENTION's authors; check for two instances
|
|
|
|
bob = '<author>- (Bob)</author>'
|
|
|
|
assert got.text.index(bob) != got.text.rindex(bob)
|
|
|
|
# COMMENT's author
|
|
|
|
self.assertIn('Dr. Eve', got.text)
|
|
|
|
|
2024-03-13 20:53:30 +00:00
|
|
|
# TODO: re-enable for launch
|
|
|
|
@skip
|
2023-09-28 21:42:18 +00:00
|
|
|
@patch.object(tasks_client, 'create_task', return_value=Task(name='my task'))
|
Revert "cache outbound HTTP request responses, locally to each inbound request"
This reverts commit 30debfc8faf730190bd51a3aef49df6c6bfbd50a.
seemed promising, but broke in production. Saw a lot of `IncompleteRead`s on both GETs and POSTs. Rolled back for now.
```
('Connection broken: IncompleteRead(9172 bytes read, -4586 more expected)', IncompleteRead(9172 bytes read, -4586 more expected))
...
File "oauth_dropins/webutil/util.py", line 1673, in call
resp = getattr((session or requests), fn)(url, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/session.py", line 102, in get
return self.request('GET', url, params=params, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/session.py", line 158, in request
return super().request(method, url, *args, headers=headers, **kwargs) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/session.py", line 205, in send
response = self._send_and_cache(request, actions, cached_response, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/session.py", line 233, in _send_and_cache
self.cache.save_response(response, actions.cache_key, actions.expires)
File "requests_cache/backends/base.py", line 89, in save_response
cached_response = CachedResponse.from_response(response, expires=expires)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/models/response.py", line 102, in from_response
obj.raw = CachedHTTPResponse.from_response(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests_cache/models/raw_response.py", line 69, in from_response
_ = response.content # This property reads, decodes, and stores response content
^^^^^^^^^^^^^^^^
File "requests/models.py", line 899, in content
self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b""
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "requests/models.py", line 818, in generate
raise ChunkedEncodingError(e)
```
2024-03-08 21:24:28 +00:00
|
|
|
@patch('requests.post',
|
2023-09-28 21:42:18 +00:00
|
|
|
return_value=requests_response('OK')) # create DID on PLC
|
2023-10-05 22:55:31 +00:00
|
|
|
def test_bridge_user(self, mock_post, mock_create_task):
|
2023-10-31 19:49:15 +00:00
|
|
|
common.RUN_TASKS_INLINE = False
|
|
|
|
|
2023-10-05 22:47:04 +00:00
|
|
|
Fake.fetchable = {
|
|
|
|
'fake:user': {
|
|
|
|
**ACTOR_AS,
|
|
|
|
'image': None, # don't try to fetch as blob
|
|
|
|
},
|
|
|
|
}
|
2023-09-28 21:42:18 +00:00
|
|
|
|
|
|
|
got = self.client.post('/bridge-user', data={'handle': 'fake:handle:user'})
|
|
|
|
self.assertEqual(200, got.status_code)
|
|
|
|
self.assertEqual(
|
2023-10-06 05:28:36 +00:00
|
|
|
['Bridging <a href="fake:user">fake:handle:user</a> into Bluesky. <a href="https://bsky.app/search">Try searching for them</a> in a minute!'],
|
2023-09-28 21:42:18 +00:00
|
|
|
get_flashed_messages())
|
|
|
|
|
|
|
|
# check user, repo
|
|
|
|
user = Fake.get_by_id('fake:user')
|
|
|
|
self.assertEqual('fake:handle:user', user.handle)
|
2023-11-16 03:08:06 +00:00
|
|
|
did = user.get_copy(ATProto)
|
|
|
|
repo = arroba.server.storage.load_repo(did)
|
2023-09-28 21:42:18 +00:00
|
|
|
|
|
|
|
# check profile
|
|
|
|
profile = repo.get_record('app.bsky.actor.profile', 'self')
|
2023-10-03 23:52:21 +00:00
|
|
|
self.assertEqual({
|
|
|
|
'$type': 'app.bsky.actor.profile',
|
|
|
|
'displayName': 'Alice',
|
|
|
|
'description': 'hi there',
|
2024-03-01 21:31:18 +00:00
|
|
|
'labels': {
|
|
|
|
'$type': 'com.atproto.label.defs#selfLabels',
|
2024-03-11 01:50:31 +00:00
|
|
|
'values': [{'val' : 'bridged-from-bridgy-fed-fake'}],
|
2024-03-01 21:31:18 +00:00
|
|
|
},
|
2023-10-03 23:52:21 +00:00
|
|
|
}, profile)
|
2023-09-28 21:42:18 +00:00
|
|
|
|
2023-11-16 03:08:06 +00:00
|
|
|
at_uri = f'at://{did}/app.bsky.actor.profile/self'
|
2023-09-28 21:42:18 +00:00
|
|
|
self.assertEqual([Target(uri=at_uri, protocol='atproto')],
|
|
|
|
Object.get_by_id(id='fake:user').copies)
|
|
|
|
|
|
|
|
mock_create_task.assert_called()
|
|
|
|
|
2024-03-13 20:53:30 +00:00
|
|
|
# TODO: re-enable for launch
|
|
|
|
@skip
|
2023-09-28 21:42:18 +00:00
|
|
|
def test_bridge_user_bad_handle(self):
|
|
|
|
got = self.client.post('/bridge-user', data={'handle': 'bad xyz'})
|
|
|
|
self.assertEqual(400, got.status_code)
|
|
|
|
self.assertEqual(["Couldn't determine protocol for bad xyz"],
|
|
|
|
get_flashed_messages())
|
|
|
|
|
2023-05-26 23:36:45 +00:00
|
|
|
def test_nodeinfo(self):
|
|
|
|
# just check that it doesn't crash
|
|
|
|
self.client.get('/nodeinfo.json')
|
2023-10-16 21:02:17 +00:00
|
|
|
|
2023-12-31 16:42:26 +00:00
|
|
|
def test_instance_info(self):
|
|
|
|
# just check that it doesn't crash
|
|
|
|
self.client.get('/api/v1/instance')
|
|
|
|
|
2023-10-16 21:02:17 +00:00
|
|
|
def test_canonicalize_domain(self):
|
|
|
|
got = self.client.get('/', base_url='https://ap.brid.gy/')
|
|
|
|
self.assert_equals(301, got.status_code)
|
|
|
|
self.assert_equals('https://fed.brid.gy/', got.headers['Location'])
|