balanced 0.3.11 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,91 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::BankAccount do
4
+ use_vcr_cassette
5
+
6
+ before do
7
+ api_key = Balanced::ApiKey.new.save
8
+ Balanced.configure api_key.secret
9
+ @marketplace = Balanced::Marketplace.new.save
10
+ card = Balanced::Card.new(
11
+ :card_number => "5105105105105100",
12
+ :expiration_month => "12",
13
+ :expiration_year => "2015"
14
+ ).save
15
+
16
+ # An initial balance for the marketplace
17
+ @buyer = @marketplace.create_buyer(
18
+ :email_address => 'buyer@example.org',
19
+ :card_uri => card.uri
20
+ )
21
+ @buyer.debit :amount => 10000000
22
+
23
+ @incomplete_bank_account_hash = {
24
+ :account_number => "0987654321",
25
+ :bank_code => "321174851",
26
+ :name => "Timmy T. McTimmerson"
27
+ }
28
+ end
29
+
30
+ describe 'when exception is thrown' do
31
+ use_vcr_cassette
32
+
33
+ it 'should not create without a type field' do
34
+ lambda { Balanced::BankAccount.new(
35
+ :account_number => "0987654321",
36
+ :bank_code => "321174851",
37
+ :name => "Timmy T. McTimmerson"
38
+ ).save }.should raise_error(Balanced::BadRequest)
39
+ end
40
+ end
41
+
42
+ describe 'create' do
43
+ use_vcr_cassette
44
+
45
+ before do
46
+ @bank_account = Balanced::BankAccount.new(
47
+ :account_number => "0987654321",
48
+ :bank_code => "321174851",
49
+ :name => "Timmy T. McTimmerson",
50
+ :type => "checking"
51
+ ).save
52
+ end
53
+
54
+ describe 'account' do
55
+ use_vcr_cassette
56
+ subject { @bank_account.account }
57
+ it { should be_nil }
58
+ end
59
+
60
+ describe 'account_number' do
61
+ use_vcr_cassette
62
+ subject { @bank_account.account_number }
63
+ it { should end_with '4321' }
64
+ end
65
+
66
+ describe 'fingerprint' do
67
+ use_vcr_cassette
68
+ subject { @bank_account.fingerprint }
69
+ it { should have_at_least(20).characters }
70
+ end
71
+
72
+ describe 'credit' do
73
+ use_vcr_cassette
74
+
75
+ before do
76
+ @credit = @bank_account.credit(
77
+ :amount => 50,
78
+ :description => 'Blah'
79
+ )
80
+ end
81
+
82
+ describe 'bank_account' do
83
+ use_vcr_cassette
84
+
85
+ subject { @credit.bank_account }
86
+ its(:account_number) {should end_with '4321'}
87
+ its(:routing_number) {should eql '321174851'}
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,60 @@
1
+ require "spec_helper"
2
+
3
+ describe Balanced::Credit do
4
+
5
+ use_vcr_cassette
6
+
7
+ before do
8
+ api_key = Balanced::ApiKey.new.save
9
+ Balanced.configure api_key.secret
10
+ @marketplace = Balanced::Marketplace.new.save
11
+ card = Balanced::Card.new(
12
+ :card_number => "5105105105105100",
13
+ :expiration_month => "12",
14
+ :expiration_year => "2015"
15
+ ).save
16
+
17
+ # An initial balance for the marketplace
18
+ @buyer = @marketplace.create_buyer(
19
+ :email_address => 'buyer@example.org',
20
+ :card_uri => card.uri
21
+ )
22
+ @buyer.debit :amount => 10000000
23
+ end
24
+
25
+ describe "#create" do
26
+ use_vcr_cassette
27
+
28
+ before do
29
+ @credit = Balanced::Credit.new(
30
+ :amount => 5000,
31
+ :description => "A sweet ride",
32
+ :bank_account => {
33
+ :account_number => "0987654321",
34
+ :bank_code => "321174851",
35
+ :name => "Timmy T. McTimmerson",
36
+ :type => "savings"
37
+ }
38
+ ).save
39
+ end
40
+
41
+ describe 'amount' do
42
+ use_vcr_cassette
43
+ subject { @credit.amount }
44
+ it { should == 5000 }
45
+ end
46
+
47
+ describe 'account' do
48
+ use_vcr_cassette
49
+ subject { @credit.account }
50
+ it { should be_nil }
51
+ end
52
+
53
+ describe 'bank_account' do
54
+ use_vcr_cassette
55
+ subject { @credit.bank_account }
56
+ its(:account_number) { should end_with '4321' }
57
+ end
58
+
59
+ end
60
+ end
@@ -11,14 +11,17 @@ describe Balanced::Hold do
11
11
  :expiration_month => "12",
