pag_seguro 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/.gitignore +1 -0
  2. data/Gemfile +5 -2
  3. data/README.md +71 -6
  4. data/lib/pag_seguro/checkout.xml.haml +24 -5
  5. data/lib/pag_seguro/convert_field_to_digit.rb +15 -0
  6. data/lib/pag_seguro/day_of_year.rb +35 -0
  7. data/lib/pag_seguro/item.rb +9 -11
  8. data/lib/pag_seguro/notification.rb +1 -1
  9. data/lib/pag_seguro/payment.rb +26 -19
  10. data/lib/pag_seguro/pre_approval.rb +84 -0
  11. data/lib/pag_seguro/query.rb +27 -1
  12. data/lib/pag_seguro/sender.rb +2 -2
  13. data/lib/pag_seguro/shipping.rb +2 -2
  14. data/lib/pag_seguro/transaction.rb +1 -1
  15. data/lib/pag_seguro/version.rb +1 -1
  16. data/lib/pag_seguro.rb +9 -1
  17. data/lib/pagseguro_decimal_validator.rb +9 -0
  18. data/pag_seguro.gemspec +1 -0
  19. data/spec/factories.rb +137 -0
  20. data/spec/fixtures/transaction_history.xml +40 -0
  21. data/spec/pag_seguro/checkout_xml_spec.rb +142 -159
  22. data/spec/pag_seguro/convert_field_to_digit_spec.rb +68 -0
  23. data/spec/pag_seguro/day_of_year_spec.rb +49 -0
  24. data/spec/pag_seguro/integration/checkout_spec.rb +34 -67
  25. data/spec/pag_seguro/integration/config.yml +4 -4
  26. data/spec/pag_seguro/integration/query_spec.rb +56 -34
  27. data/spec/pag_seguro/item_spec.rb +46 -72
  28. data/spec/pag_seguro/payment_method_spec.rb +58 -63
  29. data/spec/pag_seguro/payment_spec.rb +150 -123
  30. data/spec/pag_seguro/pre_approval_spec.rb +112 -0
  31. data/spec/pag_seguro/query_spec.rb +111 -4
  32. data/spec/pag_seguro/sender_spec.rb +50 -62
  33. data/spec/pag_seguro/shipping_spec.rb +36 -51
  34. data/spec/spec_helper.rb +11 -20
  35. data/spec/support/transaction_shared_examples.rb +7 -7
  36. metadata +32 -3
@@ -1,155 +1,182 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe PagSeguro::Payment do
4
- context "instance" do
5
- context "accessors" do
6
- before { @payment = PagSeguro::Payment.new }
7
-
8
- it { @payment.should have_attribute_accessor(:id) }
9
- it { @payment.should have_attribute_accessor(:items) }
10
- it { @payment.should have_attribute_accessor(:sender) }
11
- it { @payment.should have_attribute_accessor(:shipping) }
12
- it { @payment.should have_attribute_accessor(:email) }
13
- it { @payment.should have_attribute_accessor(:token) }
14
- it { @payment.should have_attribute_accessor(:extra_amount) }
15
- it { @payment.should have_attribute_accessor(:redirect_url) }
16
- it { @payment.should have_attribute_accessor(:max_uses) }
17
- it { @payment.should have_attribute_accessor(:max_age) }
18
- it { @payment.should have_attribute_accessor(:response) }
19
-
20
- it "should respond to :code" do
21
- @payment.respond_to?(:code).should be_true
22
- end
4
+ let(:payment){ PagSeguro::Payment.new }
5
+ subject{ payment }
23
6
 
