spree_api 3.1.14 → 3.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -1
  3. data/app/controllers/spree/api/base_controller.rb +2 -2
  4. data/app/controllers/spree/api/v1/addresses_controller.rb +1 -1
  5. data/app/controllers/spree/api/v1/classifications_controller.rb +1 -1
  6. data/app/controllers/spree/api/v1/images_controller.rb +3 -0
  7. data/app/controllers/spree/api/v1/inventory_units_controller.rb +3 -3
  8. data/app/controllers/spree/api/v1/line_items_controller.rb +3 -0
  9. data/app/controllers/spree/api/v1/option_types_controller.rb +16 -7
  10. data/app/controllers/spree/api/v1/option_values_controller.rb +6 -3
  11. data/app/controllers/spree/api/v1/orders_controller.rb +13 -24
  12. data/app/controllers/spree/api/v1/payments_controller.rb +0 -1
  13. data/app/controllers/spree/api/v1/products_controller.rb +8 -5
  14. data/app/controllers/spree/api/v1/stock_items_controller.rb +1 -1
  15. data/app/controllers/spree/api/v1/stock_locations_controller.rb +1 -1
  16. data/app/controllers/spree/api/v1/tags_controller.rb +28 -0
  17. data/app/controllers/spree/api/v1/taxonomies_controller.rb +7 -4
  18. data/app/controllers/spree/api/v1/taxons_controller.rb +4 -1
  19. data/app/controllers/spree/api/v1/users_controller.rb +5 -3
  20. data/app/controllers/spree/api/v1/zones_controller.rb +3 -3
  21. data/app/helpers/spree/api/api_helpers.rb +4 -1
  22. data/app/models/spree/api_configuration.rb +1 -1
  23. data/app/views/spree/api/errors/invalid_api_key.v1.rabl +1 -1
  24. data/app/views/spree/api/errors/invalid_resource.v1.rabl +1 -1
  25. data/app/views/spree/api/errors/must_specify_api_key.v1.rabl +1 -1
  26. data/app/views/spree/api/errors/not_found.v1.rabl +1 -1
  27. data/app/views/spree/api/errors/unauthorized.v1.rabl +1 -1
  28. data/app/views/spree/api/v1/countries/show.v1.rabl +1 -1
  29. data/app/views/spree/api/v1/line_items/show.v1.rabl +2 -2
  30. data/app/views/spree/api/v1/option_types/show.v1.rabl +1 -1
  31. data/app/views/spree/api/v1/orders/invalid_shipping_method.v1.rabl +1 -1
  32. data/app/views/spree/api/v1/orders/payment.v1.rabl +1 -1
  33. data/app/views/spree/api/v1/orders/show.v1.rabl +9 -9
  34. data/app/views/spree/api/v1/payments/credit_over_limit.v1.rabl +1 -1
  35. data/app/views/spree/api/v1/payments/update_forbidden.v1.rabl +1 -1
  36. data/app/views/spree/api/v1/products/show.v1.rabl +5 -5
  37. data/app/views/spree/api/v1/shipments/cannot_ready_shipment.v1.rabl +1 -1
  38. data/app/views/spree/api/v1/shipments/show.v1.rabl +7 -7
  39. data/app/views/spree/api/v1/shipments/small.v1.rabl +8 -8
  40. data/app/views/spree/api/v1/tags/index.v1.rabl +9 -0
  41. data/app/views/spree/api/v1/taxonomies/jstree.rabl +2 -2
  42. data/app/views/spree/api/v1/taxonomies/nested.v1.rabl +2 -2
  43. data/app/views/spree/api/v1/taxons/jstree.rabl +3 -3
  44. data/app/views/spree/api/v1/taxons/show.v1.rabl +1 -1
  45. data/app/views/spree/api/v1/taxons/taxons.v1.rabl +1 -1
  46. data/app/views/spree/api/v1/users/show.v1.rabl +3 -2
  47. data/app/views/spree/api/v1/variants/big.v1.rabl +3 -3
  48. data/app/views/spree/api/v1/variants/small.v1.rabl +3 -2
  49. data/app/views/spree/api/v1/zones/show.v1.rabl +1 -1
  50. data/config/routes.rb +4 -6
  51. data/db/migrate/20100107141738_add_api_key_to_spree_users.rb +2 -2
  52. data/db/migrate/20120411123334_resize_api_key_field.rb +2 -2
  53. data/db/migrate/20120530054546_rename_api_key_to_spree_api_key.rb +1 -1
  54. data/db/migrate/20131017162334_add_index_to_user_spree_api_key.rb +1 -1
  55. data/lib/spree/api/engine.rb +11 -3
  56. data/lib/spree/api/responders/rabl_template.rb +1 -1
  57. data/lib/spree/api/testing_support/caching.rb +2 -2
  58. data/spec/controllers/spree/api/base_controller_spec.rb +96 -0
  59. data/spec/controllers/spree/api/v1/addresses_controller_spec.rb +56 -0
  60. data/spec/controllers/spree/api/v1/checkouts_controller_spec.rb +363 -0
  61. data/spec/controllers/spree/api/v1/classifications_controller_spec.rb +48 -0
  62. data/spec/controllers/spree/api/v1/countries_controller_spec.rb +48 -0
  63. data/spec/controllers/spree/api/v1/credit_cards_controller_spec.rb +80 -0
  64. data/spec/controllers/spree/api/v1/images_controller_spec.rb +114 -0
  65. data/spec/controllers/spree/api/v1/inventory_units_controller_spec.rb +48 -0
  66. data/spec/controllers/spree/api/v1/line_items_controller_spec.rb +203 -0
  67. data/spec/controllers/spree/api/v1/option_types_controller_spec.rb +122 -0
  68. data/spec/controllers/spree/api/v1/option_values_controller_spec.rb +141 -0
  69. data/spec/controllers/spree/api/v1/orders_controller_spec.rb +735 -0
  70. data/spec/controllers/spree/api/v1/payments_controller_spec.rb +234 -0
  71. data/spec/controllers/spree/api/v1/product_properties_controller_spec.rb +147 -0
  72. data/spec/controllers/spree/api/v1/products_controller_spec.rb +409 -0
  73. data/spec/controllers/spree/api/v1/promotion_application_spec.rb +50 -0
  74. data/spec/controllers/spree/api/v1/promotions_controller_spec.rb +64 -0
  75. data/spec/controllers/spree/api/v1/properties_controller_spec.rb +102 -0
  76. data/spec/controllers/spree/api/v1/return_authorizations_controller_spec.rb +161 -0
  77. data/spec/controllers/spree/api/v1/shipments_controller_spec.rb +187 -0
  78. data/spec/controllers/spree/api/v1/states_controller_spec.rb +86 -0
  79. data/spec/controllers/spree/api/v1/stock_items_controller_spec.rb +143 -0
  80. data/spec/controllers/spree/api/v1/stock_locations_controller_spec.rb +113 -0
  81. data/spec/controllers/spree/api/v1/stock_movements_controller_spec.rb +84 -0
  82. data/spec/controllers/spree/api/v1/stores_controller_spec.rb +133 -0
  83. data/spec/controllers/spree/api/v1/tags_controller_spec.rb +102 -0
  84. data/spec/controllers/spree/api/v1/taxonomies_controller_spec.rb +114 -0
  85. data/spec/controllers/spree/api/v1/taxons_controller_spec.rb +177 -0
  86. data/spec/controllers/spree/api/v1/unauthenticated_products_controller_spec.rb +26 -0
  87. data/spec/controllers/spree/api/v1/users_controller_spec.rb +153 -0
  88. data/spec/controllers/spree/api/v1/variants_controller_spec.rb +205 -0
  89. data/spec/controllers/spree/api/v1/zones_controller_spec.rb +91 -0
  90. data/spec/models/spree/legacy_user_spec.rb +19 -0
  91. data/spec/requests/rabl_cache_spec.rb +32 -0
  92. data/spec/requests/ransackable_attributes_spec.rb +79 -0
  93. data/spec/requests/version_spec.rb +19 -0
  94. data/spec/shared_examples/protect_product_actions.rb +17 -0
  95. data/spec/spec_helper.rb +60 -0
  96. data/spec/support/controller_hacks.rb +40 -0
  97. data/spec/support/database_cleaner.rb +14 -0
  98. data/spec/support/have_attributes_matcher.rb +13 -0
  99. data/spree_api.gemspec +7 -4
  100. metadata +99 -14
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::OrdersController, type: :controller do
5
+ render_views
6
+
7
+ before do
8
+ stub_authentication!
9
+ end
10
+
11
+ context "with an available promotion" do
12
+ let!(:order) { create(:order_with_line_items, line_items_count: 1) }
13
+ let!(:promotion) do
14
+ promotion = Spree::Promotion.create(name: "10% off", code: "10off")
15
+ calculator = Spree::Calculator::FlatPercentItemTotal.create(preferred_flat_percent: "10")
16
+ action = Spree::Promotion::Actions::CreateItemAdjustments.create(calculator: calculator)
17
+ promotion.actions << action
18
+ promotion
19
+ end
20
+
21
+ it "can apply a coupon code to the order" do
22
+ expect(order.total).to eq(110.00)
23
+ api_put :apply_coupon_code, id: order.to_param, coupon_code: "10off", order_token: order.guest_token
24
+ expect(response.status).to eq(200)
25
+ expect(order.reload.total).to eq(109.00)
26
+ expect(json_response["success"]).to eq("The coupon code was successfully applied to your order.")
27
+ expect(json_response["error"]).to be_blank
28
+ expect(json_response["successful"]).to be true
29
+ expect(json_response["status_code"]).to eq("coupon_code_applied")
30
+ end
31
+
32
+ context "with an expired promotion" do
33
+ before do
34
+ promotion.starts_at = 2.weeks.ago
35
+ promotion.expires_at = 1.week.ago
36
+ promotion.save
37
+ end
38
+
39
+ it "fails to apply" do
40
+ api_put :apply_coupon_code, id: order.to_param, coupon_code: "10off", order_token: order.guest_token
41
+ expect(response.status).to eq(422)
42
+ expect(json_response["success"]).to be_blank
43
+ expect(json_response["error"]).to eq("The coupon code is expired")
44
+ expect(json_response["successful"]).to be false
45
+ expect(json_response["status_code"]).to eq("coupon_code_expired")
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::PromotionsController, type: :controller do
5
+ render_views
6
+
7
+ shared_examples "a JSON response" do
8
+ it 'should be ok' do
9
+ expect(subject).to be_ok
10
+ end
11
+
12
+ it 'should return JSON' do
13
+ payload = HashWithIndifferentAccess.new(JSON.parse(subject.body))
14
+ expect(payload).to_not be_nil
15
+ Spree::Api::ApiHelpers.promotion_attributes.each do |attribute|
16
+ expect(payload.has_key?(attribute)).to be true
17
+ end
18
+ end
19
+ end
20
+
21
+ before do
22
+ stub_authentication!
23
+ end
24
+
25
+ let(:promotion) { create :promotion, :with_order_adjustment, code: '10off' }
26
+
27
+ describe 'GET #show' do
28
+ subject { api_get :show, id: id }
29
+
30
+ context 'when admin' do
31
+ sign_in_as_admin!
32
+
33
+ context 'when finding by id' do
34
+ let(:id) { promotion.id }
35
+
36
+ it_behaves_like "a JSON response"
37
+ end
38
+
39
+ context 'when finding by code' do
40
+ let(:id) { promotion.code }
41
+
42
+ it_behaves_like "a JSON response"
43
+ end
44
+
45
+ context 'when id does not exist' do
46
+ let(:id) { 'argh' }
47
+
48
+ it 'should be 404' do
49
+ expect(subject.status).to eq(404)
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'when non admin' do
55
+ let(:id) { promotion.id }
56
+
57
+ it 'should be unauthorized' do
58
+ subject
59
+ assert_unauthorized!
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ module Spree
3
+ describe Api::V1::PropertiesController, type: :controller do
4
+ render_views
5
+
6
+ let!(:property_1) { Property.create!(name: "foo", presentation: "Foo") }
7
+ let!(:property_2) { Property.create!(name: "bar", presentation: "Bar") }
8
+
9
+ let(:attributes) { [:id, :name, :presentation] }
10
+
11
+ before do
12
+ stub_authentication!
13
+ end
14
+
15
+ it "can see a list of all properties" do
16
+ api_get :index
17
+ expect(json_response["properties"].count).to eq(2)
18
+ expect(json_response["properties"].first).to have_attributes(attributes)
19
+ end
20
+
21
+ it "can control the page size through a parameter" do
22
+ api_get :index, per_page: 1
23
+ expect(json_response['properties'].count).to eq(1)
24
+ expect(json_response['current_page']).to eq(1)
25
+ expect(json_response['pages']).to eq(2)
26
+ end
27
+
28
+ it 'can query the results through a parameter' do
29
+ api_get :index, q: { name_cont: 'ba' }
30
+ expect(json_response['count']).to eq(1)
31
+ expect(json_response['properties'].first['presentation']).to eq property_2.presentation
32
+ end
33
+
34
+ it "retrieves a list of properties by id" do
35
+ api_get :index, ids: [property_1.id]
36
+ expect(json_response["properties"].first).to have_attributes(attributes)
37
+ expect(json_response["count"]).to eq(1)
38
+ end
39
+
40
+ it "retrieves a list of properties by ids string" do
41
+ api_get :index, ids: [property_1.id, property_2.id].join(",")
42
+ expect(json_response["properties"].first).to have_attributes(attributes)
43
+ expect(json_response["properties"][1]).to have_attributes(attributes)
44
+ expect(json_response["count"]).to eq(2)
45
+ end
46
+
47
+ it "can see a single property" do
48
+ api_get :show, id: property_1.id
49
+ expect(json_response).to have_attributes(attributes)
50
+ end
51
+
52
+ it "can see a property by name" do
53
+ api_get :show, id: property_1.name
54
+ expect(json_response).to have_attributes(attributes)
55
+ end
56
+
57
+ it "can learn how to create a new property" do
58
+ api_get :new
59
+ expect(json_response["attributes"]).to eq(attributes.map(&:to_s))
60
+ expect(json_response["required_attributes"]).to be_empty
61
+ end
62
+
63
+ it "cannot create a new property if not an admin" do
64
+ api_post :create, property: { name: "My Property 3" }
65
+ assert_unauthorized!
66
+ end
67
+
68
+ it "cannot update a property" do
69
+ api_put :update, id: property_1.name, property: { presentation: "my value 456" }
70
+ assert_unauthorized!
71
+ end
72
+
73
+ it "cannot delete a property" do
74
+ api_delete :destroy, id: property_1.id
75
+ assert_unauthorized!
76
+ expect { property_1.reload }.not_to raise_error
77
+ end
78
+
79
+ context "as an admin" do
80
+ sign_in_as_admin!
81
+
82
+ it "can create a new property" do
83
+ expect(Spree::Property.count).to eq(2)
84
+ api_post :create, property: { name: "My Property 3", presentation: "my value 3" }
85
+ expect(json_response).to have_attributes(attributes)
86
+ expect(response.status).to eq(201)
87
+ expect(Spree::Property.count).to eq(3)
88
+ end
89
+
90
+ it "can update a property" do
91
+ api_put :update, id: property_1.name, property: { presentation: "my value 456" }
92
+ expect(response.status).to eq(200)
93
+ end
94
+
95
+ it "can delete a property" do
96
+ api_delete :destroy, id: property_1.name
97
+ expect(response.status).to eq(204)
98
+ expect { property_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,161 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::ReturnAuthorizationsController, type: :controller do
5
+ render_views
6
+
7
+ let!(:order) { create(:shipped_order) }
8
+
9
+ let(:product) { create(:product) }
10
+ let(:attributes) { [:id, :memo, :state] }
11
+ let(:resource_scoping) { { order_id: order.to_param } }
12
+
13
+ before do
14
+ stub_authentication!
15
+ end
16
+
17
+ context "as the order owner" do
18
+ before do
19
+ allow_any_instance_of(Order).to receive_messages user: current_api_user
20
+ end
21
+
22
+ it "cannot see any return authorizations" do
23
+ api_get :index
24
+ assert_unauthorized!
25
+ end
26
+
27
+ it "cannot see a single return authorization" do
28
+ api_get :show, id: 1
29
+ assert_unauthorized!
30
+ end
31
+
32
+ it "cannot learn how to create a new return authorization" do
33
+ api_get :new
34
+ assert_unauthorized!
35
+ end
36
+
37
+ it "cannot create a new return authorization" do
38
+ api_post :create
39
+ assert_unauthorized!
40
+ end
41
+
42
+ it "cannot update a return authorization" do
43
+ api_put :update, id: 1
44
+ assert_not_found!
45
+ end
46
+
47
+ it "cannot delete a return authorization" do
48
+ api_delete :destroy, id: 1
49
+ assert_not_found!
50
+ end
51
+ end
52
+
53
+ context "as an admin" do
54
+ sign_in_as_admin!
55
+
56
+ it "can show return authorization" do
57
+ FactoryGirl.create(:return_authorization, order: order)
58
+ return_authorization = order.return_authorizations.first
59
+ api_get :show, order_id: order.number, id: return_authorization.id
60
+ expect(response.status).to eq(200)
61
+ expect(json_response).to have_attributes(attributes)
62
+ expect(json_response["state"]).not_to be_blank
63
+ end
64
+
65
+ it "can get a list of return authorizations" do
66
+ FactoryGirl.create(:return_authorization, order: order)
67
+ FactoryGirl.create(:return_authorization, order: order)
68
+ api_get :index, { order_id: order.number }
69
+ expect(response.status).to eq(200)
70
+ return_authorizations = json_response["return_authorizations"]
71
+ expect(return_authorizations.first).to have_attributes(attributes)
72
+ expect(return_authorizations.first).not_to eq(return_authorizations.last)
73
+ end
74
+
75
+ it 'can control the page size through a parameter' do
76
+ FactoryGirl.create(:return_authorization, order: order)
77
+ FactoryGirl.create(:return_authorization, order: order)
78
+ api_get :index, order_id: order.number, per_page: 1
79
+ expect(json_response['count']).to eq(1)
80
+ expect(json_response['current_page']).to eq(1)
81
+ expect(json_response['pages']).to eq(2)
82
+ end
83
+
84
+ it 'can query the results through a paramter' do
85
+ FactoryGirl.create(:return_authorization, order: order)
86
+ expected_result = create(:return_authorization, memo: 'damaged')
87
+ order.return_authorizations << expected_result
88
+ api_get :index, q: { memo_cont: 'damaged' }
89
+ expect(json_response['count']).to eq(1)
90
+ expect(json_response['return_authorizations'].first['memo']).to eq expected_result.memo
91
+ end
92
+
93
+ it "can learn how to create a new return authorization" do
94
+ api_get :new
95
+ expect(json_response["attributes"]).to eq(["id", "number", "state", "order_id", "memo", "created_at", "updated_at"])
96
+ required_attributes = json_response["required_attributes"]
97
+ expect(required_attributes).to include("order")
98
+ end
99
+
100
+ it "can update a return authorization on the order" do
101
+ FactoryGirl.create(:return_authorization, order: order)
102
+ return_authorization = order.return_authorizations.first
103
+ api_put :update, id: return_authorization.id, return_authorization: { memo: "ABC" }
104
+ expect(response.status).to eq(200)
105
+ expect(json_response).to have_attributes(attributes)
106
+ end
107
+
108
+ it "can cancel a return authorization on the order" do
109
+ FactoryGirl.create(:new_return_authorization, order: order)
110
+ return_authorization = order.return_authorizations.first
111
+ expect(return_authorization.state).to eq("authorized")
112
+ api_delete :cancel, id: return_authorization.id
113
+ expect(response.status).to eq(200)
114
+ expect(return_authorization.reload.state).to eq("canceled")
115
+ end
116
+
117
+ it "can delete a return authorization on the order" do
118
+ FactoryGirl.create(:return_authorization, order: order)
119
+ return_authorization = order.return_authorizations.first
120
+ api_delete :destroy, id: return_authorization.id
121
+ expect(response.status).to eq(204)
122
+ expect { return_authorization.reload }.to raise_error(ActiveRecord::RecordNotFound)
123
+ end
124
+
125
+ it "can add a new return authorization to an existing order" do
126
+ stock_location = FactoryGirl.create(:stock_location)
127
+ reason = FactoryGirl.create(:return_authorization_reason)
128
+ rma_params = { stock_location_id: stock_location.id,
129
+ return_authorization_reason_id: reason.id,
130
+ memo: "Defective" }
131
+ api_post :create, order_id: order.number, return_authorization: rma_params
132
+ expect(response.status).to eq(201)
133
+ expect(json_response).to have_attributes(attributes)
134
+ expect(json_response["state"]).not_to be_blank
135
+ end
136
+ end
137
+
138
+ context "as just another user" do
139
+ it "cannot add a return authorization to the order" do
140
+ api_post :create, return_autorization: { order_id: order.number, memo: "Defective" }
141
+ assert_unauthorized!
142
+ end
143
+
144
+ it "cannot update a return authorization on the order" do
145
+ FactoryGirl.create(:return_authorization, order: order)
146
+ return_authorization = order.return_authorizations.first
147
+ api_put :update, id: return_authorization.id, return_authorization: { memo: "ABC" }
148
+ assert_unauthorized!
149
+ expect(return_authorization.reload.memo).not_to eq("ABC")
150
+ end
151
+
152
+ it "cannot delete a return authorization on the order" do
153
+ FactoryGirl.create(:return_authorization, order: order)
154
+ return_authorization = order.return_authorizations.first
155
+ api_delete :destroy, id: return_authorization.id
156
+ assert_unauthorized!
157
+ expect { return_authorization.reload }.not_to raise_error
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,187 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Api::V1::ShipmentsController, type: :controller do
4
+ render_views
5
+ let!(:shipment) { create(:shipment) }
6
+ let!(:attributes) { [:id, :tracking, :number, :cost, :shipped_at, :stock_location_name, :order_id, :shipping_rates, :shipping_methods] }
7
+
8
+ before do
9
+ stub_authentication!
10
+ end
11
+
12
+ let!(:resource_scoping) { { id: shipment.to_param, shipment: { order_id: shipment.order.to_param } } }
13
+
14
+ context "as a non-admin" do
15
+ it "cannot make a shipment ready" do
16
+ api_put :ready
17
+ assert_not_found!
18
+ end
19
+
20
+ it "cannot make a shipment shipped" do
21
+ api_put :ship
22
+ assert_not_found!
23
+ end
24
+ end
25
+
26
+ context "as an admin" do
27
+ let!(:order) { shipment.order }
28
+ let!(:stock_location) { create(:stock_location_with_items) }
29
+ let!(:variant) { create(:variant) }
30
+
31
+ sign_in_as_admin!
32
+
33
+ # Start writing this spec a bit differently than before....
34
+ describe 'POST #create' do
35
+ let(:params) do
36
+ {
37
+ variant_id: stock_location.stock_items.first.variant.to_param,
38
+ shipment: { order_id: order.number },
39
+ stock_location_id: stock_location.to_param
40
+ }
41
+ end
42
+
43
+ subject do
44
+ api_post :create, params
45
+ end
46
+
47
+ [:variant_id, :stock_location_id].each do |field|
48
+ context "when #{field} is missing" do
49
+ before do
50
+ params.delete(field)
51
+ end
52
+
53
+ it 'should return proper error' do
54
+ subject
55
+ expect(response.status).to eq(422)
56
+ expect(json_response['exception']).to eq("param is missing or the value is empty: #{field.to_s}")
57
+ end
58
+ end
59
+ end
60
+
61
+ it 'should create a new shipment' do
62
+ expect(subject).to be_ok
63
+ expect(json_response).to have_attributes(attributes)
64
+ end
65
+ end
66
+
67
+ it 'can update a shipment' do
68
+ params = {
69
+ shipment: {
70
+ stock_location_id: stock_location.to_param
71
+ }
72
+ }
73
+
74
+ api_put :update, params
75
+ expect(response.status).to eq(200)
76
+ expect(json_response['stock_location_name']).to eq(stock_location.name)
77
+ end
78
+
79
+ it "can make a shipment ready" do
80
+ allow_any_instance_of(Spree::Order).to receive_messages(paid?: true, complete?: true)
81
+ api_put :ready
82
+ expect(json_response).to have_attributes(attributes)
83
+ expect(json_response["state"]).to eq("ready")
84
+ expect(shipment.reload.state).to eq("ready")
85
+ end
86
+
87
+ it "cannot make a shipment ready if the order is unpaid" do
88
+ allow_any_instance_of(Spree::Order).to receive_messages(paid?: false)
89
+ api_put :ready
90
+ expect(json_response["error"]).to eq("Cannot ready shipment.")
91
+ expect(response.status).to eq(422)
92
+ end
93
+
94
+ context 'for completed shipments' do
95
+ let(:order) { create :completed_order_with_totals }
96
+ let!(:resource_scoping) { { id: order.shipments.first.to_param, shipment: { order_id: order.to_param } } }
97
+
98
+ it 'adds a variant to a shipment' do
99
+ api_put :add, { variant_id: variant.to_param, quantity: 2 }
100
+ expect(response.status).to eq(200)
101
+ expect(json_response['manifest'].detect { |h| h['variant']['id'] == variant.id }["quantity"]).to eq(2)
102
+ end
103
+
104
+ it 'removes a variant from a shipment' do
105
+ order.contents.add(variant, 2)
106
+
107
+ api_put :remove, { variant_id: variant.to_param, quantity: 1 }
108
+ expect(response.status).to eq(200)
109
+ expect(json_response['manifest'].detect { |h| h['variant']['id'] == variant.id }["quantity"]).to eq(1)
110
+ end
111
+
112
+ it 'removes a destroyed variant from a shipment' do
113
+ order.contents.add(variant, 2)
114
+ variant.destroy
115
+
116
+ api_put :remove, { variant_id: variant.to_param, quantity: 1 }
117
+ expect(response.status).to eq(200)
118
+ expect(json_response['manifest'].detect { |h| h['variant']['id'] == variant.id }["quantity"]).to eq(1)
119
+ end
120
+ end
121
+
122
+ context "can transition a shipment from ready to ship" do
123
+ before do
124
+ allow_any_instance_of(Spree::Order).to receive_messages(paid?: true, complete?: true)
125
+ shipment.update!(shipment.order)
126
+ expect(shipment.state).to eq("ready")
127
+ allow_any_instance_of(Spree::ShippingRate).to receive_messages(cost: 5)
128
+ end
129
+
130
+ it "can transition a shipment from ready to ship" do
131
+ shipment.reload
132
+ api_put :ship, id: shipment.to_param, shipment: { tracking: "123123", order_id: shipment.order.to_param }
133
+ expect(json_response).to have_attributes(attributes)
134
+ expect(json_response["state"]).to eq("shipped")
135
+ end
136
+
137
+ end
138
+
139
+ describe '#mine' do
140
+ subject do
141
+ api_get :mine, format: 'json', params: params
142
+ end
143
+
144
+ let(:params) { {} }
145
+
146
+ before { subject }
147
+
148
+ context "the current api user is authenticated and has orders" do
149
+ let(:current_api_user) { shipped_order.user }
150
+ let(:shipped_order) { create(:shipped_order) }
151
+
152
+ it 'succeeds' do
153
+ expect(response.status).to eq 200
154
+ end
155
+
156
+ describe 'json output' do
157
+ render_views
158
+
159
+ let(:rendered_shipment_ids) { json_response['shipments'].map { |s| s['id'] } }
160
+
161
+ it 'contains the shipments' do
162
+ expect(rendered_shipment_ids).to match_array current_api_user.orders.flat_map(&:shipments).map(&:id)
163
+ end
164
+ end
165
+
166
+ context 'with filtering' do
167
+ let(:params) { {q: {order_completed_at_not_null: 1}} }
168
+
169
+ let!(:incomplete_order) { create(:order, user: current_api_user) }
170
+
171
+ it 'filters' do
172
+ expect(assigns(:shipments).map(&:id)).to match_array current_api_user.orders.complete.flat_map(&:shipments).map(&:id)
173
+ end
174
+ end
175
+ end
176
+
177
+ context "the current api user is not persisted" do
178
+ let(:current_api_user) { Spree.user_class.new }
179
+
180
+ it "returns a 401" do
181
+ expect(response.status).to eq(401)
182
+ end
183
+ end
184
+ end
185
+
186
+ end
187
+ end