common.get_object: fall back to HTTP fetch if stored entity has no as2

also add more logging
pull/424/head
Ryan Barrett 2023-02-14 15:17:03 -08:00
rodzic 588598c5ff
commit f44aa3b39e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 23 dodań i 1 usunięć

Wyświetl plik

@ -131,9 +131,12 @@ def get_object(id, user=None):
Returns: Object, or None if it can't be fetched
"""
if obj := Object.get_by_id(id):
obj = Object.get_by_id(id)
if obj and obj.as2:
logging.info(f'Got Object from datastore: {id}')
return obj
logging.info(f'Object not in datastore or has no as2: {id}')
obj_as2 = get_as2(id, user=user).json()
obj = Object(id=id,
as2=json_dumps(obj_as2),

Wyświetl plik

@ -252,6 +252,7 @@ class CommonTest(testutil.TestCase):
id = 'http://the/id'
stored = Object(id=id, as2=json_dumps(AS2_OBJ), as1='{}')
stored.put()
common.get_object.cache.clear()
# first time loads from datastore
got = common.get_object(id)
@ -263,3 +264,21 @@ class CommonTest(testutil.TestCase):
got = common.get_object(id)
self.assert_entities_equal(stored, got)
mock_get.assert_not_called()
@mock.patch('requests.get', return_value=AS2)
def test_get_object_datastore_no_as2(self, mock_get):
"""If the stored Object has no as2, we should fall back to HTTP."""
id = 'http://the/id'
stored = Object(id=id, as2=None, as1='{}')
stored.put()
common.get_object.cache.clear()
got = common.get_object(id)
mock_get.assert_has_calls([self.as2_req(id)])
self.assert_equals(id, got.key.id())
self.assert_equals(AS2_OBJ, json_loads(got.as2))
mock_get.assert_has_calls([self.as2_req(id)])
self.assert_object(id, as2=AS2_OBJ, as1=AS2_OBJ,
source_protocol='activitypub')