Auto-correct rubocop offences for stripe-connect

This commit is contained in:
Rob Harrington
2017-07-07 15:03:22 +10:00
parent c83755f901
commit 42dd58426e
24 changed files with 78 additions and 87 deletions

View File

@@ -39,7 +39,7 @@ module Admin
begin
status = Stripe::Account.retrieve(stripe_account.stripe_user_id)
attrs = [:id, :business_name, :charges_enabled]
attrs = %i[id business_name charges_enabled]
render json: status.to_hash.slice(*attrs).merge( status: :connected)
rescue Stripe::APIError => e
render json: { status: :access_revoked }

View File

@@ -3,14 +3,12 @@ module Admin
before_filter :load_settings, only: [:edit]
def edit
begin
return @stripe_account = { status: :empty_api_key } if Stripe.api_key.blank?
attrs = [:id, :business_name, :charges_enabled]
@obfuscated_secret_key = obfuscated_secret_key
@stripe_account = Stripe::Account.retrieve.to_hash.slice(*attrs).merge(status: :ok)
rescue Stripe::AuthenticationError => e
@stripe_account = { status: :auth_fail }
end
return @stripe_account = { status: :empty_api_key } if Stripe.api_key.blank?
attrs = %i[id business_name charges_enabled]
@obfuscated_secret_key = obfuscated_secret_key
@stripe_account = Stripe::Account.retrieve.to_hash.slice(*attrs).merge(status: :ok)
rescue Stripe::AuthenticationError => e
@stripe_account = { status: :auth_fail }
end
def update

View File

@@ -37,7 +37,7 @@ Spree::Admin::BaseController.class_eval do
redirect_to '/unauthorized'
else
store_location
redirect_to root_path(anchor: "login?after_login=#{ request.env['PATH_INFO'] }")
redirect_to root_path(anchor: "login?after_login=#{request.env['PATH_INFO']}")
end
end

View File

@@ -1,24 +1,22 @@
module Spree
class CreditCardsController < BaseController
before_filter :set_credit_card, only: [:destroy]
before_filter :destroy_at_stripe, only: [:destroy]
def new_from_token
# A new Customer is created for every credit card (same as via ActiveMerchant)
# Note that default_source is the card represented by the token
begin
@customer = create_customer(params[:token])
@credit_card = build_card_from(stored_card_attributes)
if @credit_card.save
render json: @credit_card, serializer: ::Api::CreditCardSerializer, status: :ok
else
message = t(:card_could_not_be_saved)
render json: { flash: { error: I18n.t(:spree_gateway_error_flash_for_checkout, error: message) } }, status: 400
end
rescue Stripe::CardError => e
return render json: { flash: { error: I18n.t(:spree_gateway_error_flash_for_checkout, error: e.message) } }, status: 400
@customer = create_customer(params[:token])
@credit_card = build_card_from(stored_card_attributes)
if @credit_card.save
render json: @credit_card, serializer: ::Api::CreditCardSerializer, status: :ok
else
message = t(:card_could_not_be_saved)
render json: { flash: { error: I18n.t(:spree_gateway_error_flash_for_checkout, error: message) } }, status: 400
end
rescue Stripe::CardError => e
return render json: { flash: { error: I18n.t(:spree_gateway_error_flash_for_checkout, error: e.message) } }, status: 400
end
def destroy
@@ -35,7 +33,8 @@ module Spree
end
end
private
private
def create_customer(token)
Stripe::Customer.create(email: spree_current_user.email, source: token)
end

View File

@@ -8,7 +8,7 @@ module Spree
'American Express' => 'american_express',
'Diners Club' => 'diners_club',
'Visa' => 'visa'
}
}.freeze
def method_type
'stripe'
@@ -61,11 +61,9 @@ module Spree
response = provider.store(creditcard, options)
if response.success?
payment.source.update_attributes!({
cc_type: payment.source.cc_type, # side-effect of update_source!
gateway_customer_profile_id: response.params['id'],
gateway_payment_profile_id: response.params['default_source'] || response.params['default_card']
})
payment.source.update_attributes!( cc_type: payment.source.cc_type, # side-effect of update_source!
gateway_customer_profile_id: response.params['id'],
gateway_payment_profile_id: response.params['default_source'] || response.params['default_card'])
else
payment.send(:gateway_error, response.message)
end
@@ -87,21 +85,21 @@ module Spree
creditcard = token_from_card_profile_ids(creditcard)
return money, creditcard, options
[money, creditcard, options]
end
def address_for(payment)
{}.tap do |options|
if address = payment.order.bill_address
options.merge!(address: {
options[:address] = {
address1: address.address1,
address2: address.address2,
city: address.city,
zip: address.zipcode
})
}
if country = address.country
options[:address].merge!(country: country.name)
options[:address][:country] = country.name
end
if state = address.state
@@ -142,7 +140,7 @@ module Spree
end
def tokenize_instance_customer_card(customer, card)
token = Stripe::Token.create({card: card, customer: customer}, {stripe_account: stripe_account_id})
token = Stripe::Token.create({card: card, customer: customer}, stripe_account: stripe_account_id)
token.id
rescue Stripe::StripeError => e
Rails.logger.error("Stripe Error: #{e}")