24
- it "should respond to :date" do
25
- @payment.respond_to?(:date).should be_true
26
- end
27
-
28
- it "should have a DateTime object as date" do
29
- response = double("response")
30
- response.stub(body: "<checkout><date>2001-02-03T04:05:06+07:00</date></checkout>")
31
- @payment.instance_variable_set(:"@response", response)
32
- date = @payment.send(:parse_date)
33
- date.should be_an_instance_of(DateTime)
34
- date.year.should == 2001
35
- date.month.should == 2
36
- date.day.should == 3
37
- end
7
+ it { should have_attribute_accessor(:id) }
8
+ it { should have_attribute_accessor(:items) }
9
+ it { should have_attribute_accessor(:sender) }
10
+ it { should have_attribute_accessor(:shipping) }
11
+ it { should have_attribute_accessor(:email) }
12
+ it { should have_attribute_accessor(:token) }
13
+ it { should have_attribute_accessor(:extra_amount) }
14
+ it { should have_attribute_accessor(:redirect_url) }
15
+ it { should have_attribute_accessor(:max_uses) }
16
+ it { should have_attribute_accessor(:max_age) }
17
+ it { should have_attribute_accessor(:response) }
18
+ it { should have_attribute_accessor(:pre_approval) }
38
19
 
39
- it "should have items" do
40
- @payment.items.should be_instance_of(Array)
41
- @payment.items.should be_empty
42
- end
20
+ it { should respond_to :code }
21
+ it { should respond_to :date }
43
22
 
44
- it "should have a sender" do
45
- @payment.sender.should be_instance_of(PagSeguro::Sender)
46
- end
47
- end
23
+ describe '#date' do
24
+ subject{ payment.send(:parse_date) }
25
+ before{ payment.stub response: double(:response, body: '<checkout><date>2001-02-03T04:05:06+07:00</date></checkout>') }
26
+
27
+ it { should be_an_instance_of(DateTime) }
28
+ its(:year){ should == 2001 }
29
+ its(:month){ should == 2 }
30
+ its(:day){ should == 3 }
31
+ end
32
+
33
+ describe '#code' do
34
+ before{ payment.stub response: double(:response, body: '<checkout><code>EE603A-59F0DEF0DAAD-2334FFBF9A1E-3223E3</code></checkout>') }
35
+
36
+ its(:code){ should == 'EE603A-59F0DEF0DAAD-2334FFBF9A1E-3223E3' }
37
+ end
38
+
39
+ its(:items){ should be_an_instance_of(Array) }
40
+ its(:items){ should be_empty }
41
+ its(:sender){ should be_an_instance_of(PagSeguro::Sender) }
42
+
43
+ context 'with email and token initialization' do
44
+ subject{ build(:payment) }
45
+ let(:payment){ subject }
46
+ its(:email){ should == 'myemail' }
47
+ its(:token){ should == 'mytoken' }
48
+
49
+ it { validate_presence_of :email }
50
+ it { validate_presence_of :token }
51
+
52
+ it { should_not allow_value('10,50').for(:extra_amount) }
53
+ it { should_not allow_value('R$ 10.50').for(:extra_amount) }
54
+ it { should_not allow_value('-10.50').for(:extra_amount) }
55
+ it { should allow_value('10.50').for(:extra_amount) }
56
+ it { should allow_value(10).for(:extra_amount) }
57
+ it { should allow_value(BigDecimal.new('10.5')).for(:extra_amount) }
48
58
 
49
- it "should allow to set email and token initialization" do
50
- payment = PagSeguro::Payment.new("mymail", "mytoken")
51
- payment.email.should == "mymail"
52
- payment.token.should == "mytoken"
59
+ it { should_not allow_value('something.com.br').for(:redirect_url)}
60
+ it { should allow_value('http://something.com.br').for(:redirect_url)}
61
+
62
+ it { should_not allow_value(0).for(:max_uses) }
63
+ it { should_not allow_value('0').for(:max_uses) }
64
+ it { should allow_value(10).for(:max_uses) }
65
+ it { should allow_value('10').for(:max_uses) }
66
+
67
+ it { should_not allow_value(29).for(:max_age) }
68
+ it { should allow_value(30).for(:max_age) }
69
+
70
+ it 'should not be valid if its pre_approval in invalid' do
71
+ payment.pre_approval = PagSeguro::PreApproval.new
72
+ payment.should_not be_valid
53
73
  end
