From 857c8d90d2f0dd8d69d097b746adc4310f6c67c7 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 18 Aug 2025 14:40:45 +0530 Subject: [PATCH] fix rubocop for leave spec --- spec/enterprise/policies/leave_policy_spec.rb | 136 +++++++++--------- 1 file changed, 67 insertions(+), 69 deletions(-) diff --git a/spec/enterprise/policies/leave_policy_spec.rb b/spec/enterprise/policies/leave_policy_spec.rb index 6fee746ab..950922eac 100644 --- a/spec/enterprise/policies/leave_policy_spec.rb +++ b/spec/enterprise/policies/leave_policy_spec.rb @@ -6,76 +6,73 @@ RSpec.describe LeavePolicy, type: :policy do let(:agent) { create(:user, account: account, role: :agent) } let(:other_agent) { create(:user, account: account, role: :agent) } - let(:admin_account_user) { create(:account_user, account: account, user: administrator, role: :administrator) } - let(:agent_account_user) { create(:account_user, account: account, user: agent, role: :agent) } - let(:other_agent_account_user) { create(:account_user, account: account, user: other_agent, role: :agent) } - - let(:admin_context) { { user: administrator, account: account, account_user: admin_account_user } } - let(:agent_context) { { user: agent, account: account, account_user: agent_account_user } } - let(:other_agent_context) { { user: other_agent, account: account, account_user: other_agent_account_user } } - - let(:agent_leave) { create(:leave, account: account, user: agent, status: :pending) } - let(:other_agent_leave) { create(:leave, account: account, user: other_agent, status: :pending) } + let(:admin_context) { { user: administrator, account: account, account_user: administrator.account_users.find_by(account: account) } } + let(:agent_context) { { user: agent, account: account, account_user: agent.account_users.find_by(account: account) } } + let(:other_agent_context) { { user: other_agent, account: account, account_user: other_agent.account_users.find_by(account: account) } } describe 'update?' do + let(:agent_leave) { create(:leave, account: account, user: agent, status: :pending) } + context 'when leave is pending' do context 'when user is administrator' do it 'allows update of any leave' do - policy = LeavePolicy.new(admin_context, agent_leave) + policy = described_class.new(admin_context, agent_leave) expect(policy.update?).to be true end end context 'when user owns the leave' do it 'allows update' do - policy = LeavePolicy.new(agent_context, agent_leave) + policy = described_class.new(agent_context, agent_leave) expect(policy.update?).to be true end end context 'when user does not own the leave' do it 'denies update' do - policy = LeavePolicy.new(other_agent_context, agent_leave) + policy = described_class.new(other_agent_context, agent_leave) expect(policy.update?).to be false end end end context 'when leave is not pending' do - let(:approved_leave) { create(:leave, account: account, user: agent, status: :approved) } - let(:rejected_leave) { create(:leave, account: account, user: agent, status: :rejected) } - let(:cancelled_leave) { create(:leave, account: account, user: agent, status: :cancelled) } - context 'when user is administrator' do it 'denies update of approved leave' do - policy = LeavePolicy.new(admin_context, approved_leave) + approved_leave = create(:leave, account: account, user: agent, status: :approved) + policy = described_class.new(admin_context, approved_leave) expect(policy.update?).to be false end it 'denies update of rejected leave' do - policy = LeavePolicy.new(admin_context, rejected_leave) + rejected_leave = create(:leave, account: account, user: agent, status: :rejected) + policy = described_class.new(admin_context, rejected_leave) expect(policy.update?).to be false end it 'denies update of cancelled leave' do - policy = LeavePolicy.new(admin_context, cancelled_leave) + cancelled_leave = create(:leave, account: account, user: agent, status: :cancelled) + policy = described_class.new(admin_context, cancelled_leave) expect(policy.update?).to be false end end context 'when user owns the leave' do it 'denies update of approved leave' do - policy = LeavePolicy.new(agent_context, approved_leave) + approved_leave = create(:leave, account: account, user: agent, status: :approved) + policy = described_class.new(agent_context, approved_leave) expect(policy.update?).to be false end it 'denies update of rejected leave' do - policy = LeavePolicy.new(agent_context, rejected_leave) + rejected_leave = create(:leave, account: account, user: agent, status: :rejected) + policy = described_class.new(agent_context, rejected_leave) expect(policy.update?).to be false end it 'denies update of cancelled leave' do - policy = LeavePolicy.new(agent_context, cancelled_leave) + cancelled_leave = create(:leave, account: account, user: agent, status: :cancelled) + policy = described_class.new(agent_context, cancelled_leave) expect(policy.update?).to be false end end @@ -84,91 +81,94 @@ RSpec.describe LeavePolicy, type: :policy do describe 'destroy?' do context 'when leave can be cancelled' do - let(:pending_leave) { create(:leave, account: account, user: agent, status: :pending) } - let(:approved_future_leave) do - create(:leave, account: account, user: agent, status: :approved, - start_date: 1.week.from_now.to_date, end_date: 2.weeks.from_now.to_date) - end - context 'when user is administrator' do it 'allows destroy of pending leave' do - policy = LeavePolicy.new(admin_context, pending_leave) + pending_leave = create(:leave, account: account, user: agent, status: :pending) + policy = described_class.new(admin_context, pending_leave) expect(policy.destroy?).to be true end it 'allows destroy of approved future leave' do - policy = LeavePolicy.new(admin_context, approved_future_leave) + approved_future_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: 1.week.from_now.to_date, + end_date: 2.weeks.from_now.to_date) + policy = described_class.new(admin_context, approved_future_leave) expect(policy.destroy?).to be true end it 'allows destroy of other user leaves' do other_leave = create(:leave, account: account, user: other_agent, status: :pending) - policy = LeavePolicy.new(admin_context, other_leave) + policy = described_class.new(admin_context, other_leave) expect(policy.destroy?).to be true end end context 'when user owns the leave' do it 'allows destroy of own pending leave' do - policy = LeavePolicy.new(agent_context, pending_leave) + pending_leave = create(:leave, account: account, user: agent, status: :pending) + policy = described_class.new(agent_context, pending_leave) expect(policy.destroy?).to be true end it 'allows destroy of own approved future leave' do - policy = LeavePolicy.new(agent_context, approved_future_leave) + approved_future_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: 1.week.from_now.to_date, + end_date: 2.weeks.from_now.to_date) + policy = described_class.new(agent_context, approved_future_leave) expect(policy.destroy?).to be true end end context 'when user does not own the leave' do it 'denies destroy of other user leave' do - policy = LeavePolicy.new(other_agent_context, pending_leave) + pending_leave = create(:leave, account: account, user: agent, status: :pending) + policy = described_class.new(other_agent_context, pending_leave) expect(policy.destroy?).to be false end end end context 'when leave cannot be cancelled' do - let(:approved_current_leave) do - create(:leave, account: account, user: agent, status: :approved, - start_date: Date.current, end_date: Date.current + 2.days) - end - let(:approved_past_leave) do - create(:leave, account: account, user: agent, status: :approved, - start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) - end - let(:rejected_leave) { create(:leave, account: account, user: agent, status: :rejected) } - context 'when user is administrator' do it 'denies destroy of current approved leave' do - policy = LeavePolicy.new(admin_context, approved_current_leave) + approved_current_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: Date.current, end_date: Date.current + 2.days) + policy = described_class.new(admin_context, approved_current_leave) expect(policy.destroy?).to be false end it 'denies destroy of past approved leave' do - policy = LeavePolicy.new(admin_context, approved_past_leave) + approved_past_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) + policy = described_class.new(admin_context, approved_past_leave) expect(policy.destroy?).to be false end it 'denies destroy of rejected leave' do - policy = LeavePolicy.new(admin_context, rejected_leave) + rejected_leave = create(:leave, account: account, user: agent, status: :rejected) + policy = described_class.new(admin_context, rejected_leave) expect(policy.destroy?).to be false end end context 'when user owns the leave' do it 'denies destroy of current approved leave' do - policy = LeavePolicy.new(agent_context, approved_current_leave) + approved_current_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: Date.current, end_date: Date.current + 2.days) + policy = described_class.new(agent_context, approved_current_leave) expect(policy.destroy?).to be false end it 'denies destroy of past approved leave' do - policy = LeavePolicy.new(agent_context, approved_past_leave) + approved_past_leave = create(:leave, account: account, user: agent, status: :approved, + start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) + policy = described_class.new(agent_context, approved_past_leave) expect(policy.destroy?).to be false end it 'denies destroy of rejected leave' do - policy = LeavePolicy.new(agent_context, rejected_leave) + rejected_leave = create(:leave, account: account, user: agent, status: :rejected) + policy = described_class.new(agent_context, rejected_leave) expect(policy.destroy?).to be false end end @@ -176,14 +176,10 @@ RSpec.describe LeavePolicy, type: :policy do end describe 'Scope' do - let(:admin_leave) { create(:leave, account: account, user: administrator) } - let(:agent_leave) { create(:leave, account: account, user: agent) } - let(:other_agent_leave) { create(:leave, account: account, user: other_agent) } - before do - admin_leave - agent_leave - other_agent_leave + create(:leave, account: account, user: administrator) + create(:leave, account: account, user: agent) + create(:leave, account: account, user: other_agent) end context 'when user is administrator' do @@ -191,8 +187,9 @@ RSpec.describe LeavePolicy, type: :policy do scope = LeavePolicy::Scope.new(admin_context, Leave.all) result = scope.resolve - expect(result).to include(admin_leave, agent_leave, other_agent_leave) + expect(result.count).to eq(3) expect(result.includes_values).to include(:user, :approver) + expect(result.map(&:user_id)).to contain_exactly(administrator.id, agent.id, other_agent.id) end end @@ -201,8 +198,8 @@ RSpec.describe LeavePolicy, type: :policy do scope = LeavePolicy::Scope.new(agent_context, Leave.all) result = scope.resolve - expect(result).to include(agent_leave) - expect(result).not_to include(admin_leave, other_agent_leave) + expect(result.count).to eq(1) + expect(result.first.user_id).to eq(agent.id) expect(result.includes_values).to include(:user, :approver) end @@ -210,39 +207,40 @@ RSpec.describe LeavePolicy, type: :policy do scope = LeavePolicy::Scope.new(other_agent_context, Leave.all) result = scope.resolve - expect(result).to include(other_agent_leave) - expect(result).not_to include(admin_leave, agent_leave) + expect(result.count).to eq(1) + expect(result.first.user_id).to eq(other_agent.id) end end - context 'scope initialization' do + context 'when initializing scope' do it 'properly initializes all context variables' do scope = LeavePolicy::Scope.new(admin_context, Leave.all) expect(scope.user_context).to eq(admin_context) expect(scope.user).to eq(administrator) expect(scope.account).to eq(account) - expect(scope.account_user).to eq(admin_account_user) + expect(scope.account_user).to eq(administrator.account_users.find_by(account: account)) expect(scope.scope).to eq(Leave.all) end end end describe 'ownership checks' do - let(:policy) { LeavePolicy.new(agent_context, agent_leave) } - describe '#owned_by_user?' do it 'returns true when user owns the leave' do + agent_leave = create(:leave, account: account, user: agent, status: :pending) + policy = described_class.new(agent_context, agent_leave) expect(policy.send(:owned_by_user?)).to be true end it 'returns false when user does not own the leave' do - policy = LeavePolicy.new(other_agent_context, agent_leave) + agent_leave = create(:leave, account: account, user: agent, status: :pending) + policy = described_class.new(other_agent_context, agent_leave) expect(policy.send(:owned_by_user?)).to be false end it 'returns false when record is nil' do - policy = LeavePolicy.new(agent_context, nil) + policy = described_class.new(agent_context, nil) expect(policy.send(:owned_by_user?)).to be false end end