mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Add specs for new services
This commit is contained in:
@@ -36,7 +36,7 @@ module Checkout
|
||||
end
|
||||
|
||||
def delete_payment_source_params!
|
||||
params.delete(:payment_source)[
|
||||
@params.delete(:payment_source)[
|
||||
@params[:order][:payments_attributes].first[:payment_method_id].underscore
|
||||
]
|
||||
end
|
||||
@@ -51,12 +51,12 @@ module Checkout
|
||||
existing_card_id = @params[:order].delete(:existing_card_id)
|
||||
return if existing_card_id.blank?
|
||||
|
||||
move_to_payment_attributes(existing_card_id)
|
||||
add_to_payment_attributes(existing_card_id)
|
||||
|
||||
@params[:order][:payments_attributes].first.delete :source_attributes
|
||||
end
|
||||
|
||||
def move_to_payment_attributes(existing_card_id)
|
||||
def add_to_payment_attributes(existing_card_id)
|
||||
credit_card = Spree::CreditCard.find(existing_card_id)
|
||||
if credit_card.try(:user_id).blank? || credit_card.user_id != @current_user.try(:id)
|
||||
raise Spree::Core::GatewayError, I18n.t(:invalid_credit_card)
|
||||
|
||||
@@ -1364,6 +1364,7 @@ en:
|
||||
saving_credit_card: Saving credit card...
|
||||
card_has_been_removed: "Your card has been removed (number: %{number})"
|
||||
card_could_not_be_removed: Sorry, the card could not be removed
|
||||
invalid_credit_card: "Invalid credit card"
|
||||
|
||||
ie_warning_headline: "Your browser is out of date :-("
|
||||
ie_warning_text: "For the best Open Food Network experience, we strongly recommend upgrading your browser:"
|
||||
|
||||
72
spec/services/checkout/form_data_adapter_spec.rb
Normal file
72
spec/services/checkout/form_data_adapter_spec.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Checkout::FormDataAdapter do
|
||||
describe '#order_params' do
|
||||
let(:params) { { order: { order_id: "123" } } }
|
||||
let(:order) { create(:order) }
|
||||
let(:user) { create(:user) }
|
||||
|
||||
let(:adapter) { Checkout::FormDataAdapter.new(params, order, user) }
|
||||
|
||||
it "returns the :order item in the params provided" do
|
||||
order_params = adapter.order_params
|
||||
|
||||
expect(order_params).to eq params[:order]
|
||||
end
|
||||
|
||||
describe "when payment_attributes are provided" do
|
||||
before { params[:order][:payments_attributes] = [{ payment_method_id: "123" }] }
|
||||
|
||||
describe "and source attributes are provided" do
|
||||
let(:source_attributes) { { payment_method_name: "Pay at the farm" } }
|
||||
|
||||
before { params[:payment_source] = { "123" => source_attributes } }
|
||||
|
||||
it "moves payment source attributes to the order payment attributes" do
|
||||
order_params = adapter.order_params
|
||||
|
||||
expect(order_params[:payments_attributes].
|
||||
first[:source_attributes]).to eq source_attributes
|
||||
end
|
||||
end
|
||||
|
||||
describe "and order total is not zero" do
|
||||
before { order.total = "50.0" }
|
||||
|
||||
it "sets the payment attributes amount to the order total" do
|
||||
order_params = adapter.order_params
|
||||
|
||||
expect(order_params[:payments_attributes].first[:amount]).to eq order.total
|
||||
end
|
||||
end
|
||||
|
||||
describe "and existing credit card is provided" do
|
||||
before { params[:order][:existing_card_id] = credit_card.id }
|
||||
|
||||
describe "and credit card is owned by current user" do
|
||||
let(:credit_card) { create(:credit_card, user_id: user.id) }
|
||||
|
||||
before { params[:order][:existing_card_id] = credit_card.id }
|
||||
|
||||
it "adds card details to payment attributes" do
|
||||
order_params = adapter.order_params
|
||||
|
||||
expect(order_params[:payments_attributes].first[:source][:id]).to eq credit_card.id
|
||||
expect(order_params[:payments_attributes].
|
||||
first[:source][:last_digits]).to eq credit_card.last_digits
|
||||
end
|
||||
end
|
||||
|
||||
describe "and credit card is not owned by current user" do
|
||||
let(:credit_card) { create(:credit_card) }
|
||||
|
||||
it "raises exception if credit card provided doesnt belong to the current user" do
|
||||
expect { adapter.order_params }.to raise_error Spree::Core::GatewayError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
47
spec/services/checkout/payment_redirect_spec.rb
Normal file
47
spec/services/checkout/payment_redirect_spec.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Checkout::PaymentRedirect do
|
||||
describe '#order_params' do
|
||||
let(:params) { { order: { order_id: "123" } } }
|
||||
|
||||
let(:redirect) { Checkout::PaymentRedirect.new(params) }
|
||||
|
||||
it "returns nil if payment_attributes are not provided" do
|
||||
expect(redirect.path).to be nil
|
||||
end
|
||||
|
||||
describe "when payment_attributes are provided" do
|
||||
it "raises an error if payment method does not exist" do
|
||||
params[:order][:payments_attributes] = [{ payment_method_id: "123" }]
|
||||
|
||||
expect { redirect.path }.to raise_error ActiveRecord::RecordNotFound
|
||||
end
|
||||
|
||||
describe "when payment method provided exists" do
|
||||
before { params[:order][:payments_attributes] = [{ payment_method_id: payment_method.id }] }
|
||||
|
||||
describe "and the payment method is not a paypal payment method" do
|
||||
let(:payment_method) { create(:payment_method) }
|
||||
|
||||
it "returns nil" do
|
||||
expect(redirect.path).to be nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "and the payment method is a paypal method" do
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:payment_method) do
|
||||
Spree::Gateway::PayPalExpress.create!(name: "PayPalExpress",
|
||||
distributor_ids: [distributor.id])
|
||||
end
|
||||
|
||||
it "returns the redirect path" do
|
||||
expect(redirect.path).to include payment_method.id.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user