solidus_api 1.0.7 → 1.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of solidus_api might be problematic. Click here for more details.

Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/spree/api/address_books_controller.rb +38 -0
  3. data/app/controllers/spree/api/addresses_controller.rb +19 -15
  4. data/app/controllers/spree/api/base_controller.rb +7 -28
  5. data/app/controllers/spree/api/checkouts_controller.rb +17 -8
  6. data/app/controllers/spree/api/option_values_controller.rb +1 -1
  7. data/app/controllers/spree/api/orders_controller.rb +13 -18
  8. data/app/controllers/spree/api/payments_controller.rb +0 -1
  9. data/app/controllers/spree/api/resource_controller.rb +75 -0
  10. data/app/controllers/spree/api/shipments_controller.rb +1 -0
  11. data/app/controllers/spree/api/stock_items_controller.rb +1 -3
  12. data/app/controllers/spree/api/stock_transfers_controller.rb +1 -0
  13. data/app/controllers/spree/api/users_controller.rb +13 -49
  14. data/app/controllers/spree/api/zones_controller.rb +7 -3
  15. data/app/helpers/spree/api/api_helpers.rb +9 -4
  16. data/app/views/spree/api/address_books/show.v1.rabl +4 -0
  17. data/app/views/spree/api/orders/show.v1.rabl +2 -2
  18. data/app/views/spree/api/variants/big.v1.rabl +3 -0
  19. data/config/locales/en.yml +0 -1
  20. data/config/routes.rb +3 -0
  21. data/lib/spree/api/testing_support/helpers.rb +1 -1
  22. data/lib/spree/api/testing_support/setup.rb +1 -4
  23. data/spec/controllers/spree/api/address_books_controller_spec.rb +60 -0
  24. data/spec/controllers/spree/api/addresses_controller_spec.rb +22 -9
  25. data/spec/controllers/spree/api/base_controller_spec.rb +0 -12
  26. data/spec/controllers/spree/api/checkouts_controller_spec.rb +107 -45
  27. data/spec/controllers/spree/api/credit_cards_controller_spec.rb +4 -11
  28. data/spec/controllers/spree/api/orders_controller_spec.rb +75 -105
  29. data/spec/controllers/spree/api/payments_controller_spec.rb +0 -11
  30. data/spec/controllers/spree/api/products_controller_spec.rb +2 -2
  31. data/spec/controllers/spree/api/resource_controller_spec.rb +157 -0
  32. data/spec/controllers/spree/api/shipments_controller_spec.rb +12 -13
  33. data/spec/controllers/spree/api/stock_items_controller_spec.rb +2 -2
  34. data/spec/controllers/spree/api/users_controller_spec.rb +2 -2
  35. data/spec/controllers/spree/api/variants_controller_spec.rb +32 -3
  36. data/spec/models/spree/legacy_user_spec.rb +8 -2
  37. data/spec/spec_helper.rb +8 -3
  38. data/spec/test_views/spree/api/widgets/index.v1.rabl +7 -0
  39. data/spec/test_views/spree/api/widgets/new.v1.rabl +2 -0
  40. data/spec/test_views/spree/api/widgets/show.v1.rabl +2 -0
  41. metadata +20 -9
  42. data/CHANGELOG.md +0 -1
  43. data/app/views/spree/api/shared/stock_location_required.v1.rabl +0 -2
@@ -6,16 +6,11 @@ module Spree
6
6
  render_views
7
7
 
8
8
  let!(:admin_user) do
9
- user = Spree.user_class.new(:email => "spree@example.com", :id => 1)
10
- user.generate_spree_api_key!
11
- allow(user).to receive(:has_spree_role?).with('admin').and_return(true)
12
- user
9
+ create(:admin_user)
13
10
  end
14
11
 
15
12
  let!(:normal_user) do
16
- user = Spree.user_class.new(:email => "spree2@example.com", :id => 2)
17
- user.generate_spree_api_key!
18
- user
13
+ create(:user, :with_api_key)
19
14
  end
20
15
 
21
16
  let!(:card) { create(:credit_card, :user_id => admin_user.id, gateway_customer_profile_id: "random") }
@@ -31,8 +26,7 @@ module Spree
31
26
 
32
27
  context "calling user is in admin role" do
33
28
  let(:current_api_user) do
