|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +shared_examples 'External Account API' do |
| 4 | + |
| 5 | + it 'creates/returns a bank when using account.external_accounts.create given a bank token' do |
| 6 | + account = Stripe::Account.create(id: 'test_account', managed: true, country: "US") |
| 7 | + bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) |
| 8 | + bank = account.external_accounts.create(external_account: bank_token) |
| 9 | + |
| 10 | + expect(bank.account).to eq('test_account') |
| 11 | + expect(bank.last4).to eq("1123") |
| 12 | + expect(bank.exp_month).to eq(11) |
| 13 | + expect(bank.exp_year).to eq(2099) |
| 14 | + |
| 15 | + account = Stripe::Account.retrieve('test_account') |
| 16 | + expect(account.external_accounts.count).to eq(1) |
| 17 | + bank = account.external_accounts.first |
| 18 | + expect(bank.account).to eq('test_account') |
| 19 | + expect(bank.last4).to eq("1123") |
| 20 | + expect(bank.exp_month).to eq(11) |
| 21 | + expect(bank.exp_year).to eq(2099) |
| 22 | + end |
| 23 | + |
| 24 | + it 'creates/returns a bank when using account.external_accounts.create given bank params' do |
| 25 | + account = Stripe::Account.create(id: 'test_account', managed: true, country: "US") |
| 26 | + bank = account.external_accounts.create(external_account: { |
| 27 | + object: 'bank_account', |
| 28 | + account_number: '000123456789', |
| 29 | + routing_number: '110000000', |
| 30 | + country: 'US', |
| 31 | + currency: 'usd' |
| 32 | + }) |
| 33 | + |
| 34 | + expect(bank.account).to eq('test_account') |
| 35 | + expect(bank.routing_number).to eq('110000000') |
| 36 | + expect(bank.country).to eq('US') |
| 37 | + expect(bank.currency).to eq('usd') |
| 38 | + |
| 39 | + account = Stripe::Account.retrieve('test_account') |
| 40 | + expect(account.external_accounts.count).to eq(1) |
| 41 | + bank = account.external_accounts.first |
| 42 | + expect(bank.account).to eq('test_account') |
| 43 | + expect(bank.routing_number).to eq('110000000') |
| 44 | + expect(bank.country).to eq('US') |
| 45 | + expect(bank.currency).to eq('usd') |
| 46 | + end |
| 47 | + |
| 48 | + it "creates a single bank with a generated bank token", :live => true do |
| 49 | + account = Stripe::Account.create(managed: true, country: "US") |
| 50 | + expect(account.external_accounts.count).to eq 0 |
| 51 | + |
| 52 | + account.external_accounts.create external_account: stripe_helper.generate_bank_token |
| 53 | + # Yes, stripe-ruby does not actually add the new bank to the account instance |
| 54 | + expect(account.external_accounts.count).to eq 0 |
| 55 | + |
| 56 | + account2 = Stripe::Account.retrieve(account.id) |
| 57 | + expect(account2.external_accounts.count).to eq 1 |
| 58 | + end |
| 59 | + |
| 60 | + describe "retrieval and deletion with accounts" do |
| 61 | + let!(:account) { Stripe::Account.create(id: 'test_account', managed: true, country: "US") } |
| 62 | + let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) } |
| 63 | + let!(:bank) { account.external_accounts.create(external_account: bank_token) } |
| 64 | + |
| 65 | + it "can retrieve all account's banks" do |
| 66 | + retrieved = account.external_accounts.all |
| 67 | + expect(retrieved.count).to eq(1) |
| 68 | + end |
| 69 | + |
| 70 | + it "retrieves an account bank" do |
| 71 | + retrieved = account.external_accounts.retrieve(bank.id) |
| 72 | + expect(retrieved.to_s).to eq(bank.to_s) |
| 73 | + end |
| 74 | + |
| 75 | + it "retrieves an account's bank after re-fetching the account" do |
| 76 | + retrieved = Stripe::Account.retrieve(account.id).external_accounts.retrieve(bank.id) |
| 77 | + expect(retrieved.id).to eq bank.id |
| 78 | + end |
| 79 | + |
| 80 | + it "deletes an accounts bank" do |
| 81 | + bank.delete |
| 82 | + retrieved_acct = Stripe::Account.retrieve(account.id) |
| 83 | + expect(retrieved_acct.external_accounts.data).to be_empty |
| 84 | + end |
| 85 | + |
| 86 | + context "deletion when the user has two external accounts" do |
| 87 | + let!(:bank_token_2) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) } |
| 88 | + let!(:bank_2) { account.external_accounts.create(external_account: bank_token_2) } |
| 89 | + |
| 90 | + it "has just one bank anymore" do |
| 91 | + bank.delete |
| 92 | + retrieved_acct = Stripe::Account.retrieve(account.id) |
| 93 | + expect(retrieved_acct.external_accounts.data.count).to eq 1 |
| 94 | + expect(retrieved_acct.external_accounts.data.first.id).to eq bank_2.id |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + describe "Errors", :live => true do |
| 100 | + it "throws an error when the account does not have the retrieving bank id" do |
| 101 | + account = Stripe::Account.create(managed: true, country: "US") |
| 102 | + bank_id = "bank_123" |
| 103 | + expect { account.external_accounts.retrieve(bank_id) }.to raise_error {|e| |
| 104 | + expect(e).to be_a Stripe::InvalidRequestError |
| 105 | + expect(e.message).to match /no.*source/i |
| 106 | + expect(e.message).to include bank_id |
| 107 | + expect(e.param).to eq 'id' |
| 108 | + expect(e.http_status).to eq 404 |
| 109 | + } |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + context "update bank" do |
| 114 | + let!(:account) { Stripe::Account.create(id: 'test_account', managed: true, country: "US") } |
| 115 | + let!(:bank_token) { stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) } |
| 116 | + let!(:bank) { account.external_accounts.create(external_account: bank_token) } |
| 117 | + |
| 118 | + it "updates the bank" do |
| 119 | + exp_month = 10 |
| 120 | + exp_year = 2098 |
| 121 | + |
| 122 | + bank.exp_month = exp_month |
| 123 | + bank.exp_year = exp_year |
| 124 | + bank.save |
| 125 | + |
| 126 | + retrieved = account.external_accounts.retrieve(bank.id) |
| 127 | + |
| 128 | + expect(retrieved.exp_month).to eq(exp_month) |
| 129 | + expect(retrieved.exp_year).to eq(exp_year) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + context "retrieve multiple banks" do |
| 134 | + |
| 135 | + it "retrieves a list of multiple banks" do |
| 136 | + account = Stripe::Account.create(id: 'test_account', managed: true, country: "US") |
| 137 | + |
| 138 | + bank_token = stripe_helper.generate_bank_token(last4: "1123", exp_month: 11, exp_year: 2099) |
| 139 | + bank1 = account.external_accounts.create(external_accout: bank_token) |
| 140 | + bank_token = stripe_helper.generate_bank_token(last4: "1124", exp_month: 12, exp_year: 2098) |
| 141 | + bank2 = account.external_accounts.create(external_account: bank_token) |
| 142 | + |
| 143 | + account = Stripe::Account.retrieve('test_account') |
| 144 | + |
| 145 | + list = account.external_accounts.all |
| 146 | + |
| 147 | + expect(list.object).to eq("list") |
| 148 | + expect(list.count).to eq(2) |
| 149 | + expect(list.data.length).to eq(2) |
| 150 | + |
| 151 | + expect(list.data.first.object).to eq("bank_account") |
| 152 | + expect(list.data.first.to_hash).to eq(bank1.to_hash) |
| 153 | + |
| 154 | + expect(list.data.last.object).to eq("bank_account") |
| 155 | + expect(list.data.last.to_hash).to eq(bank2.to_hash) |
| 156 | + end |
| 157 | + |
| 158 | + it "retrieves an empty list if there's no subscriptions" do |
| 159 | + Stripe::Account.create(id: 'no_banks', managed: true, country: "US") |
| 160 | + account = Stripe::Account.retrieve('no_banks') |
| 161 | + |
| 162 | + list = account.external_accounts.all |
| 163 | + |
| 164 | + expect(list.object).to eq("list") |
| 165 | + expect(list.count).to eq(0) |
| 166 | + expect(list.data.length).to eq(0) |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | +end |
0 commit comments