solidus_api 1.0.6 → 1.0.7

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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d8748a7cb17f2da88ee82038992cbe3b5b1e613
4
- data.tar.gz: 862809a543c246df6135095e777ba6f711b7bc14
3
+ metadata.gz: c108e552ec6ec720a0b101802d36eaf5d9aa4761
4
+ data.tar.gz: e376f6d8e98c28a0f193b012a3f9e62476adb5d4
5
5
  SHA512:
6
- metadata.gz: 20d3aefbd9bcf0fc0f079558bcf65855f5a6cf46d14f31291d3779562ad08ec573d3a9105876c5647cf97fa3f405280eabea5282f78cbae08b7b26c4441343e1
7
- data.tar.gz: ab295708931c7d4404377e942d8906cef0d2ab01befda544499d65eefab6a765fe8e609efb5968de822ab92d7b096bf90ab646c9f00785af7decd1be84dd0296
6
+ metadata.gz: 3deada5b7463920279f00169107d3afd9279ab8f019928b7a109731f803b515327b4486e73d84e4e7d53746999741bb8e4a48328b1aaf019fa239c6f4feb7b87
7
+ data.tar.gz: 3907075dabb2ed39ce27230ddea643aa16a8c40b48252b4d545f7d8df9f75b08dcaadf54e7ab2e48a5d85e7fa645ab9d70872e5c944ad3e51b98e65e67b0a7da
@@ -28,20 +28,23 @@ module Spree
28
28
  def create
29
29
  authorize! :create, Order
30
30
 
31
- order_user = if order_params[:user_id]
32
- Spree.user_class.find(order_params[:user_id])
33
- else
34
- current_api_user
35
- end
31
+ if can?(:admin, Order)
32
+ order_user = if order_params[:user_id]
33
+ Spree.user_class.find(order_params[:user_id])
34
+ else
35
+ current_api_user
36
+ end
36
37
 
37
- import_params = if can?(:admin, Spree::Order)
38
- params[:order].present? ? params[:order].permit! : {}
38
+ @order = Spree::Core::Importer::Order.import(order_user, order_params)
39
+ respond_with(@order, default_template: :show, status: 201)
39
40
  else
40
- order_params
41
+ @order = Spree::Order.create!(user: current_api_user, store: current_store)
42
+ if @order.contents.update_cart(order_params)
43
+ respond_with(@order, default_template: :show, status: 201)
44
+ else
45
+ invalid_resource!(@order)
46
+ end
41
47
  end
42
-
43
- @order = Spree::Core::Importer::Order.import(order_user, import_params)
44
- respond_with(@order, default_template: :show, status: 201)
45
48
  end
46
49
 
47
50
  def empty
@@ -17,6 +17,7 @@ module Spree
17
17
  end
18
18
 
19
19
  def create
20
+ @order.validate_payments_attributes(payment_params)
20
21
  @payment = @order.payments.build(payment_params)
21
22
  if @payment.save
22
23
  respond_with(@payment, status: 201, default_template: :show)
@@ -164,6 +164,19 @@ module Spree
164
164
  expect(response.status).to eq(200)
165
165
  end
166
166
 
167
+ context "with disallowed payment method" do
168
+ it "returns not found" do
169
+ order.update_column(:state, "payment")
170
+ allow_any_instance_of(Spree::Gateway::Bogus).to receive(:source_required?).and_return(false)
171
+ @payment_method.update!(display_on: "back_end")
172
+ expect {
173
+ api_put :update, id: order.to_param, order_token: order.guest_token, order: { payments_attributes: [{ payment_method_id: @payment_method.id }] }
174
+ }.not_to change { Spree::Payment.count }
175
+ expect(response.status).to eq(404)
176
+ end
177
+ end
178
+
179
+
167
180
  it "returns errors when source is required and missing" do
168
181
  order.update_column(:state, "payment")
169
182
  api_put :update, :id => order.to_param, :order_token => order.guest_token,
@@ -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) { 3.days.ago }
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 } }
33
34
 
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 } }
35
+ subject { api_post :create, order: attributes }
48
36
 
49
37
  context "when the current user cannot administrate the order" do
50
- let(:can_admin) { false }
38
+ stub_authorization! do |_|
39
+ can :create, Spree::Order
40
+ end
51
41
 
52
42
  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
57
43
  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
58
48
  end
59
49
 
60
50
  it { is_expected.to be_success }
61
- end
62
51
 
63
- context "when the current user can administrate the order" do
64
- let(:can_admin) { true }
52
+ context 'creating payment' do
53
+ let(:attributes) { super().merge(payments_attributes: [{ payment_method_id: payment_method.id }]) }
65
54
 
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
72
- end
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
73
64
 
