spree_api 3.2.9 → 3.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/spree/api/base_controller.rb +11 -12
  3. data/app/controllers/spree/api/v1/checkouts_controller.rb +5 -8
  4. data/app/controllers/spree/api/v1/customer_returns_controller.rb +24 -0
  5. data/app/controllers/spree/api/v1/orders_controller.rb +13 -24
  6. data/app/controllers/spree/api/v1/payments_controller.rb +2 -3
  7. data/app/controllers/spree/api/v1/reimbursements_controller.rb +24 -0
  8. data/app/controllers/spree/api/v1/shipments_controller.rb +4 -4
  9. data/app/controllers/spree/api/v1/zones_controller.rb +8 -3
  10. data/app/helpers/spree/api/api_helpers.rb +13 -1
  11. data/app/models/concerns/spree/user_api_authentication.rb +19 -0
  12. data/app/models/concerns/spree/user_api_methods.rb +7 -0
  13. data/app/views/spree/api/v1/customer_returns/index.v1.rabl +7 -0
  14. data/app/views/spree/api/v1/line_items/show.v1.rabl +0 -1
  15. data/app/views/spree/api/v1/reimbursements/index.v1.rabl +7 -0
  16. data/config/initializers/user_class_extensions.rb +7 -0
  17. data/config/routes.rb +3 -0
  18. data/spec/controllers/spree/api/base_controller_spec.rb +84 -0
  19. data/spec/controllers/spree/api/v1/addresses_controller_spec.rb +56 -0
  20. data/spec/controllers/spree/api/v1/checkouts_controller_spec.rb +361 -0
  21. data/spec/controllers/spree/api/v1/classifications_controller_spec.rb +48 -0
  22. data/spec/controllers/spree/api/v1/countries_controller_spec.rb +48 -0
  23. data/spec/controllers/spree/api/v1/credit_cards_controller_spec.rb +80 -0
  24. data/spec/controllers/spree/api/v1/customer_returns_controller_spec.rb +27 -0
  25. data/spec/controllers/spree/api/v1/images_controller_spec.rb +114 -0
  26. data/spec/controllers/spree/api/v1/inventory_units_controller_spec.rb +48 -0
  27. data/spec/controllers/spree/api/v1/line_items_controller_spec.rb +210 -0
  28. data/spec/controllers/spree/api/v1/option_types_controller_spec.rb +122 -0
  29. data/spec/controllers/spree/api/v1/option_values_controller_spec.rb +141 -0
  30. data/spec/controllers/spree/api/v1/orders_controller_spec.rb +735 -0
  31. data/spec/controllers/spree/api/v1/payments_controller_spec.rb +234 -0
  32. data/spec/controllers/spree/api/v1/product_properties_controller_spec.rb +156 -0
  33. data/spec/controllers/spree/api/v1/products_controller_spec.rb +409 -0
  34. data/spec/controllers/spree/api/v1/promotion_application_spec.rb +50 -0
  35. data/spec/controllers/spree/api/v1/promotions_controller_spec.rb +64 -0
  36. data/spec/controllers/spree/api/v1/properties_controller_spec.rb +102 -0
  37. data/spec/controllers/spree/api/v1/reimbursements_controller_spec.rb +24 -0
  38. data/spec/controllers/spree/api/v1/return_authorizations_controller_spec.rb +161 -0
  39. data/spec/controllers/spree/api/v1/shipments_controller_spec.rb +187 -0
  40. data/spec/controllers/spree/api/v1/states_controller_spec.rb +86 -0
  41. data/spec/controllers/spree/api/v1/stock_items_controller_spec.rb +151 -0
  42. data/spec/controllers/spree/api/v1/stock_locations_controller_spec.rb +113 -0
  43. data/spec/controllers/spree/api/v1/stock_movements_controller_spec.rb +84 -0
  44. data/spec/controllers/spree/api/v1/stores_controller_spec.rb +133 -0
  45. data/spec/controllers/spree/api/v1/tags_controller_spec.rb +102 -0
  46. data/spec/controllers/spree/api/v1/taxonomies_controller_spec.rb +114 -0
  47. data/spec/controllers/spree/api/v1/taxons_controller_spec.rb +177 -0
  48. data/spec/controllers/spree/api/v1/unauthenticated_products_controller_spec.rb +26 -0
  49. data/spec/controllers/spree/api/v1/users_controller_spec.rb +153 -0
  50. data/spec/controllers/spree/api/v1/variants_controller_spec.rb +205 -0
  51. data/spec/controllers/spree/api/v1/zones_controller_spec.rb +91 -0
  52. data/spec/models/spree/legacy_user_spec.rb +19 -0
  53. data/spec/requests/rabl_cache_spec.rb +32 -0
  54. data/spec/requests/ransackable_attributes_spec.rb +79 -0
  55. data/spec/requests/version_spec.rb +19 -0
  56. data/spec/shared_examples/protect_product_actions.rb +17 -0
  57. data/spec/spec_helper.rb +63 -0
  58. data/spec/support/controller_hacks.rb +40 -0
  59. data/spec/support/database_cleaner.rb +14 -0
  60. data/spec/support/have_attributes_matcher.rb +13 -0
  61. data/spree_api.gemspec +4 -3
  62. metadata +105 -13
  63. data/app/views/spree/api/v1/config/money.v1.rabl +0 -2
  64. data/app/views/spree/api/v1/config/show.v1.rabl +0 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1170089dba60e1b3232d333faffdbb97a4ecee23
