Skip to content

Commit

Permalink
fix (plan-override): render forbidden error for plan override without…
Browse files Browse the repository at this point in the history
… premium license (#1441)
  • Loading branch information
lovrocolic authored Nov 6, 2023
1 parent 82b40a3 commit c92e1a2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/services/plans/override_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(plan:, params:)
end

def call
return result unless License.premium?
return result.forbidden_failure! unless License.premium?

ActiveRecord::Base.transaction do
new_plan = plan.dup.tap do |p|
Expand Down
1 change: 1 addition & 0 deletions app/services/subscriptions/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def initialize(customer:, plan:, params:)

def call
return result unless valid?(customer:, plan:, subscription_at:, ending_at: params[:ending_at])
return result.forbidden_failure! if !License.premium? && params.key?(:plan_overrides)

plan.amount_currency = plan_overrides[:amount_currency] if plan_overrides[:amount_currency]
plan.amount_cents = plan_overrides[:amount_cents] if plan_overrides[:amount_cents]
Expand Down
1 change: 1 addition & 0 deletions app/services/subscriptions/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def call
)
return result
end
return result.forbidden_failure! if !License.premium? && params.key?(:plan_overrides)

subscription.name = params[:name] if params.key?(:name)
subscription.ending_at = params[:ending_at] if params.key?(:ending_at)
Expand Down
24 changes: 24 additions & 0 deletions spec/services/subscriptions/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@
end
end

context 'when License is free and plan_overrides is passed' do
let(:params) do
{
external_customer_id:,
plan_code:,
name:,
external_id:,
billing_time:,
subscription_at:,
subscription_id:,
plan_overrides: {
amount_cents: 0,
},
}
end

it 'returns an error' do
result = create_service.call

expect(result).not_to be_success
expect(result.error.code).to eq('feature_unavailable')
end
end

context 'when customer does not exists in API context' do
let(:customer) { Customer.new(organization:, external_id: SecureRandom.uuid) }

Expand Down
18 changes: 18 additions & 0 deletions spec/services/subscriptions/update_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,23 @@
expect(result.error.error_code).to eq('subscription_not_found')
end
end

context 'when License is free and plan_overrides is passed' do
let(:params) do
{
name: 'new name',
plan_overrides: {
amount_cents: 0,
},
}
end

it 'returns an error' do
result = update_service.call

expect(result).not_to be_success
expect(result.error.code).to eq('feature_unavailable')
end
end
end
end

0 comments on commit c92e1a2

Please sign in to comment.