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.
This commit is contained in:
Blake Erickson 2018-05-10 13:04:36 -06:00
parent 186623acd0
commit bd352a17bf
2 changed files with 8 additions and 3 deletions

View File

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

View File

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