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,68 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Api::StoreCreditEventsController, type: :controller do
4
+ render_views
5
+
6
+ let(:api_user) { create(:user) }
7
+
8
+ before do
9
+ allow(controller).to receive(:load_user)
10
+ controller.instance_variable_set(:@current_api_user, api_user)
11
+ end
12
+
13
+ describe "GET mine" do
14
+
15
+ subject { api_get :mine, { format: :json } }
16
+
17
+ before { allow(controller).to receive_messages(current_api_user: current_api_user) }
18
+
19
+ context "no current api user" do
20
+ let(:current_api_user) { nil }
21
+
22
+ before { subject }
23
+
24
+ it "returns a 401" do
25
+ expect(response.status).to eq 401
26
+ end
27
+ end
28
+
29
+ context "the current api user is authenticated" do
30
+ let(:current_api_user) { order.user }
31
+ let(:order) { create(:order, line_items: [line_item]) }
32
+
33
+ context "the user doesn't have store credit" do
34
+ let(:current_api_user) { create(:user) }
35
+
36
+ before { subject }
37
+
38
+ it "should set the events variable to empty list" do
39
+ expect(assigns(:store_credit_events)).to eq []
40
+ end
41
+
42
+ it "returns a 200" do
43
+ expect(subject.status).to eq 200
44
+ end
45
+ end
46
+
47
+ context "the user has store credit" do
48
+ let(:store_credit) { create(:store_credit, user: api_user) }
49
+ let(:current_api_user) { store_credit.user }
50
+
51
+ before { subject }
52
+
53
+ it "should contain one store credit event" do
54
+ expect(assigns(:store_credit_events).size).to eq 1
55
+ end
56
+
57
+ it "should contain the store credit allocation event" do
58
+ expect(assigns(:store_credit_events).first).to eq store_credit.store_credit_events.first
59
+ end
60
+
61
+ it "returns a 200" do
62
+ expect(subject.status).to eq 200
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,133 @@
1
+ require "spec_helper"
2
+
3
+ module Spree
4
+ describe Api::StoresController, type: :controller do
5
+ render_views
6
+
7
+ let!(:store) do
8
+ create(:store, name: "My Spree Store", url: "spreestore.example.com")
9
+ end
10
+
11
+ before do
12
+ stub_authentication!
13
+ end
14
+
15
+ context "as an admin" do
16
+ sign_in_as_admin!
17
+
18
+ let!(:non_default_store) do
19
+ create(:store,
20
+ name: "Extra Store",
21
+ url: "spreestore-5.example.com",
22
+ default: false
23
+ )
24
+ end
25
+
26
+ it "I can list the available stores" do
27
+ api_get :index
28
+ expect(json_response["stores"]).to eq([
29
+ {
30
+ "id" => store.id,
31
+ "name" => "My Spree Store",
32
+ "url" => "spreestore.example.com",
33
+ "meta_description" => nil,
34
+ "meta_keywords" => nil,
35
+ "seo_title" => nil,
36
+ "mail_from_address" => "spree@example.org",
37
+ "default_currency" => nil,
38
+ "code" => store.code,
39
+ "default" => true
40
+ },
41
+ {
42
+ "id" => non_default_store.id,
43
+ "name" => "Extra Store",
44
+ "url" => "spreestore-5.example.com",
45
+ "meta_description" => nil,
46
+ "meta_keywords" => nil,
47
+ "seo_title" => nil,
48
+ "mail_from_address" => "spree@example.org",
49
+ "default_currency" => nil,
50
+ "code" => non_default_store.code,
51
+ "default" => false
52
+ }
53
+ ])
54
+ end
55
+
56
+ it "I can get the store details" do
57
+ api_get :show, id: store.id
58
+ expect(json_response).to eq(
59
+ "id" => store.id,
60
+ "name" => "My Spree Store",
61
+ "url" => "spreestore.example.com",
62
+ "meta_description" => nil,
63
+ "meta_keywords" => nil,
64
+ "seo_title" => nil,
65
+ "mail_from_address" => "spree@example.org",
66
+ "default_currency" => nil,
67
+ "code" => store.code,
68
+ "default" => true
69
+ )
70
+ end
71
+
72
+ it "I can create a new store" do
73
+ store_hash = {
74
+ code: "spree123",
75
+ name: "Hack0rz",
76
+ url: "spree123.example.com",
77
+ mail_from_address: "me@example.com"
78
+ }
79
+ api_post :create, store: store_hash
80
+ expect(response.status).to eq(201)
81
+ end
82
+
83
+ it "I can update an existing store" do
84
+ store_hash = {
85
+ url: "spree123.example.com",
86
+ mail_from_address: "me@example.com"
87
+ }
88
+ api_put :update, id: store.id, store: store_hash
89
+ expect(response.status).to eq(200)
90
+ expect(store.reload.url).to eql "spree123.example.com"
91
+ expect(store.reload.mail_from_address).to eql "me@example.com"
92
+ end
93
+
94
+ context "deleting a store" do
95
+ it "will fail if it's the default Store" do
96
+ api_delete :destroy, id: store.id
97
+ expect(response.status).to eq(422)
98
+ expect(json_response["errors"]["base"]).to eql(
99
+ ["Cannot destroy the default Store."]
100
+ )
101
+ end
102
+
103
+ it "will destroy the store" do
104
+ api_delete :destroy, id: non_default_store.id
105
+ expect(response.status).to eq(204)
106
+ end
107
+ end
108
+ end
109
+
110
+ context "as an user" do
111
+
112
+ it "I cannot list all the stores" do
113
+ api_get :index
114
+ expect(response.status).to eq(401)
115
+ end
116
+
117
+ it "I cannot get the store details" do
118
+ api_get :show, id: store.id
119
+ expect(response.status).to eq(401)
120
+ end
121
+
122
+ it "I cannot create a new store" do
123
+ api_post :create, store: {}
124
+ expect(response.status).to eq(401)
125
+ end
126
+
127
+ it "I cannot update an existing store" do
128
+ api_put :update, id: store.id, store: {}
129
+ expect(response.status).to eq(401)
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::TaxonomiesController, :type => :controller do
5
+ render_views
6
+
7
+ let(:taxonomy) { create(:taxonomy) }
8
+ let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
+ let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
+ let(:attributes) { [:id, :name] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ taxon2.children << create(:taxon, :name => "3.2.2", :taxonomy => taxonomy)
15
+ taxon.children << taxon2
16
+ taxonomy.root.children << taxon
17
+ end
18
+
19
+ context "as a normal user" do
20
+ it "gets all taxonomies" do
21
+ api_get :index
22
+
23
+ expect(json_response["taxonomies"].first['name']).to eq taxonomy.name
24
+ expect(json_response["taxonomies"].first['root']['taxons'].count).to eq 1
25
+ end
26
+
27
+ it 'can control the page size through a parameter' do
28
+ create(:taxonomy)
29
+ api_get :index, :per_page => 1
30
+ expect(json_response['count']).to eq(1)
31
+ expect(json_response['current_page']).to eq(1)
32
+ expect(json_response['pages']).to eq(2)
33
+ end
34
+
35
+ it 'can query the results through a paramter' do
36
+ expected_result = create(:taxonomy, :name => 'Style')
37
+ api_get :index, :q => { :name_cont => 'style' }
38
+ expect(json_response['count']).to eq(1)
39
+ expect(json_response['taxonomies'].first['name']).to eq expected_result.name
40
+ end
41
+
42
+ it "gets a single taxonomy" do
43
+ api_get :show, :id => taxonomy.id
44
+
45
+ expect(json_response['name']).to eq taxonomy.name
46
+
47
+ children = json_response['root']['taxons']
48
+ expect(children.count).to eq 1
49
+ expect(children.first['name']).to eq taxon.name
50
+ expect(children.first.key?('taxons')).to be false
51
+ end
52
+
53
+ it "gets a single taxonomy with set=nested" do
54
+ api_get :show, :id => taxonomy.id, :set => 'nested'
55
+
56
+ expect(json_response['name']).to eq taxonomy.name
57
+
58
+ children = json_response['root']['taxons']
59
+ expect(children.first.key?('taxons')).to be true
60
+ end
61
+
62
+ it "gets the jstree-friendly version of a taxonomy" do
63
+ api_get :jstree, :id => taxonomy.id
64
+ expect(json_response["data"]).to eq(taxonomy.root.name)
65
+ expect(json_response["attr"]).to eq({ "id" => taxonomy.root.id, "name" => taxonomy.root.name})
66
+ expect(json_response["state"]).to eq("closed")
67
+ end
68
+
69
+ it "can learn how to create a new taxonomy" do
70
+ api_get :new
71
+ expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
72
+ required_attributes = json_response["required_attributes"]
73
+ expect(required_attributes).to include("name")
74
+ end
75
+
76
+ it "cannot create a new taxonomy if not an admin" do
77
+ api_post :create, :taxonomy => { :name => "Location" }
78
+ assert_unauthorized!
79
+ end
80
+
81
+ it "cannot update a taxonomy" do
82
+ api_put :update, :id => taxonomy.id, :taxonomy => { :name => "I hacked your store!" }
83
+ assert_unauthorized!
84
+ end
85
+
86
+ it "cannot delete a taxonomy" do
87
+ api_delete :destroy, :id => taxonomy.id
88
+ assert_unauthorized!
89
+ end
90
+ end
91
+
92
+ context "as an admin" do
93
+ sign_in_as_admin!
94
+
95
+ it "can create" do
96
+ api_post :create, :taxonomy => { :name => "Colors"}
97
+ expect(json_response).to have_attributes(attributes)
98
+ expect(response.status).to eq(201)
99
+ end
100
+
101
+ it "cannot create a new taxonomy with invalid attributes" do
102
+ api_post :create, :taxonomy => {}
103
+ expect(response.status).to eq(422)
104
+ expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
105
+ errors = json_response["errors"]
106
+ end
107
+
108
+ it "can destroy" do
109
+ api_delete :destroy, :id => taxonomy.id
110
+ expect(response.status).to eq(204)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,177 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::TaxonsController, :type => :controller do
5
+ render_views
6
+
7
+ let(:taxonomy) { create(:taxonomy) }
8
+ let(:taxon) { create(:taxon, :name => "Ruby", :taxonomy => taxonomy) }
9
+ let(:taxon2) { create(:taxon, :name => "Rails", :taxonomy => taxonomy) }
10
+ let(:attributes) { ["id", "name", "pretty_name", "permalink", "parent_id", "taxonomy_id"] }
11
+
12
+ before do
13
+ stub_authentication!
14
+ taxon2.children << create(:taxon, :name => "3.2.2", :taxonomy => taxonomy)
15
+ taxon.children << taxon2
16
+ taxonomy.root.children << taxon
17
+ end
18
+
19
+ context "as a normal user" do
20
+ it "gets all taxons for a taxonomy" do
21
+ api_get :index, :taxonomy_id => taxonomy.id
22
+
23
+ expect(json_response['taxons'].first['name']).to eq taxon.name
24
+ children = json_response['taxons'].first['taxons']
25
+ expect(children.count).to eq 1
26
+ expect(children.first['name']).to eq taxon2.name
27
+ expect(children.first['taxons'].count).to eq 1
28
+ end
29
+
30
+ # Regression test for #4112
31
+ it "does not include children when asked not to" do
32
+ api_get :index, :taxonomy_id => taxonomy.id, :without_children => 1
33
+
34
+ expect(json_response['taxons'].first['name']).to eq(taxon.name)
35
+ expect(json_response['taxons'].first['taxons']).to be_nil
36
+ end
37
+
38
+ it "paginates through taxons" do
39
+ new_taxon = create(:taxon, :name => "Go", :taxonomy => taxonomy)
40
+ taxonomy.root.children << new_taxon
41
+ expect(taxonomy.root.children.count).to eql(2)
42
+ api_get :index, :taxonomy_id => taxonomy.id, :page => 1, :per_page => 1
43
+ expect(json_response["count"]).to eql(1)
44
+ expect(json_response["total_count"]).to eql(2)
45
+ expect(json_response["current_page"]).to eql(1)
46
+ expect(json_response["per_page"]).to eql(1)
47
+ expect(json_response["pages"]).to eql(2)
48
+ end
49
+
50
+ describe 'searching' do
51
+ context 'with a name' do
52
+ before do
53
+ api_get :index, :q => { :name_cont => name }
54
+ end
55
+
56
+ context 'with one result' do
57
+ let(:name) { "Ruby" }
58
+
59
+ it "returns an array including the matching taxon" do
60
+ expect(json_response['taxons'].count).to eq(1)
61
+ expect(json_response['taxons'].first['name']).to eq "Ruby"
62
+ end
63
+ end
64
+
65
+ context 'with no results' do
66
+ let(:name) { "Imaginary" }
67
+
68
+ it 'returns an empty array of taxons' do
69
+ expect(json_response.keys).to include('taxons')
70
+ expect(json_response['taxons'].count).to eq(0)
71
+ end
72
+ end
73
+ end
74
+
75
+ context 'with no filters' do
76
+ it "gets all taxons" do
77
+ api_get :index
78
+
79
+ expect(json_response['taxons'].first['name']).to eq taxonomy.root.name
80
+ children = json_response['taxons'].first['taxons']
81
+ expect(children.count).to eq 1
82
+ expect(children.first['name']).to eq taxon.name
83
+ expect(children.first['taxons'].count).to eq 1
84
+ end
85
+ end
86
+ end
87
+
88
+ it "gets a single taxon" do
89
+ api_get :show, :id => taxon.id, :taxonomy_id => taxonomy.id
90
+
91
+ expect(json_response['name']).to eq taxon.name
92
+ expect(json_response['taxons'].count).to eq 1
93
+ end
94
+
95
+ it "gets all taxons in JSTree form" do
96
+ api_get :jstree, :taxonomy_id => taxonomy.id, :id => taxon.id
97
+ response = json_response.first
98
+ expect(response["data"]).to eq(taxon2.name)
99
+ expect(response["attr"]).to eq({ "name" => taxon2.name, "id" => taxon2.id})
100
+ expect(response["state"]).to eq("closed")
101
+ end
102
+
103
+ it "can learn how to create a new taxon" do
104
+ api_get :new, :taxonomy_id => taxonomy.id
105
+ expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
106
+ required_attributes = json_response["required_attributes"]
107
+ expect(required_attributes).to include("name")
108
+ end
109
+
110
+ it "cannot create a new taxon if not an admin" do
111
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => { :name => "Location" }
112
+ assert_unauthorized!
113
+ end
114
+
115
+ it "cannot update a taxon" do
116
+ api_put :update, :taxonomy_id => taxonomy.id, :id => taxon.id, :taxon => { :name => "I hacked your store!" }
117
+ assert_unauthorized!
118
+ end
119
+
120
+ it "cannot delete a taxon" do
121
+ api_delete :destroy, :taxonomy_id => taxonomy.id, :id => taxon.id
122
+ assert_unauthorized!
123
+ end
124
+ end
125
+
126
+ context "as an admin" do
127
+ sign_in_as_admin!
128
+
129
+ it "can create" do
130
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => { :name => "Colors" }
131
+ expect(json_response).to have_attributes(attributes)
132
+ expect(response.status).to eq(201)
133
+
134
+ expect(taxonomy.reload.root.children.count).to eq 2
135
+ taxon = Spree::Taxon.where(:name => 'Colors').first
136
+
137
+ expect(taxon.parent_id).to eq taxonomy.root.id
138
+ expect(taxon.taxonomy_id).to eq taxonomy.id
139
+ end
140
+
141
+ it "can update the position in the list" do
142
+ taxonomy.root.children << taxon2
143
+ api_put :update, :taxonomy_id => taxonomy.id, :id => taxon.id, :taxon => {:parent_id => taxon.parent_id, :child_index => 2 }
144
+ expect(response.status).to eq(200)
145
+ expect(taxonomy.reload.root.children[0]).to eql taxon2
146
+ expect(taxonomy.reload.root.children[1]).to eql taxon
147
+ end
148
+
149
+ it "cannot create a new taxon with invalid attributes" do
150
+ api_post :create, :taxonomy_id => taxonomy.id, :taxon => {}
151
+ expect(response.status).to eq(422)
152
+ expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
153
+ errors = json_response["errors"]
154
+
155
+ expect(taxonomy.reload.root.children.count).to eq 1
156
+ end
157
+
158
+ it "cannot create a new taxon with invalid taxonomy_id" do
159
+ api_post :create, :taxonomy_id => 1000, :taxon => { :name => "Colors" }
160
+ expect(response.status).to eq(422)
161
+ expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
162
+
163
+ errors = json_response["errors"]
164
+ expect(errors["taxonomy_id"]).not_to be_nil
165
+ expect(errors["taxonomy_id"].first).to eq "Invalid taxonomy id."
166
+
167
+ expect(taxonomy.reload.root.children.count).to eq 1
168
+ end
169
+
170
+ it "can destroy" do
171
+ api_delete :destroy, :taxonomy_id => taxonomy.id, :id => taxon.id
172
+ expect(response.status).to eq(204)
173
+ end
174
+ end
175
+
176
+ end
177
+ end
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::TransferItemsController do
5
+ render_views
6
+
7
+ let!(:stock_transfer) { create(:stock_transfer_with_items) }
8
+ let(:transfer_item) { stock_transfer.transfer_items.first }
9
+
10
+ before do
11
+ stub_authentication!
12
+ end
13
+
14
+ context "as a normal user" do
15
+ describe "#create" do
16
+ it "cannot create a transfer item" do
17
+ api_post :create, stock_transfer_id: stock_transfer.to_param
18
+ expect(response.status).to eq 401
19
+ end
20
+ end
21
+
22
+ describe "#update" do
23
+ it "cannot update a transfer item" do
24
+ api_put :update, stock_transfer_id: stock_transfer.to_param, id: transfer_item.to_param
25
+ expect(response.status).to eq 401
26
+ end
27
+ end
28
+
29
+ describe "#destroy" do
30
+ it "cannot delete a transfer item" do
31
+ api_delete :destroy, stock_transfer_id: stock_transfer.to_param, id: transfer_item.to_param
32
+ expect(response.status).to eq 401
33
+ end
34
+ end
35
+ end
36
+
37
+ context "as an admin" do
38
+ sign_in_as_admin!
39
+
40
+ describe "#create" do
41
+ subject do
42
+ create_params = {
43
+ stock_transfer_id: stock_transfer.to_param,
44
+ transfer_item: {
45
+ variant_id: variant_id,
46
+ expected_quantity: 1
47
+ }
48
+ }
49
+ api_post :create, create_params
50
+ end
51
+
52
+ context "valid parameters" do
53
+ let(:variant) { create(:variant) }
54
+ let(:variant_id) { variant.id }
55
+
56
+ context "variant is available" do
57
+ before do
58
+ variant.stock_items.update_all(count_on_hand: 1)
59
+ end
60
+
61
+ it "can create a transfer item" do
62
+ subject
63
+ expect(response.status).to eq 201
64
+ end
65
+
66
+ it "creates a transfer item" do
67
+ expect { subject }.to change { Spree::TransferItem.count }.by(1)
68
+ end
69
+ end
70
+
71
+ context "variant is not available" do
72
+ before do
73
+ variant.stock_items.update_all(count_on_hand: 0)
74
+ end
75
+
76
+ it "returns an error status" do
77
+ subject
78
+ expect(response.status).to eq 422
79
+ end
80
+
81
+ it "does not create a transfer item" do
82
+ expect { subject }.to_not change { Spree::TransferItem.count }
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ describe "#update" do
89
+ subject do
90
+ update_params = { id: transfer_item.to_param, stock_transfer_id: stock_transfer.to_param, transfer_item: { received_quantity: received_quantity } }
91
+ api_put :update, update_params
92
+ end
93
+
94
+ context "valid parameters" do
95
+ let(:received_quantity) { 2 }
96
+
97
+ it "can update a transfer item" do
98
+ subject
99
+ expect(response.status).to eq 200
100
+ end
101
+
102
+ it "updates the transfer item" do
103
+ expect { subject }.to change { transfer_item.reload.received_quantity }.to(2)
104
+ end
105
+ end
106
+
107
+ context "invalid parameters" do
108
+ let(:received_quantity) { -5 }
109
+
110
+ it "returns a 422" do
111
+ subject
112
+ expect(response.status).to eq 422
113
+ end
114
+
115
+ it "does not update the transfer item" do
116
+ expect { subject }.to_not change { transfer_item.reload.received_quantity }
117
+ end
118
+ end
119
+ end
120
+
121
+ describe "#destroy" do
122
+ subject { api_delete :destroy, id: transfer_item.to_param, stock_transfer_id: stock_transfer.to_param }
123
+
124
+ context "hasn't been finalized" do
125
+ it "can delete a transfer item" do
126
+ subject
127
+ expect(response.status).to eq 200
128
+ end
129
+
130
+ it "deletes the transfer item" do
131
+ expect { subject }.to change { Spree::TransferItem.count }.by(-1)
132
+ end
133
+ end
134
+
135
+ context "has been finalized" do
136
+ before do
137
+ stock_transfer.update_attributes(finalized_at: Time.now)
138
+ end
139
+
140
+ it "returns an error status code" do
141
+ subject
142
+ expect(response.status).to eq 422
143
+ end
144
+
145
+ it "does not delete the transfer item" do
146
+ expect { subject }.to_not change { Spree::TransferItem.count }
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,26 @@
1
+ require 'shared_examples/protect_product_actions'
2
+ require 'spec_helper'
3
+
4
+ module Spree
5
+ describe Spree::Api::ProductsController, :type => :controller do
6
+ render_views
7
+
8
+ let!(:product) { create(:product) }
9
+ let(:attributes) { [:id, :name, :description, :price, :available_on, :slug, :meta_description, :meta_keywords, :taxon_ids] }
10
+
11
+ context "without authentication" do
12
+ before { Spree::Api::Config[:requires_authentication] = false }
13
+
14
+ it "retrieves a list of products" do
15
+ api_get :index
16
+ expect(json_response["products"].first).to have_attributes(attributes)
17
+ expect(json_response["count"]).to eq(1)
18
+ expect(json_response["current_page"]).to eq(1)
19
+ expect(json_response["pages"]).to eq(1)
20
+ end
21
+
22
+ it_behaves_like "modifying product actions are restricted"
23
+ end
24
+ end
25
+ end
26
+