Skip to content

Commit 61a70e8

Browse files
forkataadammathysAlistairNormanNoah-Silvera
committed
Copy spec from solidus_paypal_commerce_platform
None of the paypal controllers in this extension had tests, which is causing the code coverage to be under the solidus required limit. This should get us to the minimum 89% code coverage to get the build green. Co-authored-by: Adam Mueller <adam@super.gd> Co-authored-by: Alistair Norman <alistair@super.gd> Co-authored-by: Noah Silvera <noah@super.gd>
1 parent 84789af commit 61a70e8

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

storefront/templates/spec/solidus_storefront_spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
require "spree/testing_support/order_walkthrough"
1919
require "spree/testing_support/translations"
2020

21+
require "solidus_paypal_commerce_platform/testing_support/factories"
22+
2123
# Define the namespace for the helpers.
2224
module SolidusStorefront; end
2325

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# frozen_string_literal: true
2+
3+
require "solidus_storefront_spec_helper"
4+
5+
RSpec.describe "Checkout" do
6+
def js_sdk_script_query
7+
URI(page.find('script[src*="sdk/js?"]', visible: false)[:src]).query.split('&')
8+
end
9+
10+
def js_sdk_script_partner_id
11+
page.find('script[src*="sdk/js?"]', visible: false)['data-partner-attribution-id']
12+
end
13+
14+
describe "paypal payment method" do
15+
let(:order) { Spree::TestingSupport::OrderWalkthrough.up_to(:payment) }
16+
let(:paypal_payment_method) { create(:paypal_payment_method) }
17+
let(:failed_response) { double('response', status_code: 500) } # rubocop:disable RSpec/VerifiedDoubles
18+
19+
before do
20+
user = create(:user)
21+
visit '/'
22+
click_link 'Login'
23+
fill_in 'spree_user[email]', with: user.email
24+
fill_in 'spree_user[password]', with: 'secret'
25+
click_button 'Login'
26+
order.user = user
27+
order.recalculate
28+
29+
paypal_payment_method
30+
allow_any_instance_of(CheckoutsController).to receive_messages(
31+
current_order: order, try_spree_current_user: user
32+
)
33+
end
34+
35+
context "when generating a script tag" do
36+
it "generates a url with the correct credentials attached" do
37+
visit '/checkout/payment'
38+
expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}")
39+
end
40+
41+
it "generates a partner_id attribute with the correct partner code attached" do
42+
visit '/checkout/payment'
43+
expect(js_sdk_script_partner_id).to eq("Solidus_PCP_SP")
44+
end
45+
46+
it "generates a URL with the correct currency" do
47+
allow(order).to receive(:currency).and_return "EUR"
48+
visit '/checkout/payment'
49+
expect(js_sdk_script_query).to include("currency=EUR")
50+
end
51+
52+
context "when auto-capture is set to true" do
53+
it "generates a url with intent capture" do
54+
paypal_payment_method.update(auto_capture: true)
55+
visit '/checkout/payment'
56+
expect(js_sdk_script_query).to include("client-id=#{paypal_payment_method.preferences[:client_id]}")
57+
expect(js_sdk_script_query).to include("intent=capture")
58+
end
59+
end
60+
end
61+
62+
context "when no payment has been made" do
63+
it "fails to process" do
64+
visit '/checkout/payment'
65+
choose(option: paypal_payment_method.id)
66+
click_button("Save and Continue")
67+
expect(page).to have_content("Payments source PayPal order can't be blank")
68+
end
69+
end
70+
71+
context "when a payment has been made" do
72+
it "proceeds to the next step" do
73+
visit '/checkout/payment'
74+
choose(option: paypal_payment_method.id)
75+
find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8)
76+
click_button("Save and Continue")
77+
expect(page).to have_css(".current", text: "Confirm")
78+
end
79+
80+
it "records the paypal email address" do
81+
visit '/checkout/payment'
82+
choose(option: paypal_payment_method.id)
83+
find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8)
84+
find(:xpath, "//input[@id='payments_source_paypal_email']", visible: false).set "fake@email.com"
85+
click_button("Save and Continue")
86+
expect(Spree::Payment.last.source.paypal_email).to eq "fake@email.com"
87+
end
88+
89+
it "records the paypal funding source" do
90+
visit '/checkout/payment'
91+
choose(option: paypal_payment_method.id)
92+
find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8)
93+
find(:xpath, "//input[@id='payments_source_paypal_email']", visible: false).set "fake@email.com"
94+
find(:xpath, "//input[@id='payments_source_paypal_funding_source']", visible: false).set "venmo"
95+
click_button("Save and Continue")
96+
expect(Spree::Payment.last.source.paypal_funding_source).to eq "venmo"
97+
end
98+
end
99+
100+
context "when a payment fails" do
101+
before { allow_any_instance_of(PayPal::PayPalHttpClient).to receive(:execute) { failed_response } }
102+
103+
it "redirects the user back to the payments page" do
104+
visit '/checkout/payment'
105+
choose(option: paypal_payment_method.id)
106+
find(:xpath, "//input[@id='payments_source_paypal_order_id']", visible: false).set SecureRandom.hex(8)
107+
click_button("Save and Continue")
108+
click_button("Place Order")
109+
expect(page).to have_current_path("/checkout/payment")
110+
expect(page).to have_content("Your payment was declined")
111+
end
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)