Replace text strings with variables in stripe request spec

This commit is contained in:
Rob Harrington
2017-06-30 13:26:01 +10:00
parent e8636c0cbf
commit 5d77ef52c5

View File

@@ -6,7 +6,7 @@ describe "Submitting Stripe Connect charge requests", type: :request do
let!(:order_cycle) { create(:simple_order_cycle) }
let!(:enterprise) { create(:distributor_enterprise) }
let!(:exchange) { create(:exchange, order_cycle: order_cycle, sender: order_cycle.coordinator, receiver: enterprise, incoming: false, pickup_time: "Monday")}
let!(:exchange) { create(:exchange, order_cycle: order_cycle, sender: order_cycle.coordinator, receiver: enterprise, incoming: false, pickup_time: "Monday") }
let!(:shipping_method) { create(:shipping_method, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0), distributors: [enterprise]) }
let!(:payment_method) { create(:payment_method, type: "Spree::Gateway::StripeConnect", distributors: [enterprise]) }
let!(:stripe_account) { create(:stripe_account, enterprise: enterprise) }
@@ -14,12 +14,17 @@ describe "Submitting Stripe Connect charge requests", type: :request do
let!(:order) { line_item.order }
let(:address) { create(:address) }
let(:token) { "token123" }
let(:params) { { format: :json, order: {
let(:new_token) { "newtoken123" }
let(:card_id) { "card_XyZ456" }
let(:customer_id) { "cus_A123" }
let(:params) do
{ format: :json, order: {
shipping_method_id: shipping_method.id,
payments_attributes: [{payment_method_id: payment_method.id, source_attributes: { gateway_payment_profile_id: token, cc_type: "visa", last_digits: "4242", month: 10, year: 2025 }}],
bill_address_attributes: address.attributes.slice("firstname","lastname","address1","address2","phone","city","zipcode","state_id","country_id"),
ship_address_attributes: address.attributes.slice("firstname","lastname","address1","address2","phone","city","zipcode","state_id","country_id")
} } }
} }
end
before do
Stripe.api_key = "sk_test_123456"
@@ -37,17 +42,17 @@ describe "Submitting Stripe Connect charge requests", type: :request do
# Requests a token from the newly saved card
stub_request(:post, "https://api.stripe.com/v1/tokens")
.with(:body => { card: "card_XyZ456", customer: "cus_A123"})
.to_return(status: 200, body: JSON.generate({id: "newtoken_123"}))
.with(:body => { card: card_id, customer: customer_id})
.to_return(status: 200, body: JSON.generate(id: new_token))
# Charges the card
stub_request(:post, "https://sk_test_123456:@api.stripe.com/v1/charges")
.with(:body => {"amount"=>"1234", "card"=>"newtoken_123", "currency"=>"aud", "description"=>"Spree Order ID: #{order.number}", "payment_user_agent"=>"Stripe/v1 ActiveMerchantBindings/1.63.0"})
.to_return(body: JSON.generate(charge_response_mock))
.with(:body => {"amount" => "1234", "card" => new_token, "currency" => "aud", "description" => "Spree Order ID: #{order.number}", "payment_user_agent" => "Stripe/v1 ActiveMerchantBindings/1.63.0"})
.to_return(status: 200, body: JSON.generate(charge_response_mock))
end
context "and the store and charge requests are accepted" do
let(:store_response_mock) { { id: "cus_A123", default_card: "card_XyZ456", sources: { data: [{id: "1"}] } } }
context "and the store, token and charge requests are successful" do
let(:store_response_mock) { { id: customer_id, default_card: card_id, sources: { data: [{id: "1"}] } } }
let(:charge_response_mock) { { id: "ch_1234", object: "charge", amount: 2000} }
it "should process the payment, and stores the card/customer details" do
@@ -56,8 +61,8 @@ describe "Submitting Stripe Connect charge requests", type: :request do
expect(json_response["path"]).to eq spree.order_path(order)
expect(order.payments.completed.count).to be 1
card = order.payments.completed.first.source
expect(card.gateway_customer_profile_id).to eq "cus_A123"
expect(card.gateway_payment_profile_id).to eq "card_XyZ456"
expect(card.gateway_customer_profile_id).to eq customer_id
expect(card.gateway_payment_profile_id).to eq card_id
end
end
@@ -75,7 +80,7 @@ describe "Submitting Stripe Connect charge requests", type: :request do
end
context "when the charge request returns an error message" do
let(:store_response_mock) { { id: "cus_A123", default_card: "card_XyZ456", sources: { data: [{id: "1"}] } } }
let(:store_response_mock) { { id: customer_id, default_card: card_id, sources: { data: [{id: "1"}] } } }
let(:charge_response_mock) { { error: { message: "Bup-bow..."} } }
it "should not process the payment" do
@@ -89,12 +94,16 @@ describe "Submitting Stripe Connect charge requests", type: :request do
end
context "when an existing card is submitted" do
let(:credit_card) { create(:credit_card,
payment_method_id: payment_method.id,
user_id: order.user_id,
gateway_payment_profile_id: "card_AbC123",
gateway_customer_profile_id: "cus_Z456",
month: 11, year: 2026) }
let(:credit_card) do
create(
:credit_card,
payment_method_id: payment_method.id,
user_id: order.user_id,
gateway_payment_profile_id: card_id,
gateway_customer_profile_id: customer_id,
month: 11, year: 2026
)
end
before do
params[:order][:existing_card] = credit_card.id
@@ -102,12 +111,12 @@ describe "Submitting Stripe Connect charge requests", type: :request do
# Requests a token
stub_request(:post, "https://api.stripe.com/v1/tokens")
.with(:body => {"card"=>"card_AbC123", "customer"=>"cus_Z456"})
.to_return(status: 200, body: JSON.generate({id: "newtoken_123"}), headers: {})
.with(:body => {"card" => card_id, "customer" => customer_id})
.to_return(status: 200, body: JSON.generate(id: new_token), headers: {})
# Charges the card
stub_request(:post, "https://sk_test_123456:@api.stripe.com/v1/charges")
.with(:body => {"amount"=>"1234", "card"=>"newtoken_123", "currency"=>"aud", "description"=>"Spree Order ID: #{order.number}", "payment_user_agent"=>"Stripe/v1 ActiveMerchantBindings/1.63.0"})
.with(:body => {"amount"=>"1234", "card"=> new_token, "currency"=>"aud", "description"=>"Spree Order ID: #{order.number}", "payment_user_agent"=>"Stripe/v1 ActiveMerchantBindings/1.63.0"})
.to_return(body: JSON.generate(charge_response_mock))
end
@@ -120,8 +129,8 @@ describe "Submitting Stripe Connect charge requests", type: :request do
expect(json_response["path"]).to eq spree.order_path(order)
expect(order.payments.completed.count).to be 1
card = order.payments.completed.first.source
expect(card.gateway_customer_profile_id).to eq "cus_Z456"
expect(card.gateway_payment_profile_id).to eq "card_AbC123"
expect(card.gateway_customer_profile_id).to eq customer_id
expect(card.gateway_payment_profile_id).to eq card_id
end
end