34
- user = admin_user
35
- user
29
+ admin_user
36
30
  end
37
31
 
38
32
  it "no credit cards exist for user" do
@@ -55,8 +49,7 @@ module Spree
55
49
 
56
50
  context "calling user is not in admin role" do
57
51
  let(:current_api_user) do
58
- user = normal_user
59
- user
52
+ normal_user
60
53
  end
61
54
 
62
55
  let!(:card) { create(:credit_card, :user_id => normal_user.id, gateway_customer_profile_id: "random") }
@@ -29,49 +29,49 @@ module Spree
29
29
 
30
30
  describe "POST create" do
31
31
  let(:target_user) { create :user }
32
- let(:date_override) { Time.parse('2015-01-01') }
33
- let(:attributes) { { user_id: target_user.id, created_at: date_override, email: target_user.email } }
32
+ let(:date_override) { 3.days.ago }
34
33
 
35
- subject { api_post :create, order: attributes }
34
+ before do
35
+ allow_any_instance_of(Spree::Ability).to receive(:can?).
36
+ and_return(true)
37
+
38
+ allow_any_instance_of(Spree::Ability).to receive(:can?).
39
+ with(:admin, Spree::Order).
40
+ and_return(can_admin)
41
+
42
+ allow(Spree.user_class).to receive(:find).
43
+ with(target_user.id).
44
+ and_return(target_user)
45
+ end
46
+
47
+ subject { api_post :create, order: { user_id: target_user.id, created_at: date_override, email: target_user.email } }
36
48
 
37
49
  context "when the current user cannot administrate the order" do
38
- stub_authorization! do |_|
39
- can :create, Spree::Order
40
- end
50
+ let(:can_admin) { false }
41
51
 
42
52
  it "does not include unpermitted params, or allow overriding the user", focus: true do
53
+ expect(Spree::Core::Importer::Order).to receive(:import).
54
+ once.
55
+ with(current_api_user, { "email" => target_user.email }).
56
+ and_call_original
43
57
  subject
44
- expect(response).to be_success
45
- order = Spree::Order.last
46
- expect(order.user).to eq current_api_user
47
- expect(order.email).to eq target_user.email
48
58
  end
49
59
 
50
60
  it { is_expected.to be_success }
61
+ end
51
62
 
52
- context 'creating payment' do
53
- let(:attributes) { super().merge(payments_attributes: [{ payment_method_id: payment_method.id }]) }
54
-
55
- context "with allowed payment method" do
56
- let!(:payment_method) { create(:check_payment_method, name: "allowed" ) }
57
- it { is_expected.to be_success }
58
- it "creates a payment" do
59
- expect {
60
- subject
61
- }.to change { Spree::Payment.count }.by(1)
62
- end
63
- end
63
+ context "when the current user can administrate the order" do
64
+ let(:can_admin) { true }
64
65
 
65
- context "with disallowed payment method" do
66
- let!(:payment_method) { create(:check_payment_method, name: "forbidden", display_on: "back_end") }
67
- it { is_expected.to be_not_found }
68
- it "creates no payments" do
69
- expect {
70
- subject
71
- }.not_to change { Spree::Payment.count }
72
- end
73
- end
66
+ it "it permits all params and allows overriding the user" do
67
+ expect(Spree::Core::Importer::Order).to receive(:import).
68
+ once.
69
+ with(target_user, { "user_id" => target_user.id, "created_at" => date_override, "email" => target_user.email}).
70
+ and_call_original
71
+ subject
74
72
  end
73
+
74
+ it { is_expected.to be_success }
75
75
  end
76
76
  end
77
77
 
@@ -81,65 +81,41 @@ module Spree
81
81
  let(:can_admin) { false }
82
82
  subject { api_put :update, id: order.to_param, order: order_params }
83
83
 
84
- context "when the user cannot administer the order" do
85
- stub_authorization! do |_|
86
- can [:update], Spree::Order
87
- end
88
-
89
- it "updates the user's email" do
90
- expect {
91
- subject
92
- }.to change { order.reload.email }.to("foo@foobar.com")
93
- end
94
-
95
- it { is_expected.to be_success }
96
-
97
- it "does not associate users" do
98
- expect {
99
- subject
100
- }.not_to change { order.reload.user }
101
- end
84
+ before do
85
+ allow_any_instance_of(Spree::Ability).to receive(:can?).
86
+ and_return(true)
102
87
 
