noop test refactoring: finally unify request context push/pop into testutil

pull/553/head
Ryan Barrett 2023-06-15 15:09:03 -07:00
rodzic 60a4a2bb9f
commit bbcb8de44a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
11 zmienionych plików z 87 dodań i 143 usunięć

Wyświetl plik

@ -275,16 +275,15 @@ class ActivityPubTest(TestCase):
has_redirects=True)
ACTOR_BASE['publicKey']['publicKeyPem'] = self.user.public_pem().decode()
with self.request_context:
self.key_id_obj = Object(id='http://my/key/id', as2={
**ACTOR,
'publicKey': {
'id': 'http://my/key/id#unused',
'owner': 'http://own/er',
'publicKeyPem': self.user.public_pem().decode(),
},
})
self.key_id_obj.put()
self.key_id_obj = Object(id='http://my/key/id', as2={
**ACTOR,
'publicKey': {
'id': 'http://my/key/id#unused',
'owner': 'http://own/er',
'publicKeyPem': self.user.public_pem().decode(),
},
})
self.key_id_obj.put()
def assert_object(self, id, **props):
return super().assert_object(id, delivered_protocol='web', **props)
@ -531,9 +530,8 @@ class ActivityPubTest(TestCase):
'id': 'https://user.com/orig',
}
del note['url']
with self.request_context:
Object(id=orig_url, mf2=microformats2.object_to_json(as2.to_as1(note)),
source_protocol='web').put()
Object(id=orig_url, mf2=microformats2.object_to_json(as2.to_as1(note)),
source_protocol='web').put()
repost = copy.deepcopy(REPOST_FULL)
repost['object'] = f'http://localhost/r/{orig_url}'
@ -1228,8 +1226,7 @@ class ActivityPubTest(TestCase):
}, resp.json)
def store_followers(self):
with self.request_context:
follow = Object(id=FOLLOW_WITH_ACTOR['id'], as2=FOLLOW_WITH_ACTOR).put()
follow = Object(id=FOLLOW_WITH_ACTOR['id'], as2=FOLLOW_WITH_ACTOR).put()
Follower.get_or_create(to=self.user,
from_=ActivityPub.get_or_create('bar.com', actor_as2=ACTOR),
@ -1319,8 +1316,7 @@ class ActivityPubTest(TestCase):
}, resp.json)
def store_following(self):
with self.request_context:
follow = Object(id=FOLLOW_WITH_ACTOR['id'], as2=FOLLOW_WITH_ACTOR).put()
follow = Object(id=FOLLOW_WITH_ACTOR['id'], as2=FOLLOW_WITH_ACTOR).put()
Follower.get_or_create(to=ActivityPub.get_or_create('bar.com', actor_as2=ACTOR),
from_=self.user, follow=follow)
@ -1406,13 +1402,8 @@ class ActivityPubTest(TestCase):
class ActivityPubUtilsTest(TestCase):
def setUp(self):
super().setUp()
self.request_context.push()
g.user = self.make_user('user.com', has_hcard=True, actor_as2=ACTOR)
def tearDown(self):
self.request_context.pop()
super().tearDown()
def test_owns_id(self):
self.assertIsNone(ActivityPub.owns_id('http://foo'))
self.assertIsNone(ActivityPub.owns_id('https://bar/baz'))

Wyświetl plik

@ -26,13 +26,8 @@ class CommonTest(TestCase):
def setUp(self):
super().setUp()
self.request_context.push()
g.user = Fake(id='user.com')
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'),

Wyświetl plik

