fake_braintree 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe FakeBraintree::SinatraApp do
4
+ context "Braintree::Transaction.sale" do
5
+ it "successfully creates a transaction" do
6
+ result = Braintree::Transaction.sale(:payment_method_token => cc_token, :amount => 10.00)
7
+ result.should be_success
8
+ end
9
+
10
+ context "when all cards are declined" do
11
+ before { FakeBraintree.decline_all_cards! }
12
+
13
+ it "fails" do
14
+ result = Braintree::Transaction.sale(:payment_method_token => cc_token, :amount => 10.00)
15
+ result.should_not be_success
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ describe FakeBraintree::SinatraApp do
22
+ context "Braintree::Transaction.find" do
23
+ it "can find a created sale" do
24
+ id = create_transaction.id
25
+ result = Braintree::Transaction.find(id)
26
+ result.amount.should == amount
27
+ end
28
+
29
+ it "can find >1 transaction" do
30
+ Braintree::Transaction.find(create_transaction.id).should be
31
+ Braintree::Transaction.find(create_transaction.id).should be
32
+ end
33
+
34
+ it "raises an error when the transaction does not exist" do
35
+ expect { Braintree::Transaction.find('foobar') }.to raise_error(Braintree::NotFoundError)
36
+ end
37
+
38
+ def create_transaction
39
+ Braintree::Transaction.sale(:payment_method_token => cc_token, :amount => amount).transaction
40
+ end
41
+
42
+ let(:amount) { 10.00 }
43
+ end
44
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe FakeBraintree::SinatraApp do
4
+ context "Braintree::TransparentRedirect.url" do
5
+ it "returns a URL that will redirect with a token for the specified request" do
6
+ redirect_url = "http://example.com/redirect_path"
7
+
8
+ response = post_transparent_redirect(:redirect_url => redirect_url, :customer => build_customer_hash)
9
+
10
+ response.code.should == "303"
11
+ response["Location"].should =~ %r{http://example\.com/redirect_path}
12
+ params = parse_redirect(response)
13
+ params[:http_status].should == "200"
14
+ params[:id].should_not be_nil
15
+ params[:kind].should_not be_nil
16
+ end
17
+
18
+ it "rejects submissions without transparent redirect data" do
19
+ response = post_transparent_redirect_without_data
20
+ response.code.should == "422"
21
+ end
22
+ end
23
+
24
+ context "Braintree::TransparentRedirect.confirm" do
25
+ it "confirms and runs the specified request" do
26
+ number = "4111111111111111"
27
+ customer_hash = build_customer_hash(:credit_card => { :number => number })
28
+ response = post_transparent_redirect(:customer => customer_hash)
29
+ query = parse_query(response)
30
+
31
+ result = Braintree::TransparentRedirect.confirm(query)
32
+
33
+ result.should be_success
34
+
35
+ customer = Braintree::Customer.find(result.customer.id)
36
+ customer.credit_cards.first.last_4.should == "1111"
37
+ end
38
+ end
39
+
40
+ def build_customer_hash(options = {})
41
+ {
42
+ :credit_card => {
43
+ :number => "4111111111111111",
44
+ :expiration_date => "4/2015",
45
+ :cardholder_name => "Susie Spender"
46
+ }.update(options[:credit_card] || {})
47
+ }
48
+ end
49
+
50
+ def post_transparent_redirect(data)
51
+ params = data.dup
52
+ redirect_url = params.delete(:redirect_url) || "http://example.com/redirect_path"
53
+ params[:tr_data] = Braintree::TransparentRedirect.create_customer_data(:redirect_url => redirect_url)
54
+ post_transparent_redirect_params(params)
55
+ end
56
+
57
+ def post_transparent_redirect_without_data
58
+ post_transparent_redirect_params({})
59
+ end
60
+
61
+ def post_transparent_redirect_params(params)
62
+ uri = URI.parse(Braintree::TransparentRedirect.url)
63
+ Net::HTTP.start(uri.host, uri.port) do |http|
64
+ http.post(uri.path, Rack::Utils.build_nested_query(params))
65
+ end
66
+ end
67
+
68
+ def parse_redirect(response)
69
+ query = parse_query(response)
70
+ Braintree::Configuration.gateway.transparent_redirect.parse_and_validate_query_string(query)
71
+ end
72
+
73
+ def parse_query(response)
74
+ URI.parse(response["Location"]).query
75
+ end
76
+ end
@@ -1,16 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FakeBraintree, ".credit_card_from_token" do
4
- let(:cc_number) { %w(4111 1111 1111 9876).join }
5
- let(:cc_number_2) { %w(4111 1111 1111 2222).join }
6
- let(:expiration_date) { "04/2016" }
7
- let(:expiration_date_2) { "05/2019" }
8
- let(:token) { braintree_credit_card_token(cc_number, expiration_date) }
9
- let(:token_2) { braintree_credit_card_token(cc_number_2, expiration_date_2) }
10
-
11
4
  it "looks up the credit card based on a CC token" do
12
5
  credit_card = FakeBraintree.credit_card_from_token(token)
13
- credit_card["last_4"].should == "9876"
6
+ credit_card["last_4"].should == TEST_CC_NUMBER[-4,4]
14
7
  credit_card["expiration_year"].should == "2016"
15
8
  credit_card["expiration_month"].should == "04"
16
9
 
@@ -19,27 +12,28 @@ describe FakeBraintree, ".credit_card_from_token" do
19
12
  credit_card["expiration_year"].should == "2019"
20
13
  credit_card["expiration_month"].should == "05"
21
14
  end
15
+
16
+ let(:cc_number_2) { %w(4111 1111 1111 2222).join }
17
+ let(:expiration_date) { "04/2016" }
18
+ let(:expiration_date_2) { "05/2019" }
19
+ let(:token) { braintree_credit_card_token(TEST_CC_NUMBER, expiration_date) }
20
+ let(:token_2) { braintree_credit_card_token(cc_number_2, expiration_date_2) }
22
21
  end
23
22
 
24
23
  describe FakeBraintree, ".decline_all_cards!" do
25
- let(:cc_number) { %w(4111 1111 1111 9876).join }
26
- let(:expiration_date) { "04/2016" }
27
- let(:token) { braintree_credit_card_token(cc_number, expiration_date) }
28
- let(:amount) { 10.00 }
29
-
30
- before do
31
- FakeBraintree.decline_all_cards!
32
- end
24
+ before { FakeBraintree.decline_all_cards! }
33
25
 
34
26
  it "declines all cards" do
35
- result = Braintree::CreditCard.sale(token, amount: amount)
36
- result.should_not be_success
27
+ create_sale.should_not be_success
37
28
  end
38
29
 
39
30
  it "stops declining cards after clear! is called" do
40
31
  FakeBraintree.clear!
41
- result = Braintree::CreditCard.sale(token, amount: amount)
42
- result.should be_success
32
+ create_sale.should be_success
33
+ end
34
+
35
+ def create_sale
36
+ Braintree::CreditCard.sale(cc_token, :amount => 10.00)
43
37
  end
44
38
  end
45
39
 
@@ -49,11 +43,11 @@ describe FakeBraintree, ".log_file_path" do
49
43
  end
50
44
  end
51
45
 
52
- describe "configuration variables" do
46
+ describe Braintree::Configuration do
53
47
  subject { Braintree::Configuration }
54
48
 
55
- it "sets the environment to production" do
56
- subject.environment.should == :production
49
+ it "sets the environment to development" do
50
+ subject.environment.should == :development
57
51
  end
58
52
 
59
53
  it "sets some fake API credentials" do
@@ -74,7 +68,7 @@ end
74
68
 
75
69
  describe FakeBraintree, ".clear_log!" do
76
70
  it "clears the log file" do
77
- %w(one two).each { |string| Braintree::Configuration.logger.info(string) }
71
+ write_to_log
78
72
  subject.clear_log!
79
73
  File.read(FakeBraintree.log_file_path).should == ""
80
74
  end
@@ -83,9 +77,17 @@ describe FakeBraintree, ".clear_log!" do
83
77
  FakeBraintree.expects(:clear_log!)
84
78
  FakeBraintree.clear!
85
79
  end
80
+
81
+ def write_to_log
82
+ Braintree::Configuration.logger.info('foo bar baz')
83
+ end
86
84
  end
87
85
 
88
86
  describe FakeBraintree, "VALID_CREDIT_CARDS" do
87
+ it "includes only credit cards that are valid in the Braintree sandbox" do
88
+ FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
89
+ end
90
+
89
91
  let(:valid_credit_cards) do
90
92
  %w(4111111111111111 4005519200000004
91
93
  4009348888881881 4012000033330026
@@ -96,8 +98,59 @@ describe FakeBraintree, "VALID_CREDIT_CARDS" do
96
98
  3530111333300000
97
99
  )
98
100
  end
101
+ end
102
+
103
+ describe FakeBraintree, ".failure_response" do
104
+ it "can be called with no arguments" do
105
+ expect { FakeBraintree.failure_response }.not_to raise_error
106
+ end
107
+ end
99
108
 
100
- it "includes only credit cards that are valid in production" do
101
- FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
109
+ describe FakeBraintree, ".generate_transaction" do
110
+ it "includes the subscription id" do
111
+ transaction = FakeBraintree.generate_transaction(:subscription_id => 'foobar')
112
+ transaction['subscription_id'].should == 'foobar'
113
+ end
114
+
115
+ it "allows setting created_at" do
116
+ time = Time.now
117
+ transaction = FakeBraintree.generate_transaction(:created_at => time)
118
+ transaction['created_at'].should == time
119
+ end
120
+
121
+ it "sets created_at to Time.now by default" do
122
+ Timecop.freeze do
123
+ transaction = FakeBraintree.generate_transaction
124
+ transaction['created_at'].should == Time.now
125
+ end
126
+ end
127
+
128
+ it "allows no arguments" do
129
+ expect { FakeBraintree.generate_transaction }.not_to raise_error
130
+ end
131
+
132
+ context "status_history" do
133
+ it "returns a hash with a status_history key" do
134
+ FakeBraintree.generate_transaction(:amount => '20').should have_key('status_history')
135
+ end
136
+
137
+ it "has a timestamp of Time.now" do
138
+ Timecop.freeze do
139
+ transaction = FakeBraintree.generate_transaction(:status => Braintree::Transaction::Status::Failed,
140
+ :subscription_id => 'my_subscription_id')
141
+ transaction['status_history'][0]['timestamp'].should == Time.now
142
+ end
143
+ end
144
+
145
+ it "has the desired amount" do
146
+ transaction = FakeBraintree.generate_transaction(:amount => '20.00')
147
+ transaction['status_history'][0]['amount'].should == '20.00'
148
+ end
149
+
150
+ it "has the desired status" do
151
+ status = Braintree::Transaction::Status::Failed
152
+ transaction = FakeBraintree.generate_transaction(:status => status)
153
+ transaction['status_history'][0]['status'].should == status
154
+ end
102
155
  end
103
156
  end
data/spec/spec_helper.rb CHANGED
@@ -1,16 +1,31 @@
1
- # Requires supporting ruby files with custom matchers and macros, etc,
2
- # in spec/support/ and its subdirectories.
3
- require 'rspec'
4
- require 'fake_braintree'
5
- Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f}
1
+ require 'spork'
6
2
 
