dup_spree_api 1.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +22 -0
  5. data/Rakefile +30 -0
  6. data/app/controllers/spree/api/addresses_controller.rb +20 -0
  7. data/app/controllers/spree/api/base_controller.rb +114 -0
  8. data/app/controllers/spree/api/countries_controller.rb +18 -0
  9. data/app/controllers/spree/api/images_controller.rb +32 -0
  10. data/app/controllers/spree/api/line_items_controller.rb +40 -0
  11. data/app/controllers/spree/api/orders_controller.rb +85 -0
  12. data/app/controllers/spree/api/payments_controller.rb +80 -0
  13. data/app/controllers/spree/api/product_properties_controller.rb +65 -0
  14. data/app/controllers/spree/api/products_controller.rb +49 -0
  15. data/app/controllers/spree/api/return_authorizations_controller.rb +54 -0
  16. data/app/controllers/spree/api/shipments_controller.rb +43 -0
  17. data/app/controllers/spree/api/taxonomies_controller.rb +50 -0
  18. data/app/controllers/spree/api/taxons_controller.rb +53 -0
  19. data/app/controllers/spree/api/variants_controller.rb +75 -0
  20. data/app/controllers/spree/api/zones_controller.rb +46 -0
  21. data/app/helpers/spree/api/api_helpers.rb +76 -0
  22. data/app/models/spree/api_configuration.rb +5 -0
  23. data/app/models/spree/line_item_decorator.rb +3 -0
  24. data/app/models/spree/option_value_decorator.rb +5 -0
  25. data/app/models/spree/order_decorator.rb +13 -0
  26. data/app/models/spree/user_decorator.rb +13 -0
  27. data/app/overrides/api_admin_user_edit_form.rb +6 -0
  28. data/app/views/spree/admin/users/_api_fields.html.erb +32 -0
  29. data/app/views/spree/api/addresses/show.v1.rabl +11 -0
  30. data/app/views/spree/api/countries/index.v1.rabl +7 -0
  31. data/app/views/spree/api/countries/show.v1.rabl +5 -0
  32. data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
  33. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
  34. data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
  35. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
  36. data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
  37. data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
  38. data/app/views/spree/api/images/show.v1.rabl +3 -0
  39. data/app/views/spree/api/line_items/new.v1.rabl +3 -0
  40. data/app/views/spree/api/line_items/show.v1.rabl +5 -0
  41. data/app/views/spree/api/orders/address.v1.rabl +0 -0
  42. data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
  43. data/app/views/spree/api/orders/cart.v1.rabl +0 -0
  44. data/app/views/spree/api/orders/complete.v1.rabl +0 -0
  45. data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
  46. data/app/views/spree/api/orders/delivery.v1.rabl +3 -0
  47. data/app/views/spree/api/orders/index.v1.rabl +7 -0
  48. data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
  49. data/app/views/spree/api/orders/payment.v1.rabl +4 -0
  50. data/app/views/spree/api/orders/show.v1.rabl +29 -0
  51. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
  52. data/app/views/spree/api/payments/index.v1.rabl +7 -0
  53. data/app/views/spree/api/payments/new.v1.rabl +6 -0
  54. data/app/views/spree/api/payments/show.v1.rabl +2 -0
  55. data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
  56. data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
  57. data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
  58. data/app/views/spree/api/products/index.v1.rabl +8 -0
  59. data/app/views/spree/api/products/new.v1.rabl +3 -0
  60. data/app/views/spree/api/products/product.v1.rabl +1 -0
  61. data/app/views/spree/api/products/show.v1.rabl +25 -0
  62. data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
  63. data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
  64. data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
  65. data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
  66. data/app/views/spree/api/shipments/show.v1.rabl +7 -0
  67. data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
  68. data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
  69. data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
  70. data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
  71. data/app/views/spree/api/taxons/index.v1.rabl +4 -0
  72. data/app/views/spree/api/taxons/new.v1.rabl +3 -0
  73. data/app/views/spree/api/taxons/show.v1.rabl +8 -0
  74. data/app/views/spree/api/taxons/taxons.v1.rabl +7 -0
  75. data/app/views/spree/api/variants/index.v1.rabl +10 -0
  76. data/app/views/spree/api/variants/new.v1.rabl +2 -0
  77. data/app/views/spree/api/variants/show.v1.rabl +3 -0
  78. data/app/views/spree/api/variants/variant.v1.rabl +1 -0
  79. data/app/views/spree/api/zones/index.v1.rabl +7 -0
  80. data/app/views/spree/api/zones/show.v1.rabl +6 -0
  81. data/config/initializers/metal_load_paths.rb +1 -0
  82. data/config/locales/en.yml +23 -0
  83. data/config/routes.rb +56 -0
  84. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
  85. data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
  86. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
  87. data/lib/spree/api.rb +12 -0
  88. data/lib/spree/api/controller_setup.rb +27 -0
  89. data/lib/spree/api/engine.rb +35 -0
  90. data/lib/spree/api/responders.rb +11 -0
  91. data/lib/spree/api/responders/rabl_template.rb +22 -0
  92. data/lib/spree/api/testing_support/helpers.rb +35 -0
  93. data/lib/spree/api/testing_support/setup.rb +28 -0
  94. data/lib/spree/api/version.rb +5 -0
  95. data/lib/spree_api.rb +3 -0
  96. data/script/rails +9 -0
  97. data/spec/controllers/spree/api/addresses_controller_spec.rb +45 -0
  98. data/spec/controllers/spree/api/base_controller_spec.rb +42 -0
  99. data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
  100. data/spec/controllers/spree/api/images_controller_spec.rb +66 -0
  101. data/spec/controllers/spree/api/line_items_controller_spec.rb +77 -0
  102. data/spec/controllers/spree/api/orders_controller_spec.rb +255 -0
  103. data/spec/controllers/spree/api/payments_controller_spec.rb +203 -0
  104. data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
  105. data/spec/controllers/spree/api/products_controller_spec.rb +211 -0
  106. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +155 -0
  107. data/spec/controllers/spree/api/shipments_controller_spec.rb +59 -0
  108. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +107 -0
  109. data/spec/controllers/spree/api/taxons_controller_spec.rb +87 -0
  110. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
  111. data/spec/controllers/spree/api/variants_controller_spec.rb +155 -0
  112. data/spec/controllers/spree/api/zones_controller_spec.rb +111 -0
  113. data/spec/fixtures/thinking-cat.jpg +0 -0
  114. data/spec/models/spree/legacy_user_spec.rb +19 -0
  115. data/spec/models/spree/order_spec.rb +18 -0
  116. data/spec/shared_examples/protect_product_actions.rb +17 -0
  117. data/spec/spec_helper.rb +27 -0
  118. data/spec/support/controller_hacks.rb +27 -0
  119. data/spec/support/database_cleaner.rb +14 -0
  120. data/spec/support/have_attributes_matcher.rb +13 -0
  121. data/spree_api.gemspec +23 -0
  122. metadata +254 -0
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Spree::Api::ImagesController do
5
+ render_views
6
+
7
+ let!(:product) { create(:product) }
8
+ let!(:attributes) { [:id, :position, :attachment_content_type,
9
+ :attachment_file_name, :type, :attachment_updated_at, :attachment_width,
10
+ :attachment_height, :alt] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ end
15
+
16
+ context "as an admin" do
17
+ sign_in_as_admin!
18
+
19
+ it "can upload a new image for a variant" do
20
+ lambda do
21
+ api_post :create,
22
+ :image => { :attachment => upload_image('thinking-cat.jpg'),
23
+ :viewable_type => 'Spree::Variant',
24
+ :viewable_id => product.master.to_param }
25
+ response.status.should == 201
26
+ json_response.should have_attributes(attributes)
27
+ end.should change(Image, :count).by(1)
28
+ end
29
+
30
+ context "working with an existing image" do
31
+ let!(:product_image) { product.master.images.create!(:attachment => image('thinking-cat.jpg')) }
32
+
33
+ it "can update image data" do
34
+ product_image.position.should == 1
35
+ api_post :update, :image => { :position => 2 }, :id => product_image.id
36
+ response.status.should == 200
37
+ json_response.should have_attributes(attributes)
38
+ product_image.reload.position.should == 2
39
+ end
40
+
41
+ it "can delete an image" do
42
+ api_delete :destroy, :id => product_image.id
43
+ response.status.should == 204
44
+ lambda { product_image.reload }.should raise_error(ActiveRecord::RecordNotFound)
45
+ end
46
+ end
47
+ end
48
+
49
+ context "as a non-admin" do
50
+ it "cannot create an image" do
51
+ api_post :create
52
+ assert_unauthorized!
53
+ end
54
+
55
+ it "cannot update an image" do
56
+ api_put :update, :id => 1
57
+ assert_unauthorized!
58
+ end
59
+
60
+ it "cannot delete an image" do
61
+ api_delete :destroy, :id => 1
62
+ assert_unauthorized!
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::LineItemsController do
5
+ render_views
6
+
7
+ let!(:order) do
8
+ order = create(:order)
9
+ order.line_items << create(:line_item)
10
+ order
11
+ end
12
+
13
+ let(:product) { create(:product) }
14
+ let(:attributes) { [:id, :quantity, :price, :variant] }
15
+ let(:resource_scoping) { { :order_id => order.to_param } }
16
+
17
+ before do
18
+ stub_authentication!
19
+ end
20
+
21
+ it "can learn how to create a new line item" do
22
+ api_get :new
23
+ json_response["attributes"].should == ["quantity", "price", "variant_id"]
24
+ required_attributes = json_response["required_attributes"]
25
+ required_attributes.should include("quantity", "variant_id")
26
+ end
27
+
28
+ context "as the order owner" do
29
+ before do
30
+ Order.any_instance.stub :user => current_api_user
31
+ end
32
+
33
+ it "can add a new line item to an existing order" do
34
+ api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
35
+ response.status.should == 201
36
+ json_response.should have_attributes(attributes)
37
+ json_response["variant"]["name"].should_not be_blank
38
+ end
39
+
40
+ it "can update a line item on the order" do
41
+ line_item = order.line_items.first
42
+ api_put :update, :id => line_item.id, :line_item => { :quantity => 1000 }
43
+ response.status.should == 200
44
+ json_response.should have_attributes(attributes)
45
+ end
46
+
47
+ it "can delete a line item on the order" do
48
+ line_item = order.line_items.first
49
+ api_delete :destroy, :id => line_item.id
50
+ response.status.should == 204
51
+ lambda { line_item.reload }.should raise_error(ActiveRecord::RecordNotFound)
52
+ end
53
+ end
54
+
55
+ context "as just another user" do
56
+ it "cannot add a new line item to the order" do
57
+ api_post :create, :line_item => { :variant_id => product.master.to_param, :quantity => 1 }
58
+ assert_unauthorized!
59
+ end
60
+
61
+ it "cannot update a line item on the order" do
62
+ line_item = order.line_items.first
63
+ api_put :update, :id => line_item.id, :line_item => { :quantity => 1000 }
64
+ assert_unauthorized!
65
+ line_item.reload.quantity.should_not == 1000
66
+ end
67
+
68
+ it "cannot delete a line item on the order" do
69
+ line_item = order.line_items.first
70
+ api_delete :destroy, :id => line_item.id
71
+ assert_unauthorized!
72
+ lambda { line_item.reload }.should_not raise_error(ActiveRecord::RecordNotFound)
73
+ end
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,255 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::OrdersController do
5
+ render_views
6
+
7
+ let!(:order) { create(:order) }
8
+ let(:attributes) { [:number, :item_total, :total,
9
+ :state, :adjustment_total,
10
+ :user_id, :created_at, :updated_at,
11
+ :completed_at, :payment_total, :shipment_state,
12
+ :payment_state, :email, :special_instructions] }
13
+
14
+
15
+ before do
16
+ stub_authentication!
17
+ end
18
+
19
+ it "cannot view all orders" do
20
+ api_get :index
21
+ assert_unauthorized!
22
+ end
23
+
24
+ it "can view their own order" do
25
+ Order.any_instance.stub :user => current_api_user
26
+ api_get :show, :id => order.to_param
27
+ response.status.should == 200
28
+ json_response.should have_attributes(attributes)
29
+ end
30
+
31
+ # Regression test for #1992
32
+ it "can view an order not in a standard state" do
33
+ Order.any_instance.stub :user => current_api_user
34
+ order.update_column(:state, 'shipped')
35
+ api_get :show, :id => order.to_param
36
+ end
37
+
38
+ it "can not view someone else's order" do
39
+ Order.any_instance.stub :user => stub_model(Spree::LegacyUser)
40
+ api_get :show, :id => order.to_param
41
+ assert_unauthorized!
42
+ end
43
+
44
+ it "cannot cancel an order that doesn't belong to them" do
45
+ order.update_attribute(:completed_at, Time.now)
46
+ order.update_attribute(:shipment_state, "ready")
47
+ api_put :cancel, :id => order.to_param
48
+ assert_unauthorized!
49
+ end
50
+
51
+ it "cannot add address information to an order that doesn't belong to them" do
52
+ api_put :address, :id => order.to_param
53
+ assert_unauthorized!
54
+ end
55
+
56
+ it "cannot change delivery information on an order that doesn't belong to them" do
57
+ api_put :delivery, :id => order.to_param
58
+ assert_unauthorized!
59
+ end
60
+
61
+ it "can create an order" do
62
+ variant = create(:variant)
63
+ api_post :create, :order => { :line_items => [{ :variant_id => variant.to_param, :quantity => 5 }] }
64
+ response.status.should == 201
65
+ order = Order.last
66
+ order.line_items.count.should == 1
67
+ order.line_items.first.variant.should == variant
68
+ order.line_items.first.quantity.should == 5
69
+ json_response["state"].should == "address"
70
+ end
71
+
72
+ it "can create an order without any parameters" do
73
+ lambda { api_post :create }.should_not raise_error(NoMethodError)
74
+ response.status.should == 201
75
+ order = Order.last
76
+ json_response["state"].should == "address"
77
+ end
78
+
79
+ context "working with an order" do
80
+ before do
81
+ Order.any_instance.stub :user => current_api_user
82
+ create(:payment_method)
83
+ order.next # Switch from cart to address
84
+ order.ship_address.should be_nil
85
+ order.state.should == "address"
86
+ end
87
+
88
+ def clean_address(address)
89
+ address.delete(:state)
90
+ address.delete(:country)
91
+ address
92
+ end
93
+
94
+ let(:address_params) { { :country_id => Country.first.id, :state_id => State.first.id } }
95
+ let(:shipping_address) { clean_address(attributes_for(:address).merge!(address_params)) }
96
+ let(:billing_address) { clean_address(attributes_for(:address).merge!(address_params)) }
97
+ let!(:shipping_method) { create(:shipping_method) }
98
+ let!(:payment_method) { create(:payment_method) }
99
+
100
+ it "can add address information to an order" do
101
+ api_put :address, :id => order.to_param, :shipping_address => shipping_address, :billing_address => billing_address
102
+
103
+ response.status.should == 200
104
+ order.reload
105
+ order.shipping_address.reload
106
+ order.billing_address.reload
107
+ # We can assume the rest of the parameters are set if these two are
108
+ order.shipping_address.firstname.should == shipping_address[:firstname]
109
+ order.billing_address.firstname.should == billing_address[:firstname]
110
+ order.state.should == "delivery"
111
+ json_response["shipping_methods"].should_not be_empty
112
+ end
113
+
114
+ it "can add just shipping address information to an order" do
115
+ api_put :address, :id => order.to_param, :shipping_address => shipping_address
116
+ response.status.should == 200
117
+ order.reload
118
+ order.shipping_address.reload
119
+ order.shipping_address.firstname.should == shipping_address[:firstname]
120
+ order.bill_address.should be_nil
121
+ end
122
+
123
+ it "cannot use an address that has no valid shipping methods" do
124
+ shipping_method.destroy
125
+ api_put :address, :id => order.to_param, :shipping_address => shipping_address, :billing_address => billing_address
126
+ response.status.should == 422
127
+ json_response["errors"]["base"].should == ["No shipping methods available for selected location, please change your address and try again."]
128
+ end
129
+
130
+ it "can not add invalid ship address information to an order" do
131
+ shipping_address[:firstname] = ""
132
+ api_put :address, :id => order.to_param, :shipping_address => shipping_address, :billing_address => billing_address
133
+
134
+ response.status.should == 422
135
+ json_response["errors"]["ship_address.firstname"].should_not be_blank
136
+ end
137
+
138
+ it "can not add invalid ship address information to an order" do
139
+ billing_address[:firstname] = ""
140
+ api_put :address, :id => order.to_param, :shipping_address => shipping_address, :billing_address => billing_address
141
+
142
+ response.status.should == 422
143
+ json_response["errors"]["bill_address.firstname"].should_not be_blank
144
+ end
145
+
146
+ it "can add line items" do
147
+ api_put :update, :id => order.to_param, :order => { :line_items => [{:variant_id => create(:variant).id, :quantity => 2}] }
148
+
149
+ response.status.should == 200
150
+ json_response['item_total'].to_f.should_not == order.item_total.to_f
151
+ end
152
+
153
+ context "with a line item" do
154
+ before do
155
+ order.line_items << create(:line_item)
156
+ end
157
+
158
+ context "for delivery" do
159
+ before do
160
+ order.update_attribute(:state, "delivery")
161
+ end
162
+
163
+ it "can select a shipping method for an order" do
164
+ order.shipping_method.should be_nil
165
+ api_put :delivery, :id => order.to_param, :shipping_method_id => shipping_method.id
166
+ response.status.should == 200
167
+ order.reload
168
+ order.state.should == "payment"
169
+ order.shipping_method.should == shipping_method
170
+ end
171
+
172
+ it "cannot select an invalid shipping method for an order" do
173
+ order.shipping_method.should be_nil
174
+ api_put :delivery, :id => order.to_param, :shipping_method_id => '1234567890'
175
+ response.status.should == 422
176
+ json_response["errors"].should include("Invalid shipping method specified.")
177
+ end
178
+ end
179
+
180
+ it "can empty an order" do
181
+ api_put :empty, :id => order.to_param
182
+ response.status.should == 200
183
+ order.reload.line_items.should be_empty
184
+ end
185
+ end
186
+ end
187
+
188
+ context "as an admin" do
189
+ sign_in_as_admin!
190
+
191
+ context "with no orders" do
192
+ before { Spree::Order.delete_all }
193
+ it "still returns a root :orders key" do
194
+ api_get :index
195
+ json_response["orders"].should == []
196
+ end
197
+ end
198
+
199
+ context "with two orders" do
200
+ before { create(:order) }
201
+
202
+ it "can view all orders" do
203
+ api_get :index
204
+ json_response["orders"].first.should have_attributes(attributes)
205
+ json_response["count"].should == 2
206
+ json_response["current_page"].should == 1
207
+ json_response["pages"].should == 1
208
+ end
209
+
210
+ # Test for #1763
211
+ it "can control the page size through a parameter" do
212
+ api_get :index, :per_page => 1
213
+ json_response["orders"].count.should == 1
214
+ json_response["orders"].first.should have_attributes(attributes)
215
+ json_response["count"].should == 1
216
+ json_response["current_page"].should == 1
217
+ json_response["pages"].should == 2
218
+ end
219
+ end
220
+
221
+ context "search" do
222
+ before do
223
+ create(:order)
224
+ Spree::Order.last.update_attribute(:email, 'spree@spreecommerce.com')
225
+ end
226
+
227
+ let(:expected_result) { Spree::Order.last }
228
+
229
+ it "can query the results through a parameter" do
230
+ api_get :index, :q => { :email_cont => 'spree' }
231
+ json_response["orders"].count.should == 1
232
+ json_response["orders"].first.should have_attributes(attributes)
233
+ json_response["orders"].first["email"].should == expected_result.email
234
+ json_response["count"].should == 1
235
+ json_response["current_page"].should == 1
236
+ json_response["pages"].should == 1
237
+ end
238
+ end
239
+
240
+ context "can cancel an order" do
241
+ before do
242
+ order.completed_at = Time.now
243
+ order.state = 'complete'
244
+ order.shipment_state = 'ready'
245
+ order.save!
246
+ end
247
+
248
+ specify do
249
+ api_put :cancel, :id => order.to_param
250
+ json_response["state"].should == "canceled"
251
+ end
252
+ end
253
+ end
254
+ end
255
+ end
@@ -0,0 +1,203 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Spree::Api::PaymentsController do
5
+ render_views
6
+ let!(:order) { create(:order) }
7
+ let!(:payment) { create(:payment, :order => order) }
8
+ let!(:attributes) { [:id, :source_type, :source_id, :amount,
9
+ :payment_method_id, :response_code, :state, :avs_response,
10
+ :created_at, :updated_at] }
11
+
12
+ let(:resource_scoping) { { :order_id => order.to_param } }
13
+ before do
14
+ stub_authentication!
15
+ end
16
+
17
+ context "as a user" do
18
+ context "when the order belongs to the user" do
19
+ before do
20
+ Order.any_instance.stub :user => current_api_user
21
+ end
22
+
23
+ it "can view the payments for their order" do
24
+ api_get :index
25
+ json_response["payments"].first.should have_attributes(attributes)
26
+ end
27
+
28
+ it "can learn how to create a new payment" do
29
+ api_get :new
30
+ json_response["attributes"].should == attributes.map(&:to_s)
31
+ json_response["payment_methods"].should_not be_empty
32
+ json_response["payment_methods"].first.should have_attributes([:id, :name, :description])
33
+ end
34
+
35
+ it "can create a new payment" do
36
+ api_post :create, :payment => { :payment_method_id => PaymentMethod.first.id, :amount => 50 }
37
+ response.status.should == 201
38
+ json_response.should have_attributes(attributes)
39
+ end
40
+
41
+ it "can view a pre-existing payment's details" do
42
+ api_get :show, :id => payment.to_param
43
+ json_response.should have_attributes(attributes)
44
+ end
45
+
46
+ it "cannot authorize a payment" do
47
+ api_put :authorize, :id => payment.to_param
48
+ assert_unauthorized!
49
+ end
50
+ end
51
+
52
+ context "when the order does not belong to the user" do
53
+ before do
54
+ Order.any_instance.stub :user => stub_model(LegacyUser)
55
+ end
56
+
57
+ it "cannot view payments for somebody else's order" do
58
+ api_get :index, :order_id => order.to_param
59
+ assert_unauthorized!
60
+ end
61
+ end
62
+ end
63
+
64
+ context "as an admin" do
65
+ sign_in_as_admin!
66
+
67
+ it "can view the payments on any order" do
68
+ api_get :index
69
+ response.status.should == 200
70
+ json_response["payments"].first.should have_attributes(attributes)
71
+ end
72
+
73
+ context "multiple payments" do
74
+ before { @payment = create(:payment, :order => order, :response_code => '99999') }
75
+
76
+ it "can view all payments on an order" do
77
+ api_get :index
78
+ json_response["count"].should == 2
79
+ end
80
+
81
+ it 'can control the page size through a parameter' do
82
+ api_get :index, :per_page => 1
83
+ json_response['count'].should == 1
84
+ json_response['current_page'].should == 1
85
+ json_response['pages'].should == 2
86
+ end
87
+
88
+ it 'can query the results through a paramter' do
89
+ api_get :index, :q => { :response_code_cont => '999' }
90
+ json_response['count'].should == 1
91
+ json_response['payments'].first['response_code'].should eq @payment.response_code
92
+ end
93
+ end
94
+
95
+ context "for a given payment" do
96
+
97
+ it "can authorize" do
98
+ api_put :authorize, :id => payment.to_param
99
+ response.status.should == 200
100
+ payment.reload
101
+ payment.state.should == "pending"
102
+ end
103
+
104
+ it "returns a 422 status when authorization fails" do
105
+ fake_response = stub(:success? => false, :to_s => "Could not authorize card")
106
+ Spree::Gateway::Bogus.any_instance.should_receive(:authorize).and_return(fake_response)
107
+ api_put :authorize, :id => payment.to_param
108
+ response.status.should == 422
109
+ json_response["error"].should == "There was a problem with the payment gateway: Could not authorize card"
110
+ payment.reload
111
+ payment.state.should == "failed"
112
+ end
113
+
114
+ it "can capture" do
115
+ api_put :capture, :id => payment.to_param
116
+ response.status.should == 200
117
+ payment.reload
118
+ payment.state.should == "completed"
119
+ end
120
+
121
+ it "returns a 422 status when purchasing fails" do
122
+ fake_response = stub(:success? => false, :to_s => "Insufficient funds")
123
+ Spree::Gateway::Bogus.any_instance.should_receive(:capture).and_return(fake_response)
124
+ api_put :capture, :id => payment.to_param
125
+ response.status.should == 422
126
+ json_response["error"].should == "There was a problem with the payment gateway: Insufficient funds"
127
+
128
+ payment.reload
129
+ payment.state.should == "failed"
130
+ end
131
+
132
+ it "can purchase" do
133
+ api_put :purchase, :id => payment.to_param
134
+ response.status.should == 200
135
+ payment.reload
136
+ payment.state.should == "completed"
137
+ end
138
+
139
+ it "returns a 422 status when purchasing fails" do
140
+ fake_response = stub(:success? => false, :to_s => "Insufficient funds")
141
+ Spree::Gateway::Bogus.any_instance.should_receive(:purchase).and_return(fake_response)
142
+ api_put :purchase, :id => payment.to_param
143
+ response.status.should == 422
144
+ json_response["error"].should == "There was a problem with the payment gateway: Insufficient funds"
145
+
146
+ payment.reload
147
+ payment.state.should == "failed"
148
+ end
149
+
150
+ it "can void" do
151
+ api_put :void, :id => payment.to_param
152
+ response.status.should == 200
153
+ payment.reload
154
+ payment.state.should == "void"
155
+ end
156
+
157
+ it "returns a 422 status when voiding fails" do
158
+ fake_response = stub(:success? => false, :to_s => "NO REFUNDS")
159
+ Spree::Gateway::Bogus.any_instance.should_receive(:void).and_return(fake_response)
160
+ api_put :void, :id => payment.to_param
161
+ response.status.should == 422
162
+ json_response["error"].should == "There was a problem with the payment gateway: NO REFUNDS"
163
+
164
+ payment.reload
165
+ payment.state.should == "pending"
166
+ end
167
+
168
+ context "crediting" do
169
+ before do
170
+ payment.purchase!
171
+ end
172
+
173
+ it "can credit" do
174
+ api_put :credit, :id => payment.to_param
175
+ response.status.should == 200
176
+ payment.reload
177
+ payment.state.should == "completed"
178
+
179
+ # Ensur that a credit payment was created, and it has correct credit amount
180
+ credit_payment = Payment.where(:source_type => 'Spree::Payment', :source_id => payment.id).last
181
+ credit_payment.amount.to_f.should == -45.75
182
+ end
183
+
184
+ it "returns a 422 status when crediting fails" do
185
+ fake_response = stub(:success? => false, :to_s => "NO CREDIT FOR YOU")
186
+ Spree::Gateway::Bogus.any_instance.should_receive(:credit).and_return(fake_response)
187
+ api_put :credit, :id => payment.to_param
188
+ response.status.should == 422
189
+ json_response["error"].should == "There was a problem with the payment gateway: NO CREDIT FOR YOU"
190
+ end
191
+
192
+ it "cannot credit over credit_allowed limit" do
193
+ api_put :credit, :id => payment.to_param, :amount => 1000000
194
+ response.status.should == 422
195
+ json_response["error"].should == "This payment can only be credited up to 45.75. Please specify an amount less than or equal to this number."
196
+ end
197
+ end
198
+ end
199
+
200
+ end
201
+
202
+ end
203
+ end