Skip to content

Commit

Permalink
Move VoucherAdjustmentsController to request specs
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug authored and Matt-Yorkley committed Jul 3, 2023
1 parent 64e07e0 commit cae0052
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 87 deletions.
78 changes: 0 additions & 78 deletions spec/controllers/voucher_adjustments_controller_spec.rb

This file was deleted.

71 changes: 62 additions & 9 deletions spec/requests/voucher_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,80 @@

describe VoucherAdjustmentsController, type: :request do
let(:user) { order.user }
let(:address) { create(:address) }
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:order) { create( :order_with_line_items, line_items_count: 1, distributor: distributor) }
let(:order_cycle) { create(:order_cycle, distributors: [distributor]) }
let(:exchange) { order_cycle.exchanges.outgoing.first }
let(:order) do
create(
:order_with_line_items,
line_items_count: 1,
distributor: distributor,
order_cycle: order_cycle,
bill_address: address,
ship_address: address
)
end
let(:shipping_method) { distributor.shipping_methods.first }
let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) }
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }

before do
# Make sure the order is created by the order user, the factory doesn't set ip properly
order.created_by = user
order.save!
order.update!(created_by: user)

order.select_shipping_method shipping_method.id
OrderWorkflow.new(order).advance_to_payment

sign_in user
end

describe "POST voucher_adjustments" do
let(:params) { { order: { voucher_code: voucher.code } } }

it "adds a voucher to the user's current order" do
post "/voucher_adjustments", params: params

expect(response).to be_successful
expect(order.reload.voucher_adjustments.length).to eq(1)
end

context "when voucher doesn't exist" do
let(:params) { { order: { voucher_code: "non_voucher" } } }

it "returns 422 and an error message" do
post "/voucher_adjustments", params: params

expect(response).to be_unprocessable
expect(flash[:error]).to match "Voucher code Not found"
end
end

context "when adding fails" do
it "returns 422 and an error message" do
# Create a non valid adjustment
bad_adjustment = build(:adjustment, label: nil)
allow(voucher).to receive(:create_adjustment).and_return(bad_adjustment)
allow(Voucher).to receive(:find_by).and_return(voucher)

post "/voucher_adjustments", params: params

expect(response).to be_unprocessable
expect(flash[:error]).to match(
"There was an error while adding the voucher and Label can't be blank"
)
end
end
end

describe "DELETE voucher_adjustments/:id" do
let!(:adjustment) { voucher.create_adjustment(voucher.code, order) }

it "deletes the voucher adjustment" do
delete "/voucher_adjustments/#{adjustment.id}"

expect(order.voucher_adjustments.length).to eq(0)
expect(order.voucher_adjustments.reload.length).to eq(0)
end

it "render a succesful response" do
it "render a success response" do
delete "/voucher_adjustments/#{adjustment.id}"

expect(response).to be_successful
Expand All @@ -34,10 +87,10 @@
it "does nothing" do
delete "/voucher_adjustments/-1"

expect(order.voucher_adjustments.length).to eq(1)
expect(order.voucher_adjustments.reload.length).to eq(1)
end

it "render a succesful response" do
it "render a success response" do
delete "/voucher_adjustments/-1"

expect(response).to be_successful
Expand Down

0 comments on commit cae0052

Please sign in to comment.