rspreedly 0.1.11 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
data/spec/base_spec.rb CHANGED
@@ -8,13 +8,14 @@ describe RSpreedly::Base do
8
8
 
9
9
  describe ".api_request" do
10
10
  it "should raise AccessDenied if a 401 is received" do
11
- stub_http_with_code(401)
11
+ stub_request(:put, spreedly_url("/")).to_return(:status => 401)
12
+
12
13
  lambda{
13
14
  RSpreedly::Base.api_request(:put, '/')
14
15
  }.should raise_error(RSpreedly::Error::AccessDenied)
15
16
  end
16
17
  end
17
-
18
+
18
19
  describe "attributes=" do
19
20
  before(:each) do
20
21
  @api = TestAPI.new
@@ -24,62 +25,62 @@ describe RSpreedly::Base do
24
25
  @api.attributes = {:something => "test"}
25
26
  @api.something.should == "test"
26
27
  end
27
-
28
+
28
29
  it "should not fail if an attribute does not exist" do
29
30
  lambda{
30
31
  @api.attributes = {:foo => true}
31
32
  }.should_not raise_error
32
33
  end
33
34
  end
34
-
35
+
35
36
  describe "error messages" do
36
-
37
+
37
38
  before(:each) do
38
39
  @api = TestAPI.new
39
40
  end
40
-
41
+
41
42
  def do_request
42
43
  @api.api_request(:post, "/")
43
44
  end
44
-
45
+
45
46
  def failing_request
46
47
  lambda{
47
48
  do_request
48
49
  }.should raise_error
49
50
  end
50
-
51
+
51
52
  it "should not set any errors on a successful request" do
52
- stub_http_with_code(200)
53
+ stub_request(:post, spreedly_url("/")).to_return(:status => 200)
53
54
  do_request
54
55
  @api.errors.should be_empty
55
56
  end
56
-
57
+
57
58
  it "should set one error in the error array if a string is return " do
58
- stub_http_with_fixture("error_string.txt", 422)
59
+ stub_request(:post, spreedly_url("/")).to_return(:body => fixture("error_string.txt"), :status => 422)
59
60
  failing_request
60
61
  @api.errors.should == ["Some error back from the response as a string"]
61
62
  end
62
-
63
+
63
64
  it "should set one error in the error array if an xml error with one item is returned" do
64
- stub_http_with_fixture("error.xml", 422)
65
+ stub_request(:post, spreedly_url("/")).to_return(:body => fixture("error.xml"), :status => 422)
65
66
  failing_request
66
- @api.errors.should == ["Email can't be blank"]
67
+ @api.errors.should == ["Email can't be blank"]
67
68
  end
68
-
69
+
69
70
  it "should set multiple errors in the error array if an xml error with multiple items is returned" do
70
- stub_http_with_fixture("errors.xml", 422)
71
+ stub_request(:post, spreedly_url("/")).to_return(:body => fixture("errors.xml"), :status => 422)
71
72
  failing_request
72
- @api.errors.should == ["Email can't be blank", "Name can't be blank"]
73
- end
74
-
73
+ @api.errors.should == ["Email can't be blank", "Name can't be blank"]
74
+ end
75
+
75
76
  it "should reset errors on each call" do
76
77
  # failing one first, which will generate some errors
77
- stub_http_with_fixture("errors.xml", 422)
78
+ stub_request(:post, spreedly_url("/")).to_return(:body => fixture("errors.xml"), :status => 422)
78
79
  failing_request
79
80
  @api.errors.should_not be_empty
80
-
81
+
81
82
  # now a successful one should clear the errors
82
- stub_http_with_code(200)
83
+ stub_request(:post, spreedly_url("/")).to_return(:status => 200)
83
84
  do_request
84
85
  @api.errors.should be_empty
85
86
  end
data/spec/config_spec.rb CHANGED
@@ -2,143 +2,143 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe RSpreedly::Config do
4
4
 