4
- data.tar.gz: c7f09b516fdc211250dcf26eb4008a2a39a27e7f
3
+ metadata.gz: 26fb8c0927a1d6b2423649295a3dd8ea7aeb798f
4
+ data.tar.gz: 914d854628f209659d0693b408b5aedc24bf6a5c
5
5
  SHA512:
6
- metadata.gz: 4abd3f9eed2ae14dddae04d0d1eda3a16111f607c41623b617d74093ec1e9e08c5c8aea76f289dc84343aee66c9bd8e706dbb628d7238444971938aee8daaca9
7
- data.tar.gz: 29b658bad34c4fc7d0853b89525e8e94bd4733c186438d4fb3eb0c948b5a1622709710cb892cb92d18b62ea287b2a6b6750e88d558517ad005ee6a20181652cf
6
+ metadata.gz: 254a9e9ba85214262fa672d11d259df4dfbdb141d25a94747d0984d77e926618070ba9662a549e8a6ccd83c902fd696ababfbc7f56db9ac762e7615055d88c16
7
+ data.tar.gz: 76cee411a30b7d769e76a74e9ad3146610005e34fc413cb032d98b24122a2ac16f8a5158d4b77f127d32d1668a93698f39c4656463c79b1292478450efc3ca68
@@ -23,15 +23,6 @@ module Spree
23
23
 
24
24
  helper Spree::Api::ApiHelpers
25
25
 
26
- def map_nested_attributes_keys(klass, attributes)
27
- nested_keys = klass.nested_attributes_options.keys
28
- attributes.to_h.inject({}) do |h, (k,v)|
29
- key = nested_keys.include?(k.to_sym) ? "#{k}_attributes" : k
30
- h[key] = v
31
- h
32
- end.with_indifferent_access
33
- end
34
-
35
26
  # users should be able to set price when importing orders via api
36
27
  def permitted_line_item_attributes
37
28
  if @current_user_roles.include?("admin")
@@ -64,15 +55,23 @@ module Spree
64
55
  return if @current_api_user
65
56
 
66
57
  if requires_authentication? && api_key.blank? && order_token.blank?
67
- render "spree/api/errors/must_specify_api_key", status: 401 and return
58
+ must_specify_api_key and return
68
59
  elsif order_token.blank? && (requires_authentication? || api_key.present?)
69
- render "spree/api/errors/invalid_api_key", status: 401 and return
60
+ invalid_api_key and return
70
61
  else
71
62
  # An anonymous user
72
63
  @current_api_user = Spree.user_class.new
73
64
  end
74
65
  end
75
66
 
67
+ def invalid_api_key
68
+ render "spree/api/errors/invalid_api_key", status: 401
69
+ end
70
+
71
+ def must_specify_api_key
72
+ render "spree/api/errors/must_specify_api_key", status: 401
73
+ end
74
+
76
75
  def load_user_roles
77
76
  @current_user_roles = @current_api_user ? @current_api_user.spree_roles.pluck(:name) : []
78
77
  end
@@ -124,7 +123,7 @@ module Spree
124
123
  end
125
124
 
126
125
  def find_product(id)