74
- it { is_expected.to be_success }
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
74
+ end
75
75
  end
76
76
  end
77
77
 
@@ -81,41 +81,65 @@ module Spree
81
81
  let(:can_admin) { false }
82
82
  subject { api_put :update, id: order.to_param, order: order_params }
83
83
 
84
- before do
85
- allow_any_instance_of(Spree::Ability).to receive(:can?).
86
- and_return(true)
84
+ context "when the user cannot administer the order" do
85
+ stub_authorization! do |_|
86
+ can [:update], Spree::Order
87
+ end
87
88
 
88
- allow(Spree::Order).to receive(:find_by!).
89
- with(number: order.number).
90
- and_return(order)
89
+ it "updates the user's email" do
90
+ expect {
91
+ subject
92
+ }.to change { order.reload.email }.to("foo@foobar.com")
93
+ end
91
94
 
92
- allow(Spree.user_class).to receive(:find).
93
- with(user.id).
94
- and_return(user)
95
+ it { is_expected.to be_success }
95
96
 
96
- allow_any_instance_of(Spree::Ability).to receive(:can?).
97
- with(:admin, Spree::Order).
98
- and_return(can_admin)
99
- end
97
+ it "does not associate users" do
98
+ expect {
99
+ subject
100
+ }.not_to change { order.reload.user }
101
+ end
100
102
 
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
106
- end
103
+ it "does not change forbidden attributes" do
104
+ expect {
105
+ subject
106
+ }.to_not change{ order.reload.number }
107
+ end
108
+
109
+ context 'creating payment' do
110
+ let(:order_params) { super().merge(payments_attributes: [{ payment_method_id: payment_method.id }]) }
111
+
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
107
121
 
108
- it { is_expected.to be_success }
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
132
+ end
109
133
 
110
134
  context "when the user can administer the order" do
111
- let(:can_admin) { true }
135
+ stub_authorization! do |_|
136
+ can [:admin, :update], Spree::Order
137
+ end
112
138
 
113
139
  it "will associate users" do
114
- expect(order).to receive(:associate_user!).
115
- once.
116
- with(user)
117
-
118
- subject
140
+ expect {
141
+ subject
142
+ }.to change { order.reload.user }.to(user)
119
143
  end
120
144
 
121
145
  it "updates the otherwise forbidden attributes" do
@@ -123,17 +147,6 @@ module Spree
123
147
  to("anothernumber")
124
148
  end
125
149
  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
137
150
  end
138
151
 
139
152
  it "cannot view all orders" do
@@ -355,16 +368,13 @@ module Spree
355
368
 
356
369
  # Regression test for #3404
357
370
  it "can specify additional parameters for a line item" do
358
- expect(Order).to receive(:create!).and_return(order = Spree::Order.new)
359
- allow(order).to receive(:associate_user!)
360
- allow(order).to receive_message_chain(:contents, :add).and_return(line_item = double('LineItem'))
361
- expect(line_item).to receive(:update_attributes!).with("special" => true)
371
+ expect_any_instance_of(Spree::LineItem).to receive(:special=).with("foo")
362
372
 
363
373
  allow(controller).to receive_messages(permitted_line_item_attributes: [:id, :variant_id, :quantity, :special])
364
374
  api_post :create, :order => {
365
375
  :line_items => {
366
376
  "0" => {
367
- :variant_id => variant.to_param, :quantity => 5, :special => true
377
+ variant_id: variant.to_param, quantity: 5, special: "foo"
368
378
  }
369
379
  }
370
380
  }
@@ -43,6 +43,17 @@ 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
46
57
  end
47
58
 
48
59
  context "payment source is required" do
data/spec/spec_helper.rb CHANGED
@@ -31,6 +31,7 @@ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
31
31
  require 'spree/testing_support/factories'
32
32
  require 'spree/testing_support/preferences'
33
33
  require 'spree/testing_support/mail'
34
+ require 'spree/testing_support/authorization_helpers'
34
35
 
35
36
  require 'spree/api/testing_support/caching'
36
37
  require 'spree/api/testing_support/helpers'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Solidus Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-23 00:00:00.000000000 Z
11
+ date: 2017-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.6
19
+ version: 1.0.7
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.6
26
+ version: 1.0.7
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rabl
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -278,7 +278,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
278
278
  version: '0'
279
279
  requirements: []
280
280
  rubyforge_project:
281
- rubygems_version: 2.5.1
281
+ rubygems_version: 2.6.11
282
282
  signing_key:
283
283
  specification_version: 4
284
284
  summary: REST API for the Solidus e-commerce framework.