5
- before :each do
6
-
5
+ before :each do
6
+
7
7
  RSpreedly::Config.clear
8
8
  RSpreedly::Config.setup do |c|
9
9
  c[:one] = 1
10
10
  c[:two] = 2
11
11
  end
12
-
12
+
13
13
  end
14
14
 
15
15
  describe "logger" do
16
-
17
- before :each do
16
+
17
+ before :each do
18
18
  Object.send(:remove_const, :RAILS_DEFAULT_LOGGER) if defined?(RAILS_DEFAULT_LOGGER)
19
19
  end
20
-
20
+
21
21
  it "should default to RAILS_DEFAULT_LOGGER if defined" do
22
22
  RAILS_DEFAULT_LOGGER = "something"
23
23
  RSpreedly::Config.reset
24
24
  RSpreedly::Config.logger.should == "something"
25
25
  end
26
-
26
+
27
27
  it "should default to a Logger if RAILS_DEFAULT_LOGGER is not defined" do
28
- RSpreedly::Config.reset
28
+ RSpreedly::Config.reset
29
29
  RSpreedly::Config.logger.should be_a(Logger)
30
30
  end
31
-
31
+
32
32
  end
33
33
 
34
34
  describe "configuration" do
35
-
35
+
36
36
  it "should return the configuration hash" do
37
37
  RSpreedly::Config.configuration.should == {:one => 1, :two => 2}
38
38
  end
39
-
39
+
40
40
  end
41
-
41
+
42
42
  describe "[]" do
43
-
43
+
44
44
  it "should return the config option matching the key" do
45
45
  RSpreedly::Config[:one].should == 1
46
46
  end
47
-
47
+
48
48
  it "should return nil if the key doesn't exist" do
49
49
  RSpreedly::Config[:monkey].should be_nil
50
50
  end
51
-
51
+
52
52
  end
53
-
53
+
54
54
  describe "[]=" do
55
-
55
+
56
56
  it "should set the config option for the key" do
57
57
  lambda{
58
- RSpreedly::Config[:banana] = :yellow
59
- }.should change(RSpreedly::Config, :banana).from(nil).to(:yellow)
58
+ RSpreedly::Config[:banana] = :yellow
59
+ }.should change(RSpreedly::Config, :banana).from(nil).to(:yellow)
60
60
  end
61
-
61
+
62
62
  end
63
-
63
+
64
64
  describe "delete" do
65
-
65
+
66
66
  it "should delete the config option for the key" do
67
67
  lambda{
68
68
  RSpreedly::Config.delete(:one)
69
- }.should change(RSpreedly::Config, :one).from(1).to(nil)
69
+ }.should change(RSpreedly::Config, :one).from(1).to(nil)
70
70
  end
71
-
71
+
72
72
  it "should leave the config the same if the key doesn't exist" do
73
73
  lambda{
74
- RSpreedly::Config.delete(:test)
74
+ RSpreedly::Config.delete(:test)
75
75
  }.should_not change(RSpreedly::Config, :configuration)
76
76
  end
77
-
77
+
78
78
  end
79
-
79
+
80
80
  describe "fetch" do
81
-
81
+
82
82
  it "should return the config option matching the key if it exists" do
83
83
  RSpreedly::Config.fetch(:one, 100).should == 1
84
84
  end
85
-
85
+
86
86
  it "should return the config default if the key doesn't exist" do
87
- RSpreedly::Config.fetch(:other, 100).should == 100
87
+ RSpreedly::Config.fetch(:other, 100).should == 100
88
88
  end
89
-
89
+
90
90
  end
91
-
91
+
92
92
  describe "to_hash" do
93
-
93
+
94
94
  it "should return a hash of the configuration" do
95
- RSpreedly::Config.to_hash.should == {:one => 1, :two => 2}
95
+ RSpreedly::Config.to_hash.should == {:one => 1, :two => 2}
96
96
  end
97
-
97
+
98
98
  end
99
-
99
+
100
100
  describe "setup" do
