fake_braintree 0.2.1 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/Gemfile +1 -1
- data/LICENSE +1 -4
- data/NEWS.md +62 -0
- data/README.md +3 -1
- data/Rakefile +4 -4
- data/fake_braintree.gemspec +8 -8
- data/lib/fake_braintree.rb +36 -26
- data/lib/fake_braintree/credit_card.rb +57 -6
- data/lib/fake_braintree/customer.rb +31 -23
- data/lib/fake_braintree/helpers.rb +1 -1
- data/lib/fake_braintree/redirect.rb +17 -5
- data/lib/fake_braintree/registry.rb +2 -6
- data/lib/fake_braintree/sinatra_app.rb +71 -53
- data/lib/fake_braintree/subscription.rb +21 -17
- data/lib/fake_braintree/version.rb +1 -1
- data/spec/fake_braintree/credit_card_spec.rb +72 -20
- data/spec/fake_braintree/customer_spec.rb +86 -50
- data/spec/fake_braintree/registry_spec.rb +6 -7
- data/spec/fake_braintree/subscription_spec.rb +34 -28
- data/spec/fake_braintree/transaction_spec.rb +76 -19
- data/spec/fake_braintree/transparent_redirect_spec.rb +73 -26
- data/spec/fake_braintree_spec.rb +33 -33
- data/spec/spec_helper.rb +1 -1
- data/spec/support/braintree_helpers.rb +1 -1
- data/spec/support/matchers/clear_hash_when_cleared.rb +2 -2
- data/spec/support/matchers/have_accessor_for.rb +1 -1
- metadata +7 -7
- data/Changelog.md +0 -41
@@ -1,127 +1,163 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Braintree::Customer.create' do
|
4
4
|
after { FakeBraintree.verify_all_cards = false }
|
5
5
|
|
6
|
-
it
|
7
|
-
result = Braintree::Customer.create(
|
8
|
-
|
6
|
+
it 'successfully creates a customer' do
|
7
|
+
result = Braintree::Customer.create(
|
8
|
+
:credit_card => {
|
9
|
+
:number => TEST_CC_NUMBER,
|
10
|
+
:expiration_date => '04/2016'
|
11
|
+
}
|
12
|
+
)
|
9
13
|
result.should be_success
|
10
14
|
end
|
11
15
|
|
12
|
-
it
|
13
|
-
result = Braintree::Customer.create(
|
14
|
-
|
16
|
+
it 'associates a created credit card with the customer' do
|
17
|
+
result = Braintree::Customer.create(
|
18
|
+
:credit_card => {
|
19
|
+
:number => TEST_CC_NUMBER,
|
20
|
+
:expiration_date => '04/2016'
|
21
|
+
}
|
22
|
+
)
|
15
23
|
credit_cards = Braintree::Customer.find(result.customer.id).credit_cards
|
16
24
|
credit_cards.size.should == 1
|
17
|
-
credit_cards.first.expiration_date.should ==
|
25
|
+
credit_cards.first.expiration_date.should == '04/2016'
|
18
26
|
end
|
19
27
|
|
20
28
|
it "successfully creates the customer's credit card" do
|
21
|
-
result = Braintree::Customer.create(
|
22
|
-
|
29
|
+
result = Braintree::Customer.create(
|
30
|
+
:credit_card => {
|
31
|
+
:number => TEST_CC_NUMBER,
|
32
|
+
:expiration_date => '04/2016'
|
33
|
+
}
|
34
|
+
)
|
23
35
|
|
24
36
|
cc_token = result.customer.credit_cards.first.token
|
25
37
|
expect { Braintree::CreditCard.find(cc_token) }.not_to raise_error(Braintree::NotFoundError)
|
26
38
|
end
|
27
39
|
|
28
|
-
it "
|
40
|
+
it "sets a default credit card for the customer" do
|
41
|
+
result = Braintree::Customer.create(
|
42
|
+
:credit_card => {
|
43
|
+
:number => TEST_CC_NUMBER,
|
44
|
+
:expiration_date => '04/2016'
|
45
|
+
}
|
46
|
+
)
|
47
|
+
|
48
|
+
credit_cards = Braintree::Customer.find(result.customer.id).credit_cards
|
49
|
+
credit_cards.first.should be_default
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'can handle an empty credit card hash' do
|
29
53
|
result = Braintree::Customer.create(:credit_card => {})
|
30
54
|
result.should be_success
|
31
55
|
end
|
32
56
|
|
33
|
-
it
|
34
|
-
result = Braintree::Customer.create({
|
57
|
+
it 'does not overwrite a passed customer id' do
|
58
|
+
result = Braintree::Customer.create({ 'id' => '123' })
|
35
59
|
|
36
60
|
result.customer.id.should eq('123')
|
37
61
|
end
|
38
62
|
|
39
|
-
it
|
40
|
-
result = Braintree::Customer.create(
|
41
|
-
|
42
|
-
|
63
|
+
it 'creates a customer using an expiration month and year' do
|
64
|
+
result = Braintree::Customer.create(
|
65
|
+
:credit_card => {
|
66
|
+
:number => TEST_CC_NUMBER,
|
67
|
+
:expiration_month => '04',
|
68
|
+
:expiration_year => '2016'
|
69
|
+
}
|
70
|
+
)
|
43
71
|
result.should be_success
|
44
72
|
end
|
45
73
|
|
46
|
-
it
|
74
|
+
it 'records the billing address' do
|
47
75
|
result = create_customer(
|
48
76
|
:billing_address => {
|
49
|
-
:street_address =>
|
50
|
-
:extended_address =>
|
51
|
-
:locality =>
|
52
|
-
:region =>
|
53
|
-
:postal_code =>
|
54
|
-
:country_code_alpha2 =>
|
77
|
+
:street_address => '1 E Main St',
|
78
|
+
:extended_address => 'Suite 3',
|
79
|
+
:locality => 'Chicago',
|
80
|
+
:region => 'Illinois',
|
81
|
+
:postal_code => '60622',
|
82
|
+
:country_code_alpha2 => 'US'
|
55
83
|
}
|
56
84
|
)
|
57
85
|
|
58
86
|
billing_address = result.customer.credit_cards[0].billing_address
|
59
87
|
|
60
|
-
billing_address.street_address.should ==
|
61
|
-
billing_address.postal_code.should ==
|
88
|
+
billing_address.street_address.should == '1 E Main St'
|
89
|
+
billing_address.postal_code.should == '60622'
|
62
90
|
end
|
63
91
|
end
|
64
92
|
|
65
|
-
describe
|
66
|
-
it
|
93
|
+
describe 'Braintree::Customer.create', 'when passed :verify_card => true' do
|
94
|
+
it 'accepts valid cards' do
|
67
95
|
create_customer(:options => { :verify_card => true }).should be_success
|
68
96
|
end
|
69
97
|
|
70
|
-
it
|
98
|
+
it 'rejects invalid cards' do
|
71
99
|
create_customer_with_invalid_card(:options => { :verify_card => true }).should_not be_success
|
72
100
|
end
|
73
101
|
end
|
74
102
|
|
75
|
-
describe
|
103
|
+
describe 'Braintree::Customer.create', 'when FakeBraintree.verify_all_cards == true' do
|
76
104
|
before { FakeBraintree.verify_all_cards! }
|
77
105
|
|
78
|
-
it
|
106
|
+
it 'accepts valid cards' do
|
79
107
|
create_customer.should be_success
|
80
108
|
end
|
81
109
|
|
82
|
-
it
|
110
|
+
it 'rejects invalid cards' do
|
83
111
|
create_customer_with_invalid_card.should_not be_success
|
84
112
|
end
|
85
113
|
end
|
86
114
|
|
87
|
-
describe
|
88
|
-
it
|
89
|
-
result = Braintree::Customer.create(
|
90
|
-
|
115
|
+
describe 'Braintree::Customer.find' do
|
116
|
+
it 'successfully finds a customer' do
|
117
|
+
result = Braintree::Customer.create(
|
118
|
+
:first_name => 'Bob',
|
119
|
+
:last_name => 'Smith'
|
120
|
+
)
|
91
121
|
|
92
|
-
Braintree::Customer.find(result.customer.id).first_name.should ==
|
122
|
+
Braintree::Customer.find(result.customer.id).first_name.should == 'Bob'
|
93
123
|
end
|
94
124
|
|
95
|
-
it
|
96
|
-
lambda { Braintree::Customer.find(
|
125
|
+
it 'raises an error for a nonexistent customer' do
|
126
|
+
lambda { Braintree::Customer.find('foo') }.should raise_error(Braintree::NotFoundError)
|
97
127
|
end
|
98
128
|
end
|
99
129
|
|
100
|
-
describe
|
101
|
-
it
|
130
|
+
describe 'Braintree::Customer.update' do
|
131
|
+
it 'successfully updates a customer' do
|
102
132
|
customer_id = create_customer.customer.id
|
103
|
-
result = Braintree::Customer.update(customer_id, :first_name =>
|
133
|
+
result = Braintree::Customer.update(customer_id, :first_name => 'Jerry')
|
104
134
|
|
105
135
|
result.should be_success
|
106
|
-
Braintree::Customer.find(customer_id).first_name.should ==
|
136
|
+
Braintree::Customer.find(customer_id).first_name.should == 'Jerry'
|
107
137
|
end
|
108
138
|
|
109
|
-
it
|
110
|
-
lambda { Braintree::Customer.update(
|
139
|
+
it 'raises an error for a nonexistent customer' do
|
140
|
+
lambda { Braintree::Customer.update('foo', {:first_name => 'Bob'}) }.should raise_error(Braintree::NotFoundError)
|
111
141
|
end
|
112
142
|
|
113
|
-
it
|
114
|
-
bad_credit_card =
|
143
|
+
it 'does not allow a customer to be updated to a failing credit card' do
|
144
|
+
bad_credit_card = '123456'
|
115
145
|
FakeBraintree.registry.failures[bad_credit_card] = FakeBraintree.failure_response
|
116
146
|
|
117
147
|
customer = create_customer
|
118
|
-
result = Braintree::Customer.update(customer.customer.id,
|
148
|
+
result = Braintree::Customer.update(customer.customer.id,
|
149
|
+
{
|
150
|
+
:credit_card => {
|
151
|
+
:number => bad_credit_card
|
152
|
+
}
|
153
|
+
}
|
154
|
+
)
|
119
155
|
result.should_not be_success
|
120
156
|
end
|
121
157
|
end
|
122
158
|
|
123
|
-
describe
|
124
|
-
it
|
159
|
+
describe 'Braintree::Customer.delete' do
|
160
|
+
it 'successfully deletes a customer' do
|
125
161
|
customer_id = create_customer.customer.id
|
126
162
|
result = Braintree::Customer.delete(customer_id)
|
127
163
|
|
@@ -9,7 +9,7 @@ describe FakeBraintree::Registry do
|
|
9
9
|
it { should have_hash_accessor_for(:credit_cards) }
|
10
10
|
end
|
11
11
|
|
12
|
-
describe FakeBraintree::Registry,
|
12
|
+
describe FakeBraintree::Registry, '#clear!' do
|
13
13
|
it { should clear_hash_when_cleared(:customers) }
|
14
14
|
it { should clear_hash_when_cleared(:subscriptions) }
|
15
15
|
it { should clear_hash_when_cleared(:failures) }
|
@@ -18,15 +18,14 @@ describe FakeBraintree::Registry, "#clear!" do
|
|
18
18
|
it { should clear_hash_when_cleared(:credit_cards) }
|
19
19
|
end
|
20
20
|
|
21
|
-
describe FakeBraintree::Registry,
|
22
|
-
it
|
23
|
-
|
21
|
+
describe FakeBraintree::Registry, '#failure?' do
|
22
|
+
it 'returns false if the given CC number is not marked as a failure' do
|
23
|
+
FakeBraintree::Registry.new.failure?('not-a-failure').should be_false
|
24
24
|
end
|
25
25
|
|
26
|
-
it
|
26
|
+
it 'returns true if the given CC number is marked as a failure' do
|
27
|
+
registry = FakeBraintree::Registry.new
|
27
28
|
registry.failures['abc123'] = 'whatever'
|
28
29
|
registry.failure?('abc123').should be_true
|
29
30
|
end
|
30
|
-
|
31
|
-
let(:registry) { subject }
|
32
31
|
end
|
@@ -1,44 +1,50 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe 'Braintree::Subscription.create' do
|
4
4
|
let(:plan_id) { 'plan-id-from-braintree-control-panel' }
|
5
|
-
let(:expiration_date) {
|
5
|
+
let(:expiration_date) { '04/2016' }
|
6
6
|
|
7
|
-
it
|
8
|
-
Braintree::Subscription.create(
|
9
|
-
|
7
|
+
it 'successfully creates a subscription' do
|
8
|
+
Braintree::Subscription.create(
|
9
|
+
:payment_method_token => cc_token,
|
10
|
+
:plan_id => 'my_plan_id'
|
11
|
+
).should be_success
|
10
12
|
end
|
11
13
|
|
12
|
-
it
|
14
|
+
it 'assigns a Braintree-esque ID to the subscription' do
|
13
15
|
create_subscription.subscription.id.should =~ /^[a-z0-9]{6}$/
|
14
16
|
end
|
15
17
|
|
16
|
-
it
|
18
|
+
it 'assigns unique IDs to each subscription' do
|
17
19
|
cc_token_1 = cc_token
|
18
20
|
cc_token_2 = braintree_credit_card_token(TEST_CC_NUMBER.sub('1', '5'), expiration_date)
|
19
|
-
first_result = Braintree::Subscription.create(
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
first_result = Braintree::Subscription.create(
|
22
|
+
:payment_method_token => cc_token_1,
|
23
|
+
:plan_id => plan_id
|
24
|
+
)
|
25
|
+
second_result = Braintree::Subscription.create(
|
26
|
+
:payment_method_token => cc_token_2,
|
27
|
+
:plan_id => plan_id
|
28
|
+
)
|
23
29
|
|
24
30
|
first_result.subscription.id.should_not == second_result.subscription.id
|
25
31
|
end
|
26
32
|
|
27
|
-
it
|
33
|
+
it 'stores created subscriptions in FakeBraintree.registry.subscriptions' do
|
28
34
|
FakeBraintree.registry.subscriptions[create_subscription.subscription.id].should_not be_nil
|
29
35
|
end
|
30
36
|
|
31
|
-
it
|
37
|
+
it 'sets the next billing date to a string of 1.month.from_now in UTC' do
|
32
38
|
Timecop.freeze do
|
33
39
|
create_subscription.subscription.next_billing_date.should == 1.month.from_now.strftime('%Y-%m-%d')
|
34
40
|
end
|
35
41
|
end
|
36
42
|
end
|
37
43
|
|
38
|
-
describe
|
39
|
-
it
|
44
|
+
describe 'Braintree::Subscription.find' do
|
45
|
+
it 'can find a created subscription' do
|
40
46
|
payment_method_token = cc_token
|
41
|
-
plan_id =
|
47
|
+
plan_id = 'abc123'
|
42
48
|
subscription_id =
|
43
49
|
create_subscription(:payment_method_token => payment_method_token, :plan_id => plan_id).subscription.id
|
44
50
|
subscription = Braintree::Subscription.find(subscription_id)
|
@@ -47,13 +53,13 @@ describe "Braintree::Subscription.find" do
|
|
47
53
|
subscription.plan_id.should == plan_id
|
48
54
|
end
|
49
55
|
|
50
|
-
it
|
56
|
+
it 'raises a Braintree:NotFoundError when it cannot find a subscription' do
|
51
57
|
create_subscription
|
52
58
|
expect { Braintree::Subscription.find('abc123') }.to raise_error(Braintree::NotFoundError, /abc123/)
|
53
59
|
end
|
54
60
|
|
55
|
-
it
|
56
|
-
add_on_id =
|
61
|
+
it 'returns add-ons added with the subscription' do
|
62
|
+
add_on_id = 'def456'
|
57
63
|
subscription_id = create_subscription(:add_ons => { :add => [{ :inherited_from_id => add_on_id }] }).subscription.id
|
58
64
|
subscription = Braintree::Subscription.find(subscription_id)
|
59
65
|
add_ons = subscription.add_ons
|
@@ -61,9 +67,9 @@ describe "Braintree::Subscription.find" do
|
|
61
67
|
add_ons.first.id.should == add_on_id
|
62
68
|
end
|
63
69
|
|
64
|
-
it
|
65
|
-
discount_id =
|
66
|
-
subscription_id = create_subscription(:discounts => { :add => [{ :inherited_from_id => discount_id, :amount => BigDecimal.new(
|
70
|
+
it 'returns discounts added with the subscription' do
|
71
|
+
discount_id = 'def456'
|
72
|
+
subscription_id = create_subscription(:discounts => { :add => [{ :inherited_from_id => discount_id, :amount => BigDecimal.new('15.00') }]}).subscription.id
|
67
73
|
subscription = Braintree::Subscription.find(subscription_id)
|
68
74
|
discounts = subscription.discounts
|
69
75
|
discounts.size.should == 1
|
@@ -71,8 +77,8 @@ describe "Braintree::Subscription.find" do
|
|
71
77
|
end
|
72
78
|
end
|
73
79
|
|
74
|
-
describe
|
75
|
-
it
|
80
|
+
describe 'Braintree::Subscription.update' do
|
81
|
+
it 'can update a subscription' do
|
76
82
|
Braintree::Subscription.update(subscription_id, :plan_id => 'a_new_plan')
|
77
83
|
Braintree::Subscription.find(subscription_id).plan_id.should == 'a_new_plan'
|
78
84
|
end
|
@@ -81,14 +87,14 @@ describe "Braintree::Subscription.update" do
|
|
81
87
|
let(:subscription) { create_subscription }
|
82
88
|
end
|
83
89
|
|
84
|
-
describe
|
85
|
-
it
|
90
|
+
describe 'Braintree::Subscription.cancel' do
|
91
|
+
it 'can cancel a subscription' do
|
86
92
|
Braintree::Subscription.cancel(subscription_id).should be_success
|
87
93
|
Braintree::Subscription.find(subscription_id).status.should == Braintree::Subscription::Status::Canceled
|
88
94
|
end
|
89
95
|
|
90
|
-
it
|
91
|
-
expect { Braintree::Subscription.cancel(
|
96
|
+
it 'cannot cancel an unknown subscription' do
|
97
|
+
expect { Braintree::Subscription.cancel('totally-bogus-id') }.to raise_error(Braintree::NotFoundError)
|
92
98
|
end
|
93
99
|
|
94
100
|
let(:subscription_id) { subscription.subscription.id }
|
@@ -1,55 +1,112 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FakeBraintree::SinatraApp do
|
4
|
-
context
|
5
|
-
it
|
6
|
-
result = Braintree::Transaction.sale(
|
4
|
+
context 'Braintree::Transaction.sale' do
|
5
|
+
it 'successfully creates a transaction' do
|
6
|
+
result = Braintree::Transaction.sale(
|
7
|
+
:payment_method_token => cc_token,
|
8
|
+
:amount => 10.00
|
9
|
+
)
|
7
10
|
result.should be_success
|
8
11
|
end
|
9
12
|
|
10
|
-
context
|
13
|
+
context 'when all cards are declined' do
|
11
14
|
before { FakeBraintree.decline_all_cards! }
|
12
15
|
|
13
|
-
it
|
14
|
-
result = Braintree::Transaction.sale(
|
16
|
+
it 'fails' do
|
17
|
+
result = Braintree::Transaction.sale(
|
18
|
+
:payment_method_token => cc_token,
|
19
|
+
:amount => 10.00
|
20
|
+
)
|
15
21
|
result.should_not be_success
|
16
22
|
end
|
17
23
|
end
|
24
|
+
|
25
|
+
context "when the options hash is nil" do
|
26
|
+
it "returns a transaction with a status of authorized" do
|
27
|
+
result = Braintree::Transaction.sale(:payment_method_token => cc_token, :amount => 10.00)
|
28
|
+
result.transaction.status.should == 'authorized'
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when submit_for_settlement is not true" do
|
33
|
+
it "returns a transaction with a status of authorized" do
|
34
|
+
result = Braintree::Transaction.sale(
|
35
|
+
:payment_method_token => cc_token,
|
36
|
+
:amount => 10.00,
|
37
|
+
:options => {
|
38
|
+
:submit_for_settlement => false
|
39
|
+
}
|
40
|
+
)
|
41
|
+
result.transaction.status.should == 'authorized'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when submit_for_settlement does not exist" do
|
46
|
+
it "returns a transaction with a status of authorized" do
|
47
|
+
result = Braintree::Transaction.sale(
|
48
|
+
:payment_method_token => cc_token,
|
49
|
+
:amount => 10.00,
|
50
|
+
:options => {
|
51
|
+
:add_billing_address_to_payment_method => true
|
52
|
+
}
|
53
|
+
)
|
54
|
+
result.transaction.status.should == 'authorized'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when submit_for_settlement is true" do
|
59
|
+
it "returns a transaction with a status of submitted_for_settlement" do
|
60
|
+
result = Braintree::Transaction.sale(
|
61
|
+
:payment_method_token => cc_token,
|
62
|
+
:amount => 10.00,
|
63
|
+
:options => {
|
64
|
+
:submit_for_settlement => true
|
65
|
+
}
|
66
|
+
)
|
67
|
+
result.transaction.status.should == 'submitted_for_settlement'
|
68
|
+
end
|
69
|
+
end
|
18
70
|
end
|
19
71
|
end
|
20
72
|
|
21
73
|
describe FakeBraintree::SinatraApp do
|
22
|
-
context
|
23
|
-
it
|
74
|
+
context 'Braintree::Transaction.refund' do
|
75
|
+
it 'successfully refunds a transaction' do
|
24
76
|
result = Braintree::Transaction.refund(create_id('foobar'), '1')
|
25
77
|
result.should be_success
|
26
78
|
end
|
79
|
+
end
|
80
|
+
end
|
27
81
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
82
|
+
describe FakeBraintree::SinatraApp do
|
83
|
+
context 'Braintree::Transaction.void' do
|
84
|
+
it 'successfully voids a transaction' do
|
85
|
+
sale = Braintree::Transaction.sale(
|
86
|
+
:payment_method_token => cc_token,
|
87
|
+
:amount => 10.00
|
88
|
+
)
|
89
|
+
result = Braintree::Transaction.void(sale.transaction.id)
|
90
|
+
result.should be_success
|
91
|
+
result.transaction.status.should == Braintree::Transaction::Status::Voided
|
35
92
|
end
|
36
93
|
end
|
37
94
|
end
|
38
95
|
|
39
96
|
describe FakeBraintree::SinatraApp do
|
40
|
-
context
|
41
|
-
it
|
97
|
+
context 'Braintree::Transaction.find' do
|
98
|
+
it 'can find a created sale' do
|
42
99
|
id = create_transaction.id
|
43
100
|
result = Braintree::Transaction.find(id)
|
44
101
|
result.amount.should == amount
|
45
102
|
end
|
46
103
|
|
47
|
-
it
|
104
|
+
it 'can find >1 transaction' do
|
48
105
|
Braintree::Transaction.find(create_transaction.id).should be
|
49
106
|
Braintree::Transaction.find(create_transaction.id).should be
|
50
107
|
end
|
51
108
|
|
52
|
-
it
|
109
|
+
it 'raises an error when the transaction does not exist' do
|
53
110
|
expect { Braintree::Transaction.find('foobar') }.to raise_error(Braintree::NotFoundError)
|
54
111
|
end
|
55
112
|
|