diff --git a/spec/enterprise/controllers/api/v1/accounts/leave_records_controller_spec.rb b/spec/enterprise/controllers/api/v1/accounts/leave_records_controller_spec.rb index b27a0ac5b..0d55ce2c7 100644 --- a/spec/enterprise/controllers/api/v1/accounts/leave_records_controller_spec.rb +++ b/spec/enterprise/controllers/api/v1/accounts/leave_records_controller_spec.rb @@ -7,8 +7,10 @@ RSpec.describe 'LeaveRecords API', type: :request do let(:other_user) { create(:user, account: account, role: :agent) } describe 'GET #index' do - let!(:agent_leave_record) { create(:leave_record, account: account, user: agent) } - let!(:other_user_leave_record) { create(:leave_record, account: account, user: other_user) } + before do + create(:leave_record, account: account, user: agent) + create(:leave_record, account: account, user: other_user) + end context 'when authenticated as administrator' do it 'returns all leave records in the account' do diff --git a/spec/enterprise/models/leave_record_spec.rb b/spec/enterprise/models/leave_record_spec.rb index 79f03e670..febe26c9c 100644 --- a/spec/enterprise/models/leave_record_spec.rb +++ b/spec/enterprise/models/leave_record_spec.rb @@ -38,22 +38,22 @@ RSpec.describe LeaveRecord, type: :model do context 'when leave record is approved' do it 'returns true for future leaves' do leave_record = create(:leave_record, status: :approved, - start_date: 1.week.from_now.to_date, - end_date: 2.weeks.from_now.to_date) + start_date: 1.week.from_now.to_date, + end_date: 2.weeks.from_now.to_date) expect(leave_record.can_be_cancelled?).to be true end it 'returns false for current leaves' do leave_record = create(:leave_record, status: :approved, - start_date: Date.current, - end_date: Date.current + 1.day) + start_date: Date.current, + end_date: Date.current + 1.day) expect(leave_record.can_be_cancelled?).to be false end it 'returns false for past leaves' do leave_record = create(:leave_record, status: :approved, - start_date: 1.week.ago.to_date, - end_date: 3.days.ago.to_date) + start_date: 1.week.ago.to_date, + end_date: 3.days.ago.to_date) expect(leave_record.can_be_cancelled?).to be false end end @@ -76,7 +76,7 @@ RSpec.describe LeaveRecord, type: :model do describe '#overlaps_with?' do let(:base_leave_record) do create(:leave_record, start_date: Date.current + 10.days, - end_date: Date.current + 15.days) + end_date: Date.current + 15.days) end it 'returns false for non-LeaveRecord objects' do @@ -85,66 +85,66 @@ RSpec.describe LeaveRecord, type: :model do it 'detects overlapping leave records - same dates' do overlapping_leave_record = build(:leave_record, start_date: Date.current + 10.days, - end_date: Date.current + 15.days) + end_date: Date.current + 15.days) expect(base_leave_record.overlaps_with?(overlapping_leave_record)).to be true end it 'detects overlapping leave records - partial overlap start' do overlapping_leave_record = build(:leave_record, start_date: Date.current + 8.days, - end_date: Date.current + 12.days) + end_date: Date.current + 12.days) expect(base_leave_record.overlaps_with?(overlapping_leave_record)).to be true end it 'detects overlapping leave records - partial overlap end' do overlapping_leave_record = build(:leave_record, start_date: Date.current + 13.days, - end_date: Date.current + 18.days) + end_date: Date.current + 18.days) expect(base_leave_record.overlaps_with?(overlapping_leave_record)).to be true end it 'detects overlapping leave records - contained within' do overlapping_leave_record = build(:leave_record, start_date: Date.current + 12.days, - end_date: Date.current + 13.days) + end_date: Date.current + 13.days) expect(base_leave_record.overlaps_with?(overlapping_leave_record)).to be true end it 'detects overlapping leave records - contains other' do overlapping_leave_record = build(:leave_record, start_date: Date.current + 8.days, - end_date: Date.current + 18.days) + end_date: Date.current + 18.days) expect(base_leave_record.overlaps_with?(overlapping_leave_record)).to be true end it 'returns false for non-overlapping leave records - before' do non_overlapping_leave_record = build(:leave_record, start_date: Date.current + 5.days, - end_date: Date.current + 9.days) + end_date: Date.current + 9.days) expect(base_leave_record.overlaps_with?(non_overlapping_leave_record)).to be false end it 'returns false for non-overlapping leave records - after' do non_overlapping_leave_record = build(:leave_record, start_date: Date.current + 16.days, - end_date: Date.current + 20.days) + end_date: Date.current + 20.days) expect(base_leave_record.overlaps_with?(non_overlapping_leave_record)).to be false end it 'returns false for adjacent leave records - ending where other starts' do adjacent_leave_record = build(:leave_record, start_date: Date.current + 16.days, - end_date: Date.current + 20.days) + end_date: Date.current + 20.days) expect(base_leave_record.overlaps_with?(adjacent_leave_record)).to be false end it 'returns false for adjacent leave records - starting where other ends' do adjacent_leave_record = build(:leave_record, start_date: Date.current + 5.days, - end_date: Date.current + 9.days) + end_date: Date.current + 9.days) expect(base_leave_record.overlaps_with?(adjacent_leave_record)).to be false end end describe '#approve!' do let(:leave_record) { create(:leave_record, account: account, status: :pending) } - + before do # Ensure approver has administrator role in the account account_user = approver.account_users.find_by(account: account) - account_user.update!(role: :administrator) if account_user + account_user&.update!(role: :administrator) end it 'updates status to approved' do @@ -176,11 +176,11 @@ RSpec.describe LeaveRecord, type: :model do describe '#reject!' do let(:leave_record) { create(:leave_record, account: account, status: :pending) } - + before do # Ensure approver has administrator role in the account account_user = approver.account_users.find_by(account: account) - account_user.update!(role: :administrator) if account_user + account_user&.update!(role: :administrator) end it 'updates status to rejected' do @@ -284,4 +284,4 @@ RSpec.describe LeaveRecord, type: :model do end end end -end \ No newline at end of file +end diff --git a/spec/enterprise/policies/leave_record_policy_spec.rb b/spec/enterprise/policies/leave_record_policy_spec.rb index 67b17c372..0e69c73bf 100644 --- a/spec/enterprise/policies/leave_record_policy_spec.rb +++ b/spec/enterprise/policies/leave_record_policy_spec.rb @@ -90,8 +90,8 @@ RSpec.describe LeaveRecordPolicy, type: :policy do it 'allows destroy of approved future leave record' do approved_future_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: 1.week.from_now.to_date, - end_date: 2.weeks.from_now.to_date) + 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_record) expect(policy.destroy?).to be true end @@ -112,8 +112,8 @@ RSpec.describe LeaveRecordPolicy, type: :policy do it 'allows destroy of own approved future leave record' do approved_future_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: 1.week.from_now.to_date, - end_date: 2.weeks.from_now.to_date) + 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_record) expect(policy.destroy?).to be true end @@ -132,14 +132,14 @@ RSpec.describe LeaveRecordPolicy, type: :policy do context 'when user is administrator' do it 'denies destroy of current approved leave record' do approved_current_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: Date.current, end_date: Date.current + 2.days) + start_date: Date.current, end_date: Date.current + 2.days) policy = described_class.new(admin_context, approved_current_leave_record) expect(policy.destroy?).to be false end it 'denies destroy of past approved leave record' do approved_past_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) + start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) policy = described_class.new(admin_context, approved_past_leave_record) expect(policy.destroy?).to be false end @@ -154,14 +154,14 @@ RSpec.describe LeaveRecordPolicy, type: :policy do context 'when user owns the leave record' do it 'denies destroy of current approved leave record' do approved_current_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: Date.current, end_date: Date.current + 2.days) + start_date: Date.current, end_date: Date.current + 2.days) policy = described_class.new(agent_context, approved_current_leave_record) expect(policy.destroy?).to be false end it 'denies destroy of past approved leave record' do approved_past_leave_record = create(:leave_record, account: account, user: agent, status: :approved, - start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) + start_date: 1.week.ago.to_date, end_date: 3.days.ago.to_date) policy = described_class.new(agent_context, approved_past_leave_record) expect(policy.destroy?).to be false end @@ -245,4 +245,4 @@ RSpec.describe LeaveRecordPolicy, type: :policy do end end end -end \ No newline at end of file +end