Add delete profile method (#218)

This commit is contained in:
Jonathan Zernik 2020-08-04 03:33:14 -07:00 committed by GitHub
parent e7a515fae3
commit fe47116430
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 2 deletions

View file

@ -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
):

View file

@ -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;

View file

@ -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(

View file

@ -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

View file

@ -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)

View file

@ -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 = """