cbraspag 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe Braspag::Poster do
5
+ let(:request) { OpenStruct.new(:url => 'http://foo/bar') }
6
+ let(:response) { mock(:body => 'success') }
7
+ let(:logger) { mock(:info => nil) }
8
+ let(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
9
+ let(:connection) { Braspag::Connection.new(:merchant_id => merchant_id, :environment => :homologation)}
10
+ let(:connection_logger) { Braspag::Connection.new(:merchant_id => merchant_id, :environment => :homologation, :logger => logger)}
11
+ let(:connection_proxy) { Braspag::Connection.new(:merchant_id => merchant_id, :environment => :homologation, :proxy_address => 'http://proxy.com')}
12
+
13
+ describe "#do_post" do
14
+ before do
15
+ ::HTTPI::Request.should_receive(:new).with('http://foo/bar').and_return(request)
16
+ ::HTTPI.should_receive(:post).with(request).and_return(response)
17
+ end
18
+
19
+ context "without proxy and logger" do
20
+ subject { described_class.new(connection, 'http://foo/bar') }
21
+
22
+ it "should not set the proxy if the proxy_address is not set" do
23
+ request.should_not_receive(:proxy=)
24
+ subject.do_post(:foo, {})
25
+ end
26
+
27
+ it "should not raise an error if logger is not defined" do
28
+ expect {
29
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
30
+ }.to_not raise_error
31
+ end
32
+
33
+ end
34
+
35
+ context "with logger" do
36
+ subject { described_class.new(connection_logger, 'http://foo/bar') }
37
+
38
+ it "should log the request info" do
39
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {:foo=>:bar, :egg=>:span}')
40
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
41
+ end
42
+
43
+ it "should log the request info removing the credit card sensitive info" do
44
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {"cardNumber"=>"************", "securityCode"=>"***"}')
45
+ subject.do_post(:doe, { 'cardNumber' => '123', 'securityCode' => '456' })
46
+ end
47
+
48
+ it "should log response info" do
49
+ logger.should_receive(:info).with('[Braspag] #doe: http://foo/bar, data: {:foo=>:bar, :egg=>:span}')
50
+ subject.do_post(:doe, { :foo => :bar, :egg => :span })
51
+ end
52
+ end
53
+
54
+ context "with proxy" do
55
+ subject { described_class.new(connection_proxy, 'http://foo/bar') }
56
+
57
+ it "should set the proxy if the proxy_address is set" do
58
+ request.should_receive(:proxy=).with('http://proxy.com')
59
+ subject.do_post(:foo, {})
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Braspag::Response do
4
+
5
+ end
@@ -0,0 +1,183 @@
1
+ #encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Crypto::JarWebservice do
5
+ let!(:crypt) {Braspag::Crypto::JarWebservice}
6
+ let! (:key) {"5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7"}
7
+
8
+ pending "when encrypt data" do
9
+
10
+ it "should return a string" do
11
+ FakeWeb.register_uri(
12
+ :post,
13
+ "http://localhost:9292/v1/encrypt.json",
14
+ :body => <<-EOJSON
15
+ {"encrypt":"5u0ZN5qk8eQNuuGPHrcsk0rfi7YclF6s+ZXCE+G4uG4ztfRJrrOALlf81ra7k7p7"}
16
+ EOJSON
17
+ )
18
+
19
+ crypt.encrypt(:nome => "Chapulin", :sobrenome => "Colorado").should == key
20
+ end
21
+
22
+ it "should raise a error with invalid params" do
23
+ expect {
24
+ crypt.encrypt(9999)
25
+ }.to raise_error(Braspag::IncompleteParams)
26
+ end
27
+
28
+ it "should raise an error with invalid params after process" do
29
+ FakeWeb.register_uri(
30
+ :post,
31
+ "http://localhost:9292/v1/encrypt.json",
32
+ :body => <<-EOJSON
33
+ {
34
+ "msg" : "INVALID FORMAT"
35
+ }
36
+ EOJSON
37
+ )
38
+
39
+ expect {
40
+ crypt.encrypt(:venda => "value")
41
+ }.to raise_error(Braspag::IncompleteParams)
42
+ end
43
+
44
+ it "should raise an error with invalid params after process" do
45
+ FakeWeb.register_uri(
46
+ :post,
47
+ "http://localhost:9292/v1/encrypt.json",
48
+ :body => <<-EOJSON
49
+ INVALIDO
50
+ EOJSON
51
+ )
52
+
53
+ expect {
54
+ crypt.encrypt(:venda => "value")
55
+ }.to raise_error(Braspag::UnknownError)
56
+ end
57
+
58
+ it "should raise an error with invalid params after process" do
59
+ FakeWeb.register_uri(
60
+ :post,
61
+ "http://localhost:9292/v1/encrypt.json",
62
+ :body => <<-EOJSON
63
+ {
64
+ "msg" : "INVALID FIELDS"
65
+ }
66
+ EOJSON
67
+ )
68
+
69
+ expect {
70
+ crypt.encrypt(:venda => nil)
71
+ }.to raise_error(Braspag::IncompleteParams)
72
+ end
73
+
74
+ it "should raise an error with invalid crypt key" do
75
+ FakeWeb.register_uri(
76
+ :post,
77
+ "http://localhost:9292/v1/encrypt.json",
78
+ :body => <<-EOJSON
79
+ {
80
+ "msg" : "INVALID KEY"
81
+ }
82
+ EOJSON
83
+ )
84
+
85
+ expect {
86
+ crypt.encrypt(:venda => "value")
87
+ }.to raise_error(Braspag::InvalidCryptKey)
88
+ end
89
+
90
+ end
91
+
92
+ pending "when decrypt data" do
93
+
94
+ it "should return a hash" do
95
+ FakeWeb.register_uri(
96
+ :post,
97
+ "http://localhost:9292/v1/decrypt.json",
98
+ :body => <<-EOJSON
99
+ {"fields":{"nome":"Chapulin","sobrenome":"Colorado"}}
100
+ EOJSON
101
+ )
102
+
103
+ crypt.decrypt(key, [:nome, :sobrenome])[:nome].should eql("Chapulin")
104
+ end
105
+
106
+ it "should raise a error with invalid encrypted key" do
107
+ FakeWeb.register_uri(
108
+ :post,
109
+ "http://localhost:9292/v1/decrypt.json",
110
+ :body => <<-EOJSON
111
+ {
112
+ "msg" : "INVALID ENCRYPTED STRING"
113
+ }
114
+ EOJSON
115
+ )
116
+
117
+ expect {
118
+ crypt.decrypt("1", [:nome, :sobrenome])
119
+ }.to raise_error(Braspag::InvalidEncryptedKey)
120
+ end
121
+
122
+ it "should raise a error with invalid encrypted key" do
123
+ expect {
124
+ crypt.decrypt(1, [:nome, :sobrenome])
125
+ }.to raise_error(Braspag::InvalidEncryptedKey)
126
+ end
127
+
128
+
129
+ it "should raise a error with invalid fields" do
130
+ expect {
131
+ crypt.decrypt(key, 9999)
132
+ }.to raise_error(Braspag::IncompleteParams)
133
+ end
134
+
135
+ it "should raise a error with invalid fields" do
136
+ FakeWeb.register_uri(
137
+ :post,
138
+ "http://localhost:9292/v1/decrypt.json",
139
+ :body => <<-EOJSON
140
+ {
141
+ "msg" : "INVALID FIELDS"
142
+ }
143
+ EOJSON
144
+ )
145
+
146
+ expect {
147
+ crypt.decrypt(key, [:nome, :sobrenome])
148
+ }.to raise_error(Braspag::IncompleteParams)
149
+ end
150
+
151
+ it "should raise an error with invalid params after process" do
152
+ FakeWeb.register_uri(
153
+ :post,
154
+ "http://localhost:9292/v1/decrypt.json",
155
+ :body => <<-EOJSON
156
+ INVALIDO
157
+ EOJSON
158
+ )
159
+
160
+
161
+ expect {
162
+ crypt.decrypt(key, [:nome, :sobrenome])
163
+ }.to raise_error(Braspag::UnknownError)
164
+ end
165
+
166
+ it "should raise an error with invalid crypt key" do
167
+ FakeWeb.register_uri(
168
+ :post,
169
+ "http://localhost:9292/v1/decrypt.json",
170
+ :body => <<-EOJSON
171
+ {
172
+ "msg" : "INVALID KEY"
173
+ }
174
+ EOJSON
175
+ )
176
+
177
+ expect {
178
+ crypt.decrypt(key, [:nome, :sobrenome])
179
+ }.to raise_error(Braspag::InvalidCryptKey)
180
+ end
181
+
182
+ end
183
+ end
@@ -0,0 +1,142 @@
1
+ #encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Crypto::Webservice do
5
+ pending "encrypt" do
6
+ let!(:key) {"XXXXX"}
7
+
8
+ context "consistencies" do
9
+ it "should return error with invalid data" do
10
+ expect {
11
+ Braspag::Crypto::Webservice.encrypt("INVALID DATA")
12
+ }.to raise_error(Braspag::IncompleteParams)
13
+ end
14
+
15
+ it "should return error with invalid data after process" do
16
+ body_invalid = <<-EOXML
17
+ SERVER was unable to process
18
+ EOXML
19
+ FakeWeb.register_uri(:post,
20
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
21
+ :body => body_invalid )
22
+ expect {
23
+ Braspag::Crypto::Webservice.encrypt(:key => "INVALID DATA")
24
+ }.to raise_error(Braspag::UnknownError)
25
+ FakeWeb.clean_registry
26
+ end
27
+
28
+ it "should return error with invalid merchant_id" do
29
+ body_invalid = <<-EOXML
30
+ <?xml version="1.0" encoding="utf-8"?>
31
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
32
+ <soap:Body><EncryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
33
+ <EncryptRequestResult>Erro BP 011</EncryptRequestResult></EncryptRequestResponse>
34
+ </soap:Body></soap:Envelope>
35
+ EOXML
36
+ FakeWeb.register_uri(:post,
37
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
38
+ :body => body_invalid )
39
+ expect {
40
+ Braspag::Crypto::Webservice.encrypt(:key => "value")
41
+ }.to raise_error(Braspag::InvalidMerchantId)
42
+ FakeWeb.clean_registry
43
+ end
44
+
45
+ it "should return error with invalid ip" do
46
+ body_invalid = <<-EOXML
47
+ <?xml version="1.0" encoding="utf-8"?>
48
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
49
+ <soap:Body><EncryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
50
+ <EncryptRequestResult>Erro BP 067</EncryptRequestResult></EncryptRequestResponse>
51
+ </soap:Body></soap:Envelope>
52
+ EOXML
53
+ FakeWeb.register_uri(:post,
54
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
55
+ :body => body_invalid )
56
+ expect {
57
+ Braspag::Crypto::Webservice.encrypt(:key => "value")
58
+ }.to raise_error(Braspag::InvalidIP)
59
+ FakeWeb.clean_registry
60
+ end
61
+ end
62
+
63
+ it "should return a string" do
64
+ FakeWeb.register_uri(:post,
65
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
66
+ :body => <<-EOXML
67
+ <?xml version='1.0' encoding='utf-8'?>
68
+ <soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
69
+ <soap:Body>
70
+ <EncryptRequestResponse xmlns='https://www.pagador.com.br/webservice/BraspagGeneralService'>
71
+ <EncryptRequestResult>#{key}</EncryptRequestResult>
72
+ </EncryptRequestResponse>
73
+ </soap:Body></soap:Envelope>
74
+ EOXML
75
+ )
76
+ Braspag::Crypto::Webservice.encrypt(:key => "value").should == key
77
+ FakeWeb.clean_registry
78
+ end
79
+ end
80
+
81
+ pending "when decrypt data" do
82
+
83
+ context "consistencies" do
84
+ it "should return error with invalid data" do
85
+ expect {
86
+ Braspag::Crypto::Webservice.decrypt(1213123)
87
+ }.to raise_error(Braspag::IncompleteParams)
88
+ end
89
+
90
+ it "should return error with invalid data" do
91
+ body_invalid = <<-EOXML
92
+ SERVER was unable to process
93
+ EOXML
94
+ FakeWeb.register_uri(:post,
95
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
96
+ :body => body_invalid )
97
+ expect {
98
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")
99
+ }.to raise_error(Braspag::UnknownError)
100
+ FakeWeb.clean_registry
101
+ end
102
+
103
+ it "should return error with invalid ip" do
104
+ body_invalid = <<-EOXML
105
+ <?xml version="1.0" encoding="utf-8"?>
106
+ <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
107
+ <soap:Body><DecryptRequestResponse xmlns="https://www.pagador.com.br/webservice/BraspagGeneralService">
108
+ <DecryptRequestResult><string>Erro BP 068</string></DecryptRequestResult>
109
+ </DecryptRequestResponse></soap:Body></soap:Envelope>
110
+ EOXML
111
+ FakeWeb.register_uri(:post,
112
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
113
+ :body => body_invalid )
114
+ expect {
115
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")
116
+ }.to raise_error(Braspag::InvalidIP)
117
+ FakeWeb.clean_registry
118
+ end
119
+ end
120
+
121
+ it "should return a string" do
122
+ FakeWeb.register_uri(:post,
123
+ "https://homologacao.pagador.com.br/BraspagGeneralService/BraspagGeneralService.asmx",
124
+ :body => <<-EOXML
125
+ <?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
126
+ <soap:Body><DecryptRequestResponse xmlns='https://www.pagador.com.br/webservice/BraspagGeneralService'>
127
+ <DecryptRequestResult>
128
+ <string>CODPAGAMENTO=18</string>
129
+ <string>VENDAID=teste123</string>
130
+ <string>VALOR=100</string>
131
+ <string>PARCELAS=1</string>
132
+ <string>NOME=comprador</string>
133
+ </DecryptRequestResult></DecryptRequestResponse>
134
+ </soap:Body></soap:Envelope>
135
+ EOXML
136
+ )
137
+ Braspag::Crypto::Webservice.decrypt("{sdfsdf34543534}")[:parcelas].should eql("1")
138
+ FakeWeb.clean_registry
139
+ end
140
+
141
+ end
142
+ end
@@ -0,0 +1,156 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Braspag::Connection do
5
+ let(:merchant_id) { "{12345678-1234-1234-1234-123456789000}" }
6
+ let(:connection) { Braspag::Connection.new(:merchant_id => merchant_id, :environment => :homologation)}
7
+
8
+ context ".generate_billet" do
9
+ let(:customer) do
10
+ Braspag::Customer.new(
11
+ :document => '21473696240', # (OPTIONAL)
12
+ :name => 'Bob Dela Bobsen',
13
+ :email => 'bob@mailinator.com' # send email to consumer (OPTIONAL)
14
+ )
15
+ end
16
+
17
+ let(:order) do
18
+ Braspag::Order.new(
19
+ :id => "um order id",
20
+ :amount => 100.00,
21
+ :payment_method => Braspag::PAYMENT_METHOD[:billet_bradesco],
22
+ :customer => customer
23
+ )
24
+ end
25
+
26
+ let(:billet) do
27
+ Braspag::Billet.new(
28
+ :id => '123456',
29
+ :instructions => 'does not accepted after due date',
30
+ :due_date_on => Date.parse('2012-01-01')
31
+ )
32
+ end
33
+
34
+ let(:valid_xml) do
35
+ <<-EOXML
36
+ <?xml version="1.0" encoding="utf-8"?>
37
+ <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
38
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
39
+ xmlns="https://www.pagador.com.br/webservice/pagador">
40
+ <amount>3.00</amount>
41
+ <boletoNumber>123123</boletoNumber>
42
+ <expirationDate>2012-01-08T00:00:00</expirationDate>
43
+ <url>https://homologacao.pagador.com.br/pagador/reenvia.asp?Id_Transacao=722934be-6756-477a-87ab-42115ee1424d</url>
44
+ <returnCode>0</returnCode>
45
+ <status>0</status>
46
+ </PagadorBoletoReturn>
47
+ EOXML
48
+ end
49
+
50
+ let(:invalid_xml) do
51
+ <<-EOXML
52
+ <?xml version="1.0" encoding="utf-8"?>
53
+ <PagadorBoletoReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
55
+ xmlns="https://www.pagador.com.br/webservice/pagador">
56
+ <amount xsi:nil="true" />
57
+ <expirationDate xsi:nil="true" />
58
+ <returnCode>1</returnCode>
59
+ <message>Invalid merchantId</message>
60
+ <status xsi:nil="true" />
61
+ </PagadorBoletoReturn>
62
+ EOXML
63
+ end
64
+
65
+
66
+ it "should call gateway with correct data" do
67
+ Braspag::Poster.any_instance.should_receive(:do_post).with(:generate_billet, {
68
+ "merchantId" => "#{merchant_id}",
69
+ "orderId" => "#{order.id}",
70
+ "customerName" => "#{customer.name}",
71
+ "customerIdNumber" => "#{customer.document}",
72
+ "amount" => "100,00",
73
+ "paymentMethod" => 6,
74
+ "boletoNumber" => "#{billet.id}",
75
+ "expirationDate" => "01/01/12",
76
+ "instructions" => "#{billet.instructions}",
77
+ "emails" => "#{customer.email}"
78
+ }
79
+ ).and_return(mock(:body => valid_xml))
80
+ connection.generate_billet(order, billet)
81
+ end
82
+
83
+ it "should populate data" do
84
+ Braspag::Poster.any_instance.should_receive(:do_post).and_return(mock(:body => valid_xml))
85
+ connection.generate_billet(order, billet)
86
+
87
+ billet.url.should eq('https://homologacao.pagador.com.br/pagador/reenvia.asp?Id_Transacao=722934be-6756-477a-87ab-42115ee1424d')
88
+ order.gateway_return_code.should eq('0')
89
+ order.gateway_status.should eq('0')
90
+ order.gateway_amount.should eq(3.00)
91
+ end
92
+
93
+ it "should return response object" do
94
+ Braspag::Poster.any_instance.should_receive(:do_post).and_return(mock(:body => valid_xml))
95
+ response = connection.generate_billet(order, billet)
96
+
97
+ response.success?.should be(true)
98
+ end
99
+
100
+ it "should return error in response" do
101
+ Braspag::Poster.any_instance.should_receive(:do_post).and_return(mock(:body => invalid_xml))
102
+ response = connection.generate_billet(order, billet)
103
+
104
+ response.success?.should be(false)
105
+ response.message.should eq('Invalid merchantId')
106
+ response.params.should eq({"url"=>nil, "amount"=>nil, "number"=>nil, "expiration_date"=>nil, "return_code"=>"1", "status"=>nil, "message"=>"Invalid merchantId"})
107
+ end
108
+ end
109
+ end
110
+
111
+ describe Braspag::Billet do
112
+ context "on generate" do
113
+ it "should allow blank for id" do
114
+ subject.id = ''
115
+ subject.valid?(:generate)
116
+ subject.errors.messages[:id].should be(nil)
117
+ end
118
+
119
+ it "should validate maximum 255 length of id" do
120
+ subject.id = '*' * 260
121
+ subject.valid?(:generate)
122
+ subject.errors.messages[:id].should include("is too long (maximum is 255 characters)")
123
+ end
124
+
125
+ it "should allow blank for instructions" do
126
+ subject.instructions = ''
127
+ subject.valid?(:generate)
128
+ subject.errors.messages[:instructions].should be(nil)
129
+ end
130
+
131
+ it "should validate maximum 512 length of instructions" do
132
+ subject.instructions = '*' * 520
133
+ subject.valid?(:generate)
134
+ subject.errors.messages[:instructions].should include("is too long (maximum is 512 characters)")
135
+ end
136
+
137
+ it "should not allow blank for due_date_on" do
138
+ subject.due_date_on = ''
139
+ subject.valid?(:generate)
140
+ subject.errors.messages[:due_date_on].should include("can't be blank")
141
+ end
142
+
143
+ it "should not allow invalid date for due_date_on" do
144
+ subject.due_date_on = '12345'
145
+ subject.valid?(:generate)
146
+ subject.errors.messages[:due_date_on].should include("invalid date")
147
+ end
148
+
149
+ it "should allow date for due_date_on" do
150
+ subject.due_date_on = Date.parse('07/03/1988')
151
+ subject.valid?(:generate)
152
+ subject.errors.messages[:due_date_on].should be(nil)
153
+ end
154
+ end
155
+ end
156
+