pagseguro-oficial 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -1
  3. data/README.md +54 -2
  4. data/examples/{authorization.rb → authorization/authorization.rb} +1 -1
  5. data/examples/{authorization_by_code.rb → authorization/authorization_by_code.rb} +1 -1
  6. data/examples/{authorization_by_notification_code.rb → authorization/authorization_by_notification_code.rb} +1 -1
  7. data/examples/refund.rb +25 -0
  8. data/examples/{abandoned_transactions.rb → transaction/abandoned_transactions.rb} +1 -1
  9. data/examples/{boleto_transaction_request.rb → transaction/boleto_transaction_request.rb} +1 -1
  10. data/examples/{credit_card_transaction_request.rb → transaction/credit_card_transaction_request.rb} +1 -1
  11. data/examples/{invalid_transaction_by_notification_code.rb → transaction/invalid_transaction_by_notification_code.rb} +1 -1
  12. data/examples/{online_debit_transaction.rb → transaction/online_debit_transaction.rb} +1 -1
  13. data/examples/{transaction_by_code.rb → transaction/transaction_by_code.rb} +1 -1
  14. data/examples/{transaction_by_notification_code.rb → transaction/transaction_by_notification_code.rb} +1 -1
  15. data/examples/{transaction_by_reference.rb → transaction/transaction_by_reference.rb} +1 -1
  16. data/examples/transaction/transaction_cancellation.rb +19 -0
  17. data/examples/transaction/transaction_status_history.rb +22 -0
  18. data/examples/{transactions_by_date.rb → transaction/transactions_by_date.rb} +1 -1
  19. data/lib/pagseguro/installment.rb +14 -0
  20. data/lib/pagseguro/payment_request/serializer.rb +48 -55
  21. data/lib/pagseguro/refund/request_serializer.rb +24 -0
  22. data/lib/pagseguro/refund/response.rb +33 -0
  23. data/lib/pagseguro/refund/response_serializer.rb +17 -0
  24. data/lib/pagseguro/refund/serializer.rb +24 -0
  25. data/lib/pagseguro/refund.rb +44 -0
  26. data/lib/pagseguro/transaction/collection.rb +20 -0
  27. data/lib/pagseguro/transaction/response.rb +37 -4
  28. data/lib/pagseguro/transaction/search.rb +1 -1
  29. data/lib/pagseguro/transaction/serializer.rb +14 -3
  30. data/lib/pagseguro/transaction/status_collection.rb +20 -0
  31. data/lib/pagseguro/transaction.rb +34 -14
  32. data/lib/pagseguro/transaction_cancellation/request_serializer.rb +18 -0
  33. data/lib/pagseguro/transaction_cancellation/response.rb +32 -0
  34. data/lib/pagseguro/transaction_cancellation/response_serializer.rb +17 -0
  35. data/lib/pagseguro/transaction_cancellation/serializer.rb +18 -0
  36. data/lib/pagseguro/transaction_cancellation.rb +40 -0
  37. data/lib/pagseguro/transaction_status.rb +14 -0
  38. data/lib/pagseguro/version.rb +1 -1
  39. data/lib/pagseguro.rb +13 -0
  40. data/spec/fixtures/refund/success.xml +2 -0
  41. data/spec/fixtures/transaction_cancellation/success.xml +2 -0
  42. data/spec/fixtures/transactions/status_history.xml +12 -0
  43. data/spec/pagseguro/refund/request_serializer_spec.rb +15 -0
  44. data/spec/pagseguro/refund/response_serializer_spec.rb +12 -0
  45. data/spec/pagseguro/refund/response_spec.rb +39 -0
  46. data/spec/pagseguro/refund/serializer_spec.rb +15 -0
  47. data/spec/pagseguro/refund_spec.rb +63 -0
  48. data/spec/pagseguro/request_spec.rb +0 -4
  49. data/spec/pagseguro/transaction/collection_spec.rb +43 -0
  50. data/spec/pagseguro/transaction/response_spec.rb +96 -0
  51. data/spec/pagseguro/transaction/search/search_abandoned_spec.rb +41 -28
  52. data/spec/pagseguro/transaction/search/search_by_date_spec.rb +20 -18
  53. data/spec/pagseguro/transaction/search/search_by_reference_spec.rb +13 -18
  54. data/spec/pagseguro/transaction/search_spec.rb +1 -6
  55. data/spec/pagseguro/transaction/serializer_spec.rb +11 -0
  56. data/spec/pagseguro/transaction/status_collection_spec.rb +43 -0
  57. data/spec/pagseguro/transaction_cancellation/request_serializer_spec.rb +13 -0
  58. data/spec/pagseguro/transaction_cancellation/response_serializer_spec.rb +9 -0
  59. data/spec/pagseguro/transaction_cancellation/response_spec.rb +42 -0
  60. data/spec/pagseguro/transaction_cancellation/serializer_spec.rb +13 -0
  61. data/spec/pagseguro/transaction_cancellation_spec.rb +58 -0
  62. data/spec/pagseguro/transaction_spec.rb +159 -13
  63. data/spec/pagseguro/transaction_status_spec.rb +7 -0
  64. data/spec/spec_helper.rb +9 -0
  65. metadata +64 -14
