From 1c69f2c670e5c352349e3057bf49125cce136bf5 Mon Sep 17 00:00:00 2001 From: Steve Pettitt Date: Sat, 1 Oct 2016 19:34:39 +0100 Subject: [PATCH] Stripe Helper specs --- spec/factories.rb | 6 ++++ spec/helpers/admin/stripe_helper_spec.rb | 37 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 spec/helpers/admin/stripe_helper_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index 1faf730017..6592dc2940 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -362,6 +362,12 @@ FactoryGirl.define do tr.calculator = Spree::Calculator::FlatPercentItemTotal.new(calculable: tr) end end + + factory :stripe_account do + enterprise { FactoryGirl.create :distributor_enterprise } + stripe_user_id "abc123" + stripe_publishable_key "xyz456" + end end diff --git a/spec/helpers/admin/stripe_helper_spec.rb b/spec/helpers/admin/stripe_helper_spec.rb new file mode 100644 index 0000000000..12e2feef7e --- /dev/null +++ b/spec/helpers/admin/stripe_helper_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper' + +describe Admin::StripeHelper do + let!(:enterprise) { create(:enterprise) } + let!(:stripe_account) { create(:stripe_account, enterprise: enterprise) } + it "calls the Stripe API to get a token" do + expect(Admin::StripeHelper.client.auth_code).to receive(:get_token).with("abc",{scope: "read_write"}) + helper.get_stripe_token("abc") + end + + it "calls the Stripe API for authorization, passing the enterprise in the state param" do + expect(Admin::StripeHelper.client.auth_code).to receive(:authorize_url).with({state: {enterprise_id: "enterprise-permalink"}}) + helper.authorize_stripe("enterprise-permalink") + end + + context "Disconnecting an account" do + it "doesn't destroy the database record if the Stripe API disconnect failed" do + disconnection = Admin::StripeHelper.client + .deauthorize(stripe_account.stripe_user_id) + .stub(:deauthorize_request) + .and_return(nil) + + deauthorize_stripe(stripe_account.id) + StripeAccount.last.should eq stripe_account + end + + it "destroys the record if the Stripe API disconnect succeeds" do + disconnection = Admin::StripeHelper.client + .deauthorize(stripe_account.stripe_user_id) + .stub(:deauthorize_request) + .and_return("something truthy") + + deauthorize_stripe(stripe_account.id) + expect(StripeAccount.all).not_to include(stripe_account) + end + end +end