From 83c0b295e9ffade53703109196fdecca8ff247d2 Mon Sep 17 00:00:00 2001 From: Jonathan Zernik Date: Sun, 24 Jan 2021 16:53:09 -0800 Subject: [PATCH] Add rename profile rpc method (#682) * Add rpc method to rename profile * Add web endpoint for rename profile --- itests/tests/conftest.py | 5 +++++ itests/tests/test_squeak_node.py | 18 ++++++++++++++++++ proto/squeak_admin.proto | 15 +++++++++++++++ .../admin/squeak_admin_server_handler.py | 12 ++++++++++++ .../admin/squeak_admin_server_servicer.py | 3 +++ squeaknode/admin/webapp/app.py | 8 ++++++++ squeaknode/core/squeak_controller.py | 3 +++ squeaknode/db/squeak_db.py | 10 ++++++++++ 8 files changed, 74 insertions(+) diff --git a/itests/tests/conftest.py b/itests/tests/conftest.py index ba3ec4a3..aa8feb7f 100644 --- a/itests/tests/conftest.py +++ b/itests/tests/conftest.py @@ -138,3 +138,8 @@ def peer_id(server_stub, admin_stub): ) peer_id = create_peer_response.peer_id yield peer_id + + +@pytest.fixture +def random_name(): + yield "random_name_{}".format(uuid.uuid1()) diff --git a/itests/tests/test_squeak_node.py b/itests/tests/test_squeak_node.py index ecf02df7..6a0c0f0a 100644 --- a/itests/tests/test_squeak_node.py +++ b/itests/tests/test_squeak_node.py @@ -523,6 +523,24 @@ def test_set_profile_sharing(server_stub, admin_stub, contact_profile_id): assert get_squeak_profile_response.squeak_profile.sharing +def test_rename_profile(server_stub, admin_stub, contact_profile_id, random_name): + # Rename the profile to something new + admin_stub.RenameSqueakProfile( + squeak_admin_pb2.RenameSqueakProfileRequest( + profile_id=contact_profile_id, + profile_name=random_name, + ) + ) + + # Get the squeak profile + get_squeak_profile_response = admin_stub.GetSqueakProfile( + squeak_admin_pb2.GetSqueakProfileRequest( + profile_id=contact_profile_id, + ) + ) + assert get_squeak_profile_response.squeak_profile.profile_name == random_name + + def test_delete_profile(server_stub, admin_stub, contact_profile_id): # Delete the profile admin_stub.DeleteSqueakProfile( diff --git a/proto/squeak_admin.proto b/proto/squeak_admin.proto index 56fae818..bcc12815 100644 --- a/proto/squeak_admin.proto +++ b/proto/squeak_admin.proto @@ -104,6 +104,10 @@ service SqueakAdmin { */ rpc SetSqueakProfileSharing (SetSqueakProfileSharingRequest) returns (SetSqueakProfileSharingReply) {} + /** sqkadmin: `renamesqueakprofile` + */ + rpc RenameSqueakProfile (RenameSqueakProfileRequest) returns (RenameSqueakProfileReply) {} + /** sqkadmin: `getsqueakprofileprivatekey` */ rpc GetSqueakProfilePrivateKey (GetSqueakProfilePrivateKeyRequest) returns (GetSqueakProfilePrivateKeyReply) {} @@ -318,6 +322,17 @@ message SetSqueakProfileSharingRequest { message SetSqueakProfileSharingReply { } +message RenameSqueakProfileRequest { + /// The profile id + int32 profile_id = 1; + + /// The new profile name + string profile_name = 2; +} + +message RenameSqueakProfileReply { +} + message GetSqueakProfilePrivateKeyRequest { /// The profile id int32 profile_id = 1; diff --git a/squeaknode/admin/squeak_admin_server_handler.py b/squeaknode/admin/squeak_admin_server_handler.py index a74147dc..f15c7e2f 100644 --- a/squeaknode/admin/squeak_admin_server_handler.py +++ b/squeaknode/admin/squeak_admin_server_handler.py @@ -195,6 +195,18 @@ class SqueakAdminServerHandler(object): self.squeak_controller.set_squeak_profile_sharing(profile_id, sharing) return squeak_admin_pb2.SetSqueakProfileSharingReply() + def handle_rename_squeak_profile(self, request): + profile_id = request.profile_id + profile_name = request.profile_name + logger.info( + "Handle rename squeak profile with profile id: {}, new name: {}".format( + profile_id, + profile_name, + ) + ) + self.squeak_controller.rename_squeak_profile(profile_id, profile_name) + return squeak_admin_pb2.RenameSqueakProfileReply() + def handle_delete_squeak_profile(self, request): profile_id = request.profile_id logger.info( diff --git a/squeaknode/admin/squeak_admin_server_servicer.py b/squeaknode/admin/squeak_admin_server_servicer.py index c4b62898..c36fcbf1 100644 --- a/squeaknode/admin/squeak_admin_server_servicer.py +++ b/squeaknode/admin/squeak_admin_server_servicer.py @@ -91,6 +91,9 @@ class SqueakAdminServerServicer(squeak_admin_pb2_grpc.SqueakAdminServicer): def SetSqueakProfileSharing(self, request, context): return self.handler.handle_set_squeak_profile_sharing(request) + def RenameSqueakProfile(self, request, context): + return self.handler.handle_rename_squeak_profile(request) + def DeleteSqueakProfile(self, request, context): return self.handler.handle_delete_squeak_profile(request) diff --git a/squeaknode/admin/webapp/app.py b/squeaknode/admin/webapp/app.py index 808a6761..de8ecea3 100644 --- a/squeaknode/admin/webapp/app.py +++ b/squeaknode/admin/webapp/app.py @@ -225,6 +225,14 @@ def create_app(handler, username, password): handler.handle_set_squeak_profile_sharing, ) + @app.route("/renamesqueakprofile", methods=["POST"]) + @login_required + def renamesqueakprofile(): + return handle_request( + squeak_admin_pb2.RenameSqueakProfileRequest(), + handler.handle_rename_squeak_profile, + ) + @app.route("/getpeers", methods=["POST"]) @login_required def getpeers(): diff --git a/squeaknode/core/squeak_controller.py b/squeaknode/core/squeak_controller.py index 274c6aa8..05b9b653 100644 --- a/squeaknode/core/squeak_controller.py +++ b/squeaknode/core/squeak_controller.py @@ -181,6 +181,9 @@ class SqueakController: def set_squeak_profile_sharing(self, profile_id: int, sharing: bool): self.squeak_db.set_profile_sharing(profile_id, sharing) + def rename_squeak_profile(self, profile_id: int, profile_name: str): + self.squeak_db.set_profile_name(profile_id, profile_name) + def delete_squeak_profile(self, profile_id: int): self.squeak_db.delete_profile(profile_id) diff --git a/squeaknode/db/squeak_db.py b/squeaknode/db/squeak_db.py index 89d0cf65..3f04f9fe 100644 --- a/squeaknode/db/squeak_db.py +++ b/squeaknode/db/squeak_db.py @@ -632,6 +632,16 @@ class SqueakDb: # with self.get_cursor() as curs: # curs.execute(sql, (sharing, profile_id,)) + def set_profile_name(self, profile_id: int, profile_name: str): + """ Set a profile name. """ + stmt = ( + self.profiles.update() + .where(self.profiles.c.profile_id == profile_id) + .values(profile_name=profile_name) + ) + with self.get_connection() as connection: + connection.execute(stmt) + def delete_profile(self, profile_id: int): """ Delete a profile. """ delete_profile_stmt = self.profiles.delete().where(