Enterprise user can only add order cycle exchanges where it manages the participant enterprise

This commit is contained in:
Rohan Mitchell
2014-05-23 12:18:22 +10:00
parent 14dee8d7fe
commit 9e46108e9a
2 changed files with 20 additions and 3 deletions

View File

@@ -58,14 +58,18 @@ module OpenFoodNetwork
def add_exchange(sender_id, receiver_id, incoming, attrs={})
attrs = attrs.reverse_merge(:sender_id => sender_id, :receiver_id => receiver_id, :incoming => incoming)
exchange = @order_cycle.exchanges.create! attrs
@touched_exchanges << exchange
exchange = @order_cycle.exchanges.build attrs
if permission_for exchange
exchange.save!
@touched_exchanges << exchange
end
end
def update_exchange(sender_id, receiver_id, incoming, attrs={})
exchange = @order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id, :incoming => incoming).first
if permission_for(exchange)
if permission_for exchange
exchange.update_attributes!(attrs)
@touched_exchanges << exchange
end

View File

@@ -232,6 +232,19 @@ module OpenFoodNetwork
applicator.send(:touched_exchanges).should == [exchange]
end
it "does not add exchanges it is not permitted to touch" do
sender = FactoryGirl.create(:enterprise)
receiver = FactoryGirl.create(:enterprise)
oc = FactoryGirl.create(:simple_order_cycle)
applicator = OrderCycleFormApplicator.new(oc, [])
incoming = true
expect do
applicator.send(:touched_exchanges=, [])
applicator.send(:add_exchange, sender.id, receiver.id, incoming)
end.to change(Exchange, :count).by(0)
end
it "does not update exchanges it is not permitted to touch" do
sender = FactoryGirl.create(:enterprise)
receiver = FactoryGirl.create(:enterprise)