solidus_api 2.10.2 → 2.11.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/app/controllers/spree/api/addresses_controller.rb +1 -1
  4. data/app/controllers/spree/api/base_controller.rb +11 -3
  5. data/app/controllers/spree/api/checkouts_controller.rb +1 -11
  6. data/app/controllers/spree/api/countries_controller.rb +2 -2
  7. data/app/controllers/spree/api/credit_cards_controller.rb +2 -2
  8. data/app/controllers/spree/api/customer_returns_controller.rb +67 -0
  9. data/app/controllers/spree/api/images_controller.rb +4 -4
  10. data/app/controllers/spree/api/inventory_units_controller.rb +1 -1
  11. data/app/controllers/spree/api/option_types_controller.rb +3 -3
  12. data/app/controllers/spree/api/option_values_controller.rb +2 -2
  13. data/app/controllers/spree/api/orders_controller.rb +1 -1
  14. data/app/controllers/spree/api/payments_controller.rb +1 -1
  15. data/app/controllers/spree/api/product_properties_controller.rb +9 -14
  16. data/app/controllers/spree/api/promotions_controller.rb +3 -12
  17. data/app/controllers/spree/api/properties_controller.rb +3 -3
  18. data/app/controllers/spree/api/resource_controller.rb +2 -2
  19. data/app/controllers/spree/api/return_authorizations_controller.rb +3 -3
  20. data/app/controllers/spree/api/shipments_controller.rb +1 -1
  21. data/app/controllers/spree/api/states_controller.rb +3 -3
  22. data/app/controllers/spree/api/stock_items_controller.rb +2 -2
  23. data/app/controllers/spree/api/stock_locations_controller.rb +3 -3
  24. data/app/controllers/spree/api/stock_movements_controller.rb +3 -3
  25. data/app/controllers/spree/api/stores_controller.rb +3 -3
  26. data/app/controllers/spree/api/taxonomies_controller.rb +2 -2
  27. data/app/controllers/spree/api/taxons_controller.rb +4 -4
  28. data/app/controllers/spree/api/users_controller.rb +13 -0
  29. data/app/controllers/spree/api/variants_controller.rb +3 -3
  30. data/app/controllers/spree/api/zones_controller.rb +2 -2
  31. data/app/helpers/spree/api/api_helpers.rb +18 -5
  32. data/app/views/spree/api/customer_returns/index.json.jbuilder +6 -0
  33. data/app/views/spree/api/customer_returns/new.json.jbuilder +4 -0
  34. data/app/views/spree/api/customer_returns/show.json.jbuilder +3 -0
  35. data/app/views/spree/api/errors/could_not_transition.json.jbuilder +4 -0
  36. data/app/views/spree/api/orders/could_not_transition.json.jbuilder +5 -0
  37. data/config/locales/en.yml +2 -0
  38. data/config/routes.rb +6 -3
  39. data/lib/spree/api/testing_support/helpers.rb +11 -2
  40. data/openapi/authentication.md +9 -1
  41. data/openapi/checkout-flow.md +17 -4
  42. data/openapi/main.hub.yml +1 -1
  43. data/openapi/solidus-api.oas.yml +6753 -0
  44. data/solidus_api.gemspec +19 -19
  45. metadata +17 -117
  46. data/openapi/api.oas2.yml +0 -6108
  47. data/script/rails +0 -10
  48. data/spec/controllers/spree/api/base_controller_spec.rb +0 -118
  49. data/spec/controllers/spree/api/resource_controller_spec.rb +0 -190
  50. data/spec/features/checkout_spec.rb +0 -192
  51. data/spec/fixtures/thinking-cat.jpg +0 -0
  52. data/spec/lib/spree_api_responders_spec.rb +0 -10
  53. data/spec/models/spree/legacy_user_spec.rb +0 -103
  54. data/spec/requests/api/address_books_spec.rb +0 -240
  55. data/spec/requests/jbuilder_cache_spec.rb +0 -34
  56. data/spec/requests/ransackable_attributes_spec.rb +0 -79
  57. data/spec/requests/spree/api/addresses_controller_spec.rb +0 -57
  58. data/spec/requests/spree/api/checkouts_controller_spec.rb +0 -484
  59. data/spec/requests/spree/api/classifications_controller_spec.rb +0 -50
  60. data/spec/requests/spree/api/config_controller_spec.rb +0 -26
  61. data/spec/requests/spree/api/countries_controller_spec.rb +0 -48
  62. data/spec/requests/spree/api/coupon_codes_controller_spec.rb +0 -105
  63. data/spec/requests/spree/api/credit_cards_controller_spec.rb +0 -105
  64. data/spec/requests/spree/api/images_controller_spec.rb +0 -99
  65. data/spec/requests/spree/api/inventory_units_controller_spec.rb +0 -55
  66. data/spec/requests/spree/api/line_items_controller_spec.rb +0 -213
  67. data/spec/requests/spree/api/option_types_controller_spec.rb +0 -116
  68. data/spec/requests/spree/api/option_values_controller_spec.rb +0 -138
  69. data/spec/requests/spree/api/orders_controller_spec.rb +0 -954
  70. data/spec/requests/spree/api/payments_controller_spec.rb +0 -259
  71. data/spec/requests/spree/api/product_properties_controller_spec.rb +0 -114
  72. data/spec/requests/spree/api/products_controller_spec.rb +0 -422
  73. data/spec/requests/spree/api/promotion_application_spec.rb +0 -50
  74. data/spec/requests/spree/api/promotions_controller_spec.rb +0 -67
  75. data/spec/requests/spree/api/properties_controller_spec.rb +0 -102
  76. data/spec/requests/spree/api/return_authorizations_controller_spec.rb +0 -180
  77. data/spec/requests/spree/api/shipments_controller_spec.rb +0 -532
  78. data/spec/requests/spree/api/states_controller_spec.rb +0 -69
  79. data/spec/requests/spree/api/stock_items_controller_spec.rb +0 -311
  80. data/spec/requests/spree/api/stock_locations_controller_spec.rb +0 -170
  81. data/spec/requests/spree/api/stock_movements_controller_spec.rb +0 -81
  82. data/spec/requests/spree/api/store_credit_events_controller_spec.rb +0 -59
  83. data/spec/requests/spree/api/stores_controller_spec.rb +0 -134
  84. data/spec/requests/spree/api/taxonomies_controller_spec.rb +0 -114
  85. data/spec/requests/spree/api/taxons_controller_spec.rb +0 -217
  86. data/spec/requests/spree/api/unauthenticated_products_controller_spec.rb +0 -27
  87. data/spec/requests/spree/api/users_controller_spec.rb +0 -151
  88. data/spec/requests/spree/api/variants_controller_spec.rb +0 -340
  89. data/spec/requests/spree/api/zones_controller_spec.rb +0 -89
  90. data/spec/shared_examples/protect_product_actions.rb +0 -18
  91. data/spec/spec_helper.rb +0 -73
  92. data/spec/support/be_paginated_matcher.rb +0 -9
  93. data/spec/support/controller_hacks.rb +0 -43
  94. data/spec/support/database_cleaner.rb +0 -16
  95. data/spec/support/have_attributes_matcher.rb +0 -11
  96. data/spec/test_views/spree/api/widgets/_widget.json.jbuilder +0 -3
  97. data/spec/test_views/spree/api/widgets/index.json.jbuilder +0 -9
  98. data/spec/test_views/spree/api/widgets/new.json.jbuilder +0 -3
  99. data/spec/test_views/spree/api/widgets/show.json.jbuilder +0 -3
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- module Spree
6
- describe Api::StockMovementsController, type: :request do
7
- let!(:stock_location) { create(:stock_location_with_items) }
8
- let!(:stock_item) { stock_location.stock_items.order(:id).first }
9
- let!(:stock_movement) { create(:stock_movement, stock_item: stock_item) }
10
- let!(:attributes) { [:id, :quantity, :stock_item_id] }
11
-
12
- before do
13
- stub_authentication!
14
- end
15
-
16
- context 'as a user' do
17
- it 'cannot see a list of stock movements' do
18
- get spree.api_stock_location_stock_movements_path(stock_location)
19
- expect(response.status).to eq(401)
20
- end
21
-
22
- it 'cannot see a stock movement' do
23
- get spree.api_stock_location_stock_movement_path(stock_location, stock_movement)
24
- expect(response.status).to eq(404)
25
- end
26
-
27
- it 'cannot create a stock movement' do
28
- params = {
29
- stock_movement: {
30
- stock_item_id: stock_item.to_param
31
- }
32
- }
33
-
34
- post spree.api_stock_location_stock_movements_path(stock_location), params: params
35
- expect(response.status).to eq(401)
36
- end
37
- end
38
-
39
- context 'as an admin' do
40
- sign_in_as_admin!
41
-
42
- it 'gets list of stock movements' do
43
- get spree.api_stock_location_stock_movements_path(stock_location)
44
- expect(json_response['stock_movements'].first).to have_attributes(attributes)
45
- expect(json_response['stock_movements'].first['stock_item']['count_on_hand']).to eq 11
46
- end
47
-
48
- it 'can control the page size through a parameter' do
49
- create(:stock_movement, stock_item: stock_item)
50
- get spree.api_stock_location_stock_movements_path(stock_location), params: { per_page: 1 }
51
- expect(json_response['count']).to eq(1)
52
- expect(json_response['current_page']).to eq(1)
53
- expect(json_response['pages']).to eq(2)
54
- end
55
-
56
- it 'can query the results through a paramter' do
57
- create(:stock_movement, :received, quantity: 10, stock_item: stock_item)
58
- get spree.api_stock_location_stock_movements_path(stock_location), params: { q: { quantity_eq: '10' } }
59
- expect(json_response['count']).to eq(1)
60
- end
61
-
62
- it 'gets a stock movement' do
63
- get spree.api_stock_location_stock_movement_path(stock_location, stock_movement)
64
- expect(json_response).to have_attributes(attributes)
65
- expect(json_response['stock_item_id']).to eq stock_movement.stock_item_id
66
- end
67
-
68
- it 'can create a new stock movement' do
69
- params = {
70
- stock_movement: {
71
- stock_item_id: stock_item.to_param
72
- }
73
- }
74
-
75
- post spree.api_stock_location_stock_movements_path(stock_location), params: params
76
- expect(response.status).to eq(201)
77
- expect(json_response).to have_attributes(attributes)
78
- end
79
- end
80
- end
81
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Spree::Api::StoreCreditEventsController, type: :request do
6
- let(:api_user) { create(:user, :with_api_key) }
7
-
8
- describe "GET mine" do
9
- subject do
10
- get spree.mine_api_store_credit_events_path(format: :json), headers: { Authorization: "Bearer #{api_key}" }
11
- end
12
-
13
- context "no current api user" do
14
- let(:api_key) { nil }
15
-
16
- before { subject }
17
-
18
- it "returns a 401" do
19
- expect(response.status).to eq 401
20
- end
21
- end
22
-
23
- context "the current api user is authenticated" do
24
- let(:current_api_user) { create(:user, :with_api_key) }
25
- let(:api_key) { current_api_user.spree_api_key }
26
-
27
- context "the user doesn't have store credit" do
28
- before { subject }
29
-
30
- it "should set the events variable to empty list" do
31
- expect(json_response["store_credit_events"]).to eq []
32
- end
33
-
34
- it "returns a 200" do
35
- expect(response.status).to eq 200
36
- end
37
- end
38
-
39
- context "the user has store credit" do
40
- let!(:store_credit) { create(:store_credit, user: current_api_user) }
41
-
42
- before { subject }
43
-
44
- it "should contain the store credit allocation event" do
45
- expect(json_response["store_credit_events"].size).to eq 1
46
- expect(json_response["store_credit_events"][0]).to include(
47
- "display_amount" => "$150.00",
48
- "display_user_total_amount" => "$150.00",
49
- "display_action" => "Added"
50
- )
51
- end
52
-
53
- it "returns a 200" do
54
- expect(response.status).to eq 200
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,134 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "spec_helper"
4
-
5
- module Spree
6
- describe Api::StoresController, type: :request do
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
- end
24
-
25
- it "can list the available stores" do
26
- get spree.api_stores_path
27
- expect(json_response["stores"]).to match_array([
28
- {
29
- "id" => store.id,
30
- "name" => "My Spree Store",
31
- "url" => "spreestore.example.com",
32
- "meta_description" => nil,
33
- "meta_keywords" => nil,
34
- "seo_title" => nil,
35
- "mail_from_address" => "spree@example.org",
36
- "default_currency" => nil,
37
- "code" => store.code,
38
- "default" => true,
39
- "available_locales" => ["en"]
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
- "available_locales" => ["en"]
53
- }
54
- ])
55
- end
56
-
57
- it "can get the store details" do
58
- get spree.api_store_path(store)
59
- expect(json_response).to eq(
60
- "id" => store.id,
61
- "name" => "My Spree Store",
62
- "url" => "spreestore.example.com",
63
- "meta_description" => nil,
64
- "meta_keywords" => nil,
65
- "seo_title" => nil,
66
- "mail_from_address" => "spree@example.org",
67
- "default_currency" => nil,
68
- "code" => store.code,
69
- "default" => true,
70
- "available_locales" => ["en"]
71
- )
72
- end
73
-
74
- it "can create a new store" do
75
- store_hash = {
76
- code: "spree123",
77
- name: "Hack0rz",
78
- url: "spree123.example.com",
79
- mail_from_address: "me@example.com"
80
- }
81
- post spree.api_stores_path, params: { store: store_hash }
82
- expect(response.status).to eq(201)
83
- end
84
-
85
- it "can update an existing store" do
86
- store_hash = {
87
- url: "spree123.example.com",
88
- mail_from_address: "me@example.com"
89
- }
90
- put spree.api_store_path(store), params: { store: store_hash }
91
- expect(response.status).to eq(200)
92
- expect(store.reload.url).to eql "spree123.example.com"
93
- expect(store.reload.mail_from_address).to eql "me@example.com"
94
- end
95
-
96
- context "deleting a store" do
97
- it "will fail if it's the default Store" do
98
- delete spree.api_store_path(store)
99
- expect(response.status).to eq(422)
100
- expect(json_response["errors"]["base"]).to eql(
101
- ["Cannot destroy the default Store."]
102
- )
103
- end
104
-
105
- it "will destroy the store" do
106
- delete spree.api_store_path(non_default_store)
107
- expect(response.status).to eq(204)
108
- end
109
- end
110
- end
111
-
112
- context "as an user" do
113
- it "cannot list all the stores" do
114
- get spree.api_stores_path
115
- expect(response.status).to eq(401)
116
- end
117
-
118
- it "cannot get the store details" do
119
- get spree.api_store_path(store)
120
- expect(response.status).to eq(401)
121
- end
122
-
123
- it "cannot create a new store" do
124
- post spree.api_stores_path, params: { store: {} }
125
- expect(response.status).to eq(401)
126
- end
127
-
128
- it "cannot update an existing store" do
129
- put spree.api_store_path(store), params: { store: {} }
130
- expect(response.status).to eq(401)
131
- end
132
- end
133
- end
134
- end
@@ -1,114 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- module Spree
6
- describe Api::TaxonomiesController, type: :request do
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
- get spree.api_taxonomies_path
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
- get spree.api_taxonomies_path, params: { 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
- get spree.api_taxonomies_path, params: { 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
- get spree.api_taxonomy_path(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
- get spree.api_taxonomy_path(taxonomy.id), params: { 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
- expect(Spree::Deprecation).to(receive(:warn))
64
- get spree.jstree_api_taxonomy_path(taxonomy.id)
65
- expect(json_response["data"]).to eq(taxonomy.root.name)
66
- expect(json_response["attr"]).to eq({ "id" => taxonomy.root.id, "name" => taxonomy.root.name })
67
- expect(json_response["state"]).to eq("closed")
68
- end
69
-
70
- it "can learn how to create a new taxonomy" do
71
- get spree.new_api_taxonomy_path(taxonomy.id)
72
- expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
73
- required_attributes = json_response["required_attributes"]
74
- expect(required_attributes).to include("name")
75
- end
76
-
77
- it "cannot create a new taxonomy if not an admin" do
78
- post spree.api_taxonomies_path, params: { taxonomy: { name: "Location" } }
79
- assert_unauthorized!
80
- end
81
-
82
- it "cannot update a taxonomy" do
83
- put spree.api_taxonomy_path(taxonomy.id), params: { taxonomy: { name: "I hacked your store!" } }
84
- assert_unauthorized!
85
- end
86
-
87
- it "cannot delete a taxonomy" do
88
- delete spree.api_taxonomy_path(taxonomy.id)
89
- assert_unauthorized!
90
- end
91
- end
92
-
93
- context "as an admin" do
94
- sign_in_as_admin!
95
-
96
- it "can create" do
97
- post spree.api_taxonomies_path, params: { taxonomy: { name: "Colors" } }
98
- expect(json_response).to have_attributes(attributes)
99
- expect(response.status).to eq(201)
100
- end
101
-
102
- it "cannot create a new taxonomy with invalid attributes" do
103
- post spree.api_taxonomies_path, params: { taxonomy: {} }
104
- expect(response.status).to eq(422)
105
- expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
106
- end
107
-
108
- it "can destroy" do
109
- delete spree.api_taxonomy_path(taxonomy.id)
110
- expect(response.status).to eq(204)
111
- end
112
- end
113
- end
114
- end
@@ -1,217 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- module Spree
6
- describe Api::TaxonsController, type: :request do
7
- let!(:taxonomy) { create(:taxonomy) }
8
- let!(:taxon) { create(:taxon, name: "Ruby", parent: taxonomy.root, taxonomy: taxonomy) }
9
- let!(:taxon2) { create(:taxon, name: "Rails", parent: taxon, taxonomy: taxonomy) }
10
- let!(:rails_v3_2_2) { create(:taxon, name: "3.2.2", parent: taxon2, taxonomy: taxonomy) }
11
- let(:attributes) { ["id", "name", "pretty_name", "permalink", "parent_id", "taxonomy_id"] }
12
-
13
- before do
14
- stub_authentication!
15
- end
16
-
17
- context "as a normal user" do
18
- it "gets all taxons for a taxonomy" do
19
- get spree.api_taxonomy_taxons_path(taxonomy)
20
-
21
- expect(json_response['taxons'].first['name']).to eq taxon.name
22
- children = json_response['taxons'].first['taxons']
23
- expect(children.count).to eq 1
24
- expect(children.first['name']).to eq taxon2.name
25
- expect(children.first['taxons'].count).to eq 1
26
- end
27
-
28
- # Regression test for https://github.com/spree/spree/issues/4112
29
- it "does not include children when asked not to" do
30
- get spree.api_taxonomy_taxons_path(taxonomy), params: { without_children: 1 }
31
-
32
- expect(json_response['taxons'].first['name']).to eq(taxon.name)
33
- expect(json_response['taxons'].first['taxons']).to be_nil
34
- end
35
-
36
- it "paginates through taxons" do
37
- new_taxon = create(:taxon, name: "Go", taxonomy: taxonomy)
38
- taxonomy.root.children << new_taxon
39
- expect(taxonomy.root.children.count).to eql(2)
40
- get spree.api_taxonomy_taxons_path(taxonomy), params: { page: 1, per_page: 1 }
41
- expect(json_response["count"]).to eql(1)
42
- expect(json_response["total_count"]).to eql(2)
43
- expect(json_response["current_page"]).to eql(1)
44
- expect(json_response["per_page"]).to eql(1)
45
- expect(json_response["pages"]).to eql(2)
46
- end
47
-
48
- describe 'searching' do
49
- context 'with a name' do
50
- before do
51
- get spree.api_taxons_path, params: { q: { name_cont: name } }
52
- end
53
-
54
- context 'with one result' do
55
- let(:name) { "Ruby" }
56
-
57
- it "returns an array including the matching taxon" do
58
- expect(json_response['taxons'].count).to eq(1)
59
- expect(json_response['taxons'].first['name']).to eq "Ruby"
60
- end
61
- end
62
-
63
- context 'with no results' do
64
- let(:name) { "Imaginary" }
65
-
66
- it 'returns an empty array of taxons' do
67
- expect(json_response.keys).to include('taxons')
68
- expect(json_response['taxons'].count).to eq(0)
69
- end
70
- end
71
- end
72
-
73
- context 'with no filters' do
74
- it "gets all taxons" do
75
- get spree.api_taxons_path
76
-
77
- expect(json_response['taxons'].first['name']).to eq taxonomy.root.name
78
- children = json_response['taxons'].first['taxons']
79
- expect(children.count).to eq 1
80
- expect(children.first['name']).to eq taxon.name
81
- expect(children.first['taxons'].count).to eq 1
82
- end
83
- end
84
- end
85
-
86
- context 'filtering by taxon ids' do
87
- it 'returns only requested id' do
88
- get spree.api_taxons_path, params: { ids: [rails_v3_2_2.id] }
89
-
90
- expect(json_response['taxons'].size).to eq 1
91
- end
92
-
93
- it 'returns only requested ids' do
94
- # We need a completly new branch to avoid having parent that can be preloaded from the rails ancestors
95
- python = create(:taxon, name: "Python", parent: taxonomy.root, taxonomy: taxonomy)
96
- python_three = create(:taxon, name: "3.0", parent: python, taxonomy: taxonomy)
97
-
98
- get spree.api_taxons_path, params: { ids: [rails_v3_2_2.id, python_three.id] }
99
-
100
- expect(json_response['taxons'].size).to eq 2
101
- end
102
- end
103
-
104
- it "gets a single taxon" do
105
- get spree.api_taxonomy_taxon_path(taxonomy, taxon.id)
106
-
107
- expect(json_response['name']).to eq taxon.name
108
- expect(json_response['taxons'].count).to eq 1
109
- end
110
-
111
- it "gets all taxons in JSTree form" do
112
- expect(Spree::Deprecation).to(receive(:warn))
113
- get spree.jstree_api_taxonomy_taxon_path(taxonomy, taxon.id)
114
- response = json_response.first
115
- expect(response["data"]).to eq(taxon2.name)
116
- expect(response["attr"]).to eq({ "name" => taxon2.name, "id" => taxon2.id })
117
- expect(response["state"]).to eq("closed")
118
- end
119
-
120
- it "can learn how to create a new taxon" do
121
- get spree.new_api_taxonomy_taxon_path(taxonomy)
122
- expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
123
- required_attributes = json_response["required_attributes"]
124
- expect(required_attributes).to include("name")
125
- end
126
-
127
- it "cannot create a new taxon if not an admin" do
128
- post spree.api_taxonomy_taxons_path(taxonomy), params: { taxon: { name: "Location" } }
129
- assert_unauthorized!
130
- end
131
-
132
- it "cannot update a taxon" do
133
- put spree.api_taxonomy_taxon_path(taxonomy, taxon.id), params: { taxon: { name: "I hacked your store!" } }
134
- assert_unauthorized!
135
- end
136
-
137
- it "cannot delete a taxon" do
138
- delete spree.api_taxonomy_taxon_path(taxonomy, taxon.id)
139
- assert_unauthorized!
140
- end
141
-
142
- context "with caching enabled" do
143
- let!(:product) { create(:product, taxons: [taxon]) }
144
-
145
- before do
146
- ActionController::Base.perform_caching = true
147
- end
148
-
149
- it "handles exclude_data correctly" do
150
- get spree.api_taxon_products_path, params: { id: taxon.id, simple: true }
151
- expect(response).to be_successful
152
- simple_response = json_response
153
-
154
- get spree.api_taxon_products_path, params: { id: taxon.id }
155
- expect(response).to be_successful
156
- full_response = json_response
157
-
158
- expect(simple_response["products"][0]["description"]).to be_nil
159
- expect(full_response["products"][0]["description"]).not_to be_nil
160
- end
161
-
162
- after do
163
- ActionController::Base.perform_caching = false
164
- end
165
- end
166
- end
167
-
168
- context "as an admin" do
169
- sign_in_as_admin!
170
-
171
- it "can create" do
172
- post spree.api_taxonomy_taxons_path(taxonomy), params: { taxon: { name: "Colors" } }
173
- expect(json_response).to have_attributes(attributes)
174
- expect(response.status).to eq(201)
175
-
176
- expect(taxonomy.reload.root.children.count).to eq 2
177
- taxon = Spree::Taxon.where(name: 'Colors').first
178
-
179
- expect(taxon.parent_id).to eq taxonomy.root.id
180
- expect(taxon.taxonomy_id).to eq taxonomy.id
181
- end
182
-
183
- it "can update the position in the list" do
184
- taxonomy.root.children << taxon2
185
- put spree.api_taxonomy_taxon_path(taxonomy, taxon.id), params: { taxon: { parent_id: taxon.parent_id, child_index: 2 } }
186
- expect(response.status).to eq(200)
187
- expect(taxonomy.reload.root.children[0]).to eql taxon2
188
- expect(taxonomy.reload.root.children[1]).to eql taxon
189
- end
190
-
191
- it "cannot create a new taxon with invalid attributes" do
192
- post spree.api_taxonomy_taxons_path(taxonomy), params: { taxon: { foo: :bar } }
193
- expect(response.status).to eq(422)
194
- expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
195
-
196
- expect(taxonomy.reload.root.children.count).to eq 1
197
- end
198
-
199
- it "cannot create a new taxon with invalid taxonomy_id" do
200
- post spree.api_taxonomy_taxons_path(1000), params: { taxon: { name: "Colors" } }
201
- expect(response.status).to eq(422)
202
- expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
203
-
204
- errors = json_response["errors"]
205
- expect(errors["taxonomy_id"]).not_to be_nil
206
- expect(errors["taxonomy_id"].first).to eq "Invalid taxonomy id."
207
-
208
- expect(taxonomy.reload.root.children.count).to eq 1
209
- end
210
-
211
- it "can destroy" do
212
- delete spree.api_taxonomy_taxon_path(taxonomy, taxon.id)
213
- expect(response.status).to eq(204)
214
- end
215
- end
216
- end
217
- end