solidus_api 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of solidus_api might be problematic. Click here for more details.

Files changed (202) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/CHANGELOG.md +1 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE +27 -0
  6. data/Rakefile +16 -0
  7. data/app/controllers/spree/api/addresses_controller.rb +43 -0
  8. data/app/controllers/spree/api/base_controller.rb +189 -0
  9. data/app/controllers/spree/api/checkouts_controller.rb +133 -0
  10. data/app/controllers/spree/api/classifications_controller.rb +18 -0
  11. data/app/controllers/spree/api/config_controller.rb +6 -0
  12. data/app/controllers/spree/api/countries_controller.rb +23 -0
  13. data/app/controllers/spree/api/credit_cards_controller.rb +25 -0
  14. data/app/controllers/spree/api/images_controller.rb +47 -0
  15. data/app/controllers/spree/api/inventory_units_controller.rb +52 -0
  16. data/app/controllers/spree/api/line_items_controller.rb +74 -0
  17. data/app/controllers/spree/api/option_types_controller.rb +49 -0
  18. data/app/controllers/spree/api/option_values_controller.rb +58 -0
  19. data/app/controllers/spree/api/orders_controller.rb +155 -0
  20. data/app/controllers/spree/api/payments_controller.rb +81 -0
  21. data/app/controllers/spree/api/product_properties_controller.rb +72 -0
  22. data/app/controllers/spree/api/products_controller.rb +129 -0
  23. data/app/controllers/spree/api/promotions_controller.rb +26 -0
  24. data/app/controllers/spree/api/properties_controller.rb +71 -0
  25. data/app/controllers/spree/api/return_authorizations_controller.rb +71 -0
  26. data/app/controllers/spree/api/shipments_controller.rb +172 -0
  27. data/app/controllers/spree/api/states_controller.rb +35 -0
  28. data/app/controllers/spree/api/stock_items_controller.rb +84 -0
  29. data/app/controllers/spree/api/stock_locations_controller.rb +50 -0
  30. data/app/controllers/spree/api/stock_movements_controller.rb +42 -0
  31. data/app/controllers/spree/api/stock_transfers_controller.rb +19 -0
  32. data/app/controllers/spree/api/store_credit_events_controller.rb +9 -0
  33. data/app/controllers/spree/api/stores_controller.rb +55 -0
  34. data/app/controllers/spree/api/taxonomies_controller.rb +64 -0
  35. data/app/controllers/spree/api/taxons_controller.rb +93 -0
  36. data/app/controllers/spree/api/transfer_items_controller.rb +42 -0
  37. data/app/controllers/spree/api/users_controller.rb +56 -0
  38. data/app/controllers/spree/api/variants_controller.rb +75 -0
  39. data/app/controllers/spree/api/zones_controller.rb +50 -0
  40. data/app/helpers/spree/api/api_helpers.rb +190 -0
  41. data/app/models/spree/api_configuration.rb +5 -0
  42. data/app/models/spree/option_value_decorator.rb +9 -0
  43. data/app/views/spree/api/addresses/show.v1.rabl +10 -0
  44. data/app/views/spree/api/adjustments/show.v1.rabl +4 -0
  45. data/app/views/spree/api/config/money.v1.rabl +2 -0
  46. data/app/views/spree/api/config/show.v1.rabl +2 -0
  47. data/app/views/spree/api/countries/index.v1.rabl +7 -0
  48. data/app/views/spree/api/countries/show.v1.rabl +5 -0
  49. data/app/views/spree/api/credit_cards/index.v1.rabl +7 -0
  50. data/app/views/spree/api/credit_cards/show.v1.rabl +3 -0
  51. data/app/views/spree/api/errors/gateway_error.v1.rabl +2 -0
  52. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +2 -0
  53. data/app/views/spree/api/errors/invalid_resource.v1.rabl +3 -0
  54. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +2 -0
  55. data/app/views/spree/api/errors/not_found.v1.rabl +2 -0
  56. data/app/views/spree/api/errors/unauthorized.v1.rabl +2 -0
  57. data/app/views/spree/api/errors/variant_not_in_stock_transfer.v1.rabl +2 -0
  58. data/app/views/spree/api/images/index.v1.rabl +4 -0
  59. data/app/views/spree/api/images/show.v1.rabl +6 -0
  60. data/app/views/spree/api/inventory_units/show.rabl +2 -0
  61. data/app/views/spree/api/line_items/new.v1.rabl +3 -0
  62. data/app/views/spree/api/line_items/show.v1.rabl +15 -0
  63. data/app/views/spree/api/option_types/index.v1.rabl +3 -0
  64. data/app/views/spree/api/option_types/show.v1.rabl +5 -0
  65. data/app/views/spree/api/option_values/index.v1.rabl +3 -0
  66. data/app/views/spree/api/option_values/show.v1.rabl +2 -0
  67. data/app/views/spree/api/orders/address.v1.rabl +0 -0
  68. data/app/views/spree/api/orders/canceled.v1.rabl +0 -0
  69. data/app/views/spree/api/orders/cart.v1.rabl +0 -0
  70. data/app/views/spree/api/orders/complete.v1.rabl +0 -0
  71. data/app/views/spree/api/orders/could_not_apply_coupon.v1.rabl +2 -0
  72. data/app/views/spree/api/orders/could_not_transition.v1.rabl +3 -0
  73. data/app/views/spree/api/orders/expected_total_mismatch.v1.rabl +2 -0
  74. data/app/views/spree/api/orders/index.v1.rabl +7 -0
  75. data/app/views/spree/api/orders/invalid_shipping_method.v1.rabl +2 -0
  76. data/app/views/spree/api/orders/mine.v1.rabl +9 -0
  77. data/app/views/spree/api/orders/order.v1.rabl +9 -0
  78. data/app/views/spree/api/orders/payment.v1.rabl +3 -0
  79. data/app/views/spree/api/orders/show.v1.rabl +52 -0
  80. data/app/views/spree/api/payments/credit_over_limit.v1.rabl +2 -0
  81. data/app/views/spree/api/payments/index.v1.rabl +7 -0
  82. data/app/views/spree/api/payments/new.v1.rabl +5 -0
  83. data/app/views/spree/api/payments/show.v1.rabl +2 -0
  84. data/app/views/spree/api/payments/update_forbidden.v1.rabl +2 -0
  85. data/app/views/spree/api/product_properties/index.v1.rabl +7 -0
  86. data/app/views/spree/api/product_properties/new.v1.rabl +2 -0
  87. data/app/views/spree/api/product_properties/show.v1.rabl +2 -0
  88. data/app/views/spree/api/products/index.v1.rabl +9 -0
  89. data/app/views/spree/api/products/new.v1.rabl +3 -0
  90. data/app/views/spree/api/products/product.v1.rabl +1 -0
  91. data/app/views/spree/api/products/show.v1.rabl +31 -0
  92. data/app/views/spree/api/promotions/handler.v1.rabl +5 -0
  93. data/app/views/spree/api/promotions/show.v1.rabl +2 -0
  94. data/app/views/spree/api/properties/index.v1.rabl +7 -0
  95. data/app/views/spree/api/properties/new.v1.rabl +2 -0
  96. data/app/views/spree/api/properties/show.v1.rabl +2 -0
  97. data/app/views/spree/api/return_authorizations/index.v1.rabl +7 -0
  98. data/app/views/spree/api/return_authorizations/new.v1.rabl +3 -0
  99. data/app/views/spree/api/return_authorizations/show.v1.rabl +2 -0
  100. data/app/views/spree/api/shared/stock_location_required.v1.rabl +2 -0
  101. data/app/views/spree/api/shipments/big.v1.rabl +48 -0
  102. data/app/views/spree/api/shipments/cannot_ready_shipment.v1.rabl +2 -0
  103. data/app/views/spree/api/shipments/mine.v1.rabl +9 -0
  104. data/app/views/spree/api/shipments/show.v1.rabl +32 -0
  105. data/app/views/spree/api/shipments/small.v1.rabl +37 -0
  106. data/app/views/spree/api/shipping_rates/show.v1.rabl +2 -0
  107. data/app/views/spree/api/states/index.v1.rabl +14 -0
  108. data/app/views/spree/api/states/show.v1.rabl +2 -0
  109. data/app/views/spree/api/stock_items/index.v1.rabl +7 -0
  110. data/app/views/spree/api/stock_items/show.v1.rabl +5 -0
  111. data/app/views/spree/api/stock_locations/index.v1.rabl +7 -0
  112. data/app/views/spree/api/stock_locations/show.v1.rabl +8 -0
  113. data/app/views/spree/api/stock_movements/index.v1.rabl +7 -0
  114. data/app/views/spree/api/stock_movements/show.v1.rabl +5 -0
  115. data/app/views/spree/api/stock_transfers/receive.v1.rabl +5 -0
  116. data/app/views/spree/api/store_credit_events/mine.v1.rabl +10 -0
  117. data/app/views/spree/api/stores/index.v1.rabl +4 -0
  118. data/app/views/spree/api/stores/show.v1.rabl +2 -0
  119. data/app/views/spree/api/taxonomies/index.v1.rabl +7 -0
  120. data/app/views/spree/api/taxonomies/jstree.rabl +8 -0
  121. data/app/views/spree/api/taxonomies/nested.v1.rabl +11 -0
  122. data/app/views/spree/api/taxonomies/new.v1.rabl +3 -0
  123. data/app/views/spree/api/taxonomies/show.v1.rabl +15 -0
  124. data/app/views/spree/api/taxons/index.v1.rabl +12 -0
  125. data/app/views/spree/api/taxons/jstree.rabl +8 -0
  126. data/app/views/spree/api/taxons/new.v1.rabl +3 -0
  127. data/app/views/spree/api/taxons/show.v1.rabl +6 -0
  128. data/app/views/spree/api/taxons/taxons.v1.rabl +5 -0
  129. data/app/views/spree/api/transfer_items/show.v1.rabl +6 -0
  130. data/app/views/spree/api/users/index.v1.rabl +7 -0
  131. data/app/views/spree/api/users/new.v1.rabl +3 -0
  132. data/app/views/spree/api/users/show.v1.rabl +10 -0
  133. data/app/views/spree/api/variants/big.v1.rabl +17 -0
  134. data/app/views/spree/api/variants/index.v1.rabl +9 -0
  135. data/app/views/spree/api/variants/new.v1.rabl +2 -0
  136. data/app/views/spree/api/variants/show.v1.rabl +3 -0
  137. data/app/views/spree/api/variants/small.v1.rabl +17 -0
  138. data/app/views/spree/api/zones/index.v1.rabl +7 -0
  139. data/app/views/spree/api/zones/show.v1.rabl +6 -0
  140. data/config/initializers/metal_load_paths.rb +1 -0
  141. data/config/locales/en.yml +29 -0
  142. data/config/routes.rb +139 -0
  143. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +7 -0
  144. data/db/migrate/20120411123334_resize_api_key_field.rb +7 -0
  145. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +7 -0
  146. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +7 -0
  147. data/lib/solidus_api.rb +1 -0
  148. data/lib/spree/api/engine.rb +38 -0
  149. data/lib/spree/api/responders/rabl_template.rb +31 -0
  150. data/lib/spree/api/responders.rb +11 -0
  151. data/lib/spree/api/testing_support/caching.rb +10 -0
  152. data/lib/spree/api/testing_support/helpers.rb +44 -0
  153. data/lib/spree/api/testing_support/setup.rb +16 -0
  154. data/lib/spree/api.rb +10 -0
  155. data/lib/spree_api.rb +3 -0
  156. data/script/rails +9 -0
  157. data/solidus_api.gemspec +21 -0
  158. data/spec/controllers/spree/api/addresses_controller_spec.rb +56 -0
  159. data/spec/controllers/spree/api/base_controller_spec.rb +164 -0
  160. data/spec/controllers/spree/api/checkouts_controller_spec.rb +386 -0
  161. data/spec/controllers/spree/api/classifications_controller_spec.rb +48 -0
  162. data/spec/controllers/spree/api/config_controller_spec.rb +23 -0
  163. data/spec/controllers/spree/api/countries_controller_spec.rb +48 -0
  164. data/spec/controllers/spree/api/credit_cards_controller_spec.rb +80 -0
  165. data/spec/controllers/spree/api/images_controller_spec.rb +93 -0
  166. data/spec/controllers/spree/api/inventory_units_controller_spec.rb +50 -0
  167. data/spec/controllers/spree/api/line_items_controller_spec.rb +186 -0
  168. data/spec/controllers/spree/api/option_types_controller_spec.rb +116 -0
  169. data/spec/controllers/spree/api/option_values_controller_spec.rb +135 -0
  170. data/spec/controllers/spree/api/orders_controller_spec.rb +759 -0
  171. data/spec/controllers/spree/api/payments_controller_spec.rb +254 -0
  172. data/spec/controllers/spree/api/product_properties_controller_spec.rb +116 -0
  173. data/spec/controllers/spree/api/products_controller_spec.rb +454 -0
  174. data/spec/controllers/spree/api/promotion_application_spec.rb +50 -0
  175. data/spec/controllers/spree/api/promotions_controller_spec.rb +64 -0
  176. data/spec/controllers/spree/api/properties_controller_spec.rb +102 -0
  177. data/spec/controllers/spree/api/return_authorizations_controller_spec.rb +173 -0
  178. data/spec/controllers/spree/api/shipments_controller_spec.rb +252 -0
  179. data/spec/controllers/spree/api/states_controller_spec.rb +82 -0
  180. data/spec/controllers/spree/api/stock_items_controller_spec.rb +307 -0
  181. data/spec/controllers/spree/api/stock_locations_controller_spec.rb +172 -0
  182. data/spec/controllers/spree/api/stock_movements_controller_spec.rb +84 -0
  183. data/spec/controllers/spree/api/stock_transfers_controller_spec.rb +83 -0
  184. data/spec/controllers/spree/api/store_credit_events_controller_spec.rb +68 -0
  185. data/spec/controllers/spree/api/stores_controller_spec.rb +133 -0
  186. data/spec/controllers/spree/api/taxonomies_controller_spec.rb +114 -0
  187. data/spec/controllers/spree/api/taxons_controller_spec.rb +177 -0
  188. data/spec/controllers/spree/api/transfer_items_controller_spec.rb +152 -0
  189. data/spec/controllers/spree/api/unauthenticated_products_controller_spec.rb +26 -0
  190. data/spec/controllers/spree/api/users_controller_spec.rb +153 -0
  191. data/spec/controllers/spree/api/variants_controller_spec.rb +235 -0
  192. data/spec/controllers/spree/api/zones_controller_spec.rb +115 -0
  193. data/spec/features/checkout_spec.rb +187 -0
  194. data/spec/fixtures/thinking-cat.jpg +0 -0
  195. data/spec/models/spree/legacy_user_spec.rb +45 -0
  196. data/spec/requests/rabl_cache_spec.rb +32 -0
  197. data/spec/shared_examples/protect_product_actions.rb +17 -0
  198. data/spec/spec_helper.rb +60 -0
  199. data/spec/support/controller_hacks.rb +38 -0
  200. data/spec/support/database_cleaner.rb +14 -0
  201. data/spec/support/have_attributes_matcher.rb +13 -0
  202. metadata +334 -0