101
-
101
+
102
102
  it "should yield self" do
103
103
  RSpreedly::Config.setup do |c|
104
104
  c.should == RSpreedly::Config
105
105
  end
106
106
  end
107
-
107
+
108
108
  it "should let you set items on the configuration object as a hash" do
109
109
  lambda{
110
110
  RSpreedly::Config.setup do |c|
111
111
  c[:bananas] = 100
112
112
  end
113
- }.should change(RSpreedly::Config, :bananas).from(nil).to(100)
113
+ }.should change(RSpreedly::Config, :bananas).from(nil).to(100)
114
114
  end
115
-
115
+
116
116
  it "should let you set items on the configuration object as a method" do
117
117
  lambda{
118
118
  RSpreedly::Config.setup do |c|
119
119
  c.monkeys = 100
120
120
  end
121
- }.should change(RSpreedly::Config, :monkeys).from(nil).to(100)
122
- end
123
-
121
+ }.should change(RSpreedly::Config, :monkeys).from(nil).to(100)
122
+ end
123
+
124
124
  end
125
-
125
+
126
126
  describe "calling a missing method" do
127
-
127
+
128
128
  it "should retreive the config if the method matches a key" do
129
129
  RSpreedly::Config.one.should == 1
130
130
  end
131
-
131
+
132
132
  it "should retreive nil if the method doesn't match a key" do
133
133
  RSpreedly::Config.moo.should be_nil
134
134
  end
135
-
135
+
136
136
  it "should set the value of the config item matching the method name if it's an assignment" do
137
137
  lambda{
138
138
  RSpreedly::Config.trees = 3
139
- }.should change(RSpreedly::Config, :trees).from(nil).to(3)
140
- end
141
-
139
+ }.should change(RSpreedly::Config, :trees).from(nil).to(3)
140
+ end
141
+
142
142
  end
143
143
 
144
144
  end
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <errors>
3
+ <error>Amount is invalid. The total store credit for a subscriber must be greater than or equal to 0.</error>
4
+ </errors>
File without changes
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <nil-classes type="array"/>
@@ -54,7 +54,7 @@
54
54
  <description></description>
55
55
  <duration-quantity type="integer">1</duration-quantity>
56
56
  <duration-units>months</duration-units>
57
- <enabled type="boolean">true</enabled>
57
+ <enabled type="boolean">false</enabled>
58
58
  <feature-level>corporate</feature-level>
59
59
  <force-recurring type="boolean">true</force-recurring>
60
60
  <id type="integer">44</id>
@@ -76,7 +76,7 @@
76
76
  <description></description>
77
77
  <duration-quantity type="integer">0</duration-quantity>
78
78
  <duration-units>days</duration-units>
79
- <enabled type="boolean">true</enabled>
79
+ <enabled type="boolean">false</enabled>
80
80
  <feature-level>free</feature-level>
81
81
  <force-recurring type="boolean">false</force-recurring>
82
82
  <id type="integer">45</id>
