Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete response in admin users controller #4415

Merged
merged 1 commit into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -473,6 +473,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