amount_check_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe MonitoringHelper::AmountCheck do
  4. let(:instance) { described_class.new(params) }
  5. let(:ticket_now) { create(:ticket) }
  6. let(:ticket_10m) { travel_to(10.minutes.ago) { create(:ticket) } }
  7. let(:ticket_5d) { travel_to(5.days.ago) { create(:ticket) } }
  8. let(:ticket_2w) { travel_to(2.weeks.ago) { create(:ticket) } }
  9. let(:params) { {} }
  10. before do
  11. Ticket.destroy_all
  12. end
  13. describe '#check_amount' do
  14. it 'raises error if no params given' do
  15. expect { instance.check_amount }.to raise_error %r{periode is missing}
  16. end
  17. context 'when period given' do
  18. let(:params) { { periode: '1w' } }
  19. it 'returns count if only period given' do
  20. allow(instance).to receive(:ticket_count).and_return(2)
  21. expect(instance.check_amount).to eq({ count: 2 })
  22. end
  23. end
  24. context 'when checks given' do
  25. let(:params) do
  26. {
  27. periode: '1w',
  28. min_warning: 2
  29. }
  30. end
  31. it 'returns failure message if at least one check fails' do
  32. allow(instance).to receive(:ticket_count).and_return(1)
  33. expect(instance.check_amount)
  34. .to eq({ state: 'warning', message: 'The minimum of 2 was undercut by 1 in the last 1w', count: 1 })
  35. end
  36. it 'returns state and count if all checks pass' do
  37. allow(instance).to receive(:ticket_count).and_return(5)
  38. expect(instance.check_amount).to eq({ state: 'ok', count: 5 })
  39. end
  40. end
  41. end
  42. describe '#given_periode' do
  43. let(:params) { { periode: :test } }
  44. it 'returns period from params' do
  45. expect(instance.send(:given_periode)).to eq :test
  46. end
  47. end
  48. describe '#given_params' do
  49. it 'returns empty array if no params given' do
  50. expect(instance.send(:given_params)).to be_blank
  51. end
  52. context 'when valid params given' do
  53. let(:params) { { min_critical: 123 } }
  54. it 'returns passed params' do
  55. expect(instance.send(:given_params))
  56. .to eq([[{ notice: 'critical', param: :min_critical, type: 'lt' }, 123]])
  57. end
  58. end
  59. context 'when invalid params given' do
  60. let(:params) { { min_critical: 'abc' } }
  61. it 'raises error if param has non-integer value' do
  62. expect { instance.send(:given_params) }.to raise_error %r{needs to be an integer}
  63. end
  64. end
  65. end
  66. describe '#created_at_threshold' do
  67. before do
  68. freeze_time
  69. end
  70. it 'raises error if period missing' do
  71. expect { instance.send(:created_at_threshold) }.to raise_error %r{periode is missing}
  72. end
  73. it 'raises error if period is invalid' do
  74. instance.params[:periode] = '1z'
  75. expect { instance.send(:created_at_threshold) }.to raise_error %r{periode needs to have }
  76. end
  77. it 'raises error if period length is not included' do
  78. instance.params[:periode] = 'zd'
  79. expect { instance.send(:created_at_threshold) }.to raise_error %r{periode needs to be an integer}
  80. end
  81. it 'returns period in seconds' do
  82. instance.params[:periode] = '3s'
  83. expect(instance.send(:created_at_threshold)).to eq 3.seconds.ago
  84. end
  85. it 'returns period in minutes' do
  86. instance.params[:periode] = '1m'
  87. expect(instance.send(:created_at_threshold)).to eq 1.minute.ago
  88. end
  89. it 'returns period in hours' do
  90. instance.params[:periode] = '9h'
  91. expect(instance.send(:created_at_threshold)).to eq 9.hours.ago
  92. end
  93. it 'returns period in days' do
  94. instance.params[:periode] = '2d'
  95. expect(instance.send(:created_at_threshold)).to eq 2.days.ago
  96. end
  97. end
  98. describe '#ticket_count' do
  99. it 'returns ticket-count in given period' do
  100. ticket_10m && ticket_5d && ticket_2w
  101. allow(instance).to receive(:created_at_threshold).and_return(1.week.ago)
  102. expect(instance.send(:ticket_count)).to eq 2
  103. end
  104. end
  105. describe '#check_single_row' do
  106. let(:params) { { periode: '1w' } }
  107. before do
  108. allow(instance).to receive(:ticket_count).and_return(5)
  109. end
  110. context 'when checking if ticket count >= threshold' do
  111. let(:check) { { type: 'gt', notice: 'warning' } }
  112. it 'returns nil when check passes' do
  113. expect(instance.send(:check_single_row, check, 10)).to be_nil
  114. end
  115. it 'returns error when check fails' do
  116. expect(instance.send(:check_single_row, check, 4)).to include(state: 'warning', count: 5, message: match(%r{was exceeded with}))
  117. end
  118. end
  119. context 'when checking if ticket count <= threshold' do
  120. let(:check) { { type: 'lt', notice: 'critical' } }
  121. it 'returns nil when check passes' do
  122. expect(instance.send(:check_single_row, check, 4)).to be_nil
  123. end
  124. it 'returns error when check fails' do
  125. expect(instance.send(:check_single_row, check, 10)).to include(state: 'critical', count: 5, message: match(%r{was undercut by}))
  126. end
  127. end
  128. end
  129. end