103
- it "does not change forbidden attributes" do
104
- expect {
105
- subject
106
- }.to_not change{ order.reload.number }
107
- end
88
+ allow(Spree::Order).to receive(:find_by!).
89
+ with(number: order.number).
90
+ and_return(order)
108
91
 
109
- context 'creating payment' do
110
- let(:order_params) { super().merge(payments_attributes: [{ payment_method_id: payment_method.id }]) }
92
+ allow(Spree.user_class).to receive(:find).
93
+ with(user.id).
94
+ and_return(user)
111
95
 
112
- context "with allowed payment method" do
113
- let!(:payment_method) { create(:check_payment_method, name: "allowed" ) }
114
- it { is_expected.to be_success }
115
- it "creates a payment" do
116
- expect {
117
- subject
118
- }.to change { Spree::Payment.count }.by(1)
119
- end
120
- end
96
+ allow_any_instance_of(Spree::Ability).to receive(:can?).
97
+ with(:admin, Spree::Order).
98
+ and_return(can_admin)
99
+ end
121
100
 
122
- context "with disallowed payment method" do
123
- let!(:payment_method) { create(:check_payment_method, name: "forbidden", display_on: "back_end") }
124
- it { is_expected.to be_not_found }
125
- it "creates no payments" do
126
- expect {
127
- subject
128
- }.not_to change { Spree::Payment.count }
129
- end
130
- end
131
- end
101
+ it "updates the cart contents" do
102
+ expect(order.contents).to receive(:update_cart).
103
+ once.
104
+ with({"email" => "foo@foobar.com"})
105
+ subject
132
106
  end
133
107
 
108
+ it { is_expected.to be_success }
109
+
134
110
  context "when the user can administer the order" do
135
- stub_authorization! do |_|
136
- can [:admin, :update], Spree::Order
137
- end
111
+ let(:can_admin) { true }
138
112
 
139
113
  it "will associate users" do
140
- expect {
141
- subject
142
- }.to change { order.reload.user }.to(user)
114
+ expect(order).to receive(:associate_user!).
115
+ once.
116
+ with(user)
117
+
118
+ subject
143
119
  end
144
120
 
145
121
  it "updates the otherwise forbidden attributes" do
@@ -147,6 +123,17 @@ module Spree
147
123
  to("anothernumber")
148
124
  end
149
125
  end
126
+
127
+ context "when the user cannot administer the order" do
128
+ it "does not associate users" do
129
+ expect(order).to_not receive(:associate_user!)
130
+ subject
131
+ end
132
+
133
+ it "does not change forbidden attributes" do
134
+ expect{subject}.to_not change{order.reload.number}
135
+ end
136
+ end
150
137
  end
151
138
 
152
139
  it "cannot view all orders" do
@@ -326,10 +313,7 @@ module Spree
326
313
  after { Spree::Ability.remove_ability(::BarAbility) }
327
314
 
328
315
  it "can view an order" do
329
- user = build(:user)
330
- allow(user).to receive_message_chain(:spree_roles, :pluck).and_return(["bar"])
331
- allow(user).to receive(:has_spree_role?).with('bar').and_return(true)
332
- allow(user).to receive(:has_spree_role?).with('admin').and_return(false)
316
+ user = build(:user, spree_roles: [Spree::Role.new(name: 'bar')])
333
317
  allow(Spree.user_class).to receive_messages find_by: user
334
318
  api_get :show, :id => order.to_param
335
319
  expect(response.status).to eq(200)
@@ -368,13 +352,16 @@ module Spree
368
352
 
369
353
  # Regression test for #3404
370
354
  it "can specify additional parameters for a line item" do
371
- expect_any_instance_of(Spree::LineItem).to receive(:special=).with("foo")
355
+ expect(Order).to receive(:create!).and_return(order = Spree::Order.new)
356
+ allow(order).to receive(:associate_user!)
357
+ allow(order).to receive_message_chain(:contents, :add).and_return(line_item = double('LineItem'))
358
+ expect(line_item).to receive(:update_attributes!).with("special" => true)
372
359
 
373
360
  allow(controller).to receive_messages(permitted_line_item_attributes: [:id, :variant_id, :quantity, :special])