7
- Dir.mkdir('tmp') unless Dir.exist?('tmp')
8
- File.new('tmp/braintree_log', 'w').close
3
+ Spork.prefork do
4
+ # Requires supporting ruby files with custom matchers and macros, etc,
5
+ # in spec/support/ and its subdirectories.
6
+ require 'rspec'
7
+ require 'fake_braintree'
8
+ require 'timecop'
9
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each {|f| require f}
9
10
 
10
- RSpec.configure do |config|
11
- config.mock_with :mocha
11
+ Dir.mkdir('tmp') unless File.directory?('tmp')
12
+ File.new('tmp/braintree_log', 'w').close
12
13
 
13
- config.include BraintreeHelpers
14
+ TEST_CC_NUMBER = %w(4111 1111 1111 1111).join
14
15
 
15
- config.before { FakeBraintree.clear! }
16
+ RSpec.configure do |config|
17
+ config.mock_with :mocha
18
+
19
+ config.include BraintreeHelpers
20
+ config.include CustomerHelpers
21
+ config.include SubscriptionHelpers
22
+
23
+ config.before do
24
+ FakeBraintree.clear!
25
+ FakeBraintree.verify_all_cards = false
26
+ end
27
+ end
28
+ end
29
+
30
+ Spork.each_run do
16
31
  end
