From 2d3a809050b6994380bfe8de4ca2155935b2d48b Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Tue, 18 Feb 2025 12:11:43 -0800 Subject: [PATCH] ATProto.create_for: don't add user copy id until the end ...so that we know we've fully successfully created the repo, profile, etc for https://console.cloud.google.com/errors/detail/CID-1fzFl4iVwwE;locations=global;time=P30D?project=bridgy-federated --- atproto.py | 5 +++-- tests/test_atproto.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/atproto.py b/atproto.py index 73c411e7..845cf654 100644 --- a/atproto.py +++ b/atproto.py @@ -442,8 +442,6 @@ def create_for(cls, user): also_known_as=user.profile_id()) Object.get_or_create(did_plc.did, raw=did_plc.doc, authed_as=did_plc) - # TODO: move this to ATProto.get_or_create? - add(user.copies, Target(uri=did_plc.did, protocol='atproto')) cls.set_dns(handle=handle, did=did_plc.did) @@ -481,6 +479,9 @@ def create_for(cls, user): signing_key=did_plc.signing_key, rotation_key=did_plc.rotation_key) + # don't add the copy id until the end, here, until we've fully + # successfully created the repo, profile, etc + user.add('copies', Target(uri=did_plc.did, protocol='atproto')) user.put() @classmethod diff --git a/tests/test_atproto.py b/tests/test_atproto.py index ac60bf36..52d62316 100644 --- a/tests/test_atproto.py +++ b/tests/test_atproto.py @@ -1261,6 +1261,19 @@ def test_create_for_tombstoned(self, mock_get, mock_post, mock_create_task, mock_create_task.assert_called() # atproto-commit + @patch.object(tasks_client, 'create_task', return_value=Task(name='my task')) + @patch.object(Repo, 'create', side_effect=AssertionError('nope')) + @patch('requests.post', return_value=requests_response('OK')) # create DID on PLC + def test_create_for_error_doesnt_add_to_copies(self, _, __, ___): + user = self.make_user(id='fake:user', cls=Fake) + assert not user.obj.as1 + + with self.assertRaises(AssertionError): + ATProto.create_for(user) + + self.assertEqual([], user.copies) + self.assertEqual([], user.key.get().copies) + @patch('atproto.DEBUG', new=False) @patch.object(google.cloud.dns.client.ManagedZone, 'changes') @patch.object(atproto.dns_discovery_api, 'resourceRecordSets')