From bd352a17bff9019db21196d70cd9829e678a33be Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Thu, 10 May 2018 13:04:36 -0600 Subject: [PATCH] FIX: Show a json api response when deleting a user with posts A 500 error was actually caused with no response when using the api, so it wasn't very clear that you need to delete the posts first when using the api. --- app/controllers/admin/users_controller.rb | 5 ++++- spec/controllers/admin/users_controller_spec.rb | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 10922e3c909..b9a24b38fd5 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -379,7 +379,10 @@ class Admin::UsersController < Admin::AdminController } end rescue UserDestroyer::PostsExistError - raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.") + render json: { + deleted: false, + message: "User #{user.username} has #{user.post_count} posts, so they can't be deleted." + } end end end diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index 703021890fc..9fb6329fc77 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -529,9 +529,11 @@ describe Admin::UsersController do _post = create_post(topic: topic, user: delete_me) end - it "returns an error" do + it "returns an api response that the user can't be deleted because it has posts" do delete :destroy, params: { id: delete_me.id }, format: :json - expect(response).to be_forbidden + expect(response).to be_success + json = ::JSON.parse(response.body) + expect(json['deleted']).to eq(false) end it "doesn't return an error if delete_posts == true" do