def test_delete_user(self, *_): """use a form to update a user""" view = views.DeleteUser.as_view() form = forms.DeleteUserForm() form.data["password"] = "******" request = self.factory.post("", form.data) request.user = self.local_user middleware = SessionMiddleware() middleware.process_request(request) request.session.save() self.assertIsNone(self.local_user.name) with patch( "bookwyrm.models.activitypub_mixin.broadcast_task.apply_async" ) as delay_mock: view(request) self.assertEqual(delay_mock.call_count, 1) activity = json.loads(delay_mock.call_args[1]["args"][1]) self.assertEqual(activity["type"], "Delete") self.assertEqual(activity["actor"], self.local_user.remote_id) self.assertEqual(activity["cc"][0], "https://www.w3.org/ns/activitystreams#Public") self.local_user.refresh_from_db() self.assertFalse(self.local_user.is_active) self.assertEqual(self.local_user.deactivation_reason, "self_deletion")
def get(self, request): """delete page for a user""" data = { "form": forms.DeleteUserForm(), "user": request.user, } return TemplateResponse(request, "preferences/delete_user.html", data)
def post(self, request): """les get fancy with images""" form = forms.DeleteUserForm(request.POST, instance=request.user) # idk why but I couldn't get check_password to work on request.user user = models.User.objects.get(id=request.user.id) if form.is_valid() and user.check_password(form.cleaned_data["password"]): user.deactivation_reason = "self_deletion" user.delete() logout(request) return redirect("/") form.errors["password"] = ["Invalid password"] data = {"form": form, "user": request.user} return TemplateResponse(request, "preferences/delete_user.html", data)
def moderator_delete_user(request, user_id): """permanently delete a user""" user = get_object_or_404(models.User, id=user_id) # we can't delete users on other instances if not user.local: raise PermissionDenied() form = forms.DeleteUserForm(request.POST, instance=user) moderator = models.User.objects.get(id=request.user.id) # check the moderator's password if form.is_valid() and moderator.check_password( form.cleaned_data["password"]): user.deactivation_reason = "moderator_deletion" user.delete() return redirect("settings-user", user.id) form.errors["password"] = ["Invalid password"] data = {"user": user, "group_form": forms.UserGroupForm(), "form": form} return TemplateResponse(request, "user_admin/user.html", data)