cbraspag 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Braspag::Converter do
4
+ describe ".decimal_to_string" do
5
+ it "should convert decimal to string with comma as decimal separator" do
6
+ Braspag::Converter.decimal_to_string(10).should == "10,00"
7
+ Braspag::Converter.decimal_to_string(1).should == "1,00"
8
+ Braspag::Converter.decimal_to_string(0.1).should == "0,10"
9
+ Braspag::Converter.decimal_to_string(0.01).should == "0,01"
10
+ Braspag::Converter.decimal_to_string(9.99999).should == "10,00" # round up
11
+ Braspag::Converter.decimal_to_string(10.9).should == "10,90"
12
+ Braspag::Converter.decimal_to_string(9.1111).should == "9,11"
13
+ end
14
+ end
15
+
16
+ describe ".to_map" do
17
+ let(:document) do
18
+ <<-XML
19
+ <root>
20
+ <foo>blabla</foo>
21
+ <bar>bleble</bar>
22
+ <baz></baz>
23
+ </root>
24
+ XML
25
+ end
26
+
27
+ context "basic document and keys" do
28
+ it "should return a Hash" do
29
+ keys = { :foo => nil, :meu_elemento => "bar", :outro_elemento => "baz" }
30
+ expected = { :foo => "blabla", :meu_elemento => "bleble", :outro_elemento => nil }
31
+
32
+ Braspag::Converter::to_map(document, keys).should == expected
33
+ end
34
+ end
35
+
36
+ context "keys with a Proc" do
37
+ it "should return a Hash" do
38
+ proc = Proc.new { "value returned by Proc" }
39
+
40
+ keys = { :foo => proc, :meu_elemento => "bar", :outro_elemento => "baz" }
41
+ expected = { :foo => "value returned by Proc", :meu_elemento => "bleble", :outro_elemento => nil }
42
+
43
+ Braspag::Converter::to_map(document, keys).should == expected
44
+ end
45
+ end
46
+ end
47
+
48
+ describe ".payment_method_name?" do
49
+ it "should return name from number" do
50
+ Braspag::Converter.payment_method_name?(6).should eq(:billet_bradesco)
51
+ end
52
+
53
+ it "should return name from string" do
54
+ Braspag::Converter.payment_method_name?("6").should eq(:billet_bradesco)
55
+ end
56
+
57
+ it "should return name from string with 0" do
58
+ Braspag::Converter.payment_method_name?("06").should eq(:billet_bradesco)
59
+ end
60
+
61
+ it "should return nil when not found" do
62
+ Braspag::Converter.payment_method_name?("AAA").should be(nil)
63
+ end
64
+ end
65
+
66
+ describe ".status_name?" do
67
+ it "should return name from number" do
68
+ Braspag::Converter.status_name?(1).should eq(:starting)
69
+ end
70
+
71
+ it "should return name from string" do
72
+ Braspag::Converter.status_name?("1").should eq(:starting)
73
+ end
74
+
75
+ it "should return name from string with 0" do
76
+ Braspag::Converter.status_name?("01").should eq(:starting)
77
+ end
78
+
79
+ it "should return nil when not found" do
80
+ Braspag::Converter.status_name?("AAA").should be(nil)
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Braspag::Customer do
4
+ [:purchase, :generate, :authorize, :archive, :recurrency ].each do |context_type|
5
+ context "on #{context_type}" do
6
+ it "should validate minimum 1 length of name" do
7
+ subject.name = ''
8
+ subject.valid?(context_type)
9
+ subject.errors.messages[:name].should include("is too short (minimum is 1 characters)")
10
+ end
11
+
12
+ it "should validate maximum 100 length of name" do
13
+ subject.name = '*' * 110
14
+ subject.valid?(context_type)
15
+ subject.errors.messages[:name].should include("is too long (maximum is 100 characters)")
16
+ end
17
+
18
+ it "should allow blank for email" do
19
+ subject.email = ''
20
+ subject.valid?(context_type)
21
+ subject.errors.messages[:email].should be(nil)
22
+ end
23
+
24
+ it "should validate maximum 255 length of email" do
25
+ subject.email = '*' * 260
26
+ subject.valid?(context_type)
27
+ subject.errors.messages[:email].should include("is too long (maximum is 255 characters)")
28
+ end
29
+
30
+ it "should allow blank for document" do
31
+ subject.document = ''
32
+ subject.valid?(context_type)
33
+ subject.errors.messages[:document].should be(nil)
34
+ end
35
+
36
+ it "should validate minimum 11 length of document" do
37
+ subject.document = 'XXX'
38
+ subject.valid?(context_type)
39
+ subject.errors.messages[:document].should include("is too short (minimum is 11 characters)")
40
+ end
41
+
42
+ it "should validate maximum 18 length of document" do
43
+ subject.document = '*' * 20
44
+ subject.valid?(context_type)
45
+ subject.errors.messages[:document].should include("is too long (maximum is 18 characters)")
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,382 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Order do
5
+ let(:braspag_homologation_url) { "https://homologacao.pagador.com.br" }
6
+ let(:braspag_production_url) { "https://transaction.pagador.com.br" }
7
+ let(:merchant_id) { "um id qualquer" }
8
+
9
+
10
+ pending ".info_billet" do
11
+ let(:info_url) { "http://braspag/bla" }
12
+ let(:invalid_xml) do
13
+ <<-EOXML
14
+ <?xml version="1.0" encoding="utf-8"?>
15
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
17
+ xsi:nil="true"
18
+ xmlns="http://www.pagador.com.br/" />
19
+ EOXML
20
+ end
21
+
22
+ let(:valid_xml) do
23
+ <<-EOXML
24
+ <?xml version="1.0" encoding="utf-8"?>
25
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
27
+ xmlns="http://www.pagador.com.br/">
28
+ <NumeroDocumento>999</NumeroDocumento>
29
+ <Sacado/>
30
+ <NossoNumero>999</NossoNumero>
31
+ <LinhaDigitavel>35690.00361 03962.070003 00000.009993 4 50160000001000</LinhaDigitavel>
32
+ <DataDocumento>22/6/2011</DataDocumento>
33
+ <DataVencimento>2/7/2011</DataVencimento>
34
+ <Cedente>Gonow Tecnologia e Acessoria Empresarial Ltda</Cedente>
35
+ <Banco>356-5</Banco>
36
+ <Agencia>0003</Agencia>
37
+ <Conta>6039620</Conta>
38
+ <Carteira>57</Carteira>
39
+ <ValorDocumento>10,00</ValorDocumento>
40
+ </DadosBoleto>
41
+ EOXML
42
+ end
43
+
44
+ it "should raise an error when order id is not valid" do
45
+ Braspag::Bill.should_receive(:valid_order_id?)
46
+ .with("bla")
47
+ .and_return(false)
48
+
49
+ expect {
50
+ Braspag::Bill.info "bla"
51
+ }.to raise_error(Braspag::InvalidOrderId)
52
+ end
53
+
54
+ it "should raise an error when Braspag returned an invalid xml as response" do
55
+ FakeWeb.register_uri(:post, info_url, :body => invalid_xml)
56
+
57
+ Braspag::Bill.should_receive(:info_url)
58
+ .and_return(info_url)
59
+
60
+ expect {
61
+ Braspag::Bill.info("orderid")
62
+ }.to raise_error(Braspag::UnknownError)
63
+ end
64
+
65
+ it "should return a Hash when Braspag returned a valid xml as response" do
66
+ FakeWeb.register_uri(:post, info_url, :body => valid_xml)
67
+
68
+ Braspag::Bill.should_receive(:info_url)
69
+ .and_return(info_url)
70
+
71
+ response = Braspag::Bill.info("orderid")
72
+ response.should be_kind_of Hash
73
+
74
+ response.should == {
75
+ :document_number => "999",
76
+ :payer => nil,
77
+ :our_number => "999",
78
+ :bill_line => "35690.00361 03962.070003 00000.009993 4 50160000001000",
79
+ :document_date => "22/6/2011",
80
+ :expiration_date => "2/7/2011",
81
+ :receiver => "Gonow Tecnologia e Acessoria Empresarial Ltda",
82
+ :bank => "356-5",
83
+ :agency => "0003",
84
+ :account => "6039620",
85
+ :wallet => "57",
86
+ :amount => "10,00",
87
+ :amount_invoice => nil,
88
+ :invoice_date => nil
89
+ }
90
+ end
91
+ end
92
+
93
+ pending ".info" do
94
+ let(:info_url) { "http://braspag/bla" }
95
+
96
+ let(:invalid_xml) do
97
+ <<-EOXML
98
+ <DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
99
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
100
+ xmlns="http://www.pagador.com.br/">
101
+ <NumeroComprovante></NumeroComprovante>
102
+ <Autenticada>false</Autenticada>
103
+ <NumeroAutorizacao>557593</NumeroAutorizacao>
104
+ <NumeroCartao>345678*****0007</NumeroCartao>
105
+ <NumeroTransacao>101001225645</NumeroTransacao>
106
+ </DadosCartao>
107
+ EOXML
108
+ end
109
+
110
+ let(:valid_xml) do
111
+ <<-EOXML
112
+ <DadosCartao xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
113
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
114
+ xmlns="http://www.pagador.com.br/">
115
+ <NumeroComprovante>11111</NumeroComprovante>
116
+ <Autenticada>false</Autenticada>
117
+ <NumeroAutorizacao>557593</NumeroAutorizacao>
118
+ <NumeroCartao>345678*****0007</NumeroCartao>
119
+ <NumeroTransacao>101001225645</NumeroTransacao>
120
+ </DadosCartao>
121
+ EOXML
122
+ end
123
+
124
+ it "should raise an error when order id is not valid" do
125
+ Braspag::CreditCard.should_receive(:valid_order_id?)
126
+ .with("bla")
127
+ .and_return(false)
128
+
129
+ expect {
130
+ Braspag::CreditCard.info "bla"
131
+ }.to raise_error(Braspag::InvalidOrderId)
132
+ end
133
+
134
+ it "should raise an error when Braspag returned an invalid xml as response" do
135
+ FakeWeb.register_uri(:post, info_url, :body => invalid_xml)
136
+
137
+ Braspag::CreditCard.should_receive(:info_url)
138
+ .and_return(info_url)
139
+
140
+ expect {
141
+ Braspag::CreditCard.info("orderid")
142
+ }.to raise_error(Braspag::UnknownError)
143
+ end
144
+
145
+ it "should return a Hash when Braspag returned a valid xml as response" do
146
+ FakeWeb.register_uri(:post, info_url, :body => valid_xml)
147
+
148
+ Braspag::CreditCard.should_receive(:info_url)
149
+ .and_return(info_url)
150
+
151
+ response = Braspag::CreditCard.info("orderid")
152
+ response.should be_kind_of Hash
153
+
154
+ response.should == {
155
+ :checking_number => "11111",
156
+ :certified => "false",
157
+ :autorization_number => "557593",
158
+ :card_number => "345678*****0007",
159
+ :transaction_number => "101001225645"
160
+ }
161
+ end
162
+ end
163
+
164
+ pending ".status" do
165
+ let(:order_id) { "um order id qualquer" }
166
+ let(:status_url) { "http://foo.com/bar/baz/assererre" }
167
+
168
+ context "with invalid order id" do
169
+ let(:invalid_xml) do
170
+ <<-EOXML
171
+ <?xml version="1.0" encoding="utf-8"?>
172
+ <DadosBoleto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
173
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
174
+ xsi:nil="true"
175
+ xmlns="http://www.pagador.com.br/" />
176
+ EOXML
177
+ end
178
+
179
+ it "should raise an error when order id is not valid" do
180
+ Braspag::PaymentMethod.should_receive(:valid_order_id?)
181
+ .with(order_id)
182
+ .and_return(false)
183
+
184
+ expect {
185
+ Braspag::Order.status(order_id)
186
+ }.to raise_error(Braspag::InvalidOrderId)
187
+ end
188
+
189
+ it "should raise an error when Braspag returns an invalid xml" do
190
+ FakeWeb.register_uri(:post, status_url, :body => invalid_xml)
191
+
192
+ Braspag::Order.should_receive(:status_url)
193
+ .and_return(status_url)
194
+
195
+ expect {
196
+ Braspag::Order.status(order_id)
197
+ }.to raise_error(Braspag::Order::InvalidData)
198
+ end
199
+ end
200
+
201
+ context "with valid order id" do
202
+ let(:valid_xml) do
203
+ <<-EOXML
204
+ <?xml version="1.0" encoding="utf-8"?>
205
+ <DadosPedido xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
206
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
207
+ xmlns="http://www.pagador.com.br/">
208
+ <CodigoAutorizacao>885796</CodigoAutorizacao>
209
+ <CodigoPagamento>18</CodigoPagamento>
210
+ <FormaPagamento>American Express 2P</FormaPagamento>
211
+ <NumeroParcelas>1</NumeroParcelas>
212
+ <Status>3</Status>
213
+ <Valor>0.01</Valor>
214
+ <DataPagamento>7/8/2011 1:19:38 PM</DataPagamento>
215
+ <DataPedido>7/8/2011 1:06:06 PM</DataPedido>
216
+ <TransId>398591</TransId>
217
+ <BraspagTid>5a1d4463-1d11-4571-a877-763aba0ef7ff</BraspagTid>
218
+ </DadosPedido>
219
+ EOXML
220
+ end
221
+
222
+ before do
223
+ Braspag::Order.should_receive(:status_url)
224
+ .and_return(status_url)
225
+
226
+ FakeWeb.register_uri(:post, status_url, :body => valid_xml)
227
+ @response = Braspag::Order.status(order_id)
228
+ end
229
+
230
+ it "should return a Hash" do
231
+ @response.should be_kind_of Hash
232
+ @response.should == {
233
+ :authorization => "885796",
234
+ :error_code => nil,
235
+ :error_message => nil,
236
+ :payment_method => "18",
237
+ :payment_method_name => "American Express 2P",
238
+ :installments => "1",
239
+ :status => "3",
240
+ :amount => "0.01",
241
+ :cancelled_at => nil,
242
+ :paid_at => "7/8/2011 1:19:38 PM",
243
+ :order_date => "7/8/2011 1:06:06 PM",
244
+ :transaction_id => "398591",
245
+ :tid => "5a1d4463-1d11-4571-a877-763aba0ef7ff"
246
+ }
247
+ end
248
+ end
249
+ end
250
+
251
+
252
+ [:purchase, :generate, :authorize, :capture, :void, :recurrency].each do |context_type|
253
+ context "on #{context_type}" do
254
+ it "should validate minimum 1 length of id" do
255
+ subject.id = ''
256
+ subject.valid?(context_type)
257
+ subject.errors.messages[:id].should include("is too short (minimum is 1 characters)")
258
+ end
259
+
260
+ it "should validate maximum 20 length of id" do
261
+ subject.id = '*' * 25
262
+ subject.valid?(context_type)
263
+ subject.errors.messages[:id].should include("is too long (maximum is 20 characters)")
264
+ end
265
+
266
+ it "should allow characters without payment_method" do
267
+ subject.id = '*13*'
268
+ subject.valid?(context_type)
269
+ subject.errors.messages[:id].should eq(nil)
270
+ end
271
+
272
+ [:cielo_noauth_visa, :cielo_preauth_visa, :cielo_noauth_mastercard, :cielo_preauth_mastercard, :cielo_noauth_elo, :cielo_noauth_diners ].each do |payment_method|
273
+ context "when has payment method for #{payment_method}" do
274
+ it "should not allow spaces" do
275
+ subject.payment_method = Braspag::PAYMENT_METHOD[payment_method]
276
+ subject.id = '123 4'
277
+ subject.valid?(context_type)
278
+ subject.errors.messages[:id].should include("is invalid")
279
+ end
280
+ it "should not allow characters" do
281
+ subject.payment_method = Braspag::PAYMENT_METHOD[payment_method]
282
+ subject.id = 'abcd'
283
+ subject.valid?(context_type)
284
+ subject.errors.messages[:id].should include("is invalid")
285
+ end
286
+
287
+ it "should not allow special characters" do
288
+ subject.payment_method = Braspag::PAYMENT_METHOD[payment_method]
289
+ subject.id = '*-[]'
290
+ subject.valid?(context_type)
291
+ subject.errors.messages[:id].should include("is invalid")
292
+ end
293
+ end
294
+ end
295
+ end
296
+ end
297
+
298
+ [:purchase, :generate, :authorize, :recurrency].each do |context_type|
299
+ context "on #{context_type}" do
300
+ it "should not allow blank for payment_method" do
301
+ subject.payment_method = ''
302
+ subject.valid?(context_type)
303
+ subject.errors.messages[:payment_method].should include("can't be blank")
304
+ end
305
+
306
+ it "should not allow blank for amount" do
307
+ subject.amount = ''
308
+ subject.valid?(context_type)
309
+ subject.errors.messages[:amount].should include("can't be blank")
310
+ end
311
+
312
+ it "should validate minimum 1 of amount" do
313
+ subject.amount = 0
314
+ subject.valid?(context_type)
315
+ subject.errors.messages[:amount].should include("must be greater than 0")
316
+ end
317
+
318
+ it "should not allow blank for customer" do
319
+ subject.customer = ''
320
+ subject.valid?(context_type)
321
+ subject.errors.messages[:customer].should include("can't be blank")
322
+ end
323
+
324
+ it "should not allow invalid customer" do
325
+ subject.customer = Braspag::Customer.new
326
+ subject.valid?(context_type)
327
+ subject.errors.messages[:customer].should include("invalid data")
328
+ end
329
+
330
+ it "should accept only valid payment method" do
331
+ subject.payment_method = 0
332
+ subject.valid?(context_type)
333
+ subject.errors.messages[:payment_method].should include("invalid payment code")
334
+ end
335
+ end
336
+ end
337
+
338
+ [:purchase, :authorize, :recurrency].each do |context_type|
339
+ context "on #{context_type}" do
340
+ it "should not allow blank for installments" do
341
+ subject.installments = ''
342
+ subject.valid?(context_type)
343
+ subject.errors.messages[:installments].should include("can't be blank")
344
+ end
345
+
346
+ it "should validate minimum 1 of installments" do
347
+ subject.installments = 0
348
+ subject.valid?(context_type)
349
+ subject.errors.messages[:installments].should include("must be greater than 0")
350
+ end
351
+
352
+
353
+ it "should validate maxium 99 of installments" do
354
+ subject.installments = 100
355
+ subject.valid?(context_type)
356
+ subject.errors.messages[:installments].should include("must be less than 100")
357
+ end
358
+
359
+ it "should not allow blank for installments_type" do
360
+ subject.installments_type = ''
361
+ subject.valid?(context_type)
362
+ subject.errors.messages[:installments_type].should include("can't be blank")
363
+ end
364
+
365
+ it "should accept only valid installments_type" do
366
+ subject.installments_type = 100
367
+ subject.valid?(context_type)
368
+ subject.errors.messages[:installments_type].should include("invalid installments type")
369
+ end
370
+
371
+
372
+ context "when installments_type is NO_INTEREST" do
373
+ it "should installments is one" do
374
+ subject.installments_type = Braspag::INTEREST[:no]
375
+ subject.installments = 3
376
+ subject.valid?(context_type)
377
+ subject.errors.messages[:installments].should include("is invalid")
378
+ end
379
+ end
380
+ end
381
+ end
382
+ end