@@ -0,0 +1,63 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <transactions type="array">
3
+ <transaction>
4
+ <amount type="decimal">24.0</amount>
5
+ <created-at type="datetime">2009-09-26T03:06:30Z</created-at>
6
+ <currency-code>USD</currency-code>
7
+ <description>Subscription</description>
8
+ <detail-type>Subscription</detail-type>
9
+ <expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
10
+ <id type="integer">20</id>
11
+ <invoice-id type="integer">64</invoice-id>
12
+ <start-time type="datetime">2009-09-26T03:06:30Z</start-time>
13
+ <terms>3 months</terms>
14
+ <updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
15
+ <price>$24.00</price>
16
+ <subscriber-customer-id>39053</subscriber-customer-id>
17
+ <detail>
18
+ <payment-method>visa</payment-method>
19
+ <recurring type="boolean">false</recurring>
20
+ <feature-level type="string">example</feature-level>
21
+ </detail>
22
+ </transaction>
23
+ <transaction>
24
+ <amount type="decimal">24.0</amount>
25
+ <created-at type="datetime">2009-09-26T03:06:30Z</created-at>
26
+ <currency-code>USD</currency-code>
27
+ <description>Subscription</description>
28
+ <detail-type>Subscription</detail-type>
29
+ <expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
30
+ <id type="integer">20</id>
31
+ <invoice-id type="integer">64</invoice-id>
32
+ <start-time type="datetime">2009-09-26T03:06:30Z</start-time>
33
+ <terms>3 months</terms>
34
+ <updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
35
+ <price>$24.00</price>
36
+ <subscriber-customer-id>39053</subscriber-customer-id>
37
+ <detail>
38
+ <payment-method>visa</payment-method>
39
+ <recurring type="boolean">false</recurring>
40
+ <feature-level type="string">example</feature-level>
41
+ </detail>
42
+ </transaction>
43
+ <transaction>
44
+ <amount type="decimal">24.0</amount>
45
+ <created-at type="datetime">2009-09-26T03:06:30Z</created-at>
46
+ <currency-code>USD</currency-code>
47
+ <description>Subscription</description>
48
+ <detail-type>Subscription</detail-type>
49
+ <expires-at type="datetime">2009-12-26T04:06:30Z</expires-at>
50
+ <id type="integer">20</id>
51
+ <invoice-id type="integer">64</invoice-id>
52
+ <start-time type="datetime">2009-09-26T03:06:30Z</start-time>
53
+ <terms>3 months</terms>
54
+ <updated-at type="datetime">2009-09-26T03:06:30Z</updated-at>
55
+ <price>$24.00</price>
56
+ <subscriber-customer-id>39053</subscriber-customer-id>
57
+ <detail>
58
+ <payment-method>visa</payment-method>
59
+ <recurring type="boolean">false</recurring>
60
+ <feature-level type="string">example</feature-level>
61
+ </detail>
62
+ </transaction>
63
+ </transactions>
data/spec/invoice_spec.rb CHANGED
@@ -10,46 +10,58 @@ describe RSpreedly::Invoice do
10
10
  end
11
11
 
12
12
  it "should return true if successful" do
13
- stub_http_with_fixture("invoice_created.xml", 201)
13
+ stub_request(:post, spreedly_url("/invoices.xml")).
14
+ to_return(:body => fixture("invoice_created.xml"), :status => 201)
15
+
14
16
  @invoice.create.should be_true
15
- end
16
-
17
+ end
18
+
17
19
  it "should update the invoice if successful" do
18
- stub_http_with_fixture("invoice_created.xml", 200)
20
+ stub_request(:post, spreedly_url("/invoices.xml")).
21
+ to_return(:body => fixture("invoice_created.xml"), :status => 201)
22
+
19
23
  lambda{
20
24
  @invoice.create
21
- }.should change(@invoice, :price).to("$0.00")
25
+ }.should change(@invoice, :price).from(nil).to("$0.00")
22
26
  end
23
-
27
+
24
28
  it "should setup line items in the invoice if successful" do
25
- stub_http_with_fixture("invoice_created.xml", 200)
29
+ stub_request(:post, spreedly_url("/invoices.xml")).
30
+ to_return(:body => fixture("invoice_created.xml"), :status => 201)
31
+
26
32
  @invoice.create
27
33
  @invoice.line_items.size.should == 1
28
34
  @invoice.line_items[0].should be_a(RSpreedly::LineItem)
29
- end
30
-
35
+ end
36
+
31
37
  it "should raise NotFound if the plan doesn't exist" do
32
- stub_http_with_fixture("plan_not_found.xml", 404)
38
+ stub_request(:post, spreedly_url("/invoices.xml")).
39
+ to_return(:body => fixture("plan_not_found.xml"), :status => 404)
40
+
33
41
  lambda{
34
42
  @invoice.create!
35
- }.should raise_error(RSpreedly::Error::NotFound)
43
+ }.should raise_error(RSpreedly::Error::NotFound)
36
44
  end
