solidus_json_api 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/CHANGELOG.md +224 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE +26 -0
  7. data/README.md +94 -0
  8. data/Rakefile +38 -0
  9. data/app/controllers/concerns/spree/api/v2/renderable.rb +63 -0
  10. data/app/controllers/spree/api/v2/base_controller.rb +9 -0
  11. data/app/controllers/spree/api/v2/children_controller.rb +29 -0
  12. data/app/controllers/spree/api/v2/countries_controller.rb +21 -0
  13. data/app/controllers/spree/api/v2/images_controller.rb +29 -0
  14. data/app/controllers/spree/api/v2/line_items_controller.rb +36 -0
  15. data/app/controllers/spree/api/v2/option_types_controller.rb +31 -0
  16. data/app/controllers/spree/api/v2/option_values_controller.rb +29 -0
  17. data/app/controllers/spree/api/v2/orders_controller.rb +21 -0
  18. data/app/controllers/spree/api/v2/prices_controller.rb +27 -0
  19. data/app/controllers/spree/api/v2/products_controller.rb +37 -0
  20. data/app/controllers/spree/api/v2/states_controller.rb +29 -0
  21. data/app/controllers/spree/api/v2/taxonomies_controller.rb +21 -0
  22. data/app/controllers/spree/api/v2/taxons_controller.rb +40 -0
  23. data/app/controllers/spree/api/v2/variants_controller.rb +35 -0
  24. data/app/models/spree/base_decorator.rb +5 -0
  25. data/app/models/spree/image_decorator.rb +3 -0
  26. data/app/models/spree/line_item_decorator.rb +3 -0
  27. data/app/models/spree/order_decorator.rb +3 -0
  28. data/app/models/spree/price_decorator.rb +3 -0
  29. data/app/models/spree/state_decorator.rb +3 -0
  30. data/app/serializers/spree/address_serializer.rb +9 -0
  31. data/app/serializers/spree/base_serializer.rb +13 -0
  32. data/app/serializers/spree/country_serializer.rb +7 -0
  33. data/app/serializers/spree/error_serializer.rb +55 -0
  34. data/app/serializers/spree/image_serializer.rb +13 -0
  35. data/app/serializers/spree/line_item_serializer.rb +13 -0
  36. data/app/serializers/spree/option_type_serializer.rb +8 -0
  37. data/app/serializers/spree/option_value_serializer.rb +8 -0
  38. data/app/serializers/spree/order_serializer.rb +20 -0
  39. data/app/serializers/spree/price_serializer.rb +7 -0
  40. data/app/serializers/spree/product_serializer.rb +12 -0
  41. data/app/serializers/spree/role_serializer.rb +5 -0
  42. data/app/serializers/spree/state_serializer.rb +7 -0
  43. data/app/serializers/spree/store_serializer.rb +6 -0
  44. data/app/serializers/spree/taxon_serializer.rb +16 -0
  45. data/app/serializers/spree/taxonomy_serializer.rb +7 -0
  46. data/app/serializers/spree/user_serializer.rb +5 -0
  47. data/app/serializers/spree/variant_serializer.rb +12 -0
  48. data/circle.yml +12 -0
  49. data/config/locales/en.yml +25 -0
  50. data/config/routes.rb +53 -0
  51. data/docs/.nojekyll +0 -0
  52. data/docs/Dockerfile +12 -0
  53. data/docs/Gemfile +14 -0
  54. data/docs/README.md +12 -0
  55. data/docs/Rakefile +9 -0
  56. data/docs/config.rb +39 -0
  57. data/docs/font-selection.json +148 -0
  58. data/docs/source/CNAME +1 -0
  59. data/docs/source/fonts/slate.eot +0 -0
  60. data/docs/source/fonts/slate.svg +14 -0
  61. data/docs/source/fonts/slate.ttf +0 -0
  62. data/docs/source/fonts/slate.woff +0 -0
  63. data/docs/source/fonts/slate.woff2 +0 -0
  64. data/docs/source/images/favicon.ico +0 -0
  65. data/docs/source/images/navbar.png +0 -0
  66. data/docs/source/includes/_countries.md +133 -0
  67. data/docs/source/includes/_errors.md +17 -0
  68. data/docs/source/includes/_filtering.md +11 -0
  69. data/docs/source/includes/_images.md +201 -0
  70. data/docs/source/includes/_line_items.md +143 -0
  71. data/docs/source/includes/_option_types.md +267 -0
  72. data/docs/source/includes/_option_values.md +227 -0
  73. data/docs/source/includes/_orders.md +150 -0
  74. data/docs/source/includes/_pagination.md +10 -0
  75. data/docs/source/includes/_prices.md +188 -0
  76. data/docs/source/includes/_products.md +407 -0
  77. data/docs/source/includes/_states.md +96 -0
  78. data/docs/source/includes/_taxonomies.md +325 -0
  79. data/docs/source/includes/_taxons.md +414 -0
  80. data/docs/source/includes/_variants.md +430 -0
  81. data/docs/source/index.md +51 -0
  82. data/docs/source/javascripts/all.js +4 -0
  83. data/docs/source/javascripts/all_nosearch.js +3 -0
  84. data/docs/source/javascripts/app/_lang.js +162 -0
  85. data/docs/source/javascripts/app/_search.js +74 -0
  86. data/docs/source/javascripts/app/_toc.js +55 -0
  87. data/docs/source/javascripts/lib/_energize.js +169 -0
  88. data/docs/source/javascripts/lib/_imagesloaded.min.js +7 -0
  89. data/docs/source/javascripts/lib/_jquery.highlight.js +108 -0
  90. data/docs/source/javascripts/lib/_jquery.tocify.js +1042 -0
  91. data/docs/source/javascripts/lib/_jquery_ui.js +566 -0
  92. data/docs/source/javascripts/lib/_lunr.js +1910 -0
  93. data/docs/source/layouts/layout.erb +110 -0
  94. data/docs/source/stylesheets/_icon-font.scss +38 -0
  95. data/docs/source/stylesheets/_normalize.css +427 -0
  96. data/docs/source/stylesheets/_syntax.scss.erb +27 -0
  97. data/docs/source/stylesheets/_variables.scss +109 -0
  98. data/docs/source/stylesheets/print.css.scss +142 -0
  99. data/docs/source/stylesheets/screen.css.scss +622 -0
  100. data/lib/solidus_json_api/config.rb +8 -0
  101. data/lib/solidus_json_api/engine.rb +21 -0
  102. data/lib/solidus_json_api.rb +5 -0
  103. data/solidus_json_api.gemspec +36 -0
  104. data/spec/controllers/spree/api/v2/base_controller_spec.rb +46 -0
  105. data/spec/controllers/spree/api/v2/children_controller_spec.rb +28 -0
  106. data/spec/controllers/spree/api/v2/countries_controller_spec.rb +25 -0
  107. data/spec/controllers/spree/api/v2/images_controller_spec.rb +91 -0
  108. data/spec/controllers/spree/api/v2/line_items_controller_spec.rb +101 -0
  109. data/spec/controllers/spree/api/v2/option_types_controller_spec.rb +71 -0
  110. data/spec/controllers/spree/api/v2/option_values_controller_spec.rb +88 -0
  111. data/spec/controllers/spree/api/v2/orders_controller_spec.rb +61 -0
  112. data/spec/controllers/spree/api/v2/prices_controller_spec.rb +55 -0
  113. data/spec/controllers/spree/api/v2/products_controller_spec.rb +99 -0
  114. data/spec/controllers/spree/api/v2/states_controller_spec.rb +42 -0
  115. data/spec/controllers/spree/api/v2/taxonomies_controller_spec.rb +31 -0
  116. data/spec/controllers/spree/api/v2/taxons_controller_spec.rb +54 -0
  117. data/spec/controllers/spree/api/v2/variants_controller_spec.rb +106 -0
  118. data/spec/lib/solidus_json_api/config_spec.rb +10 -0
  119. data/spec/models/spree/base_decorator_spec.rb +9 -0
  120. data/spec/models/spree/price_decorator_spec.rb +3 -0
  121. data/spec/serializers/spree/address_serializer_spec.rb +35 -0
  122. data/spec/serializers/spree/country_serializer_spec.rb +27 -0
  123. data/spec/serializers/spree/error_serializer_spec.rb +141 -0
  124. data/spec/serializers/spree/image_serializer_spec.rb +30 -0
  125. data/spec/serializers/spree/line_item_serializer_spec.rb +40 -0
  126. data/spec/serializers/spree/option_type_serializer_spec.rb +27 -0
  127. data/spec/serializers/spree/option_value_serializer_spec.rb +29 -0
  128. data/spec/serializers/spree/order_serializer_spec.rb +68 -0
  129. data/spec/serializers/spree/price_serializer_spec.rb +28 -0
  130. data/spec/serializers/spree/product_serializer_spec.rb +47 -0
  131. data/spec/serializers/spree/role_serializer_spec.rb +17 -0
  132. data/spec/serializers/spree/state_serializer_spec.rb +25 -0
  133. data/spec/serializers/spree/store_serializer_spec.rb +25 -0
  134. data/spec/serializers/spree/taxon_serializer_spec.rb +44 -0
  135. data/spec/serializers/spree/taxonomy_serializer_spec.rb +27 -0
  136. data/spec/serializers/spree/user_serializer_spec.rb +17 -0
  137. data/spec/serializers/spree/variant_serializer_spec.rb +55 -0
  138. data/spec/spec_helper.rb +57 -0
  139. data/spec/support/shoulda_matchers.rb +6 -0
  140. metadata +442 -0
