forked from highfidelity/fake_braintree
/
fake_braintree_spec.rb
143 lines (118 loc) · 4.01 KB
/
fake_braintree_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'spec_helper'
describe FakeBraintree, ".decline_all_cards!" do
before { FakeBraintree.decline_all_cards! }
it "declines all cards" do
create_sale.should_not be_success
end
it "stops declining cards after clear! is called" do
FakeBraintree.clear!
create_sale.should be_success
end
def create_sale
Braintree::CreditCard.sale(cc_token, :amount => 10.00)
end
end
describe FakeBraintree, ".log_file_path" do
it "is tmp/log" do
FakeBraintree.log_file_path.should == 'tmp/log'
end
end
describe Braintree::Configuration do
subject { Braintree::Configuration }
it "is running in the development environment" do
subject.environment.should == :development
end
it "has some fake API credentials" do
subject.merchant_id.should == "xxx"
subject.public_key.should == "xxx"
subject.private_key.should == "xxx"
end
end
describe FakeBraintree do
it "creates a log file" do
File.exist?(FakeBraintree.log_file_path).should == true
end
it "logs to the correct path" do
Braintree::Configuration.logger.info('Logger test')
File.readlines(FakeBraintree.log_file_path).last.should == "Logger test\n"
end
end
describe FakeBraintree, ".clear_log!" do
it "clears the log file" do
write_to_log
subject.clear_log!
File.read(FakeBraintree.log_file_path).should == ""
end
it "is called by clear!" do
FakeBraintree.expects(:clear_log!)
FakeBraintree.clear!
end
def write_to_log
Braintree::Configuration.logger.info('foo bar baz')
end
end
describe FakeBraintree, "VALID_CREDIT_CARDS" do
it "includes only credit cards that are valid in the Braintree sandbox" do
FakeBraintree::VALID_CREDIT_CARDS.sort.should == valid_credit_cards.sort
end
let(:valid_credit_cards) do
%w(4111111111111111 4005519200000004
4009348888881881 4012000033330026
4012000077777777 4012888888881881
4217651111111119 4500600000000061
5555555555554444 378282246310005
371449635398431 6011111111111117
3530111333300000
)
end
end
describe FakeBraintree, ".failure_response" do
it "can be called with no arguments" do
expect { FakeBraintree.failure_response }.not_to raise_error
end
end
describe FakeBraintree, ".generate_transaction" do
it "allows setting the subscription id" do
transaction = FakeBraintree.generate_transaction(:subscription_id => 'foobar')
transaction['subscription_id'].should == 'foobar'
end
it "allows setting created_at" do
time = Time.now
transaction = FakeBraintree.generate_transaction(:created_at => time)
transaction['created_at'].should == time
end
it "sets created_at to Time.now by default" do
Timecop.freeze do
transaction = FakeBraintree.generate_transaction
transaction['created_at'].should == Time.now
end
end
it "has the correct amount" do
transaction = FakeBraintree.generate_transaction(:amount => "20.00")
transaction['amount'].should == "20.00"
end
it "allows no arguments" do
expect { FakeBraintree.generate_transaction }.not_to raise_error
end
context "status_history" do
it "returns a hash with a status_history key" do
FakeBraintree.generate_transaction(:amount => '20').should have_key('status_history')
end
it "has a timestamp of Time.now" do
Timecop.freeze do
transaction = FakeBraintree.generate_transaction(:status => Braintree::Transaction::Status::Failed,
:subscription_id => 'my_subscription_id')
transaction['status_history'].first['timestamp'].should == Time.now
end
end
it "has the desired amount" do
transaction = FakeBraintree.generate_transaction(:amount => '20.00')
transaction['status_history'].first['amount'].should == '20.00'
end
it "has the desired status" do
status = Braintree::Transaction::Status::Failed
transaction = FakeBraintree.generate_transaction(:status => status)
transaction['status_history'].first['status'].should == status
end
end
end