monitoring_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Monitoring', authenticated_as: :admin, type: :request do
  4. let(:access_token) { SecureRandom.urlsafe_base64(64) }
  5. let(:admin) { create(:admin, groups: Group.all) }
  6. let(:agent) { create(:agent, groups: Group.all) }
  7. before do
  8. Setting.set('monitoring_token', access_token)
  9. end
  10. def make_call(params = {})
  11. send(method, url, params: params, as: :json)
  12. end
  13. shared_examples 'accessible' do |token:, admin:, agent:|
  14. it "verify token #{token ? 'allows' : 'denies'} access", authenticated_as: false do
  15. make_call({ token: access_token })
  16. expect(response).to have_http_status(token ? :success : :forbidden)
  17. end
  18. if token
  19. it 'verify wrong token denies access', authenticated_as: false do
  20. make_call({ token: 'asd' })
  21. expect(response).to have_http_status(:forbidden)
  22. end
  23. end
  24. it "verify admin #{admin ? 'allows' : 'denies'} access", authenticated_as: :admin do
  25. make_call
  26. expect(response).to have_http_status(admin ? :success : :forbidden)
  27. end
  28. it "verify agent #{agent ? 'allows' : 'denies'} access", authenticated_as: :agent do
  29. make_call
  30. expect(response).to have_http_status(agent ? :success : :forbidden)
  31. end
  32. end
  33. describe '#health_check' do
  34. let(:url) { '/api/v1/monitoring/health_check' }
  35. let(:method) { 'get' }
  36. let(:successful_response) do
  37. resp = MonitoringHelper::HealthChecker::Response.new
  38. resp.issues << :issues
  39. resp.actions << :actions
  40. resp
  41. end
  42. it_behaves_like 'accessible', token: true, admin: true, agent: false
  43. context 'when logged in as admin' do
  44. it 'includes the token in the response' do
  45. make_call
  46. expect(json_response).to include('token' => access_token)
  47. end
  48. end
  49. context 'when using the token URL', authenticated_as: false do
  50. it 'does not echo the token in the response' do
  51. make_call
  52. expect(json_response).not_to have_key 'token'
  53. end
  54. end
  55. it 'returns health status' do
  56. allow_any_instance_of(MonitoringHelper::HealthChecker)
  57. .to receive(:response)
  58. .and_return(successful_response)
  59. make_call
  60. expect(json_response).to include('healthy' => false, 'message' => 'issues', 'issues' => ['issues'], 'actions' => ['actions'])
  61. end
  62. end
  63. describe '#status' do
  64. let(:url) { '/api/v1/monitoring/status' }
  65. let(:method) { 'get' }
  66. it_behaves_like 'accessible', token: true, admin: true, agent: false
  67. it 'returns status' do
  68. allow_any_instance_of(MonitoringHelper::Status)
  69. .to receive(:fetch_status)
  70. .and_return({ status_hash: :sample })
  71. make_call
  72. expect(json_response).to include('status_hash' => 'sample')
  73. end
  74. end
  75. describe '#amount_check' do
  76. let(:url) { '/api/v1/monitoring/amount_check' }
  77. let(:method) { 'get' }
  78. before do
  79. allow_any_instance_of(MonitoringHelper::AmountCheck).to receive(:check_amount).and_return({})
  80. end
  81. it_behaves_like 'accessible', token: true, admin: true, agent: false
  82. it 'returns amount' do
  83. allow_any_instance_of(MonitoringHelper::AmountCheck)
  84. .to receive(:check_amount)
  85. .and_return({ amount_hash: :sample })
  86. make_call
  87. expect(json_response).to include('amount_hash' => 'sample')
  88. end
  89. end
  90. describe '#token' do
  91. let(:url) { '/api/v1/monitoring/token' }
  92. let(:method) { 'post' }
  93. it_behaves_like 'accessible', token: false, admin: true, agent: false
  94. it 'returns token' do
  95. make_call
  96. expect(json_response).to include('token' => match(%r{^\S{54}$}))
  97. end
  98. it 'sets new token' do
  99. expect { make_call }.to change { Setting.get('monitoring_token') }.from(access_token)
  100. end
  101. end
  102. describe '#restart_failed_jobs' do
  103. let(:url) { '/api/v1/monitoring/restart_failed_jobs' }
  104. let(:method) { 'post' }
  105. it_behaves_like 'accessible', token: false, admin: true, agent: false
  106. it 'returns token' do
  107. allow(Scheduler).to receive(:restart_failed_jobs)
  108. make_call
  109. expect(Scheduler).to have_received(:restart_failed_jobs)
  110. end
  111. end
  112. end