diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index 5d35fde7..fa4f3644 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -407,6 +407,24 @@ def test_set_profile_sharing(server_stub, admin_stub, contact_profile_id): assert get_squeak_profile_response.squeak_profile.sharing == True +def test_delete_profile(server_stub, admin_stub, contact_profile_id): + # Delete the profile + admin_stub.DeleteSqueakProfile( + squeak_admin_pb2.DeleteSqueakProfileRequest( + profile_id=contact_profile_id, + ) + ) + + # Try to get the profile and fail + with pytest.raises(Exception) as excinfo: + admin_stub.GetSqueakProfile( + squeak_admin_pb2.GetSqueakProfileRequest( + profile_id=contact_profile_id, + ) + ) + assert "Profile not found." in str(excinfo.value) + + def test_get_following_squeaks( server_stub, admin_stub, saved_squeak_hash, signing_profile_id ): diff --git a/proto/squeak_admin.proto b/proto/squeak_admin.proto index 6c8f81c6..e704e682 100644 --- a/proto/squeak_admin.proto +++ b/proto/squeak_admin.proto @@ -58,6 +58,10 @@ service SqueakAdmin { */ rpc SetSqueakProfileSharing (SetSqueakProfileSharingRequest) returns (SetSqueakProfileSharingReply) {} + /** sqkadmin: `deletesqueakprofile` + */ + rpc DeleteSqueakProfile (DeleteSqueakProfileRequest) returns (DeleteSqueakProfileReply) {} + /** sqkadmin: `makesqueak` */ rpc MakeSqueak (MakeSqueakRequest) returns (MakeSqueakReply) {} @@ -208,6 +212,14 @@ message SetSqueakProfileSharingRequest { message SetSqueakProfileSharingReply { } +message DeleteSqueakProfileRequest { + /// The profile id + int32 profile_id = 1; +} + +message DeleteSqueakProfileReply { +} + message SqueakProfile { /// The profile id int32 profile_id = 1; diff --git a/squeakserver/admin/squeak_admin_server_handler.py b/squeakserver/admin/squeak_admin_server_handler.py index 17eb7f78..e7ba2535 100644 --- a/squeakserver/admin/squeak_admin_server_handler.py +++ b/squeakserver/admin/squeak_admin_server_handler.py @@ -89,6 +89,10 @@ class SqueakAdminServerHandler(object): ) self.squeak_node.set_squeak_profile_sharing(profile_id, sharing) + def handle_delete_squeak_profile(self, profile_id): + logger.info("Handle delete squeak profile with id: {}".format(profile_id)) + self.squeak_node.delete_squeak_profile(profile_id) + def handle_make_squeak(self, profile_id, content_str, replyto_hash): logger.info("Handle make squeak profile with id: {}".format(profile_id)) inserted_squeak_hash = self.squeak_node.make_squeak( diff --git a/squeakserver/admin/squeak_admin_server_servicer.py b/squeakserver/admin/squeak_admin_server_servicer.py index 86a9af5c..3d243e82 100644 --- a/squeakserver/admin/squeak_admin_server_servicer.py +++ b/squeakserver/admin/squeak_admin_server_servicer.py @@ -57,8 +57,14 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): def GetSqueakProfile(self, request, context): profile_id = request.profile_id squeak_profile = self.handler.handle_get_squeak_profile(profile_id) + if squeak_profile is None: + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details('Profile not found.') + return squeak_admin_pb2.GetSqueakProfileReply() squeak_profile_msg = self._squeak_profile_to_message(squeak_profile) - return squeak_admin_pb2.GetSqueakProfileReply(squeak_profile=squeak_profile_msg) + return squeak_admin_pb2.GetSqueakProfileReply( + squeak_profile=squeak_profile_msg, + ) def GetSqueakProfileByAddress(self, request, context): address = request.address @@ -84,6 +90,11 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): self.handler.handle_set_squeak_profile_sharing(profile_id, sharing) return squeak_admin_pb2.SetSqueakProfileSharingReply() + def DeleteSqueakProfile(self, request, context): + profile_id = request.profile_id + self.handler.handle_delete_squeak_profile(profile_id) + return squeak_admin_pb2.DeleteSqueakProfileReply() + def MakeSqueak(self, request, context): profile_id = request.profile_id content_str = request.content diff --git a/squeakserver/node/squeak_node.py b/squeakserver/node/squeak_node.py index 2901c5ec..fc5c9bc8 100644 --- a/squeakserver/node/squeak_node.py +++ b/squeakserver/node/squeak_node.py @@ -177,6 +177,9 @@ class SqueakNode: def set_squeak_profile_sharing(self, profile_id, sharing): self.postgres_db.set_profile_sharing(profile_id, sharing) + def delete_squeak_profile(self, profile_id): + self.postgres_db.delete_profile(profile_id) + def make_squeak(self, profile_id, content_str, replyto_hash): squeak_profile = self.postgres_db.get_profile(profile_id) squeak_maker = SqueakMaker(self.blockchain_client) diff --git a/squeakserver/server/postgres_db.py b/squeakserver/server/postgres_db.py index b737c34b..e8dc0527 100644 --- a/squeakserver/server/postgres_db.py +++ b/squeakserver/server/postgres_db.py @@ -304,7 +304,6 @@ class PostgresDb: """ Get a profile. """ sql = """ SELECT * FROM profile WHERE profile_id=%s""" - with self.get_cursor() as curs: curs.execute(sql, (profile_id,)) row = curs.fetchone() @@ -351,6 +350,15 @@ class PostgresDb: with self.get_cursor() as curs: curs.execute(sql, (sharing, profile_id,)) + def delete_profile(self, profile_id): + """ Delete a profile. """ + sql = """ + DELETE FROM profile + WHERE profile_id=%s; + """ + with self.get_cursor() as curs: + curs.execute(sql, (profile_id,)) + def get_unverified_block_squeaks(self): """ Get all squeaks without block header. """ sql = """