374
361
  api_post :create, :order => {
375
362
  :line_items => {
376
363
  "0" => {
377
- variant_id: variant.to_param, quantity: 5, special: "foo"
364
+ :variant_id => variant.to_param, :quantity => 5, :special => true
378
365
  }
379
366
  }
380
367
  }
@@ -395,10 +382,7 @@ module Spree
395
382
  end
396
383
 
397
384
  context "admin user imports order" do
398
- before do
399
- allow(current_api_user).to receive_messages has_spree_role?: true
400
- allow(current_api_user).to receive_message_chain :spree_roles, pluck: ["admin"]
401
- end
385
+ let!(:current_api_user) { create :admin_user }
402
386
 
403
387
  it "is able to set any default unpermitted attribute" do
404
388
  api_post :create, :order => { number: "WOW" }
@@ -410,7 +394,6 @@ module Spree
410
394
  it "can create an order without any parameters" do
411
395
  expect { api_post :create }.not_to raise_error
412
396
  expect(response.status).to eq(201)
413
- order = Order.last
414
397
  expect(json_response["state"]).to eq("cart")
415
398
  end
416
399
 
@@ -658,19 +641,6 @@ module Spree
658
641
  end
659
642
  end
660
643
 
661
- it "responds with orders updated_at with miliseconds precision" do
662
- if ActiveRecord::Base.connection.adapter_name == "Mysql2"
663
- skip "MySQL does not support millisecond timestamps."
664
- else
665
- skip "Probable need to make it call as_json. See https://github.com/rails/rails/commit/0f33d70e89991711ff8b3dde134a61f4a5a0ec06"
666
- end
667
-
668
- api_get :index
669
- milisecond = order.updated_at.strftime("%L")
670
- updated_at = json_response["orders"].first["updated_at"]
671
- expect(updated_at.split("T").last).to have_content(milisecond)
672
- end
673
-
674
644
  context "caching enabled" do
675
645
  before do
676
646
  ActionController::Base.perform_caching = true
@@ -43,17 +43,6 @@ module Spree
43
43
  expect(response.status).to eq(201)
44
44
  expect(json_response).to have_attributes(attributes)
45
45
  end
46
-
47
- context "disallowed payment method" do
48
- it "does not create a new payment" do
49
- PaymentMethod.first.update!(display_on: "back_end")
50
-
51
- expect {
52
- api_post :create, payment: { payment_method_id: PaymentMethod.first.id, amount: 50 }
53
- }.not_to change { Spree::Payment.count }
54
- expect(response.status).to eq(404)
55
- end
56
- end
57
46
  end
58
47
 
59
48
  context "payment source is required" do
@@ -395,13 +395,13 @@ module Spree
395
395
  # Regression test for #4123
396
396
  it "puts the created product in the given taxon" do
397
397
  api_put :update, :id => product.to_param, :product => {:taxon_ids => taxon_1.id.to_s}
398
- expect(json_response["taxon_ids"]).to eq([taxon_1.id,])
398
+ expect(json_response["taxon_ids"]).to eq([taxon_1.id])
399
399
  end
400
400
 
401
401
  # Regression test for #4123
402
402
  it "puts the created product in the given taxons" do
403
403
  api_put :update, :id => product.to_param, :product => {:taxon_ids => [taxon_1.id, taxon_2.id].join(',')}
404
- expect(json_response["taxon_ids"]).to eq([taxon_1.id, taxon_2.id])
404
+ expect(json_response["taxon_ids"]).to match_array([taxon_1.id, taxon_2.id])
405
405
  end
406
406
  end
407
407
 