127
- product_scope.friendly.find(id.to_s)
126
+ product_scope.friendly.distinct(false).find(id.to_s)
128
127
  rescue ActiveRecord::RecordNotFound
129
128
  product_scope.find(id)
130
129
  end
@@ -51,10 +51,6 @@ module Spree
51
51
  params[:order][:user_id] if params[:order]
52
52
  end
53
53
 
54
- def nested_params
55
- map_nested_attributes_keys Order, params[:order] || {}
56
- end
57
-
58
54
  # Should be overriden if you have areas of your checkout that don't match
59
55
  # up to a step within checkout_steps, such as a registration step
60
56
  def skip_state_validation?
@@ -73,7 +69,7 @@ module Spree
73
69
  end
74
70
 
75
71
  def raise_insufficient_quantity
76
- respond_with(@order, default_template: 'spree/api/v1/orders/insufficient_quantity')
72
+ respond_with(@order, default_template: 'spree/api/v1/orders/insufficient_quantity', status: 422)
77
73
  end
78
74
 
79
75
  def state_callback(before_or_after = :before)
@@ -82,12 +78,13 @@ module Spree
82
78
  end
83
79
 
84
80
  def after_update_attributes
85
- if nested_params && nested_params[:coupon_code].present?
86
- handler = PromotionHandler::Coupon.new(@order).apply
81
+ if params[:order] && params[:order][:coupon_code].present?
82
+ handler = PromotionHandler::Coupon.new(@order)
83
+ handler.apply
87
84
 
88
85
  if handler.error.present?
89
86
  @coupon_message = handler.error
90
- respond_with(@order, default_template: 'spree/api/v1/orders/could_not_apply_coupon')
87
+ respond_with(@order, default_template: 'spree/api/v1/orders/could_not_apply_coupon', status: 422)
91
88
  return true
92
89
  end
93
90
  end
@@ -0,0 +1,24 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class CustomerReturnsController < Spree::Api::BaseController
5
+ def index
6
+ collection(Spree::CustomerReturn)
7
+ respond_with(@collection)
8
+ end
9
+
10
+ private
11
+
12
+ def collection(resource)
13
+ return @collection if @collection.present?
14
+ params[:q] ||= {}
15
+
16
+ @collection = resource.all
17
+ # @search needs to be defined as this is passed to search_form_for
18
+ @search = @collection.ransack(params[:q])
19
+ @collection = @search.result.order(created_at: :desc).page(params[:page]).per(params[:per_page])
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -27,32 +27,21 @@ module Spree
27
27
  end
28
28
 
29
29
  def create
30
- authorize! :create, Spree::Order
31
- if can?(:admin, Spree::Order)
32
-
33
- order_user = if @current_user_roles.include?('admin') && order_params[:user_id]
34
- Spree.user_class.find(order_params[:user_id])
35
- else
36
- current_api_user
37
- end
38
-
39
- import_params = if @current_user_roles.include?("admin")
40
- params[:order].present? ? params[:order].permit! : {}
41
- else
42
- order_params
43
- end
44
-
45
- @order = Spree::Core::Importer::Order.import(order_user, import_params)
30
+ authorize! :create, Order
31
+ order_user = if @current_user_roles.include?('admin') && order_params[:user_id]
32
+ Spree.user_class.find(order_params[:user_id])
33
+ else
34
+ current_api_user
35
+ end
46
36
 
47
- respond_with(@order, default_template: :show, status: 201)
37
+ import_params = if @current_user_roles.include?("admin")
38
+ params[:order].present? ? params[:order].permit! : {}
48
39
  else
49
- @order = Spree::Order.create!(user: current_api_user, store: current_store)
50
- if @order.contents.update_cart(order_params)
51
- respond_with(@order, default_template: :show, status: 201)
52
- else
53
- invalid_resource!(@order)
54
- end
40
+ order_params
55
41
  end
42
+
43
+ @order = Spree::Core::Importer::Order.import(order_user, import_params)
44
+ respond_with(@order, default_template: :show, status: 201)
56
45
  end
57
46
 
58
47
  def empty
@@ -132,7 +121,7 @@ module Spree
132
121
  end
133
122
 
134
123
  def find_order(lock = false)
135
- @order = Spree::Order.lock(lock).friendly.find(params[:id])
124
+ @order = Spree::Order.lock(lock).find_by!(number: params[:id])
136
125
  end