54
-
55
- context "validation" do
56
- before { @payment = PagSeguro::Payment.new("mymail", "mytoken") }
57
- it "should be valid with valid attributes" do
58
- @payment.should be_valid
59
- end
60
-
61
- it "should not be valid without email" do
62
- @payment.email = nil
63
- @payment.should_not be_valid
64
- end
65
74
 
66
- it "should not be valid without token" do
67
- @payment.token = nil
68
- @payment.should_not be_valid
69
- end
70
-
71
- it "should not be valid with invalid extra amount format" do
72
- @payment.extra_amount = "10,50"
73
- @payment.should_not be_valid
74
- @payment.extra_amount = "R$ 10.50"
75
- @payment.should_not be_valid
76
- @payment.extra_amount = "10.50"
77
- @payment.should be_valid
78
- end
79
-
80
- it "should not allow invalid urls" do
81
- @payment.redirect_url = "httd://something"
82
- @payment.should_not be_valid
83
- @payment.redirect_url = "http://heavenstudio.com.br"
84
- @payment.should be_valid
75
+ it 'should not be valid if one of its items is invalid' do
76
+ payment.items = [PagSeguro::Item.new]
77
+ payment.should_not be_valid
78
+ end
79
+
80
+ context 'without items' do
81
+ it { should_not be_valid }
82
+ end
83
+
84
+ context 'with items' do
85
+ subject { build :payment_with_item }
86
+ it { should be_valid }
87
+ end
88
+
89
+ context 'checking out' do
90
+ let(:payment){ build(:payment) }
91
+ it 'should generate a checkout url with an external code' do
92
+ PagSeguro::Payment.checkout_payment_url('aabbcc').should == 'https://pagseguro.uol.com.br/v2/checkout/payment.html?code=aabbcc'
85
93
  end
86
94
 
87
- it "should not allow an invalid number of uses" do
88
- @payment.max_uses = "0"
89
- @payment.should_not be_valid
90
- @payment.max_uses = "10"
91
- @payment.should be_valid
95
+ it 'should have a checkout_url_with_params' do
96
+ payment.checkout_url_with_params.should == 'https://ws.pagseguro.uol.com.br/v2/checkout?email=myemail&token=mytoken'
92
97
  end
93
98
 
94
- it "should not allow an invalid second time" do
95
- @payment.max_age = "29"
96
- @payment.should_not be_valid
97
- @payment.max_age = "30"
98
- @payment.should be_valid
99
+ it 'should generate a checkout url based on the received response' do
100
+ payment.stub code: 'aabbcc'
101
+ payment.checkout_payment_url.should == 'https://pagseguro.uol.com.br/v2/checkout/payment.html?code=aabbcc'
99
102
  end
100
103
  end
101
- end
102
-
103
- context "checking out" do
104
- it "should generate a checkout url with an external code" do
105
- PagSeguro::Payment.checkout_payment_url("aabbcc").should == "https://pagseguro.uol.com.br/v2/checkout/payment.html?code=aabbcc"
106
- end
107
-
108
- it "should have a checkout_url_with_params" do
109
- PagSeguro::Payment.new("mymail", "mytoken").checkout_url_with_params.should == 'https://ws.pagseguro.uol.com.br/v2/checkout?email=mymail&token=mytoken'
110
- end
111
-
112
- it "should generate a checkout url based on the received response" do
113
- payment = PagSeguro::Payment.new("mymail", "mytoken")
114
- payment.stub(:code).and_return("aabbcc")
115
- payment.checkout_payment_url.should == "https://pagseguro.uol.com.br/v2/checkout/payment.html?code=aabbcc"
116
- end
117
-
118
- describe "parse_checkout_response" do
119
- before do
120
- @payment = PagSeguro::Payment.new("mymail", "mytoken")
121
- end
122
104
 