@@ -1,42 +1,55 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PagSeguro::SearchAbandoned do
4
- describe 'it searches abandoned transactions' do
5
- let(:options) { { starts_at: Time.now, ends_at: Time.now, page: 1, per_page: 10 } }
6
- let(:search) { PagSeguro::SearchAbandoned.new("transactions/abandoned", options, 1) }
7
- let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
8
- let(:xml) { Nokogiri::XML(source) }
9
- let(:response) { double(:response, data: xml, unauthorized?: false, bad_request?: false) }
4
+ subject do
5
+ PagSeguro::SearchAbandoned.new("transactions/abandoned", options, 1)
6
+ end
10
7
 
11
- describe 'the search abandoned' do
12
- before do
13
- FakeWeb.register_uri :get, %r[#{PagSeguro.api_url('v2/transactions/abandoned')}.*],
14
- body: xml
15
- end
8
+ let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
9
+ let(:body) { Nokogiri::XML(source) }
10
+
11
+ describe 'it searches abandoned transactions' do
12
+ before do
13
+ FakeWeb.register_uri :get,
14
+ %r{https://ws.pagseguro.uol.com.br/v2/transactions/abandoned.*finalDate=2015-11-12T16%3A59.*initialDate=2015-11-12T15%3A34.*page=1},
15
+ body: body
16
+ end
16
17
 
17
- let(:transaction) { double(:transaction) }
18
+ let(:options) do
19
+ {
20
+ starts_at: Time.new(2015, 11, 12, 15, 34),
21
+ ends_at: Time.new(2015, 11, 12, 16, 59),
22
+ page: 1,
23
+ per_page: 10
24
+ }
25
+ end
18
26
 
27
+ describe 'the search abandoned' do
19
28
  it 'returns an array of transactions' do
20
- expect(PagSeguro::Transaction).to receive(:load_from_xml).exactly(2)
21
- .times
22
- .and_return(transaction)
23
- expect(search.transactions).to eq([transaction, transaction])
29
+ expect(subject.transactions.size).to eq(2)
24
30
  end
31
+ end
32
+ end
25
33
 
26
- it 'returns an array whith given credentials' do
27
- options_with_credentials = {
28
- credentials: PagSeguro::ApplicationCredentials.new('APP_ID', 'APP_KEY')
29
- }.merge(options)
34
+ describe 'it searches abandoned transactions using credentials' do
35
+ before do
36
+ FakeWeb.register_uri :get,
37
+ %r{https://ws.pagseguro.uol.com.br/v2/transactions/abandoned.*appId=APP_ID.*appKey=APP_KEY.*finalDate=2015-11-12T16%3A59.*initialDate=2015-11-12T15%3A34.*},
38
+ body: body
39
+ end
30
40
 
31
- expect(PagSeguro::Transaction).to receive(:load_from_xml)
32
- .exactly(2)
33
- .times
34
- .and_return(transaction)
41
+ let(:options) do
42
+ {
43
+ starts_at: Time.new(2015, 11, 12, 15, 34),
44
+ ends_at: Time.new(2015, 11, 12, 16, 59),
45
+ page: 1,
46
+ per_page: 10,
47
+ credentials: PagSeguro::ApplicationCredentials.new('APP_ID', 'APP_KEY')
48
+ }
49
+ end
35
50
 
36
- expect(
37
- PagSeguro::SearchAbandoned.new('transactions/abandoned', options_with_credentials, 1).transactions
38
- ).to eq([transaction, transaction])
39
- end
51
+ it 'returns an array with given credentials' do
52
+ expect(subject.transactions.size).to eq(2)
40
53
  end
41
54
  end
42
55
  end
@@ -1,26 +1,28 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PagSeguro::SearchByDate do
4
- describe 'it searches transactions by date' do
5
- let(:options) { { starts_at: Time.now, ends_at: Time.now, per_page: 1 } }
6
- let(:search) { PagSeguro::SearchByDate.new("transactions", options, 1) }
7
- let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
8
- let(:xml) { Nokogiri::XML(source) }
9
- let(:response) { double(:response, data: xml, unauthorized?: false, bad_request?: false) }
4
+ subject do
5
+ PagSeguro::SearchByDate.new("transactions", options, 1)
6
+ end
7
+
8
+ before do
9
+ FakeWeb.register_uri :get,
10
+ %r{https://ws.pagseguro.uol.com.br/v3/transactions.*finalDate=2015-11-12T16%3A59.*initialDate=2015-11-12T15%3A34.*page=1},
11
+ body: body
12
+ end
10
13
 
11
- describe 'the search by date' do
12
- before do
13
- FakeWeb.register_uri :get, %r[#{PagSeguro.api_url('v3/transactions')}.*], body: xml
14
- end
14
+ let(:options) do
15
+ {
16
+ starts_at: Time.new(2015, 11, 12, 15, 34),
17
+ ends_at: Time.new(2015, 11, 12, 16, 59),
18
+ per_page: 1
19
+ }
20
+ end
15
21
 
16
- let(:transaction) { double(:transaction) }
22
+ let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
23
+ let(:body) { Nokogiri::XML(source) }
17
24
 
18
- it 'returns an array of transactions' do
19
- expect(PagSeguro::Transaction).to receive(:load_from_xml).exactly(2)
20
- .times
21
- .and_return(transaction)
22
- expect(search.transactions).to eq([transaction, transaction])
23
- end
24
- end
25
+ it 'searches transactions by date' do
26
+ expect(subject.transactions.size).to eq(2)
25
27
  end
26
28
  end
@@ -1,26 +1,21 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PagSeguro::SearchByReference do
4
- describe 'it searches transactions by reference' do
5
- let(:options) { { reference: "ref1234" } }
6
- let(:search) { PagSeguro::SearchByReference.new("transactions", options, 1) }
7
- let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
8
- let(:xml) { Nokogiri::XML(source) }
9
- let(:response) { double(:response, data: xml, unauthorized?: false, bad_request?: false) }
4
+ subject do
5
+ PagSeguro::SearchByReference.new("transactions", options, 1)
6
+ end
10
7
 
11
- describe 'the search by reference' do
12
- before do
13
- FakeWeb.register_uri :get, %r[#{PagSeguro.api_url('v3/transactions')}.*], body: xml
14
- end
8
+ before do
9
+ FakeWeb.register_uri :get,
10
+ %r{https://ws.pagseguro.uol.com.br/v3/transactions.*reference=ref1234.*},
11
+ body: body
12
+ end
15
13
 
16
- let(:transaction) { double(:transaction) }
14
+ let(:options) { { reference: "ref1234" } }
15
+ let(:source) { File.read("./spec/fixtures/transactions/search.xml") }
16
+ let(:body) { Nokogiri::XML(source) }
17
17
 
18
- it 'returns an array of transactions' do
19
- expect(PagSeguro::Transaction).to receive(:load_from_xml).exactly(2)
20
- .times
21
- .and_return(transaction)
22
- expect(search.transactions).to eq([transaction, transaction])
23
- end
24
- end
18
+ it 'searches transactions by reference' do
19
+ expect(subject.transactions.size).to eq(2)
25
20
  end
26
21
  end
@@ -29,13 +29,8 @@ describe PagSeguro::Search do
29
29
  end
30
30
 
31
31
  describe '#transactions' do
32
- let(:transaction) { double(:transaction) }
33
-
34
32
  it 'returns an array of transactions' do
35
- expect(PagSeguro::Transaction).to receive(:load_from_xml).exactly(2)
36
- .times
37
- .and_return(transaction)
38
- expect(search.transactions).to eq([transaction, transaction])
33
+ expect(search.transactions.size).to eq(2)
39
34
  end
40
35
  end
41
36
 
@@ -73,4 +73,15 @@ describe PagSeguro::Transaction::Serializer do
73
73
  it { expect(data).to include(cancellation_source: "PagSeguro") }
74
74
  it { expect(data).to include(escrow_end_date: Time.parse("2013-06-01T01:41:20.000-03:00")) }
75
75
  end
76
+
77
+ context "when looking for transaction status history" do
78
+ let(:source) { File.read("./spec/fixtures/transactions/status_history.xml") }
79
+ let(:xml) { Nokogiri::XML(source) }
80
+ let(:serializer) { described_class.new(xml) }
81
+ subject(:data) { serializer.serialize_status_history }
82
+
83
+ it { expect(data.first.code).to eq("1") }
84
+ it { expect(data.first.date).to eq(Time.parse("2015-02-24T10:23:34.000-03:00")) }
85
+ it { expect(data.first.notification_code).to eq("B7C381-7AADE5ADE576-8CC4159F8FBB-25C7D6") }
86
+ end
76
87
  end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::Transaction::StatusCollection do
4
+ let(:statuses) { [PagSeguro::TransactionStatus.new] }
5
+
6
+ it "should have a PagSeguro::Errors instance" do
7
+ expect(subject.errors).to be_a(PagSeguro::Errors)
8
+ end
9
+
10
+ context "when there are no statuses" do
11
+ before do
12
+ subject.statuses = []
13
+ end
14
+
15
+ it "is blank" do
16
+ expect(subject).to be_empty
17
+ end
18
+
19
+ it "doesn't have any status" do
20
+ expect(subject).not_to be_any
21
+ end
22
+ end
23
+
24
+ context "when there are statuses" do
25
+ before do
26
+ subject.statuses = statuses
27
+ end
28
+
29
+ it "is not blank" do
30
+ expect(subject).not_to be_empty
31
+ end
32
+
33
+ it "has any status" do
34
+ expect(subject).to be_any
35
+ end
36
+
37
+ it "has PagSeguro::TransactionStatus instances" do
38
+ subject.each do |status|
39
+ expect(status).to be_a(PagSeguro::TransactionStatus)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::TransactionCancellation::RequestSerializer do
4
+ subject do
5
+ PagSeguro::TransactionCancellation::RequestSerializer.new(cancellation)
6
+ end
7
+
8
+ let(:cancellation) do
9
+ PagSeguro::TransactionCancellation.new transaction_code: "1234"
10
+ end
11
+
12
+ it { expect(subject.to_params).to include(transactionCode: "1234") }
13
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::TransactionCancellation::ResponseSerializer do
4
+ let(:raw_xml) { File.read("./spec/fixtures/transaction_cancellation/success.xml") }
5
+ let(:xml) { Nokogiri::XML(raw_xml) }
6
+ subject { PagSeguro::TransactionCancellation::ResponseSerializer.new(xml) }
7
+
8
+ it { expect(subject.serialize).to include(result: "OK") }
9
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::TransactionCancellation::Response do
4
+ let(:http_response) do
5
+ response = double(body: "", code: 200, content_type: "text/xml", "[]" => nil)
6
+ Aitch::Response.new({xml_parser: Aitch::XMLParser}, response)
7
+ end
8
+ let(:cancellation) { PagSeguro::TransactionCancellation.new }
9
+
10
+ subject { PagSeguro::TransactionCancellation::Response.new(http_response, cancellation) }
11
+
12
+ describe "#serialize" do
13
+ context "when request succeeds" do
14
+ let(:raw_xml) { File.read("./spec/fixtures/transaction_cancellation/success.xml") }
15
+
16
+ it "returns PagSeguro::TransactionCancellation instance" do
17
+ expect(subject.serialize).to be_a(PagSeguro::TransactionCancellation)
18
+ end
19
+
20
+ it "not change cancellation errors" do
21
+ expect { subject.serialize }.not_to change { cancellation.errors.empty? }
22
+ end
23
+ end
24
+
25
+ context "when request fails" do
26
+ before do
27
+ allow(http_response).to receive(:success?).and_return(false)
28
+ allow(http_response).to receive(:bad_request?).and_return(true)
29
+ allow(http_response).to receive(:body).and_return(raw_xml)
30
+ end
31
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
32
+
33
+ it "returns PagSeguro::TransactionCancellation instance" do
34
+ expect(subject.serialize).to be_a(PagSeguro::TransactionCancellation)
35
+ end
36
+
37
+ it "change cancellation errors" do
38
+ expect { subject.serialize }.to change { cancellation.errors.empty? }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::TransactionCancellation::Serializer do
4
+ let(:cancellation) { PagSeguro::TransactionCancellation.new }
5
+ let(:params) { serializer.to_params }
6
+ subject(:serializer) { described_class.new(cancellation) }
7
+
8
+ before do
9
+ cancellation.transaction_code = "1234"
10
+ end
11
+
12
+ it { expect(params).to include(transactionCode: "1234") }
13
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+
3
+ describe PagSeguro::TransactionCancellation do
4
+ let(:xml_parsed) { Nokogiri::XML(raw_xml) }
5
+
6
+ it_assigns_attribute :transaction_code
7
+
8
+ describe "#register" do
9
+ subject { PagSeguro::TransactionCancellation.new }
10
+ let :http_request do
11
+ double(:ResponseRequest, success?: true, unauthorized?: false, bad_request?: false, data: xml_parsed, body: raw_xml, :xml? => true)
12
+ end
13
+
14
+ before do
15
+ allow(PagSeguro::Request).to receive(:post)
16
+ .with("transactions/cancels", "v2", {})
17
+ .and_return(http_request)
18
+ end
19
+
20
+ context "when request succeds" do
21
+ let(:raw_xml) { File.read("./spec/fixtures/transaction_cancellation/success.xml") }
22
+
23
+ it "returns boolean" do
24
+ expect(subject.register).to be_truthy
25
+ end
26
+
27
+ it "does not add errors" do
28
+ expect { subject.register }.not_to change { subject.errors.empty? }
29
+ end
30
+
31
+ it "updates attributes" do
32
+ expect { subject.register }.to change { subject.result }
33
+ end
34
+ end
35
+
36
+ context "when request fails" do
37
+ before do
38
+ allow(http_request).to receive(:success?).and_return(false)
39
+ allow(http_request).to receive(:bad_request?).and_return(true)
40
+ allow(http_request).to receive(:not_found?).and_return(false)
41
+ end
42
+
43
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
44
+
45
+ it "adds errors" do
46
+ expect { subject.register }.to change { subject.errors.empty? }
47
+ end
48
+ end
49
+ end
50
+
51
+ it '#update_attributes' do
52
+ cancellation = PagSeguro::TransactionCancellation.new
53
+
54
+ expect(cancellation).to receive(:result=).with("OK")
55
+
56
+ cancellation.update_attributes(result: "OK")
57
+ end
58
+ end
@@ -2,24 +2,112 @@ require "spec_helper"
2
2
 
3
3
  describe PagSeguro::Transaction do
4
4
  describe ".find_by_notification_code" do
5
- it "finds transaction by the given notificationCode" do
6
- PagSeguro::Transaction.stub :load_from_response
7
-
8
- PagSeguro::Request
9
- .should_receive(:get)
5
+ before do
6
+ allow(PagSeguro::Request).to receive(:get)
10
7
  .with("transactions/notifications/CODE", "v3", {})
11
- .and_return(double.as_null_object)
8
+ .and_return(request)
9
+ end
10
+ let(:parsed_xml) { Nokogiri::XML(raw_xml) }
11
+ let(:request) do
12
+ double(:Request, xml?: true, success?: true, unauthorized?: false,
13
+ bad_request?: false, body: raw_xml, data: parsed_xml)
14
+ end
15
+ subject { PagSeguro::Transaction.find_by_notification_code("CODE") }
16
+
17
+ context "when request succeds" do
18
+ let(:raw_xml) { File.read("./spec/fixtures/transactions/success.xml") }
19
+
20
+ it "returns an instance of transaction" do
21
+ expect(subject).to be_a(PagSeguro::Transaction)
22
+ end
23
+
24
+ context "returns transaction with correct attributes" do
25
+ it { expect(subject.code).to eq("667A3914-4F9F-4705-0EB6-CA6FA0DF8A19") }
26
+ it { expect(subject.reference).to eq("REF1234") }
27
+ it { expect(subject.type_id).to eq("1") }
28
+ it { expect(subject.payment_link).to eq("https://pagseguro.uol.com.br/checkout/imprimeBoleto.jhtml?code=667D39144F9F47059FB6CA6FA0DF8A20") }
29
+ it { expect(subject.status).to be_a(PagSeguro::PaymentStatus) }
30
+ it { expect(subject.status.id).to eq("1") }
31
+ it { expect(subject.payment_method.type).to eq(:boleto) }
32
+ it { expect(subject.gross_amount).to eq(459.5) }
33
+ it { expect(subject.discount_amount).to eq(0.0) }
34
+ it { expect(subject.net_amount).to eq(445.77) }
35
+ it { expect(subject.extra_amount).to eq(0.0) }
36
+ it { expect(subject.installments).to eq(1) }
37
+ it { expect(subject.sender.name).to eq("JOHN DOE") }
38
+ end
39
+
40
+ it "returns a collection with errors object" do
41
+ expect(subject.errors).to be_a(PagSeguro::Errors)
42
+ end
12
43
 
13
- PagSeguro::Transaction.find_by_notification_code("CODE")
44
+ it "returns a collection with no errors" do
45
+ expect(subject.errors).to be_empty
46
+ end
14
47
  end
15
48
 
16
- it "returns response with errors when request fails" do
17
- body = %[<?xml version="1.0"?><errors><error><code>1234</code><message>Sample error</message></error></errors>]
18
- FakeWeb.register_uri :get, %r[.+], status: [400, "Bad Request"], body: body, content_type: "text/xml"
19
- response = PagSeguro::Transaction.find_by_notification_code("invalid")
49
+ context "when request fails" do
50
+ before do
51
+ allow(request).to receive(:success?).and_return(false)
52
+ allow(request).to receive(:bad_request?).and_return(true)
53
+ allow(request).to receive(:not_found?).and_return(false)
54
+ end
55
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
20
56
 
21
- expect(response).to be_a(PagSeguro::Transaction::Response)
22
- expect(response.errors).to include("Sample error")
57
+ it "returns an instance of Transaction" do
58
+ expect(subject).to be_a(PagSeguro::Transaction)
59
+ end
60
+
61
+ it "returns a collection with errors" do
62
+ expect(subject.errors).not_to be_empty
63
+ end
64
+ end
65
+ end
66
+
67
+ describe ".find_by_code" do
68
+ before do
69
+ allow(PagSeguro::Request).to receive(:get)
70
+ .with("transactions/CODE", "v3", {})
71
+ .and_return(request)
72
+ end
73
+ let(:parsed_xml) { Nokogiri::XML(raw_xml) }
74
+ let(:request) do
75
+ double(:Request, xml?: true, success?: true, unauthorized?: false,
76
+ bad_request?: false, body: raw_xml, data: parsed_xml)
77
+ end
78
+ subject { PagSeguro::Transaction.find_by_code("CODE") }
79
+
80
+ context "when request succeds" do
81
+ let(:raw_xml) { File.read("./spec/fixtures/transactions/success.xml") }
82
+
83
+ it "returns an instance of transaction" do
84
+ expect(subject).to be_a(PagSeguro::Transaction)
85
+ end
86
+
87
+ it "returns a collection with errors object" do
88
+ expect(subject.errors).to be_a(PagSeguro::Errors)
89
+ end
90
+
91
+ it "returns a collection with no errors" do
92
+ expect(subject.errors).to be_empty
93
+ end
94
+ end
95
+
96
+ context "when request fails" do
97
+ before do
98
+ allow(request).to receive(:success?).and_return(false)
99
+ allow(request).to receive(:bad_request?).and_return(true)
100
+ allow(request).to receive(:not_found?).and_return(false)
101
+ end
102
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
103
+
104
+ it "returns an instance of Transaction" do
105
+ expect(subject).to be_a(PagSeguro::Transaction)
106
+ end
107
+
108
+ it "returns a collection with errors" do
109
+ expect(subject.errors).not_to be_empty
110
+ end
23
111
  end
24
112
  end
25
113
 
@@ -36,6 +124,53 @@ describe PagSeguro::Transaction do
36
124
  end
37
125
  end
38
126
 
127
+ describe ".find_status_history" do
128
+ before do
129
+ allow(PagSeguro::Request).to receive(:get)
130
+ .with("transactions/CODE/statusHistory", "v3", {})
131
+ .and_return(response)
132
+ end
133
+ let(:parsed_xml) { Nokogiri::XML(raw_xml) }
134
+ let(:response) do
135
+ double(:Response, xml?: true, success?: true, unauthorized?: false,
136
+ bad_request?: false, body: raw_xml, data: parsed_xml)
137
+ end
138
+ subject { PagSeguro::Transaction.find_status_history("CODE") }
139
+
140
+ context "when request succeds" do
141
+ let(:raw_xml) { File.read("./spec/fixtures/transactions/status_history.xml") }
142
+
143
+ it "returns an instance of StatusCollection" do
144
+ expect(subject).to be_a(PagSeguro::Transaction::StatusCollection)
145
+ end
146
+
147
+ it "returns a collection with errors object" do
148
+ expect(subject.errors).to be_a(PagSeguro::Errors)
149
+ end
150
+
151
+ it "returns a collection with no errors" do
152
+ expect(subject.errors).to be_empty
153
+ end
154
+ end
155
+
156
+ context "when request fails" do
157
+ before do
158
+ allow(response).to receive(:success?).and_return(false)
159
+ allow(response).to receive(:bad_request?).and_return(true)
160
+ allow(response).to receive(:not_found?).and_return(false)
161
+ end
162
+ let(:raw_xml) { File.read("./spec/fixtures/invalid_code.xml") }
163
+
164
+ it "returns an instance of StatusCollection" do
165
+ expect(subject).to be_a(PagSeguro::Transaction::StatusCollection)
166
+ end
167
+
168
+ it "returns a collection with errors" do
169
+ expect(subject.errors).not_to be_empty
170
+ end
171
+ end
172
+ end
173
+
39
174
  describe ".find_by_date" do
40
175
  it "initializes search with default options" do
41
176
  now = Time.now
@@ -157,4 +292,15 @@ describe PagSeguro::Transaction do
157
292
  it { expect(transaction.items.size).to eq(1) }
158
293
  it { expect(transaction).to respond_to(:escrow_end_date) }
159
294
  end
295
+
296
+ it "always set errors" do
297
+ expect(subject.errors).to be_a(PagSeguro::Errors)
298
+ end
299
+
300
+ it "#update_attributes" do
301
+ subject = PagSeguro::Transaction.new
302
+ expect(subject).to receive(:code=).with("1234")
303
+
304
+ subject.update_attributes(code: "1234")
305
+ end
160
306
  end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe PagSeguro::TransactionStatus do
4
+ it_assigns_attribute :code
5
+ it_assigns_attribute :date
6
+ it_assigns_attribute :notification_code
7
+ end
data/spec/spec_helper.rb CHANGED
@@ -25,4 +25,13 @@ RSpec.configure do |config|
25
25
  load "./lib/pagseguro.rb"
26
26
  FakeWeb.clean_registry
27
27
  end
28
+
29
+ config.after do
30
+ PagSeguro.configure do |config|
31
+ config.app_id = nil
32
+ config.app_key = nil
33
+ config.email = nil
34
+ config.token = nil
35
+ end
36
+ end
28
37
  end