-
Notifications
You must be signed in to change notification settings - Fork 1
feat[Core]: Admin and User Metadata for transactional ressources and users (#5897) #4
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
Changes from all commits
cec3053
419c962
cc5b63c
a6a4998
678750d
777b6fa
0040a2c
b954491
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,16 @@ module ApiHelpers | |
| end | ||
| end | ||
|
|
||
| Spree::Api::Config.metadata_api_parameters.each do |method_name, resource| | ||
| define_method("#{method_name}_attributes") do | ||
| authorized_attributes(resource, "#{method_name}_attributes") | ||
| end | ||
| end | ||
|
|
||
| def authorized_attributes(resource, config_attribute) | ||
| can?(:admin, resource) ? Spree::Api::Config.public_send(config_attribute) + [:admin_metadata] : Spree::Api::Config.public_send(config_attribute) | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to duplicate what already is done in the api base controller (this module is mixed into). Why was this change necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shahmayur001 this one makes sense
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are adding the attribute to the params in API helpers, meanwhile, in the base controller, we are permitting the attribute. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also do not understand why we need this. Why do we need this here if we already set |
||
|
|
||
| def required_fields_for(model) | ||
| required_fields = model._validators.select do |_field, validations| | ||
| validations.any? { |validation| validation.is_a?(ActiveModel::Validations::PresenceValidator) } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ module Spree::Api | |
|
|
||
| it "can learn how to create a new line item" do | ||
| get spree.new_api_order_line_item_path(order) | ||
| expect(json_response["attributes"]).to eq(["quantity", "price", "variant_id"]) | ||
| expect(json_response["attributes"]).to eq(["quantity", "price", "variant_id", "customer_metadata"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the specs should be squashed into the same commit the change has been introduced in the controller
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolved! |
||
| required_attributes = json_response["required_attributes"] | ||
| expect(required_attributes).to include("quantity", "variant_id") | ||
| end | ||
|
|
@@ -95,6 +95,29 @@ module Spree::Api | |
| expect(response.status).to eq(201) | ||
| end | ||
|
|
||
| it "cannot see admin_metadata" do | ||
| post spree.api_order_line_items_path(order), | ||
| params: { | ||
| line_item: { variant_id: product.master.to_param, quantity: 1 }, | ||
| order_token: order.guest_token | ||
| } | ||
|
|
||
| expect(response.status).to eq(201) | ||
| expect(json_response).not_to have_key('admin_metadata') | ||
| end | ||
|
|
||
| it "allows creating line item with customer metadata but not admin metadata" do | ||
| post spree.api_order_line_items_path(order), | ||
| params: { | ||
| line_item: { variant_id: product.master.to_param, | ||
| quantity: 1, | ||
| customer_metadata: { "Company" => "Sample Company" } } | ||
| } | ||
|
|
||
| expect(json_response['customer_metadata']).to eq({ "Company" => "Sample Company" }) | ||
| expect(json_response).not_to have_key('admin_metadata') | ||
| end | ||
|
|
||
| it '#create calls #invalid_resource! if adding a line item fails validation' do | ||
| allow_any_instance_of(Spree::LineItem).to receive(:valid?).and_return(false) | ||
| expect_any_instance_of(Spree::Api::BaseController).to receive(:invalid_resource!).once | ||
|
|
@@ -128,6 +151,22 @@ module Spree::Api | |
| expect(json_response["quantity"]).to eq(101) | ||
| end | ||
|
|
||
| it "can update a line item customer metadata on the order" do | ||
| line_item = order.line_items.first | ||
|
|
||
| put spree.api_order_line_item_path(order, line_item), | ||
| params: { line_item: { quantity: 101, customer_metadata: { "adding_quantity" => "true" } } } | ||
|
|
||
| expect(response.status).to eq(200) | ||
|
|
||
| order.reload | ||
|
|
||
| expect(order.total).to eq(1010) # 10 original due to factory, + 1000 in this test | ||
| expect(json_response).to have_attributes(attributes) | ||
| expect(json_response["quantity"]).to eq(101) | ||
| expect(json_response['customer_metadata']).to eq({ "adding_quantity" => "true" }) | ||
| end | ||
|
|
||
| it "can update a line item's options on the order" do | ||
| without_partial_double_verification do | ||
| expect_any_instance_of(Spree::LineItem).to receive(:some_option=).with("foobar") | ||
|
|
@@ -189,6 +228,34 @@ module Spree::Api | |
| end | ||
| end | ||
|
|
||
| context "as an admin" do | ||
| sign_in_as_admin! | ||
|
|
||
| it "can see admin_metadata" do | ||
| post spree.api_order_line_items_path(order), | ||
| params: { | ||
| line_item: { variant_id: product.master.to_param, quantity: 1 }, | ||
| order_token: order.guest_token | ||
| } | ||
|
|
||
| expect(response.status).to eq(201) | ||
| expect(json_response).to have_key('admin_metadata') | ||
| end | ||
|
|
||
| it "allows creating line item with customer metadata and admin metadata" do | ||
| post spree.api_order_line_items_path(order), | ||
| params: { | ||
| line_item: { variant_id: product.master.to_param, | ||
| quantity: 1, | ||
| customer_metadata: { "Company" => "Sample Company" }, | ||
| admin_metadata: { "discount" => "not_applicable" } } | ||
| } | ||
|
|
||
| expect(json_response['customer_metadata']).to eq({ "Company" => "Sample Company" }) | ||
| expect(json_response['admin_metadata']).to eq({ "discount" => "not_applicable" }) | ||
| end | ||
| end | ||
|
|
||
| context "as just another user" do | ||
| before do | ||
| user = create(:user) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add specs for this and explain why this is necessary in the commit message
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have specs for this https://github.com/gms-electronics/solidus/pull/4/files#diff-2ef02110b947167ef3c10ad87001d8c36b1425578b4126fd5d39dd234e4906b7R132
Basically, This prevents users from updating customer_metadata once order it is completed