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

Set model_class on admin promotion rules controller #2623

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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def load_promotion
@promotion = Spree::Promotion.find(params[:promotion_id])
end

def model_class
Spree::PromotionRule
end

def validate_promotion_rule_type
requested_type = params[:promotion_rule].delete(:type)
promotion_rule_types = Rails.application.config.spree.promotions.rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,34 @@
require 'spec_helper'

describe Spree::Admin::PromotionRulesController, type: :controller do
stub_authorization!

let!(:promotion) { create(:promotion) }

it "can create a promotion rule of a valid type" do
post :create, params: { promotion_id: promotion.id, promotion_rule: { type: "Spree::Promotion::Rules::Product" } }
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(promotion.rules.count).to eq(1)
context "when the user is authorized" do
stub_authorization! do |_u|
Spree::PermissionSets::PromotionManagement.new(self).activate!
end

it "can create a promotion rule of a valid type" do
post :create, params: { promotion_id: promotion.id, promotion_rule: { type: "Spree::Promotion::Rules::Product" } }
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(promotion.rules.count).to eq(1)
end

it "can not create a promotion rule of an invalid type" do
post :create, params: { promotion_id: promotion.id, promotion_rule: { type: "Spree::InvalidType" } }
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(promotion.rules.count).to eq(0)
end
end

it "can not create a promotion rule of an invalid type" do
post :create, params: { promotion_id: promotion.id, promotion_rule: { type: "Spree::InvalidType" } }
expect(response).to be_redirect
expect(response).to redirect_to spree.edit_admin_promotion_path(promotion)
expect(promotion.rules.count).to eq(0)
context "when the user is not authorized" do
it "sets an error message and redirects the user" do
post :create, params: { promotion_id: promotion.id, promotion_rule: { type: "Spree::Promotion::Rules::Product" } }

expect(flash[:error]).to eq("Authorization Failure")
expect(response).to redirect_to('/unauthorized')
end
end
end