out_of_office_validator_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Validations::OutOfOfficeValidator do
  4. subject(:validator) { described_class.new }
  5. let(:record) { create(:agent, :ooo, ooo_agent: other_agent) }
  6. let(:other_agent) { create(:user) }
  7. before { record.out_of_office = out_of_office }
  8. context 'when out of office is set to true' do
  9. let(:out_of_office) { true }
  10. it 'requires start date' do
  11. record.out_of_office_start_at = nil
  12. validator.validate(record)
  13. expect(record.errors).to be_added(:out_of_office_start_at, :blank)
  14. end
  15. it 'requires end date' do
  16. record.out_of_office_end_at = nil
  17. validator.validate(record)
  18. expect(record.errors).to be_added(:out_of_office_end_at, :blank)
  19. end
  20. it 'requires logical date range' do
  21. record.out_of_office_start_at = Date.parse('2011-03-03')
  22. record.out_of_office_end_at = Date.parse('2011-02-03')
  23. validator.validate(record)
  24. expect(record.errors.full_messages).to include(%r{has to be earlier than or equal to})
  25. end
  26. it 'allows single day range' do
  27. record.out_of_office_start_at = Date.parse('2011-03-03')
  28. record.out_of_office_end_at = Date.parse('2011-03-03')
  29. validator.validate(record)
  30. expect(record.errors).to be_blank
  31. end
  32. it 'requires replacement agent' do
  33. record.out_of_office_replacement_id = nil
  34. validator.validate(record)
  35. expect(record.errors).to be_added(:out_of_office_replacement_id, :blank)
  36. end
  37. it 'requires valid replacement agent' do
  38. record.out_of_office_replacement_id = User.maximum(:id).next
  39. validator.validate(record)
  40. expect(record.errors).to be_added(:out_of_office_replacement_id, :invalid)
  41. end
  42. end
  43. context 'when out of office is set to false' do
  44. let(:out_of_office) { false }
  45. it 'does not require start date' do
  46. record.out_of_office_start_at = nil
  47. validator.validate(record)
  48. expect(record.errors).to be_blank
  49. end
  50. it 'does not require end date' do
  51. record.out_of_office_end_at = nil
  52. validator.validate(record)
  53. expect(record.errors).to be_blank
  54. end
  55. it 'does not check date range' do
  56. record.out_of_office_start_at = Date.parse('2011-02-03')
  57. record.out_of_office_end_at = Date.parse('2011-03-03')
  58. validator.validate(record)
  59. expect(record.errors).to be_blank
  60. end
  61. it 'does not require replacement agent' do
  62. record.out_of_office_replacement_id = nil
  63. validator.validate(record)
  64. expect(record.errors).to be_blank
  65. end
  66. it 'allows to set dates and replacement agent' do
  67. validator.validate(record)
  68. expect(record.errors).to be_blank
  69. end
  70. it 'prevents assigning non existant user' do
  71. record.out_of_office_replacement_id = User.maximum(:id).next
  72. validator.validate(record)
  73. expect(record.errors).to be_added(:out_of_office_replacement_id, :invalid)
  74. end
  75. end
  76. end