Skip to content

Commit

Permalink
Merge pull request #4417 from tvdeyen/v3.0-fix-user-destroy
Browse files Browse the repository at this point in the history
[v3.0] Fix delete response in admin users controller
  • Loading branch information
waiting-for-dev authored Jun 22, 2022
2 parents da5aae7 + 8f23b7b commit 67bd4f3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/app/controllers/spree/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def user_params
# handling raise from Spree::Admin::ResourceController#destroy
def user_destroy_with_orders_error
invoke_callbacks(:destroy, :fails)
render status: :forbidden, text: t('spree.error_user_destroy_with_orders')
render status: :forbidden, plain: t("spree.error_user_destroy_with_orders")
end

def sign_in_if_change_own_password
Expand Down
29 changes: 29 additions & 0 deletions backend/spec/controllers/spree/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,35 @@ def user
end
end

describe "#destroy" do
stub_authorization! do |_user|
can :manage, Spree.user_class
end

subject do
delete :destroy, params: { id: user.id }
response
end

context "with user having no orders" do
let(:user) { create(:user) }

it "can be destroyed" do
is_expected.to be_redirect
expect(flash[:success]).to eq("User has been successfully removed!")
end
end

context "with user having orders" do
let(:user) { create(:user, :with_orders) }

it "cannot be destroyed" do
is_expected.to be_forbidden
expect(subject.body).to eq I18n.t("spree.error_user_destroy_with_orders")
end
end
end

describe "#orders" do
stub_authorization! do |_user|
can :manage, Spree.user_class
Expand Down
6 changes: 6 additions & 0 deletions core/lib/spree/testing_support/factories/user_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
end
end

trait :with_orders do
after(:create) do |user, _|
create(:order, user: user)
end
end

factory :admin_user do
after(:create) do |user, _|
admin_role = Spree::Role.find_by(name: 'admin') || create(:role, name: 'admin')
Expand Down

0 comments on commit 67bd4f3

Please sign in to comment.