12
12
  :expiration_year => "2015"
13
13
  ).save
14
- @buyer = @marketplace.create_buyer("buyer@example.org", card.uri)
14
+ @buyer = @marketplace.create_buyer(
15
+ :email_address => 'buyer@example.org',
16
+ :card_uri => card.uri,
17
+ )
15
18
  end
16
19
 
17
20
  describe "#void" do
18
21
  use_vcr_cassette
19
22
 
20
23
  before do
21
- @hold = @buyer.hold 100
24
+ @hold = @buyer.hold :amount => 100
22
25
  end
23
26
 
24
27
  describe 'before' do
@@ -38,7 +41,7 @@ describe Balanced::Hold do
38
41
  describe 'when exception is thrown' do
39
42
  use_vcr_cassette
40
43
  before do
41
- @hold = @buyer.hold 150
44
+ @hold = @buyer.hold :amount => 150
42
45
  @debit = @hold.capture
43
46
  end
44
47
 
@@ -46,8 +49,6 @@ describe Balanced::Hold do
46
49
  lambda { @hold.void }.should raise_error(Balanced::Conflict)
47
50
  @hold.is_void.should be_false
48
51
  end
49
- # void here.
50
-
51
52
 
52
53
  end
53
54
 
@@ -3,18 +3,20 @@ require "spec_helper"
3
3
 
4
4
  describe Balanced::Marketplace do
5
5
  use_vcr_cassette
6
+
6
7
  before do
7
8
  api_key = Balanced::ApiKey.new.save
8
9
  Balanced.configure api_key.secret
9
10
  @marketplace = Balanced::Marketplace.new.save
