Skip to content

Commit

Permalink
Make sure users can set price via api
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxito committed Oct 8, 2013
1 parent 7e17a33 commit 7f80d0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
5 changes: 5 additions & 0 deletions api/app/controllers/spree/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def map_nested_attributes_keys(klass, attributes)
end.with_indifferent_access
end

# users should be able to set price when importing orders via api
def permitted_line_item_attributes
super << [:price]
end

private

def set_content_type
Expand Down
49 changes: 32 additions & 17 deletions api/spec/controllers/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,32 @@ module Spree
render_views

let!(:order) { create(:order) }
let(:variant) { create(:variant) }

let(:attributes) { [:number, :item_total, :display_total, :total,
:state, :adjustment_total,
:user_id, :created_at, :updated_at,
:completed_at, :payment_total, :shipment_state,
:payment_state, :email, :special_instructions,
:total_quantity, :display_item_total] }

let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }

let(:billing_address) { { :firstname => "Tiago", :lastname => "Motta", :address1 => "Av Paulista",
:city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
:country_id => Country.first.id, :state_id => State.first.id} }

let(:shipping_address) { { :firstname => "Tiago", :lastname => "Motta", :address1 => "Av Paulista",
:city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
:country_id => Country.first.id, :state_id => State.first.id} }

let!(:payment_method) { create(:payment_method) }

let(:current_api_user) do
user = Spree.user_class.new(:email => "[email protected]")
user.generate_spree_api_key!
user
end

before do
stub_authentication!
Expand Down Expand Up @@ -68,22 +87,7 @@ module Spree
assert_unauthorized!
end

let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }
let(:billing_address) { { :firstname => "Tiago", :lastname => "Motta", :address1 => "Av Paulista",
:city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
:country_id => Country.first.id, :state_id => State.first.id} }
let(:shipping_address) { { :firstname => "Tiago", :lastname => "Motta", :address1 => "Av Paulista",
:city => "Sao Paulo", :zipcode => "1234567", :phone => "12345678",
:country_id => Country.first.id, :state_id => State.first.id} }
let!(:payment_method) { create(:payment_method) }
let(:current_api_user) do
user = Spree.user_class.new(:email => "[email protected]")
user.generate_spree_api_key!
user
end

it "can create an order" do
variant = create(:variant)
api_post :create, :order => { :line_items => { "0" => { :variant_id => variant.to_param, :quantity => 5 } } }
response.status.should == 201
order = Order.last
Expand All @@ -99,7 +103,6 @@ module Spree

# Regression test for #3404
it "can specify additional parameters for a line item" do
variant = create(:variant)
Order.should_receive(:create!).and_return(order = Spree::Order.new)
order.stub(:associate_user!)
order.stub_chain(:contents, :add).and_return(line_item = double('LineItem'))
Expand All @@ -116,9 +119,21 @@ module Spree
response.status.should == 201
end

it "sets line items price" do
api_post :create, :order => {
:line_items => {
"0" => {
:price => 33.0, :variant_id => variant.to_param, :quantity => 5
}
}
}

expect(response.status).to eq 201
expect(Order.last.line_items.first.price.to_f).to eq 33.0
end

# Regression test for #3404
it "does not update line item needlessly" do
variant = create(:variant)
Order.should_receive(:create!).and_return(order = Spree::Order.new)
order.stub(:associate_user!)
order.stub_chain(:contents, :add).and_return(line_item = double('LineItem'))
Expand Down

0 comments on commit 7f80d0b

Please sign in to comment.