@@ -939,7 +939,7 @@ def test_no_auth(self):
939939 """
940940 channel = self .make_request ("POST" , self .url , b"{}" )
941941
942- self .assertEqual (401 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
942+ self .assertEqual (401 , channel .code , msg = channel .json_body )
943943 self .assertEqual (Codes .MISSING_TOKEN , channel .json_body ["errcode" ])
944944
945945 def test_requester_is_not_admin (self ):
@@ -950,7 +950,7 @@ def test_requester_is_not_admin(self):
950950
951951 channel = self .make_request ("POST" , url , access_token = self .other_user_token )
952952
953- self .assertEqual (403 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
953+ self .assertEqual (403 , channel .code , msg = channel .json_body )
954954 self .assertEqual ("You are not a server admin" , channel .json_body ["error" ])
955955
956956 channel = self .make_request (
@@ -960,7 +960,7 @@ def test_requester_is_not_admin(self):
960960 content = b"{}" ,
961961 )
962962
963- self .assertEqual (403 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
963+ self .assertEqual (403 , channel .code , msg = channel .json_body )
964964 self .assertEqual ("You are not a server admin" , channel .json_body ["error" ])
965965
966966 def test_user_does_not_exist (self ):
@@ -990,7 +990,7 @@ def test_erase_is_not_bool(self):
990990 access_token = self .admin_user_tok ,
991991 )
992992
993- self .assertEqual (400 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
993+ self .assertEqual (400 , channel .code , msg = channel .json_body )
994994 self .assertEqual (Codes .BAD_JSON , channel .json_body ["errcode" ])
995995
996996 def test_user_is_not_local (self ):
@@ -1006,7 +1006,7 @@ def test_user_is_not_local(self):
10061006
10071007 def test_deactivate_user_erase_true (self ):
10081008 """
1009- Test deactivating an user and set `erase` to `true`
1009+ Test deactivating a user and set `erase` to `true`
10101010 """
10111011
10121012 # Get user
@@ -1016,24 +1016,22 @@ def test_deactivate_user_erase_true(self):
10161016 access_token = self .admin_user_tok ,
10171017 )
10181018
1019- self .assertEqual (200 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
1019+ self .assertEqual (200 , channel .code , msg = channel .json_body )
10201020 self .assertEqual ("@user:test" , channel .json_body ["name" ])
10211021 self .assertEqual (False , channel .json_body ["deactivated" ])
10221022 self .
assertEqual (
"[email protected] " ,
channel .
json_body [
"threepids" ][
0 ][
"address" ])
10231023 self .assertEqual ("mxc://servername/mediaid" , channel .json_body ["avatar_url" ])
10241024 self .assertEqual ("User1" , channel .json_body ["displayname" ])
10251025
1026- # Deactivate user
1027- body = json .dumps ({"erase" : True })
1028-
1026+ # Deactivate and erase user
10291027 channel = self .make_request (
10301028 "POST" ,
10311029 self .url ,
10321030 access_token = self .admin_user_tok ,
1033- content = body . encode ( encoding = "utf_8" ) ,
1031+ content = { "erase" : True } ,
10341032 )
10351033
1036- self .assertEqual (200 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
1034+ self .assertEqual (200 , channel .code , msg = channel .json_body )
10371035
10381036 # Get user
10391037 channel = self .make_request (
@@ -1042,7 +1040,7 @@ def test_deactivate_user_erase_true(self):
10421040 access_token = self .admin_user_tok ,
10431041 )
10441042
1045- self .assertEqual (200 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
1043+ self .assertEqual (200 , channel .code , msg = channel .json_body )
10461044 self .assertEqual ("@user:test" , channel .json_body ["name" ])
10471045 self .assertEqual (True , channel .json_body ["deactivated" ])
10481046 self .assertEqual (0 , len (channel .json_body ["threepids" ]))
@@ -1053,7 +1051,7 @@ def test_deactivate_user_erase_true(self):
10531051
10541052 def test_deactivate_user_erase_false (self ):
10551053 """
1056- Test deactivating an user and set `erase` to `false`
1054+ Test deactivating a user and set `erase` to `false`
10571055 """
10581056
10591057 # Get user
@@ -1063,21 +1061,19 @@ def test_deactivate_user_erase_false(self):
10631061 access_token = self .admin_user_tok ,
10641062 )
10651063
1066- self .assertEqual (200 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
1064+ self .assertEqual (200 , channel .code , msg = channel .json_body )
10671065 self .assertEqual ("@user:test" , channel .json_body ["name" ])
10681066 self .assertEqual (False , channel .json_body ["deactivated" ])
10691067 self .
assertEqual (
"[email protected] " ,
channel .
json_body [
"threepids" ][
0 ][
"address" ])
10701068 self .assertEqual ("mxc://servername/mediaid" , channel .json_body ["avatar_url" ])
10711069 self .assertEqual ("User1" , channel .json_body ["displayname" ])
10721070
10731071 # Deactivate user
1074- body = json .dumps ({"erase" : False })
1075-
10761072 channel = self .make_request (
10771073 "POST" ,
10781074 self .url ,
10791075 access_token = self .admin_user_tok ,
1080- content = body . encode ( encoding = "utf_8" ) ,
1076+ content = { "erase" : False } ,
10811077 )
10821078
10831079 self .assertEqual (200 , int (channel .result ["code" ]), msg = channel .result ["body" ])
@@ -1089,7 +1085,7 @@ def test_deactivate_user_erase_false(self):
10891085 access_token = self .admin_user_tok ,
10901086 )
10911087
1092- self .assertEqual (200 , int ( channel .result [ " code" ]) , msg = channel .result [ "body" ] )
1088+ self .assertEqual (200 , channel .code , msg = channel .json_body )
10931089 self .assertEqual ("@user:test" , channel .json_body ["name" ])
10941090 self .assertEqual (True , channel .json_body ["deactivated" ])
10951091 self .assertEqual (0 , len (channel .json_body ["threepids" ]))
@@ -1098,6 +1094,60 @@ def test_deactivate_user_erase_false(self):
10981094
10991095 self ._is_erased ("@user:test" , False )
11001096
1097+ def test_deactivate_user_erase_true_no_profile (self ):
1098+ """
1099+ Test deactivating a user and set `erase` to `true`
1100+ if user has no profile information (stored in the database table `profiles`).
1101+ """
1102+
1103+ # Users normally have an entry in `profiles`, but occasionally they are created without one.
1104+ # To test deactivation for users without a profile, we delete the profile information for our user.
1105+ self .get_success (
1106+ self .store .db_pool .simple_delete_one (
1107+ table = "profiles" , keyvalues = {"user_id" : "user" }
1108+ )
1109+ )
1110+
1111+ # Get user
1112+ channel = self .make_request (
1113+ "GET" ,
1114+ self .url_other_user ,
1115+ access_token = self .admin_user_tok ,
1116+ )
1117+
1118+ self .assertEqual (200 , channel .code , msg = channel .json_body )
1119+ self .assertEqual ("@user:test" , channel .json_body ["name" ])
1120+ self .assertEqual (False , channel .json_body ["deactivated" ])
1121+ self .
assertEqual (
"[email protected] " ,
channel .
json_body [
"threepids" ][
0 ][
"address" ])
1122+ self .assertIsNone (channel .json_body ["avatar_url" ])
1123+ self .assertIsNone (channel .json_body ["displayname" ])
1124+
1125+ # Deactivate and erase user
1126+ channel = self .make_request (
1127+ "POST" ,
1128+ self .url ,
1129+ access_token = self .admin_user_tok ,
1130+ content = {"erase" : True },
1131+ )
1132+
1133+ self .assertEqual (200 , channel .code , msg = channel .json_body )
1134+
1135+ # Get user
1136+ channel = self .make_request (
1137+ "GET" ,
1138+ self .url_other_user ,
1139+ access_token = self .admin_user_tok ,
1140+ )
1141+
1142+ self .assertEqual (200 , channel .code , msg = channel .json_body )
1143+ self .assertEqual ("@user:test" , channel .json_body ["name" ])
1144+ self .assertEqual (True , channel .json_body ["deactivated" ])
1145+ self .assertEqual (0 , len (channel .json_body ["threepids" ]))
1146+ self .assertIsNone (channel .json_body ["avatar_url" ])
1147+ self .assertIsNone (channel .json_body ["displayname" ])
1148+
1149+ self ._is_erased ("@user:test" , True )
1150+
11011151 def _is_erased (self , user_id : str , expect : bool ) -> None :
11021152 """Assert that the user is erased or not"""
11031153 d = self .store .is_user_erased (user_id )
0 commit comments