Add spec for creating a payment from admin page

This commit is contained in:
Gaetan Craig-Riou
2024-10-28 14:32:06 +11:00
committed by Rachel Arnould
parent 6251814152
commit 724d5a2ca0

View File

@@ -10,6 +10,68 @@ RSpec.describe Spree::Admin::PaymentsController, type: :request do
sign_in create(:admin_user)
end
describe "POST /admin/orders/:order_number/payments.json" do
let(:params) do
{
payment: {
payment_method_id: payment_method.id, amount: order.total
}
}
end
let(:payment_method) do
create(:payment_method, distributors: [order.distributor])
end
it "creates a payment" do
expect {
post("/admin/orders/#{order.number}/payments.json", params:)
}.to change { order.payments.count }.by(1)
end
it "redirect to payments page" do
post("/admin/orders/#{order.number}/payments.json", params:)
expect(response).to redirect_to(spree.admin_order_payments_path(order))
expect(flash[:success]).to eq "Payment has been successfully created!"
end
context "when failing to create payment" do
it "redirects to payments page" do
payment_mock = instance_double(Spree::Payment)
allow(order.payments).to receive(:build).and_return(payment_mock)
allow(payment_mock).to receive(:save).and_return(false)
post("/admin/orders/#{order.number}/payments.json", params:)
expect(response).to redirect_to(spree.admin_order_payments_path(order))
end
end
context "when a getway error happens" do
let(:payment_method) do
create(:stripe_sca_payment_method, distributors: [order.distributor])
end
it "redirect to payments page" do
allow(Spree::Order).to receive(:find_by!).and_return(order)
stripe_sca_payment_authorize =
instance_double(OrderManagement::Order::StripeScaPaymentAuthorize)
allow(OrderManagement::Order::StripeScaPaymentAuthorize).to receive(:new)
.and_return(stripe_sca_payment_authorize)
# Simulate an error
allow(stripe_sca_payment_authorize).to receive(:call!) do
order.errors.add(:base, "authorization_failure")
end
post("/admin/orders/#{order.number}/payments.json", params:)
expect(response).to redirect_to(spree.admin_order_payments_path(order))
expect(flash[:error]).to eq("Authorization Failure")
end
end
end
describe "PUT /admin/orders/:order_number/payments/:id/fire" do
let(:payment) do
create(