@@ -0,0 +1,46 @@
1
+ describe Spree::Api::V2::BaseController do
2
+ controller do
3
+ skip_before_action :authenticate_user
4
+
5
+ def index
6
+ render_collection Spree::Order
7
+ end
8
+ end
9
+
10
+ describe '#filter_params' do
11
+ before do
12
+ allow(controller).to receive(:serializer_attributes) { Spree::OrderSerializer._attributes }
13
+ end
14
+
15
+ let!(:orders) do
16
+ [create(:order), create(:order), create(:order, frontend_viewable: false)]
17
+ end
18
+
19
+ it 'can filter by a serializer\'s attribute' do
20
+ first_order = orders.first
21
+ get :index, filter: { id: first_order.id }
22
+ order_ids = parse_json(response.body)['data'].map do |order|
23
+ order['id']
24
+ end
25
+ expect(order_ids).to match [first_order.id.to_s]
26
+ end
27
+
28
+ it 'can filter multiple attributes' do
29
+ first_order, second_order, _third_order = orders
30
+ get :index, filter: { id: [first_order.id, second_order.id] }
31
+ order_ids = parse_json(response.body)['data'].map do |order|
32
+ order['id'].to_i
33
+ end
34
+
35
+ expect(order_ids).to match [first_order.id, second_order.id]
36
+ end
37
+
38
+ it 'will allow filtering of non-serializer attributes' do
39
+ get :index, filter: { frontend_viewable: false }
40
+ order_ids = parse_json(response.body)['data'].map do |order|
41
+ order['id'].to_i
42
+ end
43
+ expect(order_ids).to match orders.map(&:id)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,28 @@
1
+ describe Spree::Api::V2::ChildrenController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let(:taxon) { create :taxon }
5
+ let!(:child) { create :taxon, parent: taxon, taxonomy: taxon.taxonomy }
6
+
7
+ describe '#index' do
8
+ context 'by taxon' do
9
+ it 'can list all the children' do
10
+ get :index, taxon_id: taxon.id
11
+ taxon_ids = parse_json(response.body)['data'].map do |taxon|
12
+ taxon['id']
13
+ end
14
+ expect(taxon_ids).to include child.id.to_s
15
+ end
16
+ end
17
+ end
18
+
19
+ describe '#show' do
20
+ context 'by taxon' do
21
+ it 'will fetch the selected child' do
22
+ get :show, taxon_id: taxon.id, id: child.id
23
+ child_id = parse_json(response.body)['data']['id']
24
+ expect(child_id).to eql child.id.to_s
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ describe Spree::Api::V2::CountriesController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:country) { create :country }
5
+ let(:state) { create :state, country: country }
6
+
7
+ describe '#index' do
8
+ it 'will list all countries' do
9
+ get :index
10
+ expect(JSON.parse(response.body)['data'].length).to eql 1
11
+ end
12
+ end
13
+
14
+ describe '#show' do
15
+ it 'will list the selected country' do
16
+ get :show, id: country.id
17
+ expect(JSON.parse(response.body)['data']['id']).to eql country.id.to_s
18
+ end
19
+
20
+ it 'will the country via a state' do
21
+ get :show, state_id: state.id
22
+ expect(JSON.parse(response.body)['data']['id']).to eql country.id.to_s
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,91 @@
1
+ describe Spree::Api::V2::ImagesController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:image) { create :image }
5
+
6
+ let(:product) { create :product }
7
+ let(:new_product) { create :product }
8
+
9
+ let(:variant) { create :variant, images: [image] }
10
+ let(:new_variant) { create :variant, images: [create(:image)] }
11
+
12
+ describe '#index' do
13
+ it 'will list the images' do
14
+ get :index
15
+ image_ids = parse_json(response.body)['data'].map do |image|
16
+ image['id']
17
+ end
18
+ expect(image_ids).to include image.id.to_s
19
+ end
20
+
21
+ context 'by product id' do
22
+ it 'will list its images' do
23
+ product.images << image
24
+ get :index, product_id: product.id
25
+ image_ids = parse_json(response.body)['data'].map do |image|
26
+ image['id']
27
+ end
28
+ expect(image_ids).to include image.id.to_s
29
+ end
30
+
31
+ it 'will not list another products images' do
32
+ get :index, product_id: new_product.id
33
+ data = parse_json(response.body)['data']
34
+ expect(data).to be_empty
35
+ end
36
+ end
37
+
38
+ context 'by variant id' do
39
+ it 'will list its images' do
40
+ get :index, variant_id: variant.id
41
+ image_ids = parse_json(response.body)['data'].map do |image|
42
+ image['id']
43
+ end
44
+ expect(image_ids).to include image.id.to_s
45
+ end
46
+
47
+ it 'will not list another variants images' do
48
+ get :index, variant_id: new_variant.id
49
+ image_ids = parse_json(response.body)['data'].map do |image|
50
+ image['id']
51
+ end
52
+ expect(image_ids).to_not include image.id.to_s
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '#show' do
58
+ it 'will show the image' do
59
+ get :show, id: image.id
60
+ image_id = parse_json(response.body)['data']['id']
61
+ expect(image_id).to eql image.id.to_s
62
+ end
63
+
64
+ context 'by product id' do
65
+ it 'will show its image' do
66
+ product.images << image
67
+ get :show, id: image.id, product_id: product.id
68
+ image_id = parse_json(response.body)['data']['id']
69
+ expect(image_id).to eql image.id.to_s
70
+ end
71
+
72
+ it 'will not show another products image' do
73
+ get :show, id: image.id, product_id: new_product.id
74
+ expect(response).to have_http_status 404
75
+ end
76
+ end
77
+
78
+ context 'by variant id' do
79
+ it 'will show its image' do
80
+ get :show, id: image.id, variant_id: variant.id
81
+ image_id = parse_json(response.body)['data']['id']
82
+ expect(image_id).to eql image.id.to_s
83
+ end
84
+
85
+ it 'will not show another products image' do
86
+ get :show, id: image.id, variant_id: new_variant.id
87
+ expect(response).to have_http_status 404
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,101 @@
1
+ describe Spree::Api::V2::LineItemsController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let(:order) { create :order }
5
+ let(:user) { order.user }
6
+ let(:variant) { create :variant }
7
+
8
+ before { user.generate_spree_api_key! }
9
+
10
+ describe '#create' do
11
+ let(:line_item_data) do
12
+ {
13
+ attributes: {
14
+ order_id: order.id,
15
+ variant_id: variant.id,
16
+ quantity: 1
17
+ }
18
+ }
19
+ end
20
+
21
+ context 'on success' do
22
+ it 'will respond with line item' do
23
+ post :create, token: user.spree_api_key, data: line_item_data
24
+ expect(JSON.parse(response.body)['data']['type']).to eql 'spree_line_items'
25
+ end
26
+ end
27
+
28
+ context 'when out of range' do
29
+ before do
30
+ line_item_data[:attributes][:quantity] = 123_123_123_123
31
+ post :create, token: user.spree_api_key, data: line_item_data
32
+ end
33
+
34
+ it 'will respond with quantity too high error' do
35
+ expect(response.body).to be_json_eql <<-JSON
36
+ {
37
+ "errors" : [
38
+ {
39
+ "code": "400",
40
+ "detail" : #{Spree.t('api.errors.quantity_too_high.detail').gsub("\n", '').to_json},
41
+ "meta" : {},
42
+ "status": "Bad Request",
43
+ "title" : #{Spree.t('api.errors.quantity_too_high.title').titleize.to_json}
44
+ }
45
+ ]
46
+ }
47
+ JSON
48
+ end
49
+ end
50
+
51
+ context 'when record not found' do
52
+ before do
53
+ line_item_data[:attributes][:order_id] = 0
54
+ post :create, token: user.spree_api_key, data: line_item_data
55
+ end
56
+
57
+ it 'will respond with record not found error' do
58
+ expect(response.body).to be_json_eql <<-JSON
59
+ {
60
+ "errors" : [
61
+ {
62
+ "code": "400",
63
+ "detail" : #{Spree.t('api.errors.record_not_found.detail').gsub("\n", '').to_json},
64
+ "meta" : {},
65
+ "status": "Bad Request",
66
+ "title" : #{Spree.t('api.errors.record_not_found.title').to_json}
67
+ }
68
+ ]
69
+ }
70
+ JSON
71
+ end
72
+ end
73
+
74
+ context 'when record is invalid' do
75
+ let(:stock_item) { variant.stock_items.first }
76
+
77
+ before do
78
+ quantity = stock_item.count_on_hand + 1
79
+ stock_item.update(backorderable: false)
80
+ line_item_data[:attributes][:quantity] = quantity
81
+ post :create, token: user.spree_api_key, data: line_item_data
82
+ end
83
+
84
+ it 'will respond with product out of stock' do
85
+ expect(response.body).to be_json_eql <<-JSON
86
+ {
87
+ "errors" : [
88
+ {
89
+ "code": "400",
90
+ "detail" : "#{Spree.t('api.errors.product_out_of_stock.detail').gsub("\n", '')}",
91
+ "meta" : {},
92
+ "status": "Bad Request",
93
+ "title" : #{Spree.t('api.errors.product_out_of_stock.title').titleize.to_json}
94
+ }
95
+ ]
96
+ }
97
+ JSON
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,71 @@
1
+ describe Spree::Api::V2::OptionTypesController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:option_type) { create :option_type }
5
+ let(:product) { create :product, option_types: [option_type] }
6
+ let(:new_product) { create :product }
7
+
8
+ describe '#index' do
9
+ it 'will list all option types' do
10
+ get :index
11
+ option_type_ids = parse_json(response.body)['data'].map do |option_type|
12
+ option_type['id']
13
+ end
14
+ expect(option_type_ids).to include option_type.id.to_s
15
+ end
16
+
17
+ context 'by product' do
18
+ it 'will list option types' do
19
+ get :index, product_id: product.id
20
+ option_type_ids = parse_json(response.body)['data'].map do |option_type|
21
+ option_type['id']
22
+ end
23
+ expect(option_type_ids).to include option_type.id.to_s
24
+ end
25
+
26
+ it 'will not list option types belonging to a different product' do
27
+ get :index, product_id: new_product.id
28
+ data = parse_json(response.body)['data']
29
+ expect(data).to be_empty
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '#show' do
35
+ it 'will find selected option_type by id' do
36
+ get :show, id: option_type.id
37
+ option_type_id = parse_json(response.body)['data']['id']
38
+ expect(option_type_id).to eql option_type.id.to_s
39
+ end
40
+
41
+ context 'by product' do
42
+ it 'will list option types' do
43
+ get :show, id: option_type.id, product_id: product.id
44
+ option_type_id = parse_json(response.body)['data']['id']
45
+ expect(option_type_id).to eql option_type.id.to_s
46
+ end
47
+
48
+ it 'will not list option types belonging to a different product' do
49
+ get :show, id: option_type.id, product_id: new_product.id
50
+ expect(response).to have_http_status 404
51
+ end
52
+ end
53
+
54
+ context 'by option value' do
55
+ let(:option_value) { create :option_value, option_type: option_type }
56
+ let(:new_option_value) { create :option_value }
57
+
58
+ it 'will list option types' do
59
+ get :show, option_value_id: option_value.id
60
+ option_type_id = parse_json(response.body)['data']['id']
61
+ expect(option_type_id).to eql option_type.id.to_s
62
+ end
63
+
64
+ it 'will not list option types belonging to a different option value' do
65
+ get :show, option_value_id: new_option_value.id
66
+ option_type_id = parse_json(response.body)['data']['id']
67
+ expect(option_type_id).to_not eql option_type.id.to_s
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,88 @@
1
+ describe Spree::Api::V2::OptionValuesController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:option_value) { create :option_value }
5
+ let(:new_option_value) { create :option_value }
6
+ let(:variant) { create :variant, option_values: [option_value] }
7
+ let(:new_variant) { create :variant }
8
+
9
+ describe '#index' do
10
+ it 'will list all option values' do
11
+ get :index
12
+ option_value_ids = parse_json(response.body)['data'].map do |option_value|
13
+ option_value['id']
14
+ end
15
+ expect(option_value_ids).to include option_value.id.to_s
16
+ end
17
+
18
+ context 'by option type' do
19
+ it 'will list all option values' do
20
+ get :index, option_type_id: option_value.option_type.id
21
+ option_value_ids = parse_json(response.body)['data'].map do |option_value|
22
+ option_value['id']
23
+ end
24
+ expect(option_value_ids).to include option_value.id.to_s
25
+ end
26
+
27
+ it 'will not list option value belonging to a different option type' do
28
+ get :index, option_type_id: new_option_value.option_type.id
29
+ option_value_ids = parse_json(response.body)['data'].map do |option_value|
30
+ option_value['id']
31
+ end
32
+ expect(option_value_ids).to_not include option_value.id.to_s
33
+ end
34
+ end
35
+
36
+ context 'by variant' do
37
+ it 'will list all option values' do
38
+ get :index, variant_id: variant.id
39
+ option_value_ids = parse_json(response.body)['data'].map do |option_value|
40
+ option_value['id']
41
+ end
42
+ expect(option_value_ids).to include option_value.id.to_s
43
+ end
44
+
45
+ it 'will not list option value belonging to a different variant' do
46
+ get :index, variant_id: new_variant.id
47
+ option_value_ids = parse_json(response.body)['data'].map do |option_value|
48
+ option_value['id']
49
+ end
50
+ expect(option_value_ids).to_not include option_value.id.to_s
51
+ end
52
+ end
53
+ end
54
+
55
+ describe '#show' do
56
+ it 'will find selected option value by id' do
57
+ get :show, id: option_value.id
58
+ option_value_id = parse_json(response.body)['data']['id']
59
+ expect(option_value_id).to eql option_value.id.to_s
60
+ end
61
+
62
+ context 'by option type' do
63
+ it 'will find option value by option type' do
64
+ get :show, option_type_id: option_value.option_type.id, id: option_value.id
65
+ option_value_id = parse_json(response.body)['data']['id']
66
+ expect(option_value_id).to eql option_value.id.to_s
67
+ end
68
+
69
+ it 'will not find option value belonging to a different option type' do
70
+ get :show, option_type_id: new_option_value.option_type.id, id: option_value.id
71
+ expect(response).to have_http_status 404
72
+ end
73
+ end
74
+
75
+ context 'by variant' do
76
+ it 'will find option value by option type' do
77
+ get :show, variant_id: variant.id, id: option_value.id
78
+ option_value_id = parse_json(response.body)['data']['id']
79
+ expect(option_value_id).to eql option_value.id.to_s
80
+ end
81
+
82
+ it 'will not find option value belonging to a different option type' do
83
+ get :show, variant_id: new_variant.id, id: option_value.id
84
+ expect(response).to have_http_status 404
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,61 @@
1
+ describe Spree::Api::V2::OrdersController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let(:order) { create :order }
5
+ let(:user) { order.user }
6
+ let(:admin) { create :admin_user }
7
+
8
+ before { user.generate_spree_api_key! }
9
+
10
+ describe '#index' do
11
+ context 'when admin' do
12
+ before { admin.generate_spree_api_key! }
13
+
14
+ it 'will list all orders' do
15
+ get :index, token: admin.spree_api_key
16
+ ids = JSON.parse(response.body)['data'].map { |d| d['id'].to_i }
17
+ expect(ids).to include order.id
18
+ end
19
+ end
20
+
21
+ context 'when user' do
22
+ let!(:new_order) { create :order }
23
+
24
+ before { get :index, token: user.spree_api_key }
25
+
26
+ it 'will not list another users order' do
27
+ ids = JSON.parse(response.body)['data'].map { |d| d['id'].to_i }
28
+ expect(ids).to_not include new_order.id
29
+ end
30
+
31
+ it 'will list the current users orders' do
32
+ ids = JSON.parse(response.body)['data'].map { |d| d['id'].to_i }
33
+ expect(ids).to include order.id
34
+ end
35
+ end
36
+ end
37
+
38
+ describe '#show' do
39
+ context 'when admin' do
40
+ before { admin.generate_spree_api_key! }
41
+
42
+ it 'can respond with another users order' do
43
+ get :show, token: admin.spree_api_key, id: order.id
44
+ expect(JSON.parse(response.body)['data']['type']).to eql 'spree_orders'
45
+ end
46
+ end
47
+
48
+ context 'when user' do
49
+ it 'will not respond with another users order' do
50
+ new_order = create :order
51
+ get :show, token: user.spree_api_key, id: new_order.id
52
+ expect(response).to have_http_status 404
53
+ end
54
+
55
+ it 'will respond with the current users order' do
56
+ get :show, token: user.spree_api_key, id: order.id
57
+ expect(JSON.parse(response.body)['data']['id'].to_i).to eql order.id
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,55 @@
1
+ describe Spree::Api::V2::PricesController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:price) { create :price }
5
+ let(:new_variant) { create :variant }
6
+
7
+ describe '#index' do
8
+ it 'will list all prices' do
9
+ get :index
10
+ price_ids = parse_json(response.body)['data'].map do |price|
11
+ price['id']
12
+ end
13
+ expect(price_ids).to include price.id.to_s
14
+ end
15
+
16
+ context 'by variant id' do
17
+ it 'will list its prices' do
18
+ get :index, variant_id: price.variant.id
19
+ price_ids = parse_json(response.body)['data'].map do |price|
20
+ price['id']
21
+ end
22
+ expect(price_ids).to include price.id.to_s
23
+ end
24
+
25
+ it 'will not list another variants prices' do
26
+ get :index, variant_id: new_variant.id
27
+ price_ids = parse_json(response.body)['data'].map do |price|
28
+ price['id']
29
+ end
30
+ expect(price_ids).to_not include price.id.to_s
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#show' do
36
+ it 'will show the price' do
37
+ get :show, id: price.id
38
+ price_id = parse_json(response.body)['data']['id']
39
+ expect(price_id).to eql price.id.to_s
40
+ end
41
+
42
+ context 'by variant id' do
43
+ it 'will show its prices' do
44
+ get :show, id: price.id, variant_id: price.variant.id
45
+ price_id = parse_json(response.body)['data']['id']
46
+ expect(price_id).to eql price.id.to_s
47
+ end
48
+
49
+ it 'will not show another variants price' do
50
+ get :show, id: price.id, variant_id: new_variant.id
51
+ expect(response).to have_http_status 404
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,99 @@
1
+ describe Spree::Api::V2::ProductsController do
2
+ routes { Spree::Core::Engine.routes }
3
+
4
+ let!(:product) { create :product }
5
+ let(:taxon) { create :taxon, products: [product] }
6
+ let(:new_taxon) { create :taxon }
7
+
8
+ describe '#index' do
9
+ it 'will respond with products' do
10
+ get :index
11
+ expect(JSON.parse(response.body)['data'].size).to eql 1
12
+ end
13
+
14
+ context 'by taxon id' do
15
+ it 'will respond with the taxons products' do
16
+ get :index, taxon_id: taxon.id
17
+ product_ids = parse_json(response.body)['data'].map do |products|
18
+ products['id']
19
+ end
20
+ expect(product_ids).to include product.id.to_s
21
+ end
22
+
23
+ it 'will not respond with a product that is not associated to the taxon' do
24
+ get :index, taxon_id: new_taxon.id
25
+ data = parse_json(response.body)['data']
26
+ expect(data).to be_empty
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#show' do
32
+ it 'will render the selected product' do
33
+ get :show, id: product.id
34
+ expect(parse_json(response.body)['data']['id']).to eql product.id.to_s
35
+ end
36
+
37
+ it 'can find a product by slug' do
38
+ get :show, id: product.slug
39
+ product_id = parse_json(response.body)['data']['id']
40
+ expect(product_id).to eql product.id.to_s
41
+ end
42
+
43
+ context 'by taxon id' do
44
+ it 'will respond with the taxons products' do
45
+ get :show, taxon_id: taxon.id, id: product.id
46
+ product_id = parse_json(response.body)['data']['id']
47
+ expect(product_id).to eql product.id.to_s
48
+ end
49
+
50
+ it 'will not respond with a product that is not associated to the taxon' do
51
+ get :show, taxon_id: new_taxon.id, id: product.id
52
+ expect(response).to have_http_status 404
53
+ end
54
+ end
55
+
56
+ context 'by option type' do
57
+ let(:option_type) { create :option_type, products: [product] }
58
+ let(:new_option_type) { create :option_type }
59
+
60
+ it 'will find product' do
61
+ get :show, option_type_id: option_type.id, id: product.id
62
+ product_id = parse_json(response.body)['data']['id']
63
+ expect(product_id).to eql product.id.to_s
64
+ end
65
+
66
+ it 'will not find a product belong to a different option type' do
67
+ get :show, option_type_id: new_option_type.id, id: product.id
68
+ expect(response).to have_http_status 404
69
+ end
70
+ end
71
+
72
+ context 'by variant id' do
73
+ it 'will show its product' do
74
+ get :show, variant_id: product.master.id
75
+ product_id = parse_json(response.body)['data']['id']
76
+ expect(product_id).to eql product.id.to_s
77
+ end
78
+ end
79
+
80
+ context 'by price id' do
81
+ it 'will show its product' do
82
+ get :show, price_id: product.master.prices.first.id
83
+ product_id = parse_json(response.body)['data']['id']
84
+ expect(product_id).to eql product.id.to_s
85
+ end
86
+ end
87
+
88
+ context 'by image id' do
89
+ let(:image) { create :image }
90
+
91
+ it 'will show its product' do
92
+ product.images << image
93
+ get :show, image_id: image.id
94
+ product_id = parse_json(response.body)['data']['id']
95
+ expect(product_id).to eql product.id.to_s
96
+ end
97
+ end
98
+ end
99
+ end