Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make API independent of promotion configuration #5686

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions api/lib/spree/api_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ class ApiConfiguration < Preferences::Configuration
:variant_id
]

preference :promotion_attributes, :array, default: [
:id, :name, :description, :expires_at, :starts_at, :type, :usage_limit,
:advertise, :path
]
preference :promotion_attributes, :array, default: Spree::Config.promotions.promotion_api_attributes

preference :store_attributes, :array, default: [
:id, :name, :url, :meta_description, :meta_keywords, :seo_title,
Expand Down
83 changes: 62 additions & 21 deletions api/spec/requests/spree/api/coupon_codes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ module Spree::Api

before do
stub_authentication!
expect(Spree::Config.promotions.coupon_code_handler_class).to receive(:new).and_return(handler)
end

describe '#create' do
let(:promo) { create(:promotion_with_item_adjustment, code: 'night_melody') }
let(:promo_code) { promo.codes.first }

before do
allow_any_instance_of(Spree::Order).to receive_messages user: current_api_user
end

context 'when successful' do
let(:order) { create(:order_with_line_items) }
let(:successful_application) do
double(
'handler',
successful?: true,
success: "The coupon code was successfully applied to your order.",
error: nil,
status_code: "coupon_code_applied"
)
end

let(:handler) do
double('handler', apply: successful_application)
end

it 'applies the coupon' do
post spree.api_order_coupon_codes_path(order), params: { coupon_code: promo_code.value }
post spree.api_order_coupon_codes_path(order), params: { coupon_code: "10OFF" }

expect(response.status).to eq(200)
expect(order.reload.promotions).to eq([promo])
expect(json_response).to eq({
"success" => I18n.t('spree.coupon_code_applied'),
"success" => "The coupon code was successfully applied to your order.",
"error" => nil,
"successful" => true,
"status_code" => "coupon_code_applied"
Expand All @@ -41,12 +51,24 @@ module Spree::Api

context 'when unsuccessful' do
let(:order) { create(:order) }
let(:unsuccessful_application) do
double(
'handler',
successful?: false,
success: nil,
error: "This coupon code could not be applied to the cart at this time.",
status_code: "coupon_code_unknown_error"
)
end

let(:handler) do
double('handler', apply: unsuccessful_application)
end

it 'returns an error' do
post spree.api_order_coupon_codes_path(order), params: { coupon_code: promo_code.value }
post spree.api_order_coupon_codes_path(order), params: { coupon_code: "10OFF" }

expect(response.status).to eq(422)
expect(order.reload.promotions).to eq([])
expect(json_response).to eq({
"success" => nil,
"error" => I18n.t('spree.coupon_code_unknown_error'),
Expand All @@ -58,25 +80,30 @@ module Spree::Api
end

describe '#destroy' do
let(:promo) {
create(:promotion_with_item_adjustment,
code: 'tenoff',
per_code_usage_limit: 5,
adjustment_rate: 10)
}

let(:promo_code) { promo.codes.first }
let(:order) { create(:order_with_line_items, user: current_api_user) }

before do
post spree.api_order_coupon_codes_path(order), params: { coupon_code: promo_code.value }
delete spree.api_order_coupon_code_path(order, promo_code.value)
subject do
delete spree.api_order_coupon_code_path(order, "10OFF")
end

context 'when successful' do
let(:successful_removal) do
double(
'handler',
successful?: true,
success: "The coupon code was successfully removed from this order.",
error: nil,
status_code: "coupon_code_removed"
)
end

let(:handler) do
double('handler', remove: successful_removal)
end

it 'removes the coupon' do
subject
expect(response.status).to eq(200)
expect(order.reload.promotions).to eq([])
expect(json_response).to eq({
"success" => I18n.t('spree.coupon_code_removed'),
"error" => nil,
Expand All @@ -87,8 +114,22 @@ module Spree::Api
end

context 'when unsuccessful' do
let(:unsuccessful_removal) do
double(
'handler',
successful?: false,
success: nil,
error: "The coupon code you are trying to remove is not present on this order.",
status_code: "coupon_code_not_present"
)
end

let(:handler) do
double('handler', remove: unsuccessful_removal)
end

it 'returns an error' do
delete spree.api_order_coupon_code_path(order, promo_code.value)
subject

expect(response.status).to eq(422)
expect(order.reload.promotions).to eq([])
Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/core/null_promotion_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class NullPromotionConfiguration < Spree::Preferences::Configuration
# the standard promotion finder class
# Spree::NullPromotionFinder.
class_name_attribute :promotion_finder_class, default: 'Spree::NullPromotionFinder'

# !@attribute [rw] promotion_api_attributes
# @return [Array<Symbol>] Attributes to be returned by the API for a promotion
preference :promotion_api_attributes, :array, default: []
end
end
end
14 changes: 14 additions & 0 deletions core/lib/spree/core/promotion_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ class PromotionConfiguration < Spree::Preferences::Configuration
# @return [Integer] Promotions to show per-page in the admin (default: +15+)
preference :promotions_per_page, :integer, default: 15

# @!attribute [rw] promotion_attributes
# @return [Array<Symbol>] Attributes to be returned by the API for a promotion
preference :promotion_api_attributes, :array, default: [
:id,
:name,
:description,
:expires_at,
:starts_at,
:type,
:usage_limit,
:advertise,
:path
]

# promotion_chooser_class allows extensions to provide their own PromotionChooser
class_name_attribute :promotion_chooser_class, default: 'Spree::PromotionChooser'

Expand Down
Loading