chore: Move Enterprise pricing validation to on create (#8856)

* chore: Move Pricing validation to on create

The previous validation was getting triggered on all transactions,
this would prevent login to existing users and session data need to be updated.
Opting for a less intrusive approach.

* chore: minore change
This commit is contained in:
Sojan Jose
2024-02-05 17:48:29 +04:00
committed by GitHub
parent 39e27d2a23
commit 1b753720c1
2 changed files with 20 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ module Enterprise::Concerns::User
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
before_validation :ensure_installation_pricing_plan_quantity before_validation :ensure_installation_pricing_plan_quantity, on: :create
end end
def ensure_installation_pricing_plan_quantity def ensure_installation_pricing_plan_quantity

View File

@@ -3,9 +3,10 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe User do RSpec.describe User do
let(:user) { create(:user) } let!(:user) { create(:user) }
describe 'before validation for pricing plans' do describe 'before validation for pricing plans' do
let!(:existing_user) { create(:user) }
let(:new_user) { build(:user) } let(:new_user) { build(:user) }
context 'when pricing plan is not premium' do context 'when pricing plan is not premium' do
@@ -26,17 +27,25 @@ RSpec.describe User do
end end
context 'when the user limit is reached' do context 'when the user limit is reached' do
it 'adds an error to the user' do it 'adds an error when trying to create a user' do
allow(ChatwootHub).to receive(:pricing_plan_quantity).and_return(1) allow(ChatwootHub).to receive(:pricing_plan_quantity).and_return(1)
user.valid? new_user.valid?
expect(user.errors[:base]).to include('User limit reached. Please purchase more licenses from super admin') expect(new_user.errors[:base]).to include('User limit reached. Please purchase more licenses from super admin')
end
it 'will not add error when trying to update a existing user' do
allow(ChatwootHub).to receive(:pricing_plan_quantity).and_return(1)
existing_user.update(name: 'new name')
# since there is user and existing user, we are already over limits
existing_user.valid?
expect(existing_user.errors[:base]).to be_empty
end end
end end
context 'when the user limit is not reached' do context 'when the user limit is not reached' do
it 'does not add an error to the user' do it 'does not add an error to the user' do
allow(ChatwootHub).to receive(:pricing_plan_quantity).and_return(2) allow(ChatwootHub).to receive(:pricing_plan_quantity).and_return(3)
user.valid? new_user.valid?
expect(user.errors[:base]).to be_empty expect(user.errors[:base]).to be_empty
end end
end end
@@ -44,6 +53,10 @@ RSpec.describe User do
end end
describe 'audit log' do describe 'audit log' do
before do
create(:user)
end
context 'when user is created' do context 'when user is created' do
it 'has no associated audit log created' do it 'has no associated audit log created' do
expect(Audited::Audit.where(auditable_type: 'User', action: 'create').count).to eq 0 expect(Audited::Audit.where(auditable_type: 'User', action: 'create').count).to eq 0