@@ -1,10 +1,10 @@
1
1
  module BraintreeHelpers
2
2
  def create_braintree_customer(cc_number, expiration_date)
3
3
  Braintree::Customer.create(
4
- email: "me@example.com",
5
- credit_card: {
6
- number: cc_number,
7
- expiration_date: expiration_date
4
+ :email => "me@example.com",
5
+ :credit_card => {
6
+ :number => cc_number,
7
+ :expiration_date => expiration_date
8
8
  }
9
9
  ).customer
10
10
  end
@@ -12,4 +12,8 @@ module BraintreeHelpers
12
12
  def braintree_credit_card_token(cc_number, expiration_date)
13
13
  create_braintree_customer(cc_number, expiration_date).credit_cards[0].token
14
14
  end
15
+
16
+ def cc_token
17
+ braintree_credit_card_token(TEST_CC_NUMBER, '04/2016')
18
+ end
15
19
  end
@@ -0,0 +1,12 @@
1
+ module CustomerHelpers
2
+ def create_customer(credit_card_options = {})
3
+ credit_card_options[:number] ||= TEST_CC_NUMBER
4
+ credit_card_options[:expiration_date] ||= '04/2016'
5
+ Braintree::Customer.create(:credit_card => credit_card_options)
6
+ end
7
+
8
+ def create_customer_with_invalid_card(credit_card_options = {})
9
+ credit_card_options[:number] = '123456'
10
+ create_customer(credit_card_options)
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module SubscriptionHelpers
2
+ def create_subscription(options = {})
3
+ Braintree::Subscription.create({ :payment_method_token => cc_token,
4
+ :plan_id => 'my_plan_id' }.merge(options))
5
+ end
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fake_braintree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-07 00:00:00.000000000 Z
12
+ date: 2011-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: sham_rack
16
- requirement: &70312449445460 !ruby/object:Gem::Requirement
15
+ name: capybara
16
+ requirement: &70247558335960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70312449445460
24
+ version_requirements: *70247558335960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70312449445040 !ruby/object:Gem::Requirement
27
+ requirement: &70247558333760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70312449445040
35
+ version_requirements: *70247558333760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: i18n
38
- requirement: &70312449444620 !ruby/object:Gem::Requirement
38
+ requirement: &70247558345380 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70312449444620
46
+ version_requirements: *70247558345380
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sinatra
49
- requirement: &70312460820820 !ruby/object:Gem::Requirement
49
+ requirement: &70247558343320 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70312460820820
57
+ version_requirements: *70247558343320
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: braintree
60
- requirement: &70312460820320 !ruby/object:Gem::Requirement
60
+ requirement: &70247558341980 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,21 @@ dependencies:
65
65
  version: '2.5'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70312460820320
68
+ version_requirements: *70247558341980
69
+ - !ruby/object:Gem::Dependency
70
+ name: mongrel
71
+ requirement: &70247562466060 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.2.0.pre
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: *70247562466060
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rspec
71
- requirement: &70312460819820 !ruby/object:Gem::Requirement
82
+ requirement: &70247562473440 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ~>
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: 2.6.0
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70312460819820
90
+ version_requirements: *70247562473440
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: mocha
82
- requirement: &70312460819360 !ruby/object:Gem::Requirement
93
+ requirement: &70247562468440 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ~>
@@ -87,7 +98,51 @@ dependencies:
87
98
  version: 0.9.12
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *70312460819360
101
+ version_requirements: *70247562468440
102
+ - !ruby/object:Gem::Dependency
103
+ name: timecop
104
+ requirement: &70247562490280 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 0.3.5
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70247562490280
113
+ - !ruby/object:Gem::Dependency
114
+ name: spork
115
+ requirement: &70247558422540 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ~>
119
+ - !ruby/object:Gem::Version
120
+ version: 0.9.0.rc9
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70247558422540
124
+ - !ruby/object:Gem::Dependency
125
+ name: bundler
126
+ requirement: &70247558426500 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: 1.0.14
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *70247558426500
135
+ - !ruby/object:Gem::Dependency
136
+ name: rake
137
+ requirement: &70247558440000 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: *70247558440000
91
146
  description: A fake Braintree that you can run integration tests against
92
147
  email:
93
148
  - gabe@thoughtbot.com
@@ -97,6 +152,7 @@ extensions: []
97
152
  extra_rdoc_files: []
98
153
  files:
99
154
  - .gitignore
155
+ - .travis.yml
100
156
  - Changelog.md
101
157
  - Gemfile
102
158
  - README.md
@@ -105,6 +161,7 @@ files:
105
161
  - lib/fake_braintree.rb
106
162
  - lib/fake_braintree/customer.rb
107
163
  - lib/fake_braintree/helpers.rb
164
+ - lib/fake_braintree/redirect.rb
108
165
  - lib/fake_braintree/sinatra_app.rb
109
166
  - lib/fake_braintree/subscription.rb
110
167
  - lib/fake_braintree/valid_credit_cards.rb
@@ -112,9 +169,13 @@ files:
112
169
  - spec/fake_braintree/credit_card_spec.rb
113
170
  - spec/fake_braintree/customer_spec.rb
114
171
  - spec/fake_braintree/subscription_spec.rb
172
+ - spec/fake_braintree/transaction_spec.rb
173
+ - spec/fake_braintree/transparent_redirect_spec.rb
115
174
  - spec/fake_braintree_spec.rb
116
175
  - spec/spec_helper.rb
117
176
  - spec/support/braintree_helpers.rb
177
+ - spec/support/customer_helpers.rb
178
+ - spec/support/subscription_helpers.rb
118
179
  homepage: ''
119
180
  licenses: []
120
181
  post_install_message:
@@ -127,15 +188,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
188
  - - ! '>='
128
189
  - !ruby/object:Gem::Version
129
190
  version: '0'
191
+ segments:
192
+ - 0
193
+ hash: -527011835497576816
130
194
  required_rubygems_version: !ruby/object:Gem::Requirement
131
195
  none: false
132
196
  requirements:
133
197
  - - ! '>='
134
198
  - !ruby/object:Gem::Version
135
199
  version: '0'
200
+ segments:
201
+ - 0
202
+ hash: -527011835497576816
136
203
  requirements: []
137
204
  rubyforge_project:
138
- rubygems_version: 1.8.10
205
+ rubygems_version: 1.8.11
139
206
  signing_key:
140
207
  specification_version: 3
141
208
  summary: A fake Braintree that you can run integration tests against
@@ -143,6 +210,10 @@ test_files:
143
210
  - spec/fake_braintree/credit_card_spec.rb
144
211
  - spec/fake_braintree/customer_spec.rb
145
212
  - spec/fake_braintree/subscription_spec.rb
213
+ - spec/fake_braintree/transaction_spec.rb
214
+ - spec/fake_braintree/transparent_redirect_spec.rb
146
215
  - spec/fake_braintree_spec.rb
147
216
  - spec/spec_helper.rb
148
217
  - spec/support/braintree_helpers.rb
218
+ - spec/support/customer_helpers.rb
219
+ - spec/support/subscription_helpers.rb