diff --git a/app/controllers/voucher_adjustments_controller.rb b/app/controllers/voucher_adjustments_controller.rb index 857e7c5e6f..8b8928944b 100644 --- a/app/controllers/voucher_adjustments_controller.rb +++ b/app/controllers/voucher_adjustments_controller.rb @@ -90,11 +90,13 @@ class VoucherAdjustmentsController < BaseController voucher_code: voucher_params[:voucher_code], enterprise: @order.distributor ) voucher = vine_voucher_validator.validate + errors = vine_voucher_validator.errors - return nil if vine_voucher_validator.errors[:not_found_voucher].present? + return nil if errors[:not_found_voucher].present? - if vine_voucher_validator.errors.present? - @order.errors.add(:voucher_code, I18n.t('checkout.errors.add_voucher_error')) + if errors.present? + message = errors[:invalid_voucher] || I18n.t('checkout.errors.add_voucher_error') + @order.errors.add(:voucher_code, message) return nil end diff --git a/app/services/vine/voucher_validator_service.rb b/app/services/vine/voucher_validator_service.rb index 57af49718d..259f52e84e 100644 --- a/app/services/vine/voucher_validator_service.rb +++ b/app/services/vine/voucher_validator_service.rb @@ -47,7 +47,7 @@ module Vine end def handle_errors(response) - if response[:status] == 400 + if [400, 409].include?(response[:status]) message = response[:body] && JSON.parse(response[:body]).dig("meta", "message") key = VINE_ERRORS.fetch(message, :invalid_voucher) errors[:invalid_voucher] = I18n.t("vine_voucher_validator_service.errors.#{key}") diff --git a/spec/requests/voucher_adjustments_spec.rb b/spec/requests/voucher_adjustments_spec.rb index 4fcb382f5b..352d20c14f 100644 --- a/spec/requests/voucher_adjustments_spec.rb +++ b/spec/requests/voucher_adjustments_spec.rb @@ -161,7 +161,19 @@ RSpec.describe VoucherAdjustmentsController do post("/voucher_adjustments", params:) expect(response).to be_unprocessable - expect(flash[:error]).to match "There was an error while adding the voucher" + expect(flash[:error]).to match "The voucher is not valid" + end + end + + context "when voucher has expired" do + it "returns 422 and an error message" do + mock_vine_voucher_validator(voucher: nil, + errors: { invalid_voucher: "The voucher has expired" }) + + post("/voucher_adjustments", params:) + + expect(response).to be_unprocessable + expect(flash[:error]).to match "The voucher has expired" end end @@ -219,6 +231,20 @@ RSpec.describe VoucherAdjustmentsController do ) end end + + context "when voucher has expired" do + it "returns 422 and an error message" do + vine_voucher = build(:vine_voucher, code: vine_voucher_code, + enterprise: distributor) + mock_vine_voucher_validator(voucher: vine_voucher, + errors: { invalid_voucher: "The voucher has expired" }) + + post("/voucher_adjustments", params:) + + expect(response).to be_unprocessable + expect(flash[:error]).to match "The voucher has expired" + end + end end end end diff --git a/spec/services/vine/voucher_validator_service_spec.rb b/spec/services/vine/voucher_validator_service_spec.rb index 68cd1f0a03..686d95c09c 100644 --- a/spec/services/vine/voucher_validator_service_spec.rb +++ b/spec/services/vine/voucher_validator_service_spec.rb @@ -448,7 +448,7 @@ RSpec.describe Vine::VoucherValidatorService, feature: :connected_apps do } it "adds a specific error message" do - mock_api_exception(type: Faraday::BadRequestError, status: 400, body: data) + mock_api_exception(type: Faraday::BadRequestError, status: 409, body: data) validate_voucher_service.validate diff --git a/spec/system/consumer/checkout/payment_spec.rb b/spec/system/consumer/checkout/payment_spec.rb index 7daff49b42..81c26225bd 100644 --- a/spec/system/consumer/checkout/payment_spec.rb +++ b/spec/system/consumer/checkout/payment_spec.rb @@ -198,7 +198,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do fill_in "Enter voucher code", with: "KM1891" click_button("Apply") - expect(page).to have_content("There was an error while adding the voucher") + expect(page).to have_content("The voucher is not valid") expect(Vouchers::Vine.find_by(code: "KM1891", enterprise: distributor)).to be_nil end end