@ -90,8 +90,7 @@ class ConvertTest(testutil.TestCase):
def test_activitypub_to_web_object(self):
url = 'https://user.com/bar?baz=baj&biff'
with self.request_context:
Object(id=url, our_as1=COMMENT).put()
Object(id=url, our_as1=COMMENT).put()
resp = self.client.get('/convert/web/https://user.com/bar?baz=baj&biff',
base_url='https://ap.brid.gy/')
@ -100,8 +99,7 @@ class ConvertTest(testutil.TestCase):
ignore_blanks=True)
def test_activitypub_to_web_object_empty(self):
with self.request_context:
Object(id='http://foo').put()
Object(id='http://foo').put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -131,11 +129,10 @@ class ConvertTest(testutil.TestCase):
mock_get.assert_has_calls((self.as2_req('http://foo'),))
def test_activitypub_to_web_with_author(self):
with self.request_context:
Object(id='http://foo', our_as1={**COMMENT, 'author': 'http://bar'},
source_protocol='activitypub').put()
Object(id='http://bar', our_as1=ACTOR,
source_protocol='activitypub').put()
Object(id='http://foo', our_as1={**COMMENT, 'author': 'http://bar'},
source_protocol='activitypub').put()
Object(id='http://bar', our_as1=ACTOR,
source_protocol='activitypub').put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -146,8 +143,7 @@ class ConvertTest(testutil.TestCase):
def test_activitypub_to_web_no_url(self):
comment = copy.deepcopy(COMMENT)
del comment['url']
with self.request_context:
Object(id='http://foo', our_as1=comment).put()
Object(id='http://foo', our_as1=comment).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -159,26 +155,23 @@ class ConvertTest(testutil.TestCase):
ignore_blanks=True)
def test_activitypub_to_web_deleted_object(self):
with self.request_context:
Object(id='http://foo', as2={'content': 'foo'}, deleted=True).put()
Object(id='http://foo', as2={'content': 'foo'}, deleted=True).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
self.assertEqual(410, resp.status_code)
def test_activitypub_to_web_delete_activity(self):
with self.request_context:
Object(id='http://foo', our_as1=DELETE_OF_ID).put()
Object(id='http://foo', our_as1=DELETE_OF_ID).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
self.assertEqual(410, resp.status_code)
def test_activitypub_to_web_update_inner_obj_exists_redirect(self):
with self.request_context:
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
Object(id=UPDATE['object']['id'], as2={'content': 'foo'}).put()
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
Object(id=UPDATE['object']['id'], as2={'content': 'foo'}).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -187,10 +180,9 @@ class ConvertTest(testutil.TestCase):
resp.headers['Location'])
def test_activitypub_to_web_delete_inner_obj_exists_redirect(self):
with self.request_context:
# DELETE_OF_ID's object field is a bare string id
Object(id='http://foo', our_as1=DELETE_OF_ID).put()
Object(id=DELETE_OF_ID['object'], as2={'content': 'foo'}).put()
# DELETE_OF_ID's object field is a bare string id
Object(id='http://foo', our_as1=DELETE_OF_ID).put()
Object(id=DELETE_OF_ID['object'], as2={'content': 'foo'}).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -199,9 +191,8 @@ class ConvertTest(testutil.TestCase):
resp.headers['Location'])
def test_activitypub_to_web_update_no_inner_obj_serve_as_is(self):
with self.request_context:
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -214,10 +205,9 @@ A ☕ reply
""", resp.get_data(as_text=True), ignore_blanks=True)
def test_activitypub_to_web_update_inner_obj_too_minimal_serve_as_is(self):
with self.request_context:
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
Object(id=UPDATE['object']['id'], as2={'id': 'foo'}).put()
# UPDATE's object field is a full object
Object(id='http://foo', our_as1=UPDATE).put()
Object(id=UPDATE['object']['id'], as2={'id': 'foo'}).put()
resp = self.client.get('/convert/web/http://foo',
base_url='https://ap.brid.gy/')
@ -239,8 +229,7 @@ A ☕ reply
url = 'https://user.com/bar?baz=baj&biff'
self.make_user('user.com')
with self.request_context:
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
resp = self.client.get(f'/convert/ap/{url}',
base_url='https://ap.brid.gy/')
@ -253,8 +242,7 @@ A ☕ reply
url = 'https://user.com/bar?baz=baj&biff'
self.make_user('user.com')
with self.request_context:
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
Object(id=url, mf2=parse_mf2(HTML)['items'][0]).put()
resp = self.client.get(f'/convert/ap/{url}',
base_url='https://web.brid.gy/')

Wyświetl plik

@ -313,14 +313,12 @@ class UnfollowTest(TestCase):
def setUp(self):
super().setUp()
self.user = self.make_user('alice.com')
with self.request_context:
self.follower = Follower.get_or_create(
from_=self.user,
to=ActivityPub.get_or_create('https://bar/id', actor_as2=FOLLOWEE),
follow=Object(id=FOLLOW_ADDRESS['id'], as2=FOLLOW_ADDRESS).put(),
status='active',
)
self.follower = Follower.get_or_create(
from_=self.user,
to=ActivityPub.get_or_create('https://bar/id', actor_as2=FOLLOWEE),
follow=Object(id=FOLLOW_ADDRESS['id'], as2=FOLLOW_ADDRESS).put(),
status='active',
)
self.state = util.encode_oauth_state({
'endpoint': 'http://auth/endpoint',
@ -357,8 +355,7 @@ class UnfollowTest(TestCase):
obj = self.follower.follow.get()
obj.as2['object'] = FOLLOWEE['id']
with self.request_context:
obj.put()
obj.put()
mock_get.side_effect = (
# oauth-dropins indieauth https://alice.com fetch for user json
@ -407,12 +404,11 @@ class UnfollowTest(TestCase):
self.user.use_instead = user.key
self.user.put()
with self.request_context:
Follower.get_or_create(
from_=self.user,
to=ActivityPub.get_or_create('https://bar/id', actor_as2=FOLLOWEE),
follow=Object(id=FOLLOW_ADDRESS['id'], as2=FOLLOW_ADDRESS).put(),
status='active')
Follower.get_or_create(
from_=self.user,
to=ActivityPub.get_or_create('https://bar/id', actor_as2=FOLLOWEE),
follow=Object(id=FOLLOW_ADDRESS['id'], as2=FOLLOW_ADDRESS).put(),
status='active')
mock_get.side_effect = (
requests_response(''),

Wyświetl plik

@ -25,13 +25,8 @@ class UserTest(TestCase):
def setUp(self):
super().setUp()
self.request_context.push()
g.user = self.make_user('y.z')
def tearDown(self):
self.request_context.pop()
super().tearDown()
def test_get_or_create(self):
user = Fake.get_or_create('a.b')
@ -111,13 +106,8 @@ class UserTest(TestCase):
class ObjectTest(TestCase):
def setUp(self):
super().setUp()
self.request_context.push()
g.user = None
def tearDown(self):
self.request_context.pop()
super().tearDown()
def test_proxy_url(self):
obj = Object(id='abc', source_protocol='bluesky')
self.assertEqual('http://localhost/convert/bluesky/web/abc',

Wyświetl plik

@ -109,9 +109,8 @@ class PagesTest(TestCase):
self.assert_equals(200, got.status_code)
def test_user_object_bare_string_id(self):
with self.request_context:
Object(id='a', users=[self.user.key], labels=['notification'],
as2=REPOST_AS2).put()
Object(id='a', users=[self.user.key], labels=['notification'],
as2=REPOST_AS2).put()
got = self.client.get('/web/user.com')
self.assert_equals(200, got.status_code)

Wyświetl plik

@ -33,12 +33,10 @@ class ProtocolTest(TestCase):
def setUp(self):
super().setUp()
self.user = self.make_user('foo.com', has_hcard=True)
self.request_context.push()
g.user = None
def tearDown(self):
PROTOCOLS.pop('greedy', None)
self.request_context.pop()
super().tearDown()
def test_protocols_global(self):

Wyświetl plik

@ -70,8 +70,7 @@ class RedirectTest(testutil.TestCase):
self._test_as2(as2.CONTENT_TYPE_LD)
def test_as2_creates_user(self):
with self.request_context:
Object(id='https://user.com/repost', as2=REPOST_AS2).put()
Object(id='https://user.com/repost', as2=REPOST_AS2).put()
self.user.key.delete()
@ -152,8 +151,7 @@ class RedirectTest(testutil.TestCase):
self.assertEqual('https://user.com/bar', resp.headers['Location'])
def _test_as2(self, content_type):
with self.request_context:
self.obj = Object(id='https://user.com/', as2=REPOST_AS2).put()
self.obj = Object(id='https://user.com/', as2=REPOST_AS2).put()
resp = self.client.get('/r/https://user.com/', headers={'Accept': content_type})
self.assertEqual(200, resp.status_code, resp.get_data(as_text=True))
@ -161,8 +159,7 @@ class RedirectTest(testutil.TestCase):
self.assert_equals(REPOST_AS2, resp.json)
def test_as2_deleted(self):
with self.request_context:
Object(id='https://user.com/bar', as2={}, deleted=True).put()
Object(id='https://user.com/bar', as2={}, deleted=True).put()
resp = self.client.get('/r/https://user.com/bar',
headers={'Accept': as2.CONTENT_TYPE})

Wyświetl plik

@ -397,7 +397,6 @@ class WebTest(TestCase):
def setUp(self):
super().setUp()
g.user = self.make_user('user.com', has_redirects=True)
self.request_context.push()
def assert_deliveries(self, mock_post, inboxes, data, ignore=()):
self.assertEqual(len(inboxes), len(mock_post.call_args_list))
@ -667,8 +666,7 @@ class WebTest(TestCase):
'content': ['other'],
},
}
with self.request_context:
Object(id='https://user.com/reply', status='complete', mf2=mf2).put()
Object(id='https://user.com/reply', status='complete', mf2=mf2).put()
mock_get.side_effect = ACTIVITYPUB_GETS
mock_post.return_value = requests_response('abc xyz')
@ -686,8 +684,7 @@ class WebTest(TestCase):
def test_redo_repost_isnt_update(self, mock_get, mock_post):
"""Like and Announce shouldn't use Update, they should just resend as is."""
with self.request_context:
Object(id='https://user.com/repost', mf2={}, status='complete').put()
Object(id='https://user.com/repost', mf2={}, status='complete').put()
mock_get.side_effect = [REPOST, TOOT_AS2, ACTOR]
mock_post.return_value = requests_response('abc xyz')
@ -702,8 +699,7 @@ class WebTest(TestCase):
def test_skip_update_if_content_unchanged(self, mock_get, mock_post):
"""https://github.com/snarfed/bridgy-fed/issues/78"""
with self.request_context:
Object(id='https://user.com/reply', mf2=REPLY_MF2).put()
Object(id='https://user.com/reply', mf2=REPLY_MF2).put()
mock_get.side_effect = ACTIVITYPUB_GETS
@ -715,8 +711,7 @@ class WebTest(TestCase):
mock_post.assert_not_called()
def test_force_with_content_unchanged_sends_create(self, mock_get, mock_post):
with self.request_context:
Object(id='https://user.com/reply', mf2=REPLY_MF2).put()
Object(id='https://user.com/reply', mf2=REPLY_MF2).put()
mock_get.side_effect = ACTIVITYPUB_GETS
mock_post.return_value = requests_response('abc xyz')
@ -986,10 +981,9 @@ class WebTest(TestCase):
mock_get.side_effect = [NOTE, ACTOR]
mock_post.return_value = requests_response('abc xyz')
with self.request_context:
mf2 = copy.deepcopy(NOTE_MF2)
mf2['properties']['content'] = 'different'
Object(id='https://user.com/post', users=[g.user.key], mf2=mf2).put()
mf2 = copy.deepcopy(NOTE_MF2)
mf2['properties']['content'] = 'different'
Object(id='https://user.com/post', users=[g.user.key], mf2=mf2).put()
self.make_followers()
@ -1300,9 +1294,8 @@ class WebTest(TestCase):
mock_get.return_value = requests_response('"unused"', status=410,
url='http://final/delete')
with self.request_context:
Object(id='https://user.com/post#bridgy-fed-create',
mf2=NOTE_MF2, status='in progress')
Object(id='https://user.com/post#bridgy-fed-create',
mf2=NOTE_MF2, status='in progress')
got = self.client.post('/_ah/queue/webmention', data={
'source': 'https://user.com/post',
@ -1696,13 +1689,8 @@ class WebProtocolTest(TestCase):
def setUp(self):
super().setUp()
self.request_context.__enter__()
g.user = self.make_user('user.com')
def tearDown(self):
self.request_context.__enter__()
super().tearDown()
def test_key_for(self, *_):
for id in 'user.com', 'http://user.com', 'https://user.com/':
self.assertEqual(Web(id='user.com').key, Web.key_for(id))

Wyświetl plik

@ -82,17 +82,17 @@ class XrpcFeedTest(testutil.TestCase):
def test_getAuthorFeed(self):
post_as2 = as2.from_as1(POST_AS)
with self.request_context:
Object(id='a', domains=['user.com'], labels=['user'], as2=post_as2).put()
Object(id='b', domains=['user.com'], labels=['user'],
as2=as2.from_as1(REPLY_AS)).put()
# not outbound from user
Object(id='d', domains=['user.com'], labels=['feed'], as2=post_as2).put()
# deleted
Object(id='e', domains=['user.com'], labels=['user'], as2=post_as2,
deleted=True).put()
# other user's
Object(id='f', domains=['bar.org'], labels=['user'], as2=post_as2).put()
Object(id='a', domains=['user.com'], labels=['user'], as2=post_as2).put()
Object(id='b', domains=['user.com'], labels=['user'],
as2=as2.from_as1(REPLY_AS)).put()
# not outbound from user
Object(id='d', domains=['user.com'], labels=['feed'], as2=post_as2).put()
# deleted
Object(id='e', domains=['user.com'], labels=['user'], as2=post_as2,
deleted=True).put()
# other user's
Object(id='f', domains=['bar.org'], labels=['user'], as2=post_as2).put()
resp = self.client.get('/xrpc/app.bsky.feed.getAuthorFeed',
query_string={'author': 'user.com'})
@ -122,9 +122,8 @@ class XrpcFeedTest(testutil.TestCase):
self.assert_equals({'feed': []}, resp.json)
def test_getPostThread(self):
with self.request_context:
Object(id='http://a/post', domains=['user.com'], labels=['user'],
as2=as2.from_as1(POST_THREAD_AS)).put()
Object(id='http://a/post', domains=['user.com'], labels=['user'],
as2=as2.from_as1(POST_THREAD_AS)).put()
resp = self.client.get('/xrpc/app.bsky.feed.getPostThread',
query_string={'uri': 'http://a/post'})
@ -141,16 +140,15 @@ class XrpcFeedTest(testutil.TestCase):
self.assertEqual(400, resp.status_code, resp.get_data(as_text=True))
def test_getRepostedBy(self):
with self.request_context:
Object(id='repost/1', domains=['user.com'], as2=as2.from_as1({
**REPOST_AS,
'object': 'http://a/post',
})).put()
Object(id='repost/2', domains=['user.com'], as2=as2.from_as1({
**REPOST_AS,
'object': 'http://a/post',
'actor': as2.to_as1(ACTOR),
})).put()
Object(id='repost/1', domains=['user.com'], as2=as2.from_as1({
**REPOST_AS,
'object': 'http://a/post',
})).put()
Object(id='repost/2', domains=['user.com'], as2=as2.from_as1({
**REPOST_AS,
'object': 'http://a/post',
'actor': as2.to_as1(ACTOR),
})).put()
got = self.client.get('/xrpc/app.bsky.feed.getRepostedBy',
query_string={'uri': 'http://a/post'})

Wyświetl plik

@ -144,6 +144,7 @@ class TestCase(unittest.TestCase, testutil.Asserts):
init_globals()
self.request_context = app.test_request_context('/')
self.request_context.push()
# suppress a few warnings
# local/lib/python3.9/site-packages/bs4/__init__.py:435: MarkupResemblesLocatorWarning: The input looks more like a filename than markup. You may want to open this file and pass the filehandle into Beautiful Soup.
@ -155,6 +156,9 @@ class TestCase(unittest.TestCase, testutil.Asserts):
self.client.__exit__(None, None, None)
super().tearDown()
# this breaks if it's before super().tearDown(). why?!
self.request_context.pop()
def run(self, result=None):
"""Override to hide stdlib and virtualenv lines in tracebacks.