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

[BUU] Add missing permission check on product actions #12868

Merged
merged 1 commit into from
Sep 18, 2024
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
4 changes: 4 additions & 0 deletions app/controllers/admin/products_v3_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def destroy
{ id: params[:id] }
).find_product

authorize! :delete, @record

@record.destroyed_by = spree_current_user
status = :ok

Expand Down Expand Up @@ -74,6 +76,8 @@ def destroy_variant

def clone
@product = Spree::Product.find(params[:id])
authorize! :clone, @product

status = :ok

begin
Expand Down
64 changes: 64 additions & 0 deletions spec/requests/admin/products_v3_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe "Admin::ProductsV3" do
include AuthenticationHelper

let(:user) { create(:user) }
let(:headers) { { Accept: "text/vnd.turbo-stream.html" } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it would be nice if the turbo gem gave us shortcuts like it does for controller specs (format: :turbo_stream)

let(:product) { create(:simple_product, supplier_id: create(:supplier_enterprise).id) }

before do
login_as user
end

describe "DELETE /admin/product_v3/:id" do
it "checks for permission" do
delete(admin_product_destroy_path(product), headers: )

expect(response).to redirect_to('/unauthorized')
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that we're not checking that the product wasn't actually deleted.


describe "POST /admin/clone/:id" do
it "checks for permission" do
post(admin_clone_product_path(product), headers: )

expect(response).to redirect_to('/unauthorized')
end
end

describe "DELETE /admin/product_v3/destroy_variant/:id" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I think destroy_variant should have been on a variant controller.. but that's nothing to do with this PR of course.

it "checks for permission" do
delete(admin_destroy_variant_path(product.variants.first), headers: )

expect(response).to redirect_to('/unauthorized')
end
end

describe "POST /admin/products/bulk_update" do
it "checks for permission" do
variant = product.variants.first

params = {
products: {
'0': {
id: product.id,
name: "Updated product name",
variants_attributes: {
'0': {
id: variant.id,
display_name: "Updated variant display name",
}
}
}
}
}

post(admin_products_bulk_update_path, params:, headers: )

expect(response).to redirect_to('/unauthorized')
end
end
end
Loading