137
126
 
138
127
  def find_current_order
@@ -17,7 +17,6 @@ module Spree
17
17
  end
18
18
 
19
19
  def create
20
- @order.validate_payments_attributes([payment_params])
21
20
  @payment = @order.payments.build(payment_params)
22
21
  if @payment.save
23
22
  respond_with(@payment, status: 201, default_template: :show)
@@ -60,12 +59,12 @@ module Spree
60
59
  private
61
60
 
62
61
  def find_order
63
- @order = Spree::Order.friendly.find(order_id)
62
+ @order = Spree::Order.find_by!(number: order_id)
64
63
  authorize! :read, @order, order_token
65
64
  end
66
65
 
67
66
  def find_payment
68
- @payment = @order.payments.friendly.find(params[:id])
67
+ @payment = @order.payments.find_by!(number: params[:id])
69
68
  end
70
69
 
71
70
  def perform_payment_action(action, *args)
@@ -0,0 +1,24 @@
1
+ module Spree
2
+ module Api
3
+ module V1
4
+ class ReimbursementsController < Spree::Api::BaseController
5
+ def index
6
+ collection(Spree::Reimbursement)
7
+ respond_with(@collection)
8
+ end
9
+
10
+ private
11
+
12
+ def collection(resource)
13
+ return @collection if @collection.present?
14
+ params[:q] ||= {}
15
+
16
+ @collection = resource.all
17
+ # @search needs to be defined as this is passed to search_form_for
18
+ @search = @collection.ransack(params[:q])
19
+ @collection = @search.result.order(created_at: :desc).page(params[:page]).per(params[:per_page])
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -33,7 +33,7 @@ module Spree
33
33
  end
34
34
 
35
35
  def update
36
- @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).friendly.find(params[:id])
36
+ @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
37
37
  @shipment.update_attributes_and_order(shipment_params)
38
38
 
39
39
  respond_with(@shipment.reload, default_template: :show)
@@ -86,7 +86,7 @@ module Spree
86
86
  end
87
87
 
88
88
  def transfer_to_shipment
89
- @target_shipment = Spree::Shipment.friendly.find(params[:target_shipment_number])
89
+ @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
90
90
 
91
91
  if @quantity < 0 || @target_shipment == @original_shipment
92
92
  unprocessable_entity('ArgumentError')
@@ -100,7 +100,7 @@ module Spree
100
100
  private
101
101
 
102
102
  def load_transfer_params
103
- @original_shipment = Spree::Shipment.friendly.find(params[:original_shipment_number])
103
+ @original_shipment = Spree::Shipment.find_by!(number: params[:original_shipment_number])
104
104
  @variant = Spree::Variant.find(params[:variant_id])
105
105
  @quantity = params[:quantity].to_i
106
106
  authorize! :read, @original_shipment
@@ -108,7 +108,7 @@ module Spree
108
108
  end
109
109
 
110
110
  def find_and_update_shipment
111
- @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).friendly.find(params[:id])
111
+ @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
112
112
  @shipment.update_attributes(shipment_params)
113
113
  @shipment.reload
114
114
  end
@@ -5,7 +5,7 @@ module Spree
5
5
 
6
6
  def create
7
7
  authorize! :create, Zone
8
- @zone = Zone.new(map_nested_attributes_keys(Spree::Zone, zone_params))
8
+ @zone = Spree::Zone.new(zone_params)
9
9
  if @zone.save
10
10
  respond_with(@zone, status: 201, default_template: :show)
11
11
  else
@@ -30,7 +30,7 @@ module Spree
30
30
 
31
31
  def update
32
32
  authorize! :update, zone
33
- if zone.update_attributes(map_nested_attributes_keys(Spree::Zone, zone_params))
33
+ if zone.update_attributes(zone_params)
34
34
  respond_with(zone, status: 200, default_template: :show)
35
35
  else
36
36
  invalid_resource!(zone)
@@ -38,8 +38,13 @@ module Spree
38
38
  end
39
39
 
40
40
  private
41
+
41
42
  def zone_params
42
- params.require(:zone).permit!
43
+ attrs = params.require(:zone).permit!
44
+ if attrs[:zone_members]
45
+ attrs[:zone_members_attributes] = attrs.delete(:zone_members)
46
+ end
47
+ attrs
43
48
  end