123
- it "should not raise errors if response code is 200" do
105
+ describe '#parse_checkout_response' do
106
+
107
+ it 'should not raise errors if response code is 200' do
124
108
  PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 200 }
125
- PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some body info" }
126
- lambda { @payment.send(:parse_checkout_response) }.should_not raise_error
109
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ 'some body info' }
110
+ expect { payment.send(:parse_checkout_response) }.to_not raise_error
127
111
  end
128
112
 
129
- it "should raise PagSeguro::Errors::InvalidData if response code is 400" do
113
+ it 'should raise PagSeguro::Errors::InvalidData if response code is 400' do
130
114
  PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 400 }
131
- PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some error description" }
132
- lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::InvalidData)
115
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ 'some error description' }
116
+ expect { payment.send(:parse_checkout_response) }.to raise_error(PagSeguro::Errors::InvalidData)
133
117
  end
134
118
 
135
- it "should raise PagSeguro::Errors::Unauthorized if response code is 400" do
119
+ it 'should raise PagSeguro::Errors::Unauthorized if response code is 400' do
136
120
  PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 401 }
137
- lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::Unauthorized)
121
+ expect { payment.send(:parse_checkout_response) }.to raise_error(PagSeguro::Errors::Unauthorized)
138
122
  end
139
123
 
140
- it "should raise PagSeguro::Errors::UnknownError if response code is not 200, 400 or 401" do
124
+ it 'should raise PagSeguro::Errors::UnknownError if response code is not 200, 400 or 401' do
141
125
  PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 300 }
142
- PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ "some response body" }
143
- lambda { @payment.send(:parse_checkout_response) }.should raise_error(PagSeguro::Errors::UnknownError)
126
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ 'some response body' }
127
+ expect { payment.send(:parse_checkout_response) }.to raise_error(PagSeguro::Errors::UnknownError)
128
+ end
129
+
130
+ it 'should set response attribute if code is 200' do
131
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :code){ 200 }
132
+ PagSeguro::Payment.any_instance.stub_chain(:send_checkout, :body){ 'some response body' }
133
+ expect { payment.send(:parse_checkout_response) }.to change { payment.response }.from(nil).to('some response body')
144
134
  end
145
135
 
146
- it "should be able to reset response" do
147
- @payment.response = "something"
148
- @payment.response.should be_present
149
- @payment.reset!
150
- @payment.response.should be_nil
136
+ it 'should be able to reset response' do
137
+ payment.response = 'something'
138
+ expect { payment.reset! }.to change{ payment.response }.from('something').to(nil)
151
139
  end
152
140
  end
153
-
141
+ end
142
+
143
+ describe '#code' do
144
+ it 'should call #parse_checkout_response if #response is nil' do
145
+ payment.stub(response: nil, parse_code: nil)
146
+ payment.should_receive(:parse_checkout_response)
147
+ payment.code
148
+ end
149
+
150
+ it 'should not call #parse_checkout_response if #response is present' do
151
+ payment.stub(response: true, parse_code: nil)
152
+ payment.should_not_receive(:parse_checkout_response)
153
+ payment.code
154
+ end
155
+
156
+ it 'should call #parse_code' do
157
+ payment.stub(response: true)
158
+ payment.should_receive(:parse_code)
159
+ payment.code
160
+ end
161
+ end
162
+
163
+ describe '#date' do
164
+ it 'should call #parse_checkout_response if #response is nil' do
165
+ payment.stub(response: nil, parse_date: nil)
166
+ payment.should_receive(:parse_checkout_response)
167
+ payment.date
168
+ end
169
+
170
+ it 'should not call #parse_checkout_response if #response is present' do
171
+ payment.stub(response: true, parse_date: nil)
172
+ payment.should_not_receive(:parse_checkout_response)
173
+ payment.date
174
+ end
175
+
176
+ it 'should call #parse_code' do
177
+ payment.stub(response: true)
178
+ payment.should_receive(:parse_date)
179
+ payment.date
180
+ end
154
181
  end
