From 74073946e6ab43d88745acd91ce0c958bbfed341 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 30 Jun 2023 16:07:31 +1000 Subject: [PATCH] Move VoucherAdjustmentsController to request specs --- .../voucher_adjustments_controller_spec.rb | 78 ------------------- spec/requests/voucher_adjustments_spec.rb | 71 ++++++++++++++--- 2 files changed, 62 insertions(+), 87 deletions(-) delete mode 100644 spec/controllers/voucher_adjustments_controller_spec.rb diff --git a/spec/controllers/voucher_adjustments_controller_spec.rb b/spec/controllers/voucher_adjustments_controller_spec.rb deleted file mode 100644 index df11d9c06b..0000000000 --- a/spec/controllers/voucher_adjustments_controller_spec.rb +++ /dev/null @@ -1,78 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe VoucherAdjustmentsController, type: :controller do - let(:user) { order.user } - let(:address) { create(:address) } - let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) } - let(:order_cycle) { create(:order_cycle, distributors: [distributor]) } - let(:exchange) { order_cycle.exchanges.outgoing.first } - let(:order) { - create(:order_with_line_items, line_items_count: 1, distributor: distributor, - order_cycle: order_cycle, bill_address: address, - ship_address: address) - } - let(:payment_method) { distributor.payment_methods.first } - let(:shipping_method) { distributor.shipping_methods.first } - - let(:voucher) { create(:voucher, code: 'some_code', enterprise: distributor) } - - before do - exchange.variants << order.line_items.first.variant - order.select_shipping_method shipping_method.id - OrderWorkflow.new(order).advance_to_payment - - allow(controller).to receive(:current_order) { order } - allow(controller).to receive(:spree_current_user) { user } - end - - describe "#create" do - describe "adding a voucher" do - let(:params) { { order: { voucher_code: voucher.code } } } - - it "adds a voucher to the user's current order" do - post :create, params: params - - expect(response.status).to eq(200) - 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 :create, params: params - - expect(response.status).to eq 422 - 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 - adjustment = build(:adjustment, label: nil) - allow(voucher).to receive(:create_adjustment).and_return(adjustment) - allow(Voucher).to receive(:find_by).and_return(voucher) - - post :create, params: params - - expect(response.status).to eq 422 - expect(flash[:error]).to match( - "There was an error while adding the voucher and Label can't be blank" - ) - end - end - end - end - - describe "#destroy" do - it "removes the voucher from the current order" do - put :destroy, params: { id: voucher.id } - - expect(order.reload.voucher_adjustments.count).to eq 0 - expect(response.status).to eq 200 - end - end -end diff --git a/spec/requests/voucher_adjustments_spec.rb b/spec/requests/voucher_adjustments_spec.rb index fc533473b8..8c93fb3f69 100644 --- a/spec/requests/voucher_adjustments_spec.rb +++ b/spec/requests/voucher_adjustments_spec.rb @@ -4,27 +4,80 @@ require 'spec_helper' 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 @@ -34,10 +87,10 @@ describe VoucherAdjustmentsController, type: :request do 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