44
49
 
45
50
  def zone
@@ -30,7 +30,9 @@ module Spree
30
30
  :stock_item_attributes,
31
31
  :promotion_attributes,
32
32
  :store_attributes,
33
- :tag_attributes
33
+ :tag_attributes,
34
+ :customer_return_attributes,
35
+ :reimbursement_attributes
34
36
  ]
35
37
 
36
38
  mattr_reader *ATTRIBUTES
@@ -164,6 +166,16 @@ module Spree
164
166
 
165
167
  @@tag_attributes = [:id, :name]
166
168
 
169
+ @@customer_return_attributes = [
170
+ :id, :number, :order_id, :fully_reimbursed?, :pre_tax_total,
171
+ :created_at, :updated_at
172
+ ]
173
+
174
+ @@reimbursement_attributes = [
175
+ :id, :reimbursement_status, :customer_return_id, :order_id,
176
+ :number, :total, :created_at, :updated_at
177
+ ]
178
+
167
179
  def variant_attributes
168
180
  if @current_user_roles && @current_user_roles.include?("admin")
169
181
  @@variant_attributes + [:cost_price]
@@ -0,0 +1,19 @@
1
+ module Spree
2
+ module UserApiAuthentication
3
+ def generate_spree_api_key!
4
+ self.spree_api_key = generate_spree_api_key
5
+ save!
6
+ end
7
+
8
+ def clear_spree_api_key!
9
+ self.spree_api_key = nil
10
+ save!
11
+ end
12
+
13
+ private
14
+
15
+ def generate_spree_api_key
16
+ SecureRandom.hex(24)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Spree
2
+ module UserApiMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ include Spree::UserApiAuthentication
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@collection => :customer_returns) do
3
+ attributes *customer_return_attributes
4
+ end
5
+ node(:count) { @collection.count }
6
+ node(:current_page) { params[:page].try(:to_i) || 1 }
7
+ node(:pages) { @collection.total_pages }
@@ -7,7 +7,6 @@ node(:total) { |li| li.total }
7
7
  child :variant do
8
8
  extends "spree/api/v1/variants/small"
9
9
  attributes :product_id
10
- child(images: :images) { extends "spree/api/v1/images/show" }
11
10
  end
12
11
 
13
12
  child adjustments: :adjustments do
@@ -0,0 +1,7 @@
1
+ object false
2
+ child(@collection => :reimbursements) do
3
+ attributes *reimbursement_attributes
4
+ end
5
+ node(:count) { @collection.count }
6
+ node(:current_page) { params[:page].try(:to_i) || 1 }
7
+ node(:pages) { @collection.total_pages }
@@ -0,0 +1,7 @@
1
+ # Ensure that Spree.user_class includes the UserApiMethods concern
2
+
3
+ Spree::Core::Engine.config.to_prepare do
4
+ if Spree.user_class && !Spree.user_class.included_modules.include?(Spree::UserApiMethods)
5
+ Spree.user_class.include Spree::UserApiMethods
6
+ end
7
+ end
data/config/routes.rb CHANGED
@@ -3,6 +3,9 @@ Spree::Core::Engine.add_routes do
3
3
  namespace :v1 do
4
4
  resources :promotions, only: [:show]
5
5
 
6
+ resources :customer_returns, only: [:index]
7
+ resources :reimbursements, only: [:index]
8
+
6
9
  resources :products do
7
10
  resources :images