View File

@@ -78,10 +78,10 @@ module Spree
# Import from future Spree
def build_source
return if source_attributes.nil?
if payment_method and payment_method.payment_source_class
if payment_method && payment_method.payment_source_class
self.source = payment_method.payment_source_class.new(source_attributes)
self.source.payment_method_id = payment_method.id
self.source.user_id = self.order.user_id if self.order
source.payment_method_id = payment_method.id
source.user_id = order.user_id if order
end
end

View File

@@ -1,7 +1,7 @@
class StripeAccount < ActiveRecord::Base
belongs_to :enterprise
validates_presence_of :stripe_user_id, :stripe_publishable_key
validates_uniqueness_of :enterprise_id
validates :stripe_user_id, :stripe_publishable_key, presence: true
validates :enterprise_id, uniqueness: true
def deauthorize_and_destroy
accounts = StripeAccount.where(stripe_user_id: stripe_user_id)

View File

@@ -1,7 +1,5 @@
class Api::Admin::PaymentMethodSerializer < ActiveModel::Serializer
def serializable_hash
method_serializer.serializable_hash
end
delegate :serializable_hash, to: :method_serializer
def method_serializer
if object.type == 'Spree::Gateway::StripeConnect'

View File

@@ -1,12 +1,11 @@
require 'spec_helper'
describe Admin::StripeAccountsController, type: :controller do
describe "destroy_from_webhook" do
let!(:stripe_account) { create(:stripe_account, stripe_user_id: "webhook_id") }
let(:params) do
{
"format"=> "json",
"format" => "json",
"id" => "evt_123",
"object" => "event",
"data" => { "object" => { "id" => "ca_9B" } },
@@ -49,7 +48,7 @@ describe Admin::StripeAccountsController, type: :controller do
describe "destroy" do
let(:enterprise) { create(:distributor_enterprise) }
let(:params) { { format: :json, id: "some_id" } }
let(:params) { { format: :json, id: "some_id" } }
context "when the specified stripe account doesn't exist" do
it "raises an error?" do
@@ -109,7 +108,7 @@ describe Admin::StripeAccountsController, type: :controller do
before do
Stripe.api_key = "sk_test_12345"
Spree::Config.set({stripe_connect_enabled: false})
Spree::Config.set(stripe_connect_enabled: false)
end
context "where I don't manage the specified enterprise" do
@@ -117,7 +116,7 @@ describe Admin::StripeAccountsController, type: :controller do
let(:enterprise2) { create(:enterprise) }
before do
user.owned_enterprises << enterprise2
params.merge!({enterprise_id: enterprise.id})
params[:enterprise_id] = enterprise.id
allow(controller).to receive(:spree_current_user) { user }
end
@@ -129,7 +128,7 @@ describe Admin::StripeAccountsController, type: :controller do
context "where I manage the specified enterprise" do
before do
params.merge!({enterprise_id: enterprise.id})
params[:enterprise_id] = enterprise.id
allow(controller).to receive(:spree_current_user) { enterprise.owner }
end
@@ -142,7 +141,7 @@ describe Admin::StripeAccountsController, type: :controller do
end
context "and Stripe is enabled" do
before { Spree::Config.set({stripe_connect_enabled: true}) }
before { Spree::Config.set(stripe_connect_enabled: true) }
context "but it has no associated stripe account" do
it "returns with a status of 'account_missing'" do
@@ -168,12 +167,14 @@ describe Admin::StripeAccountsController, type: :controller do
end
context "which is connected" do
let(:stripe_account_mock) { {
id: "acc_123",
business_name: "My Org",
charges_enabled: true,
some_other_attr: "something"
} }
let(:stripe_account_mock) do
{
id: "acc_123",
business_name: "My Org",
charges_enabled: true,
some_other_attr: "something"
}
end
before do
stub_request(:get, "https://api.stripe.com/v1/accounts/acc_123").to_return(body: JSON.generate(stripe_account_mock))

View File

@@ -37,7 +37,7 @@ describe Admin::StripeConnectSettingsController, type: :controller do
context "and the request to retrieve Stripe account info fails" do
before do
stub_request(:get, "https://api.stripe.com/v1/account").
to_return(:status => 401, :body => "{\"error\": {\"message\": \"Invalid API Key provided: sk_test_****xxxx\"}}")
to_return(:status => 401, :body => "{\"error\": {\"message\": \"Invalid API Key provided: sk_test_****xxxx\"}}")
end
it "sets the account status to :auth_fail" do
@@ -50,7 +50,7 @@ describe Admin::StripeConnectSettingsController, type: :controller do
context "and the request to retrieve Stripe account info succeeds" do
before do
stub_request(:get, "https://api.stripe.com/v1/account").
to_return(:status => 200, :body => "{ \"id\": \"acct_1234\", \"business_name\": \"OFN\" }")
to_return(:status => 200, :body => "{ \"id\": \"acct_1234\", \"business_name\": \"OFN\" }")
end
it "sets the account status to :ok, loads settings into Struct" do
@@ -77,7 +77,7 @@ describe Admin::StripeConnectSettingsController, type: :controller do
end
context "as super admin" do
before {allow(controller).to receive(:spree_current_user) { admin } }
before { allow(controller).to receive(:spree_current_user) { admin } }
it "sets global config to the specified values" do
expect(Spree::Config.stripe_connect_enabled).to be true

View File

@@ -26,7 +26,7 @@ describe Spree::CreditCardsController do
describe "#new_from_token" do
context "when the request to store the customer/card with Stripe is successful" do
let(:response_mock) { { status: 200, body: JSON.generate({ id: "cus_AZNMJ", default_source: "card_1AEEb" }) } }
let(:response_mock) { { status: 200, body: JSON.generate(id: "cus_AZNMJ", default_source: "card_1AEEb") } }
it "saves the card locally" do
expect{ post :new_from_token, params }.to change(Spree::CreditCard, :count).by(1)
@@ -54,7 +54,7 @@ describe Spree::CreditCardsController do
end
context "when the request to store the customer/card with Stripe fails" do
let(:response_mock) { { status: 402, body: JSON.generate({ error: { message: "Bup-bow..." }}) } }
let(:response_mock) { { status: 402, body: JSON.generate(error: { message: "Bup-bow..." }) } }
it "doesn't save the card locally, and renders a flash error" do
expect{ post :new_from_token, params }.to_not change(Spree::CreditCard, :count)

View File

@@ -13,14 +13,14 @@ describe Spree::UsersController do
let!(:d1_order_for_u2) { create(:completed_order_with_totals, distributor: distributor1, user_id: u2.id) }
let!(:d1o3) { create(:order, state: 'cart', distributor: distributor1, user_id: u1.id) }
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: u2.id) }
let!(:accounts_distributor) {create :distributor_enterprise}
let!(:accounts_distributor) { create :distributor_enterprise }
let!(:order_account_invoice) { create(:order, distributor: accounts_distributor, state: 'complete', user: u1) }
let(:orders) { assigns(:orders) }
let(:shops) { Enterprise.where(id: orders.pluck(:distributor_id)) }
before do
Spree::Config.set({ accounts_distributor_id: accounts_distributor.id })
Spree::Config.set(accounts_distributor_id: accounts_distributor.id)
allow(controller).to receive(:spree_current_user) { u1 }
end

