status_spec.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe MonitoringHelper::Status do
  4. let(:instance) { described_class.new }
  5. describe '#fetch_status' do
  6. before do
  7. allow(instance).to receive_messages(counts: :count, last_created_at: :created, last_login: :login, agents_count: :agents, storage: :storage_size)
  8. end
  9. it 'returns output of computation methods' do
  10. expect(instance.fetch_status).to include(
  11. counts: :count, last_created_at: :created, last_login: :login, agents: :agents, storage: :storage_size
  12. )
  13. end
  14. end
  15. describe '#last_login' do
  16. it 'returns nil if no last login' do
  17. expect(instance.send(:last_login)).to be_nil
  18. end
  19. it 'returns latest last_login' do
  20. freeze_time
  21. create(:agent, last_login: 2.days.ago)
  22. create(:agent, last_login: 1.day.ago)
  23. expect(instance.send(:last_login)).to eq 1.day.ago
  24. end
  25. end
  26. describe '#agents_count' do
  27. it 'returns count of agents' do
  28. create_list(:agent, 5)
  29. create_list(:customer, 5)
  30. expect(instance.send(:agents_count)).to be 5
  31. end
  32. end
  33. describe '#counts' do
  34. include_context 'basic Knowledge Base'
  35. before do
  36. stub_const("#{described_class}::INCLUDE_CLASSES", [KnowledgeBase::Category, KnowledgeBase::Answer])
  37. end
  38. it 'counts items' do
  39. published_answer && draft_answer
  40. expect(instance.send(:counts)).to eq({ 'knowledge_base_categories' => 1, 'knowledge_base_answers' => 2 })
  41. end
  42. end
  43. describe '#last_created_at' do
  44. include_context 'basic Knowledge Base'
  45. before do
  46. stub_const("#{described_class}::INCLUDE_CLASSES", [KnowledgeBase::Category, KnowledgeBase::Answer])
  47. freeze_time
  48. category
  49. travel 12.hours
  50. published_answer
  51. travel 12.hours
  52. draft_answer
  53. end
  54. it 'counts items' do
  55. expect(instance.send(:last_created_at)).to eq({ 'knowledge_base_categories' => 1.day.ago, 'knowledge_base_answers' => Time.current })
  56. end
  57. end
  58. describe '#storage' do
  59. case ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
  60. when 'postgresql'
  61. it 'returns size of attached files' do
  62. 20.times { create(:store).update! size: 65_000_000 }
  63. expect(instance.send(:storage)).to include({ kB: 1_269_531, MB: 1239, GB: 1 })
  64. end
  65. it 'returns nil if no files attached' do # rubocop:disable RSpec/RepeatedExample
  66. expect(instance.send(:storage)).to be_nil
  67. end
  68. when 'mysql'
  69. it 'returns nil' do # rubocop:disable RSpec/RepeatedExample
  70. expect(instance.send(:storage)).to be_nil
  71. end
  72. end
  73. end
  74. end