8
11
  resources :variants
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ class FakesController < Spree::Api::BaseController
4
+ end
5
+
6
+ describe Spree::Api::BaseController, type: :controller do
7
+ render_views
8
+ controller(Spree::Api::BaseController) do
9
+ def index
10
+ render plain: { "products" => [] }.to_json
11
+ end
12
+ end
13
+
14
+ before do
15
+ @routes = ActionDispatch::Routing::RouteSet.new.tap do |r|
16
+ r.draw { get 'index', to: 'spree/api/base#index' }
17
+ end
18
+ end
19
+
20
+ context "when validating based on an order token" do
21
+ let!(:order) { create :order }
22
+
23
+ context "with a correct order token" do
24
+ it "succeeds" do
25
+ api_get :index, order_token: order.guest_token, order_id: order.number
26
+ expect(response.status).to eq(200)
27
+ end
28
+
29
+ it "succeeds with an order_number parameter" do
30
+ api_get :index, order_token: order.guest_token, order_number: order.number
31
+ expect(response.status).to eq(200)
32
+ end
33
+ end
34
+
35
+ context "with an incorrect order token" do
36
+ it "returns unauthorized" do
37
+ api_get :index, order_token: "NOT_A_TOKEN", order_id: order.number
38
+ expect(response.status).to eq(401)
39
+ end
40
+ end
41
+ end
42
+
43
+ context "cannot make a request to the API" do
44
+ it "without an API key" do
45
+ api_get :index
46
+ expect(json_response).to eq({ "error" => "You must specify an API key." })
47
+ expect(response.status).to eq(401)
48
+ end
49
+
50
+ it "with an invalid API key" do
51
+ request.headers["X-Spree-Token"] = "fake_key"
52
+ get :index
53
+ expect(json_response).to eq({ "error" => "Invalid API key (fake_key) specified." })
54
+ expect(response.status).to eq(401)
55
+ end
56
+
57
+ it "using an invalid token param" do
58
+ get :index, params: { token: "fake_key" }
59
+ expect(json_response).to eq({ "error" => "Invalid API key (fake_key) specified." })
60
+ end
61
+ end
62
+
63
+ it 'handles parameter missing exceptions' do
64
+ expect(subject).to receive(:authenticate_user).and_return(true)
65
+ expect(subject).to receive(:load_user_roles).and_return(true)
66
+ expect(subject).to receive(:index).and_raise(ActionController::ParameterMissing.new('foo'))
67
+ get :index, params: { token: 'exception-message' }
68
+ expect(json_response).to eql('exception' => 'param is missing or the value is empty: foo')
69
+ end
70
+
71
+ it 'handles record invalid exceptions' do
72
+ expect(subject).to receive(:authenticate_user).and_return(true)
73
+ expect(subject).to receive(:load_user_roles).and_return(true)
74
+ resource = Spree::Product.new
75
+ resource.valid? # get some errors
76
+ expect(subject).to receive(:index).and_raise(ActiveRecord::RecordInvalid.new(resource))
77
+ get :index, params: { token: 'exception-message' }
78
+ expect(json_response).to eql('exception' => "Validation failed: Name can't be blank, Shipping Category can't be blank, Price can't be blank")
79
+ end
80
+
81
+ it "lets a subclass override the product associations that are eager-loaded" do
82
+ expect(controller.respond_to?(:product_includes, true)).to be
83
+ end
84
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ describe Api::V1::AddressesController, type: :controller do
5
+ render_views
6
+
7
+ before do
8
+ stub_authentication!
9
+ @address = create(:address)
10
+ @order = create(:order, bill_address: @address)
11
+ end
12
+
13
+ context "with their own address" do
14
+ before do
15
+ allow_any_instance_of(Order).to receive_messages user: current_api_user
16
+ end
17
+
18
+ it "gets an address" do
19
+ api_get :show, id: @address.id, order_id: @order.number
20
+ expect(json_response['address1']).to eq @address.address1
21
+ end
22
+
23
+ it "updates an address" do
24
+ api_put :update, id: @address.id, order_id: @order.number,
25
+ address: { address1: "123 Test Lane" }
26
+ expect(json_response['address1']).to eq '123 Test Lane'
27
+ end
28
+
29
+ it "receives the errors object if address is invalid" do
30
+ api_put :update, id: @address.id, order_id: @order.number,
31
+ address: { address1: "" }
32
+
33
+ expect(json_response['error']).not_to be_nil
34
+ expect(json_response['errors']).not_to be_nil
35
+ expect(json_response['errors']['address1'].first).to eq "can't be blank"
36
+ end
37
+ end
38
+
39
+ context "on an address that does not belong to this order" do
40
+ before do
41
+ @order.bill_address_id = nil
42
+ @order.ship_address = nil
43
+ end
44
+
45
+ it "cannot retrieve address information" do
46
+ api_get :show, id: @address.id, order_id: @order.number
47
+ assert_unauthorized!
48
+ end
49
+
50
+ it "cannot update address information" do
51
+ api_get :update, id: @address.id, order_id: @order.number
52
+ assert_unauthorized!
53
+ end
54
+ end
55
+ end
56
+ end