solidus_api 2.9.6 → 2.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -2
- data/app/controllers/spree/api/addresses_controller.rb +2 -2
- data/app/controllers/spree/api/base_controller.rb +21 -13
- data/app/controllers/spree/api/checkouts_controller.rb +1 -11
- data/app/controllers/spree/api/countries_controller.rb +2 -2
- data/app/controllers/spree/api/credit_cards_controller.rb +3 -3
- data/app/controllers/spree/api/customer_returns_controller.rb +67 -0
- data/app/controllers/spree/api/images_controller.rb +5 -5
- data/app/controllers/spree/api/inventory_units_controller.rb +2 -2
- data/app/controllers/spree/api/option_types_controller.rb +4 -4
- data/app/controllers/spree/api/option_values_controller.rb +3 -3
- data/app/controllers/spree/api/orders_controller.rb +11 -6
- data/app/controllers/spree/api/payments_controller.rb +2 -2
- data/app/controllers/spree/api/product_properties_controller.rb +9 -14
- data/app/controllers/spree/api/promotions_controller.rb +3 -12
- data/app/controllers/spree/api/properties_controller.rb +4 -4
- data/app/controllers/spree/api/resource_controller.rb +3 -3
- data/app/controllers/spree/api/return_authorizations_controller.rb +4 -4
- data/app/controllers/spree/api/shipments_controller.rb +2 -2
- data/app/controllers/spree/api/states_controller.rb +3 -3
- data/app/controllers/spree/api/stock_items_controller.rb +3 -3
- data/app/controllers/spree/api/stock_locations_controller.rb +4 -4
- data/app/controllers/spree/api/stock_movements_controller.rb +3 -3
- data/app/controllers/spree/api/stores_controller.rb +4 -4
- data/app/controllers/spree/api/taxonomies_controller.rb +5 -3
- data/app/controllers/spree/api/taxons_controller.rb +5 -5
- data/app/controllers/spree/api/users_controller.rb +13 -0
- data/app/controllers/spree/api/variants_controller.rb +4 -4
- data/app/controllers/spree/api/zones_controller.rb +3 -3
- data/app/helpers/spree/api/api_helpers.rb +20 -7
- data/app/views/spree/api/customer_returns/index.json.jbuilder +6 -0
- data/app/views/spree/api/customer_returns/new.json.jbuilder +4 -0
- data/app/views/spree/api/customer_returns/show.json.jbuilder +3 -0
- data/app/views/spree/api/errors/could_not_transition.json.jbuilder +4 -0
- data/app/views/spree/api/images/_image.json.jbuilder +2 -2
- data/app/views/spree/api/orders/_order.json.jbuilder +1 -1
- data/app/views/spree/api/orders/could_not_transition.json.jbuilder +5 -0
- data/app/views/spree/api/shared/_pagination.json.jbuilder +1 -1
- data/config/locales/en.yml +2 -0
- data/config/routes.rb +6 -3
- data/lib/spree/api.rb +0 -7
- data/lib/spree/api/config.rb +9 -0
- data/lib/spree/api/engine.rb +4 -4
- data/lib/spree/api/responders.rb +3 -2
- data/lib/spree/api/responders/{rabl_template.rb → jbuilder_template.rb} +3 -1
- data/lib/spree/api/testing_support/helpers.rb +1 -1
- data/{app/models → lib}/spree/api_configuration.rb +0 -0
- data/openapi/.stoplight.yml +7 -0
- data/openapi/authentication.md +33 -0
- data/openapi/checkout-flow.md +63 -0
- data/openapi/errors.md +3 -0
- data/openapi/lint.yml +1 -0
- data/openapi/main.hub.yml +65 -0
- data/openapi/pagination.md +7 -0
- data/openapi/solidus-api.oas.yml +6753 -0
- data/openapi/theme.css +0 -0
- data/solidus_api.gemspec +19 -19
- metadata +27 -115
- data/script/rails +0 -10
- data/spec/controllers/spree/api/base_controller_spec.rb +0 -118
- data/spec/controllers/spree/api/resource_controller_spec.rb +0 -190
- data/spec/features/checkout_spec.rb +0 -192
- data/spec/fixtures/thinking-cat.jpg +0 -0
- data/spec/models/spree/legacy_user_spec.rb +0 -103
- data/spec/requests/api/address_books_spec.rb +0 -240
- data/spec/requests/rabl_cache_spec.rb +0 -34
- data/spec/requests/ransackable_attributes_spec.rb +0 -79
- data/spec/requests/spree/api/addresses_controller_spec.rb +0 -57
- data/spec/requests/spree/api/checkouts_controller_spec.rb +0 -484
- data/spec/requests/spree/api/classifications_controller_spec.rb +0 -50
- data/spec/requests/spree/api/config_controller_spec.rb +0 -26
- data/spec/requests/spree/api/countries_controller_spec.rb +0 -48
- data/spec/requests/spree/api/coupon_codes_controller_spec.rb +0 -105
- data/spec/requests/spree/api/credit_cards_controller_spec.rb +0 -105
- data/spec/requests/spree/api/images_controller_spec.rb +0 -99
- data/spec/requests/spree/api/inventory_units_controller_spec.rb +0 -55
- data/spec/requests/spree/api/line_items_controller_spec.rb +0 -213
- data/spec/requests/spree/api/option_types_controller_spec.rb +0 -116
- data/spec/requests/spree/api/option_values_controller_spec.rb +0 -138
- data/spec/requests/spree/api/orders_controller_spec.rb +0 -954
- data/spec/requests/spree/api/payments_controller_spec.rb +0 -259
- data/spec/requests/spree/api/product_properties_controller_spec.rb +0 -114
- data/spec/requests/spree/api/products_controller_spec.rb +0 -422
- data/spec/requests/spree/api/promotion_application_spec.rb +0 -50
- data/spec/requests/spree/api/promotions_controller_spec.rb +0 -67
- data/spec/requests/spree/api/properties_controller_spec.rb +0 -102
- data/spec/requests/spree/api/return_authorizations_controller_spec.rb +0 -180
- data/spec/requests/spree/api/shipments_controller_spec.rb +0 -532
- data/spec/requests/spree/api/states_controller_spec.rb +0 -69
- data/spec/requests/spree/api/stock_items_controller_spec.rb +0 -311
- data/spec/requests/spree/api/stock_locations_controller_spec.rb +0 -170
- data/spec/requests/spree/api/stock_movements_controller_spec.rb +0 -81
- data/spec/requests/spree/api/store_credit_events_controller_spec.rb +0 -59
- data/spec/requests/spree/api/stores_controller_spec.rb +0 -134
- data/spec/requests/spree/api/taxonomies_controller_spec.rb +0 -114
- data/spec/requests/spree/api/taxons_controller_spec.rb +0 -217
- data/spec/requests/spree/api/unauthenticated_products_controller_spec.rb +0 -27
- data/spec/requests/spree/api/users_controller_spec.rb +0 -151
- data/spec/requests/spree/api/variants_controller_spec.rb +0 -340
- data/spec/requests/spree/api/zones_controller_spec.rb +0 -89
- data/spec/shared_examples/protect_product_actions.rb +0 -18
- data/spec/spec_helper.rb +0 -73
- data/spec/support/be_paginated_matcher.rb +0 -9
- data/spec/support/controller_hacks.rb +0 -43
- data/spec/support/database_cleaner.rb +0 -16
- data/spec/support/have_attributes_matcher.rb +0 -11
- data/spec/test_views/spree/api/widgets/_widget.json.jbuilder +0 -3
- data/spec/test_views/spree/api/widgets/index.json.jbuilder +0 -9
- data/spec/test_views/spree/api/widgets/new.json.jbuilder +0 -3
- data/spec/test_views/spree/api/widgets/show.json.jbuilder +0 -3
@@ -1,192 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Spree
|
6
|
-
describe 'Api Feature Specs', type: :request do
|
7
|
-
before do
|
8
|
-
stub_spree_preferences(Spree::Api::Config, requires_authentication: false)
|
9
|
-
end
|
10
|
-
let!(:promotion) { FactoryBot.create(:promotion, :with_order_adjustment, code: 'foo', weighted_order_adjustment_amount: 10) }
|
11
|
-
let(:promotion_code) { promotion.codes.first }
|
12
|
-
let!(:store) { FactoryBot.create(:store) }
|
13
|
-
let(:bill_address) { FactoryBot.create(:address) }
|
14
|
-
let(:ship_address) { FactoryBot.create(:address) }
|
15
|
-
let(:variant_1) { FactoryBot.create(:variant, price: 100.00) }
|
16
|
-
let(:variant_2) { FactoryBot.create(:variant, price: 200.00) }
|
17
|
-
let(:payment_method) { FactoryBot.create(:check_payment_method) }
|
18
|
-
let!(:shipping_method) do
|
19
|
-
FactoryBot.create(:shipping_method).tap do |shipping_method|
|
20
|
-
shipping_method.zones.first.zone_members.create!(zoneable: ship_address.country)
|
21
|
-
shipping_method.calculator.set_preference(:amount, 10.0)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def parsed
|
26
|
-
JSON.parse(response.body)
|
27
|
-
end
|
28
|
-
|
29
|
-
def login
|
30
|
-
expect {
|
31
|
-
post '/api/users', params: {
|
32
|
-
user: {
|
33
|
-
email: "featurecheckoutuser@example.com",
|
34
|
-
password: "featurecheckoutuser"
|
35
|
-
}
|
36
|
-
}
|
37
|
-
}.to change { Spree.user_class.count }.by 1
|
38
|
-
expect(response).to have_http_status(:created)
|
39
|
-
@user = Spree.user_class.find(parsed['id'])
|
40
|
-
|
41
|
-
# copied from api testing helpers support since we can't really sign in
|
42
|
-
allow(Spree::LegacyUser).to receive(:find_by).with(hash_including(:spree_api_key)) { @user }
|
43
|
-
end
|
44
|
-
|
45
|
-
def create_order(order_params: {})
|
46
|
-
expect { post '/api/orders', params: order_params }.to change { Order.count }.by 1
|
47
|
-
expect(response).to have_http_status(:created)
|
48
|
-
@order = Order.find(parsed['id'])
|
49
|
-
expect(@order.email).to eq "featurecheckoutuser@example.com"
|
50
|
-
end
|
51
|
-
|
52
|
-
def update_order(order_params: {})
|
53
|
-
put "/api/orders/#{@order.number}", params: order_params
|
54
|
-
expect(response).to have_http_status(:ok)
|
55
|
-
end
|
56
|
-
|
57
|
-
def create_line_item(variant, quantity = 1)
|
58
|
-
expect {
|
59
|
-
post "/api/orders/#{@order.number}/line_items",
|
60
|
-
params: { line_item: { variant_id: variant.id, quantity: quantity } }
|
61
|
-
}.to change { @order.line_items.count }.by 1
|
62
|
-
expect(response).to have_http_status(:created)
|
63
|
-
end
|
64
|
-
|
65
|
-
def add_promotion(_promotion)
|
66
|
-
expect {
|
67
|
-
post "/api/orders/#{@order.number}/coupon_codes",
|
68
|
-
params: { coupon_code: promotion_code.value }
|
69
|
-
}.to change { @order.promotions.count }.by 1
|
70
|
-
expect(response).to have_http_status(:ok)
|
71
|
-
end
|
72
|
-
|
73
|
-
def add_address(address, billing: true)
|
74
|
-
address_type = billing ? :bill_address : :ship_address
|
75
|
-
# It seems we are missing an order-scoped address api endpoint since we need
|
76
|
-
# to use update here.
|
77
|
-
expect {
|
78
|
-
update_order(order_params: { order: { address_type => address.attributes.except('id') } })
|
79
|
-
}.to change { @order.reload.public_send(address_type) }.to address
|
80
|
-
end
|
81
|
-
|
82
|
-
def add_payment
|
83
|
-
expect {
|
84
|
-
post "/api/orders/#{@order.number}/payments",
|
85
|
-
params: { payment: { payment_method_id: payment_method.id } }
|
86
|
-
}.to change { @order.reload.payments.count }.by 1
|
87
|
-
expect(response).to have_http_status(:created)
|
88
|
-
expect(@order.payments.last.payment_method).to eq payment_method
|
89
|
-
end
|
90
|
-
|
91
|
-
def advance
|
92
|
-
put "/api/checkouts/#{@order.number}/advance"
|
93
|
-
expect(response).to have_http_status(:ok)
|
94
|
-
end
|
95
|
-
|
96
|
-
def complete
|
97
|
-
put "/api/checkouts/#{@order.number}/complete"
|
98
|
-
expect(response).to have_http_status(:ok)
|
99
|
-
end
|
100
|
-
|
101
|
-
def assert_order_expectations
|
102
|
-
@order.reload
|
103
|
-
expect(@order.state).to eq 'complete'
|
104
|
-
expect(@order.completed_at).to be_a ActiveSupport::TimeWithZone
|
105
|
-
expect(@order.item_total).to eq 600.00
|
106
|
-
expect(@order.total).to eq 600.00
|
107
|
-
expect(@order.adjustment_total).to eq(-10.00)
|
108
|
-
expect(@order.shipment_total).to eq 10.00
|
109
|
-
expect(@order.user).to eq @user
|
110
|
-
expect(@order.bill_address).to eq bill_address
|
111
|
-
expect(@order.ship_address).to eq ship_address
|
112
|
-
expect(@order.payments.length).to eq 1
|
113
|
-
expect(@order.line_items.any? { |li| li.variant == variant_1 && li.quantity == 2 }).to eq true
|
114
|
-
expect(@order.line_items.any? { |li| li.variant == variant_2 && li.quantity == 2 }).to eq true
|
115
|
-
expect(@order.promotions).to eq [promotion]
|
116
|
-
end
|
117
|
-
|
118
|
-
it "is able to checkout with individualized requests" do
|
119
|
-
login
|
120
|
-
create_order
|
121
|
-
|
122
|
-
create_line_item(variant_1, 2)
|
123
|
-
add_promotion(promotion)
|
124
|
-
create_line_item(variant_2, 2)
|
125
|
-
|
126
|
-
add_address(bill_address)
|
127
|
-
add_address(ship_address, billing: false)
|
128
|
-
|
129
|
-
add_payment
|
130
|
-
|
131
|
-
advance
|
132
|
-
complete
|
133
|
-
|
134
|
-
assert_order_expectations
|
135
|
-
end
|
136
|
-
|
137
|
-
it "is able to checkout with the create request" do
|
138
|
-
login
|
139
|
-
|
140
|
-
create_order(order_params: {
|
141
|
-
order: {
|
142
|
-
bill_address: bill_address.as_json.except('id'),
|
143
|
-
ship_address: ship_address.as_json.except('id'),
|
144
|
-
line_items: {
|
145
|
-
0 => { variant_id: variant_1.id, quantity: 2 },
|
146
|
-
1 => { variant_id: variant_2.id, quantity: 2 }
|
147
|
-
},
|
148
|
-
# Would like to do this, but it puts the payment in a complete state,
|
149
|
-
# which the order does not like when transitioning from confirm to complete
|
150
|
-
# since it looks to process pending payments.
|
151
|
-
# payments: [ { payment_method: payment_method.name, state: "pending" } ],
|
152
|
-
}
|
153
|
-
})
|
154
|
-
|
155
|
-
add_promotion(promotion)
|
156
|
-
add_payment
|
157
|
-
|
158
|
-
advance
|
159
|
-
complete
|
160
|
-
|
161
|
-
assert_order_expectations
|
162
|
-
end
|
163
|
-
|
164
|
-
it "is able to checkout with the update request" do
|
165
|
-
login
|
166
|
-
|
167
|
-
create_order
|
168
|
-
update_order(order_params: {
|
169
|
-
order: {
|
170
|
-
bill_address: bill_address.as_json.except('id'),
|
171
|
-
ship_address: ship_address.as_json.except('id'),
|
172
|
-
line_items: {
|
173
|
-
0 => { variant_id: variant_1.id, quantity: 2 },
|
174
|
-
1 => { variant_id: variant_2.id, quantity: 2 }
|
175
|
-
},
|
176
|
-
# Would like to do this, but it puts the payment in a complete state,
|
177
|
-
# which the order does not like when transitioning from confirm to complete
|
178
|
-
# since it looks to process pending payments.
|
179
|
-
# payments: [ { payment_method: payment_method.name, state: "pending" } ],
|
180
|
-
}
|
181
|
-
})
|
182
|
-
|
183
|
-
add_promotion(promotion)
|
184
|
-
add_payment
|
185
|
-
|
186
|
-
advance
|
187
|
-
complete
|
188
|
-
|
189
|
-
assert_order_expectations
|
190
|
-
end
|
191
|
-
end
|
192
|
-
end
|
Binary file
|
@@ -1,103 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Spree
|
6
|
-
describe LegacyUser, type: :model do
|
7
|
-
let(:user) { LegacyUser.new }
|
8
|
-
|
9
|
-
it "can generate an API key" do
|
10
|
-
expect(user).to receive(:save!)
|
11
|
-
expect { user.generate_spree_api_key! }.to change(user, :spree_api_key).to be_present
|
12
|
-
end
|
13
|
-
|
14
|
-
it "can generate an API key without persisting" do
|
15
|
-
expect(user).not_to receive(:save!)
|
16
|
-
expect { user.generate_spree_api_key }.to change(user, :spree_api_key).to be_present
|
17
|
-
end
|
18
|
-
|
19
|
-
it "can clear an API key" do
|
20
|
-
user.spree_api_key = 'abc123'
|
21
|
-
expect(user).to receive(:save!)
|
22
|
-
expect { user.clear_spree_api_key! }.to change(user, :spree_api_key).to be_blank
|
23
|
-
end
|
24
|
-
|
25
|
-
it "can clear an api key without persisting" do
|
26
|
-
user.spree_api_key = 'abc123'
|
27
|
-
expect(user).not_to receive(:save!)
|
28
|
-
expect { user.clear_spree_api_key }.to change(user, :spree_api_key).to be_blank
|
29
|
-
end
|
30
|
-
|
31
|
-
context "auto-api-key grant" do
|
32
|
-
context "after role user create" do
|
33
|
-
let(:user) { create(:user) }
|
34
|
-
before { expect(user.spree_roles).to be_blank }
|
35
|
-
subject { user.spree_roles << role }
|
36
|
-
|
37
|
-
context "roles_for_auto_api_key default" do
|
38
|
-
let(:role) { create(:role, name: "admin") }
|
39
|
-
|
40
|
-
context "the user has no api key" do
|
41
|
-
before { user.clear_spree_api_key! }
|
42
|
-
it { expect { subject }.to change { user.reload.spree_api_key }.from(nil) }
|
43
|
-
end
|
44
|
-
|
45
|
-
context "the user already has an api key" do
|
46
|
-
before { user.generate_spree_api_key! }
|
47
|
-
it { expect { subject }.not_to change { user.reload.spree_api_key } }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "roles_for_auto_api_key is defined" do
|
52
|
-
let(:role) { create(:role, name: 'hobbit') }
|
53
|
-
let(:undesired_role) { create(:role, name: "foo") }
|
54
|
-
|
55
|
-
before {
|
56
|
-
user.clear_spree_api_key!
|
57
|
-
stub_spree_preferences(roles_for_auto_api_key: ['hobbit'])
|
58
|
-
}
|
59
|
-
|
60
|
-
it { expect { subject }.to change { user.reload.spree_api_key }.from(nil) }
|
61
|
-
it { expect { user.spree_roles << undesired_role }.not_to change { user.reload.spree_api_key } }
|
62
|
-
end
|
63
|
-
|
64
|
-
context "for all roles" do
|
65
|
-
let(:role) { create(:role, name: 'hobbit') }
|
66
|
-
let(:other_role) { create(:role, name: 'wizard') }
|
67
|
-
let(:other_user) { create(:user) }
|
68
|
-
|
69
|
-
before {
|
70
|
-
user.clear_spree_api_key!
|
71
|
-
other_user.clear_spree_api_key!
|
72
|
-
stub_spree_preferences(generate_api_key_for_all_roles: true)
|
73
|
-
}
|
74
|
-
|
75
|
-
it { expect { subject }.to change { user.reload.spree_api_key }.from(nil) }
|
76
|
-
it { expect { other_user.spree_roles << other_role }.to change { other_user.reload.spree_api_key }.from(nil) }
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context "after user create" do
|
81
|
-
let(:user) { LegacyUser.new }
|
82
|
-
|
83
|
-
context "generate_api_key_for_all_roles" do
|
84
|
-
it "does not grant api key default" do
|
85
|
-
expect(user.spree_api_key).to eq(nil)
|
86
|
-
|
87
|
-
user.save!
|
88
|
-
expect(user.spree_api_key).to eq(nil)
|
89
|
-
end
|
90
|
-
|
91
|
-
it "grants an api key on create when set to true" do
|
92
|
-
stub_spree_preferences(generate_api_key_for_all_roles: true)
|
93
|
-
|
94
|
-
expect(user.spree_api_key).to eq(nil)
|
95
|
-
|
96
|
-
user.save!
|
97
|
-
expect(user.spree_api_key).not_to eq(nil)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
@@ -1,240 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
module Spree
|
6
|
-
describe Api::AddressBooksController, type: :request do
|
7
|
-
let!(:state) { create(:state) }
|
8
|
-
let!(:harry_address_attributes) do
|
9
|
-
{
|
10
|
-
'firstname' => 'Harry',
|
11
|
-
'lastname' => 'Potter',
|
12
|
-
'address1' => '4 Privet Drive',
|
13
|
-
'address2' => 'cupboard under the stairs',
|
14
|
-
'city' => 'Surrey',
|
15
|
-
'zipcode' => '10010',
|
16
|
-
'phone' => '555-5555',
|
17
|
-
'state_id' => state.id,
|
18
|
-
'country_id' => state.country.id
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
let!(:ron_address_attributes) do
|
23
|
-
{
|
24
|
-
'firstname' => 'Ron',
|
25
|
-
'lastname' => 'Weasly',
|
26
|
-
'address1' => 'Ottery St. Catchpole',
|
27
|
-
'address2' => '4th floor',
|
28
|
-
'city' => 'Devon, West Country',
|
29
|
-
'zipcode' => '10010',
|
30
|
-
'phone' => '555-5555',
|
31
|
-
'state_id' => state.id,
|
32
|
-
'country_id' => state.country.id
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'as address book owner' do
|
37
|
-
context 'with ability' do
|
38
|
-
it 'returns my address book' do
|
39
|
-
user = create(:user, spree_api_key: 'galleon')
|
40
|
-
user.save_in_address_book(harry_address_attributes, true)
|
41
|
-
user.save_in_address_book(ron_address_attributes, false)
|
42
|
-
|
43
|
-
get "/api/users/#{user.id}/address_book",
|
44
|
-
headers: { Authorization: 'Bearer galleon' }
|
45
|
-
|
46
|
-
json_response = JSON.parse(response.body)
|
47
|
-
expect(response.status).to eq(200)
|
48
|
-
expect(json_response.length).to eq(2)
|
49
|
-
expect(json_response).to include(
|
50
|
-
hash_including(harry_address_attributes.merge!('default' => true)),
|
51
|
-
hash_including(ron_address_attributes.merge!('default' => false))
|
52
|
-
)
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'updates my address book' do
|
56
|
-
user = create(:user, spree_api_key: 'galleon')
|
57
|
-
address = user.save_in_address_book(harry_address_attributes, true)
|
58
|
-
harry_address_attributes['firstname'] = 'Ron'
|
59
|
-
|
60
|
-
expect {
|
61
|
-
put "/api/users/#{user.id}/address_book",
|
62
|
-
params: { address_book: harry_address_attributes.merge('id' => address.id) },
|
63
|
-
headers: { Authorization: 'Bearer galleon' }
|
64
|
-
}.to change { UserAddress.count }.from(1).to(2)
|
65
|
-
|
66
|
-
expect(response.status).to eq(200)
|
67
|
-
expect(JSON.parse(response.body).first).to include(harry_address_attributes)
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'when creating an address' do
|
71
|
-
it 'marks the update_target' do
|
72
|
-
user = create(:user, spree_api_key: 'galleon')
|
73
|
-
|
74
|
-
expect {
|
75
|
-
put "/api/users/#{user.id}/address_book",
|
76
|
-
params: { address_book: harry_address_attributes },
|
77
|
-
headers: { Authorization: 'Bearer galleon' }
|
78
|
-
}.to change { UserAddress.count }.by(1)
|
79
|
-
|
80
|
-
user_address = UserAddress.last
|
81
|
-
|
82
|
-
expect(response.status).to eq(200)
|
83
|
-
update_target_ids = JSON.parse(response.body).select { |a| a['update_target'] }.map { |a| a['id'] }
|
84
|
-
expect(update_target_ids).to eq([user_address.address_id])
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
context 'when updating an address' do
|
89
|
-
it 'marks the update_target' do
|
90
|
-
user = create(:user, spree_api_key: 'galleon')
|
91
|
-
address = user.save_in_address_book(harry_address_attributes, true)
|
92
|
-
|
93
|
-
expect {
|
94
|
-
put "/api/users/#{user.id}/address_book",
|
95
|
-
params: { address_book: harry_address_attributes },
|
96
|
-
headers: { Authorization: 'Bearer galleon' }
|
97
|
-
}.to_not change { UserAddress.count }
|
98
|
-
|
99
|
-
expect(response.status).to eq(200)
|
100
|
-
update_target_ids = JSON.parse(response.body).select { |a| a['update_target'] }.map { |a| a['id'] }
|
101
|
-
expect(update_target_ids).to eq([address.id])
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'archives my address' do
|
106
|
-
address = create(:address)
|
107
|
-
user = create(:user, spree_api_key: 'galleon')
|
108
|
-
user.save_in_address_book(address.attributes, false)
|
109
|
-
|
110
|
-
expect {
|
111
|
-
delete "/api/users/#{user.id}/address_book",
|
112
|
-
params: { address_id: address.id },
|
113
|
-
headers: { Authorization: 'Bearer galleon' }
|
114
|
-
}.to change { user.reload.user_addresses.count }.from(1).to(0)
|
115
|
-
|
116
|
-
expect(response.status).to eq(200)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context 'on behalf of address book owner' do
|
122
|
-
context 'with ability' do
|
123
|
-
before do
|
124
|
-
Spree::Config.roles.assign_permissions 'Prefect', [Spree::PermissionSets::UserManagement]
|
125
|
-
create(:user, spree_api_key: 'galleon', spree_roles: [build(:role, name: 'Prefect')])
|
126
|
-
end
|
127
|
-
|
128
|
-
it "returns another user's address book" do
|
129
|
-
other_user = create(:user)
|
130
|
-
other_user.save_in_address_book(harry_address_attributes, true)
|
131
|
-
other_user.save_in_address_book(ron_address_attributes, false)
|
132
|
-
|
133
|
-
get "/api/users/#{other_user.id}/address_book",
|
134
|
-
headers: { Authorization: 'Bearer galleon' }
|
135
|
-
|
136
|
-
json_response = JSON.parse(response.body)
|
137
|
-
expect(response.status).to eq(200)
|
138
|
-
expect(json_response.length).to eq(2)
|
139
|
-
expect(json_response).to include(
|
140
|
-
hash_including(harry_address_attributes.merge!('default' => true)),
|
141
|
-
hash_including(ron_address_attributes.merge!('default' => false))
|
142
|
-
)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "updates another user's address" do
|
146
|
-
other_user = create(:user)
|
147
|
-
address = other_user.save_in_address_book(harry_address_attributes, true)
|
148
|
-
updated_harry_address = harry_address_attributes.merge('firstname' => 'Ron')
|
149
|
-
|
150
|
-
expect {
|
151
|
-
put "/api/users/#{other_user.id}/address_book",
|
152
|
-
params: { address_book: updated_harry_address.merge('id' => address.id) },
|
153
|
-
headers: { Authorization: 'Bearer galleon' }
|
154
|
-
}.to change { UserAddress.count }.from(1).to(2)
|
155
|
-
|
156
|
-
expect(response.status).to eq(200)
|
157
|
-
expect(JSON.parse(response.body).first).to include(updated_harry_address)
|
158
|
-
end
|
159
|
-
|
160
|
-
it "archives another user's address" do
|
161
|
-
address = create(:address)
|
162
|
-
other_user = create(:user)
|
163
|
-
other_user.save_in_address_book(address.attributes, false)
|
164
|
-
|
165
|
-
expect {
|
166
|
-
delete "/api/users/#{other_user.id}/address_book",
|
167
|
-
params: { address_id: address.id },
|
168
|
-
headers: { Authorization: 'Bearer galleon' }
|
169
|
-
}.to change { other_user.reload.user_addresses.count }.from(1).to(0)
|
170
|
-
|
171
|
-
expect(response.status).to eq(200)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
context 'without ability' do
|
176
|
-
it 'does not return another user address book' do
|
177
|
-
create(:user, spree_api_key: 'galleon')
|
178
|
-
other_user = create(:user)
|
179
|
-
other_user.save_in_address_book(harry_address_attributes, true)
|
180
|
-
|
181
|
-
get "/api/users/#{other_user.id}/address_book",
|
182
|
-
headers: { Authorization: 'Bearer galleon' }
|
183
|
-
|
184
|
-
expect(response.status).to eq(401)
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'does not update another user address' do
|
188
|
-
address = create(:address)
|
189
|
-
other_user = create(:user)
|
190
|
-
other_user_address = other_user.save_in_address_book(address.attributes, true)
|
191
|
-
create(:user, spree_api_key: 'galleon')
|
192
|
-
|
193
|
-
expect {
|
194
|
-
put "/api/users/#{other_user.id}/address_book",
|
195
|
-
params: { address_book: other_user_address.attributes.merge('address1' => 'Hogwarts') },
|
196
|
-
headers: { Authorization: 'Bearer galleon' }
|
197
|
-
}.not_to change { UserAddress.count }
|
198
|
-
|
199
|
-
expect(response.status).to eq(401)
|
200
|
-
end
|
201
|
-
|
202
|
-
it 'does not archive another user address' do
|
203
|
-
address = create(:address)
|
204
|
-
other_user = create(:user)
|
205
|
-
other_user.save_in_address_book(address.attributes, true)
|
206
|
-
create(:user, spree_api_key: 'galleon')
|
207
|
-
|
208
|
-
expect {
|
209
|
-
delete "/api/users/#{other_user.id}/address_book",
|
210
|
-
params: { address_id: address.id },
|
211
|
-
headers: { Authorization: 'Bearer galleon' }
|
212
|
-
}.not_to change { other_user.user_addresses.count }
|
213
|
-
|
214
|
-
expect(response.status).to eq(401)
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
context 'unauthenticated' do
|
220
|
-
before do
|
221
|
-
@user = create(:user)
|
222
|
-
end
|
223
|
-
|
224
|
-
it 'GET returns a 401' do
|
225
|
-
get "/api/users/#{@user.id}/address_book"
|
226
|
-
expect(response.status).to eq(401)
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'UPDATE returns a 401' do
|
230
|
-
put "/api/users/#{@user.id}/address_book"
|
231
|
-
expect(response.status).to eq(401)
|
232
|
-
end
|
233
|
-
|
234
|
-
it 'DELETE returns a 401' do
|
235
|
-
delete "/api/users/#{@user.id}/address_book"
|
236
|
-
expect(response.status).to eq(401)
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|