channels_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe MonitoringHelper::HealthChecker::Channels do
  4. let(:instance) { described_class.new }
  5. let(:channel1) { create(:email_channel, area: 'testarea') }
  6. let(:channel2) { create(:email_channel) }
  7. before do
  8. Channel.destroy_all
  9. end
  10. describe '#check_health' do
  11. it 'calls channel check' do
  12. channel1
  13. allow(instance).to receive(:single_channel_check)
  14. instance.check_health
  15. expect(instance).to have_received(:single_channel_check).with(channel1)
  16. end
  17. end
  18. describe '#scope' do
  19. it 'returns active channels' do
  20. channel1
  21. channel2.update!(active: false)
  22. expect(instance.send(:scope)).to eq [channel1]
  23. end
  24. end
  25. describe '#single_channel_check' do
  26. before do
  27. allow(instance).to receive(:status_in)
  28. allow(instance).to receive(:status_out)
  29. allow(instance).to receive(:last_fetch)
  30. instance.send(:single_channel_check, channel1)
  31. end
  32. it 'checks status in' do
  33. expect(instance).to have_received(:status_in).with(channel1)
  34. end
  35. it 'checks status out' do
  36. expect(instance).to have_received(:status_out).with(channel1)
  37. end
  38. it 'checks status last fetch' do
  39. expect(instance).to have_received(:last_fetch).with(channel1)
  40. end
  41. end
  42. describe '#status_in' do
  43. it 'does nothing if status is not error' do
  44. instance.send(:status_in, channel1)
  45. expect(instance.response.issues).to be_blank
  46. end
  47. it 'adds issue if status is error' do
  48. channel1.status_in = 'error'
  49. instance.send(:status_in, channel1)
  50. expect(instance.response.issues).to be_present
  51. end
  52. end
  53. describe '#status_out' do
  54. it 'does nothing if status is not error' do
  55. instance.send(:status_out, channel1)
  56. expect(instance.response.issues).to be_blank
  57. end
  58. it 'adds issue if status is error' do
  59. channel1.status_out = 'error'
  60. instance.send(:status_out, channel1)
  61. expect(instance.response.issues).to be_present
  62. end
  63. end
  64. describe '#status_message' do
  65. it 'includes channel name and direction' do
  66. message = instance.send(:status_message, channel1, :dir)
  67. expect(message).to start_with('Channel: testarea dir')
  68. end
  69. it 'includes present keys' do
  70. channel1.options = { 'host' => 'example.com', 'uid' => 123 }
  71. message = instance.send(:status_message, channel1, :dir)
  72. expect(message).to end_with('host:example.com;uid:123;')
  73. end
  74. end
  75. describe '#last_fetch' do
  76. it 'does nothing if last fetch is not present' do
  77. channel1.preferences['last_fetch'] = nil
  78. instance.send(:last_fetch, channel1)
  79. expect(instance.response.issues).to be_blank
  80. end
  81. it 'does nothing if last fetch is within tolerance' do
  82. channel1.preferences['last_fetch'] = 30.minutes.ago
  83. instance.send(:last_fetch, channel1)
  84. expect(instance.response.issues).to be_blank
  85. end
  86. it 'adds issue if last fetch is too long ago' do
  87. channel1.preferences['last_fetch'] = 30.hours.ago
  88. instance.send(:last_fetch, channel1)
  89. expect(instance.response.issues.first).to end_with('is active but not fetched for 1 day')
  90. end
  91. end
  92. end