@@ -0,0 +1,254 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Spree::Api::PaymentsController, :type => :controller 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, :display_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
+
14
+ before do
15
+ stub_authentication!
16
+ end
17
+
18
+ context "as a user" do
19
+ context "when the order belongs to the user" do
20
+ before do
21
+ allow_any_instance_of(Order).to receive_messages :user => current_api_user
22
+ end
23
+
24
+ it "can view the payments for their order" do
25
+ api_get :index
26
+ expect(json_response["payments"].first).to have_attributes(attributes)
27
+ end
28
+
29
+ it "can learn how to create a new payment" do
30
+ api_get :new
31
+ expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
32
+ expect(json_response["payment_methods"]).not_to be_empty
33
+ expect(json_response["payment_methods"].first).to have_attributes([:id, :name, :description])
34
+ end
35
+
36
+ context "payment source is not required" do
37
+ before do
38
+ Spree::Gateway::Bogus.any_instance.stub(:source_required?).and_return(false)
39
+ end
40
+
41
+ it "can create a new payment" do
42
+ api_post :create, :payment => { :payment_method_id => PaymentMethod.first.id, :amount => 50 }
43
+ response.status.should == 201
44
+ json_response.should have_attributes(attributes)
45
+ end
46
+ end
47
+
48
+ context "payment source is required" do
49
+ context "no source is provided" do
50
+ it "returns errors" do
51
+ api_post :create, :payment => { :payment_method_id => PaymentMethod.first.id, :amount => 50 }
52
+ response.status.should == 422
53
+ json_response['error'].should == "Invalid resource. Please fix errors and try again."
54
+ json_response['errors']['source'].should == ["can't be blank"]
55
+ end
56
+ end
57
+
58
+ context "source is provided" do
59
+ it "can create a new payment" do
60
+ api_post :create, :payment => { :payment_method_id => PaymentMethod.first.id, :amount => 50, source_attributes: {gateway_payment_profile_id: 1} }
61
+ response.status.should == 201
62
+ json_response.should have_attributes(attributes)
63
+ end
64
+ end
65
+ end
66
+
67
+ it "can view a pre-existing payment's details" do
68
+ api_get :show, :id => payment.to_param
69
+ expect(json_response).to have_attributes(attributes)
70
+ end
71
+
72
+ it "cannot update a payment" do
73
+ api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
74
+ assert_unauthorized!
75
+ end
76
+
77
+ it "cannot authorize a payment" do
78
+ api_put :authorize, :id => payment.to_param
79
+ assert_unauthorized!
80
+ end
81
+ end
82
+
83
+ context "when the order does not belong to the user" do
84
+ before do
85
+ allow_any_instance_of(Order).to receive_messages :user => stub_model(LegacyUser)
86
+ end
87
+
88
+ it "cannot view payments for somebody else's order" do
89
+ api_get :index, :order_id => order.to_param
90
+ assert_unauthorized!
91
+ end
92
+
93
+ it "can view the payments for an order given the order token" do
94
+ api_get :index, :order_id => order.to_param, :order_token => order.guest_token
95
+ expect(json_response["payments"].first).to have_attributes(attributes)
96
+ end
97
+ end
98
+ end
99
+
100
+ context "as an admin" do
101
+ sign_in_as_admin!
102
+
103
+ it "can view the payments on any order" do
104
+ api_get :index
105
+ expect(response.status).to eq(200)
106
+ expect(json_response["payments"].first).to have_attributes(attributes)
107
+ end
108
+
109
+ context "multiple payments" do
110
+ before { @payment = create(:payment, :order => order, :response_code => '99999') }
111
+
112
+ it "can view all payments on an order" do
113
+ api_get :index
114
+ expect(json_response["count"]).to eq(2)
115
+ end
116
+
117
+ it 'can control the page size through a parameter' do
118
+ api_get :index, :per_page => 1
119
+ expect(json_response['count']).to eq(1)
120
+ expect(json_response['current_page']).to eq(1)
121
+ expect(json_response['pages']).to eq(2)
122
+ end
123
+
124
+ it 'can query the results through a paramter' do
125
+ api_get :index, :q => { :response_code_cont => '999' }
126
+ expect(json_response['count']).to eq(1)
127
+ expect(json_response['payments'].first['response_code']).to eq @payment.response_code
128
+ end
129
+ end
130
+
131
+ context "for a given payment" do
132
+ context "updating" do
133
+ it "can update" do
134
+ payment.update_attributes(:state => 'pending')
135
+ api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
136
+ expect(response.status).to eq(200)
137
+ expect(payment.reload.amount).to eq(2.01)
138
+ end
139
+
140
+ context "update fails" do
141
+ it "returns a 422 status when the amount is invalid" do
142
+ payment.update_attributes(:state => 'pending')
143
+ api_put :update, :id => payment.to_param, :payment => { :amount => 'invalid' }
144
+ expect(response.status).to eq(422)
145
+ expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
146
+ end
147
+
148
+ it "returns a 403 status when the payment is not pending" do
149
+ payment.update_attributes(:state => 'completed')
150
+ api_put :update, :id => payment.to_param, :payment => { :amount => 2.01 }
151
+ expect(response.status).to eq(403)
152
+ expect(json_response["error"]).to eq("This payment cannot be updated because it is completed.")
153
+ end
154
+ end
155
+ end
156
+
157
+ context "authorizing" do
158
+ it "can authorize" do
159
+ api_put :authorize, :id => payment.to_param
160
+ expect(response.status).to eq(200)
161
+ expect(payment.reload.state).to eq("pending")
162
+ end
163
+
164
+ context "authorization fails" do
165
+ before do
166
+ fake_response = double(:success? => false, :to_s => "Could not authorize card")
167
+ expect_any_instance_of(Spree::Gateway::Bogus).to receive(:authorize).and_return(fake_response)
168
+ api_put :authorize, :id => payment.to_param
169
+ end
170
+
171
+ it "returns a 422 status" do
172
+ expect(response.status).to eq(422)
173
+ expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
174
+ expect(json_response["errors"]["base"][0]).to eq "Could not authorize card"
175
+ end
176
+
177
+ it "does not raise a stack level error" do
178
+ skip "Investigate why a payment.reload after the request raises 'stack level too deep'"
179
+ expect(payment.reload.state).to eq("failed")
180
+ end
181
+ end
182
+ end
183
+
184
+ context "capturing" do
185
+ it "can capture" do
186
+ api_put :capture, :id => payment.to_param
187
+ expect(response.status).to eq(200)
188
+ expect(payment.reload.state).to eq("completed")
189
+ end
190
+
191
+ context "capturing fails" do
192
+ before do
193
+ fake_response = double(:success? => false, :to_s => "Insufficient funds")
194
+ expect_any_instance_of(Spree::Gateway::Bogus).to receive(:capture).and_return(fake_response)
195
+ end
196
+
197
+ it "returns a 422 status" do
198
+ api_put :capture, :id => payment.to_param
199
+ expect(response.status).to eq(422)
200
+ expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
201
+ expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
202
+ end
203
+ end
204
+ end
205
+
206
+ context "purchasing" do
207
+ it "can purchase" do
208
+ api_put :purchase, :id => payment.to_param
209
+ expect(response.status).to eq(200)
210
+ expect(payment.reload.state).to eq("completed")
211
+ end
212
+
213
+ context "purchasing fails" do
214
+ before do
215
+ fake_response = double(:success? => false, :to_s => "Insufficient funds")
216
+ expect_any_instance_of(Spree::Gateway::Bogus).to receive(:purchase).and_return(fake_response)
217
+ end
218
+
219
+ it "returns a 422 status" do
220
+ api_put :purchase, :id => payment.to_param
221
+ expect(response.status).to eq(422)
222
+ expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
223
+ expect(json_response["errors"]["base"][0]).to eq "Insufficient funds"
224
+ end
225
+ end
226
+ end
227
+
228
+ context "voiding" do
229
+ it "can void" do
230
+ api_put :void, id: payment.to_param
231
+ expect(response.status).to eq 200
232
+ expect(payment.reload.state).to eq "void"
233
+ end
234
+
235
+ context "voiding fails" do
236
+ before do
237
+ fake_response = double(success?: false, to_s: "NO REFUNDS")
238
+ expect_any_instance_of(Spree::Gateway::Bogus).to receive(:void).and_return(fake_response)
239
+ end
240
+
241
+ it "returns a 422 status" do
242
+ api_put :void, id: payment.to_param
243
+ expect(response.status).to eq 422
244
+ expect(json_response["error"]).to eq "Invalid resource. Please fix errors and try again."
245
+ expect(json_response["errors"]["base"][0]).to eq "NO REFUNDS"
246
+ expect(payment.reload.state).to eq "checkout"
247
+ end
248
+ end
249
+ end
250
+
251
+ end
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+ require 'shared_examples/protect_product_actions'
3
+
4
+ module Spree
5
+ describe Spree::Api::ProductPropertiesController, :type => :controller do
6
+ render_views
7
+
8
+ let!(:product) { create(:product) }
9
+ let!(:property_1) {product.product_properties.create(:property_name => "My Property 1", :value => "my value 1", :position => 0)}
10
+ let!(:property_2) {product.product_properties.create(:property_name => "My Property 2", :value => "my value 2", :position => 1)}
11
+
12
+ let(:attributes) { [:id, :product_id, :property_id, :value, :property_name] }
13
+ let(:resource_scoping) { { :product_id => product.to_param } }
14
+
15
+ before do
16
+ stub_authentication!
17
+ end
18
+
19
+ context "if product is deleted" do
20
+ before do
21
+ product.update_column(:deleted_at, 1.day.ago)
22
+ end
23
+
24
+ it "can not see a list of product properties" do
25
+ api_get :index
26
+ expect(response.status).to eq(404)
27
+ end
28
+ end
29
+
30
+ it "can see a list of all product properties" do
31
+ api_get :index
32
+ expect(json_response["product_properties"].count).to eq 2
33
+ expect(json_response["product_properties"].first).to have_attributes(attributes)
34
+ end
35
+
36
+ it "can control the page size through a parameter" do
37
+ api_get :index, :per_page => 1
38
+ expect(json_response['product_properties'].count).to eq(1)
39
+ expect(json_response['current_page']).to eq(1)
40
+ expect(json_response['pages']).to eq(2)
41
+ end
42
+
43
+ it 'can query the results through a parameter' do
44
+ Spree::ProductProperty.last.update_attribute(:value, 'loose')
45
+ property = Spree::ProductProperty.last
46
+ api_get :index, :q => { :value_cont => 'loose' }
47
+ expect(json_response['count']).to eq(1)
48
+ expect(json_response['product_properties'].first['value']).to eq property.value
49
+ end
50
+
51
+ it "can see a single product_property" do
52
+ api_get :show, :id => property_1.property_name
53
+ expect(json_response).to have_attributes(attributes)
54
+ end
55
+
56
+ it "can learn how to create a new product property" do
57
+ api_get :new
58
+ expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
59
+ expect(json_response["required_attributes"]).to be_empty
60
+ end
61
+
62
+ it "cannot create a new product property if not an admin" do
63
+ api_post :create, :product_property => { :property_name => "My Property 3" }
64
+ assert_unauthorized!
65
+ end
66
+
67
+ it "cannot update a product property" do
68
+ api_put :update, :id => property_1.property_name, :product_property => { :value => "my value 456" }
69
+ assert_unauthorized!
70
+ end
71
+
72
+ it "cannot delete a product property" do
73
+ api_delete :destroy, id: property_1.property_name
74
+ assert_unauthorized!
75
+ expect { property_1.reload }.not_to raise_error
76
+ end
77
+
78
+ context "as an admin" do
79
+ sign_in_as_admin!
80
+
81
+ it "can create a new product property" do
82
+ expect do
83
+ api_post :create, :product_property => { :property_name => "My Property 3", :value => "my value 3" }
84
+ end.to change(product.product_properties, :count).by(1)
85
+ expect(json_response).to have_attributes(attributes)
86
+ expect(response.status).to eq(201)
87
+ end
88
+
89
+ it "can update a product property" do
90
+ api_put :update, :id => property_1.property_name, :product_property => { :value => "my value 456" }
91
+ expect(response.status).to eq(200)
92
+ end
93
+
94
+ it "can delete a product property" do
95
+ api_delete :destroy, :id => property_1.property_name
96
+ expect(response.status).to eq(204)
97
+ expect { property_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
98
+ end
99
+ end
100
+
101
+ context "with product identified by id" do
102
+ let(:resource_scoping) { { :product_id => product.id } }
103
+ it "can see a list of all product properties" do
104
+ api_get :index
105
+ expect(json_response["product_properties"].count).to eq 2
106
+ expect(json_response["product_properties"].first).to have_attributes(attributes)
107
+ end
108
+
109
+ it "can see a single product_property by id" do
110
+ api_get :show, :id => property_1.id
111
+ expect(json_response).to have_attributes(attributes)
112
+ end
113
+ end
114
+
115
+ end
116
+ end