2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'stripe'
|
|
|
|
require 'stripe_mock'
|
|
|
|
|
|
|
|
describe StripeAccount do
|
|
|
|
let(:stripe_helper) { StripeMock.create_test_helper }
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) { StripeMock.start }
|
|
|
|
after(:each) { StripeMock.stop }
|
2020-04-16 20:50:03 +00:00
|
|
|
let(:nonprofit) { force_create(:nm_justice) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
describe '.find_or_create' do
|
|
|
|
describe 'param validation' do
|
|
|
|
it 'basic param validation' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { StripeAccount.find_or_create(nil) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :nonprofit_id, name: :required },
|
|
|
|
{ key: :nonprofit_id, name: :is_integer }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'validate np' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { StripeAccount.find_or_create(5555) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :nonprofit_id }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
# basically the same as running create
|
|
|
|
describe 'creates new Stripe Account if none is set exists' do
|
2019-07-30 21:29:24 +00:00
|
|
|
let!(:result) { StripeAccount.find_or_create(nonprofit.id) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'returns a Stripe acct id' do
|
|
|
|
expect(result).to_not be_blank
|
|
|
|
end
|
|
|
|
it 'sets the Account values on Stripe' do
|
|
|
|
saved_account = Stripe::Account.retrieve(result)
|
|
|
|
expect(saved_account['managed']).to eq true
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(saved_account['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['email']).to eq nonprofit.email
|
|
|
|
expect(saved_account['business_url']).to eq nonprofit.website
|
|
|
|
expect(saved_account['legal_entity']['type']).to eq 'company'
|
|
|
|
expect(saved_account['legal_entity']['address']['city']).to eq nonprofit.city
|
|
|
|
expect(saved_account['legal_entity']['address']['state']).to eq nonprofit.state_code
|
|
|
|
expect(saved_account['legal_entity']['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['product_description']).to eq 'Nonprofit donations'
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(saved_account['transfer_schedule']['interval']).to eq('manual')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the nonprofit itself' do
|
|
|
|
np = Nonprofit.find(nonprofit.id)
|
|
|
|
expect(np.stripe_account_id).to eq result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'get stripe account from database' do
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:stripe_acct_id) { 'stripe_account_id' }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let!(:result) do
|
2018-03-25 17:30:42 +00:00
|
|
|
nonprofit.stripe_account_id = stripe_acct_id
|
2019-07-30 21:29:24 +00:00
|
|
|
nonprofit.slug = 'slug'
|
2018-03-25 17:30:42 +00:00
|
|
|
nonprofit.save!
|
|
|
|
nonprofit.reload
|
|
|
|
StripeAccount.find_or_create(nonprofit.id)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'returns the expected id' do
|
|
|
|
expect(result).to eq stripe_acct_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.create' do
|
|
|
|
it 'param validation' do
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { StripeAccount.create(nil) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a ParamValidation::ValidationError
|
2019-07-30 21:29:24 +00:00
|
|
|
expect_validation_errors(error.data, [{ key: :np, name: :required },
|
|
|
|
{ key: :np, name: :is_a }])
|
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'testing with valid nonprofit' do
|
|
|
|
it 'handles Stripe errors properly' do
|
|
|
|
StripeMock.prepare_error(Stripe::StripeError.new, :new_account)
|
2019-07-30 21:29:24 +00:00
|
|
|
expect { StripeAccount.create(nonprofit) }.to(raise_error do |error|
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(error).to be_a Stripe::StripeError
|
|
|
|
expect(nonprofit.stripe_account_id).to be_blank
|
2019-07-30 21:29:24 +00:00
|
|
|
end)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
2018-04-02 18:42:34 +00:00
|
|
|
describe 'saves properly without org email' do
|
2019-07-30 21:29:24 +00:00
|
|
|
let!(:result) { StripeAccount.create(nonprofit) }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
it 'returns a Stripe acct id' do
|
|
|
|
expect(result).to_not be_blank
|
|
|
|
end
|
|
|
|
it 'sets the Account values on Stripe' do
|
|
|
|
saved_account = Stripe::Account.retrieve(result)
|
|
|
|
expect(saved_account['managed']).to eq true
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(saved_account['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['email']).to eq nonprofit.email
|
|
|
|
expect(saved_account['business_url']).to eq nonprofit.website
|
|
|
|
expect(saved_account['legal_entity']['type']).to eq 'company'
|
|
|
|
expect(saved_account['legal_entity']['address']['city']).to eq nonprofit.city
|
|
|
|
expect(saved_account['legal_entity']['address']['state']).to eq nonprofit.state_code
|
|
|
|
expect(saved_account['legal_entity']['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['product_description']).to eq 'Nonprofit donations'
|
2018-03-25 17:30:42 +00:00
|
|
|
expect(saved_account['transfer_schedule']['interval']).to eq('manual')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the nonprofit itself' do
|
|
|
|
np = Nonprofit.find(nonprofit.id)
|
|
|
|
expect(np.stripe_account_id).to eq result
|
|
|
|
end
|
|
|
|
end
|
2018-04-02 18:42:34 +00:00
|
|
|
|
|
|
|
describe 'saves properly without org email' do
|
2019-07-30 21:29:24 +00:00
|
|
|
before(:each) do
|
2018-04-02 18:42:34 +00:00
|
|
|
nonprofit.email = nil
|
|
|
|
nonprofit.save!
|
|
|
|
|
|
|
|
role
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-04-02 18:42:34 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:admin_role_email) { 'email_user@email.email' }
|
|
|
|
let(:user) { force_create(:user, email: admin_role_email) }
|
|
|
|
let(:role) { force_create(:role, user: user, host: nonprofit, name: :nonprofit_admin) }
|
2018-04-02 18:42:34 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
let(:result) { StripeAccount.create(nonprofit) }
|
2018-04-02 18:42:34 +00:00
|
|
|
|
|
|
|
it 'returns a Stripe acct id' do
|
|
|
|
expect(result).to_not be_blank
|
|
|
|
end
|
|
|
|
it 'sets the Account values on Stripe' do
|
|
|
|
saved_account = Stripe::Account.retrieve(result)
|
|
|
|
expect(saved_account['managed']).to eq true
|
2019-07-30 21:29:24 +00:00
|
|
|
expect(saved_account['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['email']).to eq admin_role_email
|
|
|
|
expect(saved_account['business_url']).to eq nonprofit.website
|
|
|
|
expect(saved_account['legal_entity']['type']).to eq 'company'
|
|
|
|
expect(saved_account['legal_entity']['address']['city']).to eq nonprofit.city
|
|
|
|
expect(saved_account['legal_entity']['address']['state']).to eq nonprofit.state_code
|
|
|
|
expect(saved_account['legal_entity']['business_name']).to eq nonprofit.name
|
|
|
|
expect(saved_account['product_description']).to eq 'Nonprofit donations'
|
2018-04-02 18:42:34 +00:00
|
|
|
expect(saved_account['transfer_schedule']['interval']).to eq('manual')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the nonprofit itself' do
|
|
|
|
result
|
|
|
|
np = Nonprofit.find(nonprofit.id)
|
|
|
|
expect(np.stripe_account_id).to eq result
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|