View File

@@ -368,7 +368,6 @@ FactoryGirl.define do
stripe_user_id "abc123"
stripe_publishable_key "xyz456"
end
end

View File

@@ -40,7 +40,7 @@ feature %q{
let!(:stripe_account_mock) { { id: "acc_connected123", business_name: "My Org", charges_enabled: true } }
before do
Spree::Config.set({stripe_connect_enabled: true})
Spree::Config.set(stripe_connect_enabled: true)
Stripe.api_key = "sk_test_12345"
stub_request(:get, "https://api.stripe.com/v1/accounts/acc_connected123").to_return(body: JSON.generate(stripe_account_mock))
stub_request(:get, "https://api.stripe.com/v1/accounts/acc_revoked123").to_return(status: 404)

View File

@@ -8,5 +8,4 @@ feature "Connecting a Stripe Account" do
before(:each) do
login_to_admin_as enterprise_user
end
end

View File

@@ -10,13 +10,13 @@ feature "Credit Cards", js: true do
quick_login_as user
Stripe.api_key = "sk_test_xxxx"
Spree::Config.set({stripe_connect_enabled: true})
Spree::Config.set(stripe_connect_enabled: true)
stub_request(:get, "https://api.stripe.com/v1/customers/cus_AZNMJ").
to_return(:status => 200, :body => JSON.generate({id: "cus_AZNMJ"}))
to_return(:status => 200, :body => JSON.generate(id: "cus_AZNMJ"))
stub_request(:delete, "https://api.stripe.com/v1/customers/cus_AZNMJ").
to_return(:status => 200, :body => JSON.generate({deleted: true, id: "cus_AZNMJ"}))
to_return(:status => 200, :body => JSON.generate(deleted: true, id: "cus_AZNMJ"))
end
it "lists saved cards, shows interface for adding new cards" do

View File

@@ -157,7 +157,7 @@ feature "As a consumer I want to check out my cart", js: true, retry: 3 do
before do
Stripe.api_key = "sk_test_123456"
Spree::Config.set({stripe_connect_enabled: true})
Spree::Config.set(stripe_connect_enabled: true)
stub_request(:post, "https://sk_test_123456:@api.stripe.com/v1/charges")
.to_return(body: JSON.generate(response_mock))

View File

@@ -225,7 +225,7 @@ describe EnterprisesHelper do
before { allow(helper).to receive(:current_distributor) { distributor } }
context "and Stripe Connect is disabled" do
before { Spree::Config.set({stripe_connect_enabled: false}) }
before { Spree::Config.set(stripe_connect_enabled: false) }
it "ignores the Stripe payment method" do
expect(helper.available_payment_methods.map(&:id)).to_not include pm3.id
@@ -233,7 +233,7 @@ describe EnterprisesHelper do
end
context "and Stripe Connect is enabled" do
before { Spree::Config.set({stripe_connect_enabled: true}) }
before { Spree::Config.set(stripe_connect_enabled: true) }
it "includes the Stripe payment method" do
expect(helper.available_payment_methods.map(&:id)).to include pm3.id

View File

@@ -13,7 +13,7 @@ module Stripe
context "when params have no 'code' key" do
it "raises a StripeError" do
expect{ AccountConnector.new(user, params) }.to raise_error StripeError
expect{ AccountConnector.new(user, params) }.to raise_error StripeError
end
end
@@ -53,7 +53,7 @@ module Stripe
it "allows creations of a new Stripe Account from the callback params" do
connector = AccountConnector.new(user, params)
expect{connector.create_account}.to change(StripeAccount, :count).by(1)
expect{ connector.create_account }.to change(StripeAccount, :count).by(1)
end
end
@@ -67,7 +67,7 @@ module Stripe
it "allows creations of a new Stripe Account from the callback params" do
connector = AccountConnector.new(user, params)
expect{connector.create_account}.to change(StripeAccount, :count).by(1)
expect{ connector.create_account }.to change(StripeAccount, :count).by(1)
end
end
end

View File

@@ -13,7 +13,7 @@ module Stripe
it "builds a url with all of the necessary params" do
url = OAuth.authorize_url(enterprise_id)
uri = URI.parse(url)
params = CGI::parse(uri.query)
params = CGI.parse(uri.query)
expect(params.keys).to include 'client_id', 'response_type', 'state', 'scope'
expect(params["state"]).to eq [OAuth.jwt_encode(enterprise_id: enterprise_id)]
expect(uri.scheme).to eq 'https'

View File

@@ -11,7 +11,7 @@ describe Spree::Gateway::StripeConnect, type: :model do
before do
Stripe.api_key = "sk_test_123456"
subject.stub(:options_for_purchase_or_auth).and_return(['money','cc','opts'])
subject.stub(:options_for_purchase_or_auth).and_return(['money', 'cc', 'opts'])
subject.stub(:provider).and_return provider
end
@@ -59,8 +59,8 @@ describe Spree::Gateway::StripeConnect, type: :model do
before do
stub_request(:post, "https://api.stripe.com/v1/tokens")
.with(body: { "card"=>"card123", "customer"=>"customer123"})
.to_return(body: JSON.generate(token_mock))
.with(body: { "card" => "card123", "customer" => "customer123"})
.to_return(body: JSON.generate(token_mock))
end
it "requests a new token for the customer and card from Stripe, and returns the id of the response" do

View File

@@ -10,9 +10,9 @@ describe StripeAccount do
context "when the Stripe API disconnect fails" do
before do
Stripe::OAuth.client
.deauthorize(stripe_account.stripe_user_id)
.stub(:deauthorize_request)
.and_return(nil)
.deauthorize(stripe_account.stripe_user_id)
.stub(:deauthorize_request)
.and_return(nil)
end
it "doesn't destroy the record" do
@@ -24,9 +24,9 @@ describe StripeAccount do
context "when the Stripe API disconnect succeeds" do
before do
Stripe::OAuth.client
.deauthorize(stripe_account.stripe_user_id)
.stub(:deauthorize_request)
.and_return("something truthy")
.deauthorize(stripe_account.stripe_user_id)
.stub(:deauthorize_request)
.and_return("something truthy")
end
it "destroys the record" do

View File

@@ -21,8 +21,8 @@ describe "Submitting Stripe Connect charge requests", type: :request 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")
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

View File

@@ -12,5 +12,4 @@ describe Api::CreditCardSerializer do
it "formats an identifying string with the card number masked" do
expect(serializer.formatted).to eq "Visa x-1111 Exp:12/2013"
end
end