37
-
45
+
38
46
  it "should raise BadRequest if the invoice is invalid" do
39
- stub_http_with_fixture("invoice_invalid.xml", 422)
47
+ stub_request(:post, spreedly_url("/invoices.xml")).
48
+ to_return(:body => fixture("invoice_invalid.xml"), :status => 422)
49
+
40
50
  lambda{
41
51
  @invoice.create!
42
52
  }.should raise_error(RSpreedly::Error::BadRequest)
43
53
  end
44
-
54
+
45
55
  it "should raise Forbidden if the plan is disabled" do
46
- stub_http_with_fixture("plan_disabled.xml", 403)
56
+ stub_request(:post, spreedly_url("/invoices.xml")).
57
+ to_return(:body => fixture("plan_disabled.xml"), :status => 403)
58
+
47
59
  lambda{
48
60
  @invoice.create!
49
- }.should raise_error(RSpreedly::Error::Forbidden)
50
- end
61
+ }.should raise_error(RSpreedly::Error::Forbidden)
62
+ end
51
63
  end
52
-
64
+
53
65
  describe "#create" do
54
66
 
55
67
  before(:each) do
@@ -58,104 +70,127 @@ describe RSpreedly::Invoice do
58
70
  end
59
71
 
60
72
  it "should return true if successful" do
61
- stub_http_with_fixture("invoice_created.xml", 201)
73
+ stub_request(:post, spreedly_url("/invoices.xml")).
74
+ to_return(:body => fixture("invoice_created.xml"), :status => 201)
75
+
62
76
  @invoice.create.should be_true
63
- end
64
-
77
+ end
78
+
65
79
  it "should return nil if the plan doesn't exist" do
66
- stub_http_with_fixture("plan_not_found.xml", 404)
80
+ stub_request(:post, spreedly_url("/invoices.xml")).
81
+ to_return(:body => fixture("plan_not_found.xml"), :status => 404)
82
+
67
83
  @invoice.create.should be_nil
68
84
  end
69
-
85
+
70
86
  it "should return nil if the invoice is invalid" do
71
- stub_http_with_fixture("invoice_invalid.xml", 422)
87
+ stub_request(:post, spreedly_url("/invoices.xml")).
88
+ to_return(:body => fixture("invoice_invalid.xml"), :status => 422)
89
+
72
90
  @invoice.create.should be_nil
73
91
  end
74
-
92
+
75
93
  it "should return nil if the plan is disabled" do
76
- stub_http_with_fixture("plan_disabled.xml", 403)
94
+ stub_request(:post, spreedly_url("/invoices.xml")).
95
+ to_return(:body => fixture("plan_disabled.xml"), :status => 403)
96
+
77
97
  @invoice.create.should be_nil
78
- end
98
+ end
79
99
  end
80
-
81
-
100
+
101
+
82
102
  describe "#pay" do
83
103
 
84
104
  before(:each) do
85
105
  @invoice = RSpreedly::Invoice.new(:token => "5b1f186651dd988865c6573921ec87fa4bec23b8")