10
- @bank_account = Balanced::BankAccount.new(
11
+ @bank_account = @marketplace.create_bank_account(
11
12
  :account_number => "1234567890",
12
13
  :bank_code => "321174851",
13
14
  :name => "Jack Q Merchant"
14
- ).save
15
+ )
15
16
  @merchant = @marketplace.create_merchant(
16
- "merchant@example.org",
17
- {
17
+ :name =>"Jack Q Merchant",
18
+ :email_address => "merchant@example.org",
19
+ :merchant => {
18
20
  :type => "person",
19
21
  :name => "Billy Jones",
20
22
  :street_address => "801 High St.",
@@ -23,11 +25,64 @@ describe Balanced::Marketplace do
23
25
  :dob => "1842-01",
24
26
  :phone_number => "+16505551234",
25
27
  },
26
- @bank_account.uri,
27
- "Jack Q Merchant",
28
+ :bank_account_uri => @bank_account.uri,
28
29
  )
29
30
  end
30
31
 
32
+ describe "create_bank_account" do
33
+ use_vcr_cassette
34
+
35
+ it "can create a bank account using the option hash" do
36
+ bank_account = @marketplace.create_bank_account(
37
+ :name => "Jon Q.",
38
+ :account_number => "11111111111",
39
+ :bank_code => "123456789",
40
+ )
41
+ bank_account.should be_instance_of Balanced::BankAccount
42
+ Balanced.is_collection(bank_account.uri).should be_false
43
+ end
44
+
45
+ end
46
+
47
+ describe "create_account" do
48
+ use_vcr_cassette :new_episodes
49
+
50
+ before do
51
+ @account = @marketplace.create_account
52
+ @account_with_attributes = @marketplace.create_account(
53
+ :name => "Jon Q. Timmy",
54
+ :email_address => "bog@example.com"
55
+ )
56
+ end
57
+
58
+ it "creates an account without any roles" do
59
+ @account.should be_instance_of Balanced::Account
60
+ Balanced.is_collection(@account.uri).should be_false
61
+ @account.uri.should match ACCOUNTS_URI_REGEX
62
+ @account.roles.size.should eql 0
63
+ end
64
+
65
+ it "creates an account with some options" do
66
+ @account_with_attributes.name.should == "Jon Q. Timmy"
67
+ @account_with_attributes.email_address.should == "bog@example.com"
68
+ end
69
+
70
+ end
71
+
72
+ describe "create_card" do
73
+ use_vcr_cassette
74
+
75
+ it "can create a card" do
76
+ card = @marketplace.create_card(
77
+ :card_number => "4111111111111111",
78
+ :expiration_month => "12",
79
+ :expiration_year => "2018",
80
+ )
81
+ card.should be_instance_of Balanced::Card
82
+ Balanced.is_collection(card.uri).should be_false
83
+ end
84
+ end
85
+
31
86
  describe "create_merchant" do
32
87
  use_vcr_cassette
33
88
 
@@ -95,3 +150,57 @@ describe Balanced::Marketplace do
95
150
  end
96
151
  end
97
152
 
153
+ describe Balanced::Marketplace, '.marketplace_uri' do
154
+ context 'when invoking .my_marketplace' do
155
+ use_vcr_cassette
156
+
157
+ it 'sets the marketplace_id after the first call implicitly' do
158
+
159
+ Balanced.configure nil
160
+ Balanced.is_configured_with_api_key?.should be_false
161
+ Balanced::Marketplace.class_variable_set(:@@marketplace_uri, nil)
162
+ Balanced::Marketplace.class_variable_get(:@@marketplace_uri).should be_nil
163
+
164
+ api_key = Balanced::ApiKey.new.save
165
+ Balanced.configure api_key.secret
166
+ marketplace = Balanced::Marketplace.new.save
167
+ # creating the marketplace sets `Balanced::Marketplace.marketplace_uri`,
168
+ # it will never be unset.
169
+ Balanced::Marketplace.class_variable_get(:@@marketplace_uri).should equal marketplace.uri
170
+ old_marketplace_uri = marketplace.uri
171
+
172
+ api_key = Balanced::ApiKey.new.save
173
+ Balanced.configure api_key.secret
174
+ marketplace = Balanced::Marketplace.new.save
175
+ # creating the marketplace sets `Balanced::Marketplace.marketplace_uri`,
176
+ # it will never be unset.
177
+ Balanced::Marketplace.class_variable_get(:@@marketplace_uri).should_not equal old_marketplace_uri
178
+ Balanced::Marketplace.class_variable_get(:@@marketplace_uri).should equal marketplace.uri
179
+ end
180
+ end
181
+
182
+ context 'when creating a Balanced::Marketplace resource' do
183
+ use_vcr_cassette
184
+
185
+ it 'sets the marketplace_uri' do
186
+ api_key = Balanced::ApiKey.new.save
187
+ Balanced.configure api_key.secret
188
+
189
+ res = Balanced::Marketplace.new.save
190
+ Balanced::Marketplace.marketplace_uri.should == res.uri
191
+ Balanced::Marketplace.marketplace_uri.nil?.should be_false
192
+ end
193
+ end
194
+ end
195
+
196
+ describe Balanced::Marketplace, '.marketplace_exists?' do
197
+ it 'returns false when nil' do
198
+ Balanced::Marketplace.stub(:marketplace_uri) { nil }
199
+ Balanced::Marketplace.marketplace_exists?.should == false
200
+ end
201
+
202
+ it 'returns true when not nil' do
203
+ Balanced::Marketplace.stub(:marketplace_uri) { 'some uri' }
204
+ Balanced::Marketplace.marketplace_exists?.should == true
205
+ end
206
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe Balanced::Merchant, '.uri' do
4
+ subject { Balanced::Merchant }
5
+ its(:uri) { should == '/v1/merchants' }
6
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Balanced::Resource, '.uri' do
4
+ use_vcr_cassette
5
+
6
+ describe "before the marketplace is configured" do
7
+ it 'raises an exception' do
8
+ Balanced::Marketplace.stub(:marketplace_uri) { nil }
9
+ expect {
10
+ Balanced::Account.uri
11
+ }.to raise_error(Balanced::StandardError, "Balanced::Account is nested under a marketplace, which is not created or configured.")
12
+ end
13
+ end
14
+
15
+ describe 'when the marketplace is configured' do
16
+ it 'returns the resource uri corresponding to the resource name passed in' do
17
+ Balanced::Marketplace.stub(:marketplace_uri) { '/v1/marketplaces/TEST-MPynogsPWE3xLMnLbEbuM0g' }
18
+ Balanced::Account.uri.should == '/v1/marketplaces/TEST-MPynogsPWE3xLMnLbEbuM0g/accounts'
19
+ end
20
+ end
21
+ end
22
+
23
+ describe Balanced::Resource, 'loading a resource and generating methods from the response body' do
24
+ use_vcr_cassette
25
+
26
+ before do
27
+ make_marketplace
28
+ @account = Balanced::Account.new(email: 'user@example.com', name: 'John Doe').save
29
+ end
30
+
31
+ it 'generates a predicate method' do
32
+ @account.name?.should be_true
33
+ end
34
+
35
+ it 'generates a getter method' do
36
+ @account.name.should == 'John Doe'
37
+ end
38
+
39
+ it 'generates a setter' do
40
+ @account.name = 'Bob Bobberson'
41
+ @account.name.should == 'Bob Bobberson'
42
+ end
43
+ end
@@ -8,6 +8,7 @@ describe Balanced::Transaction do
8
8
  api_key = Balanced::ApiKey.new.save
9
9
  Balanced.configure api_key.secret
10
10
  @marketplace = Balanced::Marketplace.new.save
11
+
11
12
  @merchant_attributes = {
12
13
  :type => "person",
13
14
  :name => "Billy Jones",
@@ -17,11 +18,11 @@ describe Balanced::Transaction do
17
18
  :dob => "1842-01",
18
19
  :phone_number => "+16505551234",
19
20
  }
20
- bank_account = Balanced::BankAccount.new(
21
+ bank_account = @marketplace.create_bank_account(
21
22
  :account_number => "1234567890",
22
23
  :bank_code => "321174851",
23
24
  :name => "Jack Q Merchant"
24
- ).save
25
+ )
25
26
  card = Balanced::Card.new(
26
27
  :card_number => "4111111111111111",
27
28
  :expiration_month => "1",
@@ -38,10 +39,12 @@ describe Balanced::Transaction do
38
39
  :card_uri => card.uri,
39
40
  :name => "Jack Q Buyer"
40
41
  ).save
42
+
41
43
  1.upto 5 do |n|
42
- @buyer.debit(1000, :description => "Transaction ##{n}")
43
- @merchant.credit(500, :description => "Credit from Debit ##{n}")
44
+ @buyer.debit(:amount => 1000, :description => "Transaction ##{n}")
45
+ @merchant.credit(:amount => 500, :description => "Credit from Debit ##{n}")
44
46
  end
47
+
45
48
  end
46
49
 
47
50
  describe "Transaction" do
@@ -69,4 +72,4 @@ describe Balanced::Transaction do
69
72
  end
70
73
 
71
74
  end
72
- end
75
+ end
@@ -32,7 +32,8 @@ describe Balanced do
32
32
 
33
33
  describe "#url" do
34
34
  subject { Balanced.client.url.to_s }
35
- it { should eql "https://api.balancedpayments.com" }
35
+ it { should satisfy {|s|
36
+ ["https://api.balancedpayments.com", "http://localhost:5000"].include?(s) } }
36
37
  end
37
38
  end
38
39
 
@@ -75,3 +76,21 @@ describe Balanced do
75
76
  end
76
77
  end
77
78
  end
79
+
80
+ describe Balanced, '.from_uri' do
81
+ it 'returns BankAccount for a bank_accounts collection nested under a marketplace' do
82
+ Balanced.from_uri("/v1/marketplaces/123/bank_accounts").should == Balanced::BankAccount
83
+ end
84
+
85
+ it 'returns BankAccount for an individual bank account nested under a marketplace' do
86
+ Balanced.from_uri('/v1/marketplaces/123/bank_accounts/456').should == Balanced::BankAccount
87
+ end
88
+
89
+ it 'returns BankAccount for a root-level bank account collection resource' do
90
+ Balanced.from_uri('/v1/bank_accounts').should == Balanced::BankAccount
91
+ end
92
+
93
+ it 'returns BankAccount for a root-level bank account individual resource' do
94
+ Balanced.from_uri('/v1/bank_accounts/123').should == Balanced::BankAccount
95
+ end
96
+ end