155
182
  end
@@ -0,0 +1,112 @@
1
+ # encoding: UTF-8
2
+ require "spec_helper"
3
+
4
+ describe PagSeguro::PreApproval do
5
+ it { should have_attribute_accessor(:name) }
6
+ it { should have_attribute_accessor(:details) }
7
+ it { should have_attribute_accessor(:amount_per_payment) }
8
+ it { should have_attribute_accessor(:period) }
9
+ it { should have_attribute_accessor(:day_of_week) }
10
+ it { should have_attribute_accessor(:day_of_month) }
11
+ it { should have_attribute_accessor(:day_of_year) }
12
+ it { should have_attribute_accessor(:initial_date) }
13
+ it { should have_attribute_accessor(:final_date) }
14
+ it { should have_attribute_accessor(:max_amount_per_period) }
15
+ it { should have_attribute_accessor(:max_total_amount) }
16
+ it { should have_attribute_accessor(:review_URL) }
17
+
18
+ it { should validate_presence_of :name }
19
+ it { should validate_presence_of :period }
20
+ it { should validate_presence_of :final_date }
21
+ it { should validate_presence_of :max_amount_per_period }
22
+ it { should validate_presence_of :max_total_amount }
23
+
24
+ [:weekly, :monthly, :bimonthly, :trimonthly, :semiannually, :yearly].each do |period_type|
25
+ it { should allow_value(period_type).for(:period) }
26
+ it { should allow_value(period_type.to_s).for(:period) }
27
+ it { should allow_value(period_type.to_s.upcase).for(:period) }
28
+ end
29
+ it { should_not allow_value(:some_other_period_type).for(:period) }
30
+
31
+ it { should allow_value( nil ).for(:initial_date) }
32
+ it { should_not allow_value( Time.now - 10.minutes ).for(:initial_date) }
33
+ it { should allow_value( Time.now ).for(:initial_date) }
34
+ it { should allow_value( (PagSeguro::PreApproval::DATE_RANGE - 5.minutes).from_now ).for(:initial_date) }
35
+ it { should_not allow_value( PagSeguro::PreApproval::DATE_RANGE.from_now + 5.minutes ).for(:initial_date) }
36
+
37
+ it { should_not allow_value( nil ).for(:final_date) }
38
+ it { should_not allow_value( Time.now - 10.minutes ).for(:final_date) }
39
+ it { should allow_value( Time.now ).for(:final_date) }
40
+ it { should allow_value( (PagSeguro::PreApproval::DATE_RANGE - 5.minutes).from_now ).for(:final_date) }
41
+ it { should_not allow_value( PagSeguro::PreApproval::DATE_RANGE.from_now + 5.minutes ).for(:final_date) }
42
+
43
+ context "with an initial date" do
44
+ subject { PagSeguro::PreApproval.new(initial_date: 5.days.from_now) }
45
+
46
+ it { should_not allow_value( nil ).for(:final_date) }
47
+ it { should_not allow_value( Time.now - 10.minutes + 5.days ).for(:final_date) }
48
+ it { should allow_value( Time.now + 5.days ).for(:final_date) }
49
+ it { should allow_value( (PagSeguro::PreApproval::DATE_RANGE - 5.minutes + 5.days).from_now ).for(:final_date) }
50
+ it { should_not allow_value( PagSeguro::PreApproval::DATE_RANGE.from_now + 5.minutes + 5.days ).for(:final_date) }
51
+ end
52
+
53
+ describe "initialized with minimum attributes" do
54
+ subject{ build :minimum_pre_approval }
55
+ it { should be_valid }
56
+ end
57
+
58
+ describe "initialized with attributes" do
59
+ subject{ build :pre_approval }
60
+
61
+ it { should be_valid }
62
+ its(:name){ should == "Super seguro para notebook" }
63
+ its(:details){ should == "Toda segunda feira será cobrado o valor de R$150,00 para o seguro do notebook" }
64
+ its(:amount_per_payment){ should == '150.00' }
65
+ its(:initial_date){ Date.new(2015, 1, 17) }
66
+ its(:final_date){ Date.new(2017, 1, 17) }
67
+ its(:max_amount_per_period){ should == '200.00' }
68
+ its(:max_total_amount){ should == '900.00' }
69
+ its(:review_URL){ should == "http://sounoob.com.br/produto1" }
70
+
71
+ context "weekly" do
72
+ subject{ build :weekly_pre_approval }
73
+
74
+ it { should ensure_inclusion_of(:day_of_week).in_array(%w(monday tuesday wednesday thursday friday saturday sunday)) }
75
+
76
+ its(:period){ should == 'weekly' }
77
+ its(:day_of_week){ should == 'monday' }
78
+ it { should be_weekly }
79
+ it { should_not be_monthly }
80
+ it { should_not be_yearly }
81
+
82
+ end
83
+
84
+ context "monthly" do
85
+ subject{ build :monthly_pre_approval }
86
+
87
+ it { should ensure_inclusion_of(:day_of_month).in_range(1..28) }
88
+
89
+ its(:period){ should == 'monthly' }
90
+ its(:day_of_month){ should == 3 }
91
+ it { should_not be_weekly }
92
+ it { should be_monthly }
93
+ it { should_not be_yearly }
94
+ end
95
+
96
+ context "yearly" do
97
+ subject{ build :yearly_pre_approval }
98
+
99
+ it { should validate_presence_of(:day_of_year) }
100
+ it { should allow_value('10-22').for(:day_of_year) }
101
+ it { should allow_value('01-01').for(:day_of_year) }
102
+ it { should allow_value(PagSeguro::DayOfYear.new(month: 1, day: 1)).for(:day_of_year) }
103
+ it { should_not allow_value('1-1').for(:day_of_year) }
104
+
105
+ its(:period){ should == 'yearly' }
106
+ its(:day_of_year){ should == '03-01' }
107
+ it { should_not be_weekly }
108
+ it { should_not be_monthly }
109
+ it { should be_yearly }
110
+ end
111
+ end
112
+ end
@@ -3,9 +3,116 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe PagSeguro::Query do
6
- before { PagSeguro::Query.any_instance.stub(transaction_data: transaction_data) }
7
- let(:transaction){ PagSeguro::Query.new("mail", "token", "trans_code") }
8
- subject{ PagSeguro::Query }
6
+ let(:transactions_xml){ File.open( File.expand_path( File.dirname(__FILE__) + '/../fixtures/transaction_history.xml') ) }
9
7
 