86
- @payment = RSpreedly::PaymentMethod::CreditCard.new(:number => "4222222222222",
87
- :card_type => "visa",
88
- :verification_value => "234",
89
- :month => 1,
90
- :year => 2011,
91
- :first_name => "Joe",
106
+ @payment = RSpreedly::PaymentMethod::CreditCard.new(:number => "4222222222222",
107
+ :card_type => "visa",
108
+ :verification_value => "234",
109
+ :month => 1,
110
+ :year => 2011,
111
+ :first_name => "Joe",
92
112
  :last_name => "Bob")
93
- end
94
-
113
+ end
114
+
95
115
  it "should return true if successful" do
96
- stub_http_with_fixture("payment_success.xml", 200)
116
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
117
+ to_return(:body => fixture("payment_success.xml"), :status => 200)
118
+
97
119
  @invoice.pay(@payment).should be_true
98
- end
99
-
120
+ end
121
+
100
122
  it "should return nil if not successful" do
101
- stub_http_with_fixture("payment_not_found.xml", 404)
123
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
124
+ to_return(:body => fixture("payment_not_found.xml"), :status => 404)
125
+
102
126
  @invoice.pay(@payment).should be_nil
103
127
  end
104
-
128
+
105
129
  end
106
-
130
+
107
131
  describe "#pay!" do
108
-
132
+
109
133
  before(:each) do
110
134
  @invoice = RSpreedly::Invoice.new(:token => "5b1f186651dd988865c6573921ec87fa4bec23b8")
111
- @payment = RSpreedly::PaymentMethod::CreditCard.new(:number => "4222222222222",
112
- :card_type => "visa",
113
- :verification_value => "234",
114
- :month => 1,
115
- :year => 2011,
116
- :first_name => "Joe",
135
+ @payment = RSpreedly::PaymentMethod::CreditCard.new(:number => "4222222222222",
136
+ :card_type => "visa",
137
+ :verification_value => "234",
138
+ :month => 1,
139
+ :year => 2011,
140
+ :first_name => "Joe",
117
141
  :last_name => "Bob")
118
- end
119
-
142
+ end
143
+
120
144
  it "should return true if successful" do
121
- stub_http_with_fixture("payment_success.xml", 200)
145
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
146
+ to_return(:body => fixture("payment_success.xml"), :status => 200)
147
+
122
148
  @invoice.pay!(@payment).should be_true
123
- end
124
-
149
+ end
150
+
125
151
  it "should update the Invoice if successful" do
126
- stub_http_with_fixture("payment_success.xml", 200)
127
-
152
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
153
+ to_return(:body => fixture("payment_success.xml"), :status => 200)
154
+
128
155
  lambda{
129
156
  @invoice.pay!(@payment)
130
157
  }.should change(@invoice, :closed).to(true)
131
158
  end
132
-
159
+
133
160
  it "should raise NotFound if the invoice doesn't exist" do
134
- stub_http_with_fixture("payment_not_found.xml", 404)
161
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
162
+ to_return(:body => fixture("payment_not_found.xml"), :status => 404)
163
+
135
164
  lambda{
136
165
  @invoice.pay!(@payment)
137
- }.should raise_error(RSpreedly::Error::NotFound)
166
+ }.should raise_error(RSpreedly::Error::NotFound)
138
167
  end
139
-
168
+
140
169
  it "should raise GatewayTimeout if the payment gateway times out" do
141
- stub_http_with_code(504)
170
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
171
+ to_return(:status => 504)
172
+
142
173
  lambda{
143
174
  @invoice.pay!(@payment)
144
175
  }.should raise_error(RSpreedly::Error::GatewayTimeout)
145
176
  end
146
-
177
+
147
178
  it "should raise BadRequest if the payment method is invalid" do
148
- stub_http_with_fixture("payment_invalid.xml", 422)
179
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
180
+ to_return(:body => fixture("payment_invalid.xml"), :status => 422)
181
+
149
182
  lambda{
150
183
  @invoice.pay!(@payment)
151
184
  }.should raise_error(RSpreedly::Error::BadRequest)
152
185
  end
153
-
186
+
154
187
  it "should raise Forbidden if the invoice is already paid" do
155
- stub_http_with_fixture("payment_already_paid.xml", 403)
188
+ stub_request(:put, spreedly_url("/invoices/5b1f186651dd988865c6573921ec87fa4bec23b8/pay.xml")).
189
+ to_return(:body => fixture("payment_already_paid.xml"), :status => 403)
190
+
156
191
  lambda{
157
192
  @invoice.pay!(@payment)
158
- }.should raise_error(RSpreedly::Error::Forbidden)
159
- end
193
+ }.should raise_error(RSpreedly::Error::Forbidden)
194
+ end
160
195
  end
161
196
  end