@@ -0,0 +1,157 @@
1
+ require 'spec_helper'
2
+
3
+ module Spree
4
+ module Api
5
+ class WidgetsController < Spree::Api::ResourceController
6
+ prepend_view_path('spec/test_views')
7
+
8
+ def model_class
9
+ Widget
10
+ end
11
+
12
+ def permitted_widget_attributes
13
+ [:name]
14
+ end
15
+ end
16
+ end
17
+
18
+ describe Api::WidgetsController, type: :controller do
19
+ render_views
20
+
21
+ after(:all) do
22
+ Rails.application.reload_routes!
23
+ end
24
+
25
+ with_model 'Widget', scope: :all do
26
+ table do |t|
27
+ t.string :name
28
+ t.integer :position
29
+ t.timestamps null: false
30
+ end
31
+
32
+ model do
33
+ acts_as_list
34
+ validates :name, presence: true
35
+ end
36
+ end
37
+
38
+ before do
39
+ Spree::Core::Engine.routes.draw do
40
+ namespace :api do
41
+ resources :widgets
42
+ end
43
+ end
44
+ end
45
+
46
+ let(:user) { create(:user, :with_api_key) }
47
+ let(:admin_user) { create(:admin_user, :with_api_key) }
48
+
49
+ describe "#index" do
50
+ let!(:widget) { Widget.create!(name: "a widget") }
51
+
52
+ it "returns no widgets" do
53
+ api_get :index, token: user.spree_api_key
54
+ expect(response).to be_success
55
+ expect(json_response['widgets']).to be_blank
56
+ end
57
+
58
+ context "it has authorization to read widgets" do
59
+ it "returns widgets" do
60
+ api_get :index, token: admin_user.spree_api_key
61
+ expect(response).to be_success
62
+ expect(json_response['widgets']).to include(
63
+ 'name' => 'a widget',
64
+ 'position' => 1
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ describe "#show" do
71
+ let(:widget) { Widget.create!(name: "a widget") }
72
+
73
+ it "returns not found" do
74
+ api_get :show, id: widget.to_param, token: user.spree_api_key
75
+ assert_not_found!
76
+ end
77
+
78
+ context "it has authorization read widgets" do
79
+ it "returns widget details" do
80
+ api_get :show, id: widget.to_param, token: admin_user.spree_api_key
81
+ expect(response).to be_success
82
+ expect(json_response['name']).to eq 'a widget'
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#new" do
88
+ it "returns unauthorized" do
89
+ api_get :new, token: user.spree_api_key
90
+ expect(response).to be_unauthorized
91
+ end
92
+
93
+ context "it is allowed to view a new widget" do
94
+ it "can learn how to create a new widget" do
95
+ api_get :new, token: admin_user.spree_api_key
96
+ expect(response).to be_success
97
+ expect(json_response["attributes"]).to eq(['name'])
98
+ end
99
+ end
100
+ end
101
+
102
+ describe "#create" do
103
+ it "returns unauthorized" do
104
+ expect {
105
+ api_post :create, widget: { name: "a widget" }, token: user.spree_api_key
106
+ }.not_to change(Widget, :count)
107
+ expect(response).to be_unauthorized
108
+ end
109
+
110
+ context "it is authorized to create widgets" do
111
+ it "can create a widget" do
112
+ expect {
113
+ api_post :create, widget: { name: "a widget" }, token: admin_user.spree_api_key
114
+ }.to change(Widget, :count).by(1)
115
+ expect(response).to be_created
116
+ expect(json_response['name']).to eq 'a widget'
117
+ expect(Widget.last.name).to eq 'a widget'
118
+ end
119
+ end
120
+ end
121
+
122
+ describe "#update" do
123
+ let!(:widget) { Widget.create!(name: "a widget") }
124
+ it "returns unauthorized" do
125
+ api_put :update, id: widget.to_param, widget: { name: "another widget" }, token: user.spree_api_key
126
+ assert_not_found!
127
+ expect(widget.reload.name).to eq 'a widget'
128
+ end
129
+
130
+ context "it is authorized to update widgets" do
131
+ it "can update a widget" do
132
+ api_put :update, id: widget.to_param, widget: { name: "another widget" }, token: admin_user.spree_api_key
133
+ expect(response).to be_success
134
+ expect(json_response['name']).to eq 'another widget'
135
+ expect(widget.reload.name).to eq 'another widget'
136
+ end
137
+ end
138
+ end
139
+
140
+ describe "#destroy" do
141
+ let!(:widget) { Widget.create!(name: "a widget") }
142
+ it "returns unauthorized" do
143
+ api_delete :destroy, id: widget.to_param, token: user.spree_api_key
144
+ assert_not_found!
145
+ expect { widget.reload }.not_to raise_error
146
+ end
147
+
148
+ context "it is authorized to destroy widgets" do
149
+ it "can destroy a widget" do
150
+ api_delete :destroy, id: widget.to_param, token: admin_user.spree_api_key
151
+ expect(response.status).to eq 204
152
+ expect { widget.reload }.to raise_error(ActiveRecord::RecordNotFound)
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end