user_out_of_office_test.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'test_helper'
  2. class UserOutOfOfficeTest < ActiveSupport::TestCase
  3. setup do
  4. UserInfo.current_user_id = 1
  5. groups = Group.all
  6. roles = Role.where(name: 'Agent')
  7. @agent1 = User.create_or_update(
  8. login: 'user-out_of_office-agent1@example.com',
  9. firstname: 'UserOutOfOffice',
  10. lastname: 'Agent1',
  11. email: 'user-out_of_office-agent1@example.com',
  12. password: 'agentpw',
  13. active: true,
  14. out_of_office: false,
  15. roles: roles,
  16. groups: groups,
  17. )
  18. @agent2 = User.create_or_update(
  19. login: 'user-out_of_office-agent2@example.com',
  20. firstname: 'UserOutOfOffice',
  21. lastname: 'Agent2',
  22. email: 'user-out_of_office-agent2@example.com',
  23. password: 'agentpw',
  24. active: true,
  25. out_of_office: false,
  26. roles: roles,
  27. groups: groups,
  28. )
  29. @agent3 = User.create_or_update(
  30. login: 'user-out_of_office-agent3@example.com',
  31. firstname: 'UserOutOfOffice',
  32. lastname: 'Agent3',
  33. email: 'user-out_of_office-agent3@example.com',
  34. password: 'agentpw',
  35. active: true,
  36. out_of_office: false,
  37. roles: roles,
  38. groups: groups,
  39. )
  40. end
  41. test 'check out_of_office?' do
  42. # check
  43. assert_not(@agent1.out_of_office?)
  44. assert_not(@agent2.out_of_office?)
  45. assert_not(@agent3.out_of_office?)
  46. assert_raises(Exceptions::UnprocessableEntity) do
  47. @agent1.out_of_office = true
  48. @agent1.out_of_office_start_at = Time.zone.now + 2.days
  49. @agent1.out_of_office_end_at = Time.zone.now
  50. @agent1.save!
  51. end
  52. assert_raises(Exceptions::UnprocessableEntity) do
  53. @agent1.out_of_office = true
  54. @agent1.out_of_office_start_at = Time.zone.now
  55. @agent1.out_of_office_end_at = Time.zone.now - 2.days
  56. @agent1.save!
  57. end
  58. assert_raises(Exceptions::UnprocessableEntity) do
  59. @agent1.out_of_office = true
  60. @agent1.out_of_office_start_at = nil
  61. @agent1.out_of_office_end_at = Time.zone.now
  62. @agent1.save!
  63. end
  64. assert_raises(Exceptions::UnprocessableEntity) do
  65. @agent1.out_of_office = true
  66. @agent1.out_of_office_start_at = Time.zone.now
  67. @agent1.out_of_office_end_at = nil
  68. @agent1.save!
  69. end
  70. @agent1.out_of_office = false
  71. @agent1.out_of_office_start_at = Time.zone.now + 2.days
  72. @agent1.out_of_office_end_at = Time.zone.now
  73. @agent1.save!
  74. assert_not(@agent1.out_of_office?)
  75. assert_raises(Exceptions::UnprocessableEntity) do
  76. @agent1.out_of_office = true
  77. @agent1.out_of_office_start_at = Time.zone.now + 2.days
  78. @agent1.out_of_office_end_at = Time.zone.now + 4.days
  79. @agent1.save!
  80. end
  81. assert_raises(Exceptions::UnprocessableEntity) do
  82. @agent1.out_of_office_replacement_id = 999_999_999_999 # not existing
  83. @agent1.save!
  84. end
  85. @agent1.out_of_office_replacement_id = @agent2.id
  86. @agent1.save!
  87. assert_not(@agent1.out_of_office?)
  88. travel 2.days
  89. assert(@agent1.out_of_office?)
  90. assert(@agent1.out_of_office_agent_of.blank?)
  91. assert_equal(1, @agent2.out_of_office_agent_of.count)
  92. assert_equal(@agent1.id, @agent2.out_of_office_agent_of[0].id)
  93. travel 1.day
  94. assert(@agent1.out_of_office?)
  95. travel 1.day
  96. assert(@agent1.out_of_office?)
  97. travel 1.day
  98. assert_not(@agent1.out_of_office?)
  99. assert_not(@agent1.out_of_office_agent)
  100. assert_not(@agent2.out_of_office_agent)
  101. assert_equal(0, @agent1.out_of_office_agent_of.count)
  102. assert_equal(0, @agent2.out_of_office_agent_of.count)
  103. @agent2.out_of_office = true
  104. @agent2.out_of_office_start_at = Time.zone.now
  105. @agent2.out_of_office_end_at = Time.zone.now + 4.days
  106. @agent2.out_of_office_replacement_id = @agent3.id
  107. @agent2.save!
  108. assert(@agent2.out_of_office?)
  109. assert_equal(@agent2.out_of_office_agent.id, @agent3.id)
  110. assert_equal(0, @agent1.out_of_office_agent_of.count)
  111. assert_equal(0, @agent2.out_of_office_agent_of.count)
  112. assert_equal(1, @agent3.out_of_office_agent_of.count)
  113. assert_equal(@agent2.id, @agent3.out_of_office_agent_of[0].id)
  114. travel 4.days
  115. assert_equal(0, @agent1.out_of_office_agent_of.count)
  116. assert_equal(0, @agent2.out_of_office_agent_of.count)
  117. assert_equal(1, @agent3.out_of_office_agent_of.count)
  118. assert_equal(@agent2.id, @agent3.out_of_office_agent_of[0].id)
  119. travel 1.day
  120. assert_equal(0, @agent1.out_of_office_agent_of.count)
  121. assert_equal(0, @agent2.out_of_office_agent_of.count)
  122. assert_equal(0, @agent3.out_of_office_agent_of.count)
  123. end
  124. end