10
- it_behaves_like "a transaction"
8
+ describe "instance" do
9
+ before { PagSeguro::Query.any_instance.stub(transaction_data: transaction_data) }
10
+ let(:transaction){ PagSeguro::Query.new("mail", "token", "trans_code") }
11
+ subject{ PagSeguro::Query }
12
+
13
+ it_behaves_like "a transaction"
14
+ end
15
+
16
+ describe "::find" do
17
+ it "raises error without valid credentials" do
18
+ expect { PagSeguro::Query.find nil, nil }.to raise_error(RestClient::Exception, "401 Unauthorized")
19
+ end
20
+
21
+ context "with a stubbed response" do
22
+ before { RestClient.stub(get: transactions_xml) }
23
+ subject{ PagSeguro::Query.find "email", "token", options: true }
24
+
25
+ it "calls search_url with received options" do
26
+ PagSeguro::Query.should_receive(:search_params).with("email", "token", {options: true})
27
+ PagSeguro::Query.find "email", "token", options: true
28
+ end
29
+
30
+ it{ should be_a Array }
31
+ it{ should have(2).transactions }
32
+ it("should have transaction ids"){ subject.map(&:id).should == ["REF1234", "REF5678"] }
33
+ end
34
+ end
35
+
36
+ describe "::search_params" do
37
+ it("returns an Hash"){ PagSeguro::Query.search_params("email", "token").should be_a Hash }
38
+
39
+ context "with default options" do
40
+ let(:search_params) { PagSeguro::Query.search_params("email", "token") }
41
+ it { search_params.keys.should_not include :maxPageResults }
42
+ it { search_params.keys.should_not include :page }
43
+ it { search_params[:finalDate].should include Date.today.iso8601 }
44
+ it { search_params[:initialDate].should include Date.yesterday.iso8601 }
45
+
46
+ it "should call parse_dates" do
47
+ PagSeguro::Query.should_receive(:parse_dates)
48
+ search_params
49
+ end
50
+ end
51
+
52
+ context "with page number set to 2" do
53
+ let(:search_params){ PagSeguro::Query.search_params("email", "token", page: 2) }
54
+ it { search_params[:page].should == 2 }
55
+ end
56
+
57
+ context "with max_page_results set to 100" do
58
+ let(:search_params){ PagSeguro::Query.search_params("email", "token", max_page_results: 100) }
59
+ it { search_params[:maxPageResults].should == 100 }
60
+ end
61
+
62
+ context "with initial_date set to 20 days ago" do
63
+ let(:search_params){ PagSeguro::Query.search_params("email", "token", initial_date: 20.days.ago) }
64
+ it { search_params[:initialDate].should include 20.days.ago.iso8601 }
65
+ end
66
+
67
+ context "with final_date set to 10 days ago" do
68
+ let(:search_params){ PagSeguro::Query.search_params("email", "token", initial_date: 20.days.ago, final_date: 10.days.ago) }
69
+ it { search_params[:finalDate].should include 10.days.ago.iso8601 }
70
+ end
71
+ end
72
+
73
+ describe "::parse_dates" do
74
+ it "raises error if initial date is less than 6 months ago" do
75
+ expect {
76
+ PagSeguro::Query.parse_dates initial_date: 7.months.ago
77
+ }.to raise_error("Invalid initial date. Must be bigger than 6 months ago")
78
+ end
79
+
80
+ it "raises error if final date is bigger than today" do
81
+ expect {
82
+ PagSeguro::Query.parse_dates final_date: 1.day.from_now
83
+ }.to raise_error("Invalid end date. Must be less than today")
84
+ end
85
+
86
+ it "raises error if final date is less than initial date" do
87
+ expect {
88
+ PagSeguro::Query.parse_dates initial_date: 30.days.ago, final_date: 31.days.ago
89
+ }.to raise_error("Invalid end date. Must be bigger than initial date")
90
+ end
91
+
92
+ it "raises error if final date is bigger than initial date in more than 30 days" do
93
+ expect {
94
+ PagSeguro::Query.parse_dates initial_date: 32.days.ago, final_date: 1.day.ago
95
+ }.to raise_error("Invalid end date. Must not differ from initial date in more than 30 days")
96
+ end
97
+
98
+ context "first return argument" do
99
+ it "returns yesterday if no initial date was given" do
100
+ PagSeguro::Query.parse_dates.first.should == (Time.now - 1.day).iso8601
101
+ end
102
+
103
+ it "returns the initial date in iso8601 format if provided" do
104
+ PagSeguro::Query.parse_dates(initial_date: 3.days.ago).first.should == 3.days.ago.iso8601
105
+ end
106
+ end
107
+
108
+ context "second return argument" do
109
+ it "returns initial_date + 1 day no final date was given" do
110
+ PagSeguro::Query.parse_dates(initial_date: 2.days.ago).last.should == 1.day.ago.iso8601
111
+ end
112
+
113
+ it "returns the final date in iso8601 format if provided" do
114
+ PagSeguro::Query.parse_dates(initial_date: 3.days.ago, final_date: 2.days.ago).last.should == 2.days.ago.iso8601
115
+ end
116
+ end
117
+ end
11
118
  end