123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776 |
- require 'rails_helper'
- RSpec.describe 'Monitoring', type: :request do
- let!(:admin) do
- create(:admin, groups: Group.all)
- end
- let!(:agent) do
- create(:agent, groups: Group.all)
- end
- let!(:customer) do
- create(:customer)
- end
- let!(:token) do
- SecureRandom.urlsafe_base64(64)
- end
- before do
- Setting.set('monitoring_token', token)
-
- Channel.where.not(area: 'Email::Notification').destroy_all
- Channel.all.each do |channel|
- channel.status_in = 'ok'
- channel.status_out = 'ok'
- channel.last_log_in = nil
- channel.last_log_out = nil
- channel.save!
- end
- dir = Rails.root.join('tmp/unprocessable_mail')
- Dir.glob("#{dir}/*.eml") do |entry|
- File.delete(entry)
- end
- Scheduler.where(active: true).each do |scheduler|
- scheduler.last_run = Time.zone.now
- scheduler.save!
- end
- permission = Permission.find_by(name: 'admin.monitoring')
- permission.active = true
- permission.save!
- end
- describe 'request handling' do
- it 'does monitoring without token' do
-
- get '/api/v1/monitoring/health_check', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['healthy']).to be_falsey
- expect(json_response['error']).to eq('Not authorized')
-
- get '/api/v1/monitoring/status', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['agents']).to be_falsey
- expect(json_response['last_login']).to be_falsey
- expect(json_response['counts']).to be_falsey
- expect(json_response['last_created_at']).to be_falsey
- expect(json_response['error']).to eq('Not authorized')
-
- post '/api/v1/monitoring/token', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Authentication required')
- end
- it 'does monitoring with wrong token' do
-
- get '/api/v1/monitoring/health_check?token=abc', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['healthy']).to be_falsey
- expect(json_response['error']).to eq('Not authorized')
-
- get '/api/v1/monitoring/status?token=abc', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['agents']).to be_falsey
- expect(json_response['last_login']).to be_falsey
- expect(json_response['counts']).to be_falsey
- expect(json_response['last_created_at']).to be_falsey
- expect(json_response['error']).to eq('Not authorized')
-
- post '/api/v1/monitoring/token', params: { token: 'abc' }, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Authentication required')
- end
- it 'does monitoring with correct token' do
-
- string = ''
- 1000.times do
- string += 'Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text'
- end
- Store.add(
- object: 'User',
- o_id: 1,
- data: string,
- filename: 'filename.txt',
- created_by_id: 1,
- )
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response['issues']).to eq([])
- expect(json_response['healthy']).to eq(true)
- expect(json_response['message']).to eq('success')
-
- get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response).to be_key('agents')
- expect(json_response).to be_key('last_login')
- expect(json_response).to be_key('counts')
- expect(json_response).to be_key('last_created_at')
- first_json_response_kb = 0
- if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
- expect(json_response['storage']).to be_truthy
- expect(json_response['storage']).to be_key('kB')
- expect(json_response['storage']['kB']).to be > 0
- expect(json_response['storage']).to be_key('MB')
- expect(json_response['storage']).to be_key('GB')
- first_json_response_kb = json_response['storage']['kB']
- else
- expect(json_response['storage']).to be_falsey
- end
-
- Store.add(
- object: 'User',
- o_id: 1,
- data: string,
- filename: 'filename.txt',
- created_by_id: 1,
- )
-
- get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response).to be_key('agents')
- expect(json_response).to be_key('last_login')
- expect(json_response).to be_key('counts')
- expect(json_response).to be_key('last_created_at')
- if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
- expect(json_response['storage']).to be_truthy
- expect(json_response['storage']).to be_key('kB')
-
- expect(json_response['storage']['kB']).to eq(first_json_response_kb * 2)
- expect(json_response['storage']).to be_key('MB')
- expect(json_response['storage']).to be_key('GB')
- else
- expect(json_response['storage']).to be_falsey
- end
- Store.add(
- object: 'User',
- o_id: 1,
- data: "#{string}123",
- filename: 'filename2.txt',
- created_by_id: 1,
- )
-
- get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response).to be_key('agents')
- expect(json_response).to be_key('last_login')
- expect(json_response).to be_key('counts')
- expect(json_response).to be_key('last_created_at')
- if ActiveRecord::Base.connection_config[:adapter] == 'postgresql'
- expect(json_response['storage']).to be_truthy
- expect(json_response['storage']).to be_key('kB')
-
- expect(json_response['storage']['kB']).to be > first_json_response_kb
- expect(json_response['storage']).to be_key('MB')
- expect(json_response['storage']).to be_key('GB')
- else
- expect(json_response['storage']).to be_falsey
- end
-
- post '/api/v1/monitoring/token', params: { token: token }, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Authentication required')
- end
- it 'does monitoring with admin user' do
-
- authenticated_as(admin)
- get '/api/v1/monitoring/health_check', params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response['healthy']).to eq(true)
- expect(json_response['message']).to eq('success')
-
- get '/api/v1/monitoring/status', params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response).to be_key('agents')
- expect(json_response).to be_key('last_login')
- expect(json_response).to be_key('counts')
- expect(json_response).to be_key('last_created_at')
-
- post '/api/v1/monitoring/token', params: { token: token }, as: :json
- expect(response).to have_http_status(:created)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_truthy
- expect(json_response['error']).to be_falsey
- end
- it 'does monitoring with agent user' do
-
- authenticated_as(agent)
- get '/api/v1/monitoring/health_check', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['healthy']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
-
- get '/api/v1/monitoring/status', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['agents']).to be_falsey
- expect(json_response['last_login']).to be_falsey
- expect(json_response['counts']).to be_falsey
- expect(json_response['last_created_at']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
-
- post '/api/v1/monitoring/token', params: { token: token }, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
- end
- it 'does monitoring with admin user and invalid permission' do
- permission = Permission.find_by(name: 'admin.monitoring')
- permission.active = false
- permission.save!
-
- authenticated_as(admin)
- get '/api/v1/monitoring/health_check', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['healthy']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
-
- get '/api/v1/monitoring/status', params: {}, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['agents']).to be_falsey
- expect(json_response['last_login']).to be_falsey
- expect(json_response['counts']).to be_falsey
- expect(json_response['last_created_at']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
-
- post '/api/v1/monitoring/token', params: { token: token }, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Not authorized (user)!')
- permission.active = true
- permission.save!
- end
- it 'does monitoring with correct token and invalid permission' do
- permission = Permission.find_by(name: 'admin.monitoring')
- permission.active = false
- permission.save!
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response['healthy']).to eq(true)
- expect(json_response['message']).to eq('success')
-
- get "/api/v1/monitoring/status?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['error']).to be_falsey
- expect(json_response).to be_key('agents')
- expect(json_response).to be_key('last_login')
- expect(json_response).to be_key('counts')
- expect(json_response).to be_key('last_created_at')
-
- post '/api/v1/monitoring/token', params: { token: token }, as: :json
- expect(response).to have_http_status(:forbidden)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['token']).to be_falsey
- expect(json_response['error']).to eq('Authentication required')
- permission.active = true
- permission.save!
- end
- it 'does check health false' do
- channel = Channel.find_by(active: true)
- channel.status_in = 'ok'
- channel.status_out = 'error'
- channel.last_log_in = nil
- channel.last_log_out = nil
- channel.save!
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq('Channel: Email::Notification out ')
-
- scheduler = Scheduler.where(active: true).last
- scheduler.last_run = Time.zone.now - 20.minutes
- scheduler.period = 600
- scheduler.save!
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;scheduler may not run (last execution of #{scheduler.method} 10 minutes over) - please contact your system administrator")
-
- scheduler = Scheduler.where(active: true).last
- scheduler.last_run = Time.zone.now - 1.day
- scheduler.period = 600
- scheduler.save!
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;scheduler may not run (last execution of #{scheduler.method} about 24 hours over) - please contact your system administrator")
-
- travel 2.seconds
- 8001.times do |fake_ticket_id|
- SearchIndexJob.perform_later('Ticket', fake_ticket_id)
- end
- Scheduler.where(active: true).each do |local_scheduler|
- local_scheduler.last_run = Time.zone.now
- local_scheduler.save!
- end
- total_jobs = Delayed::Job.count
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq('Channel: Email::Notification out ')
- travel 20.minutes
- Scheduler.where(active: true).each do |local_scheduler|
- local_scheduler.last_run = Time.zone.now
- local_scheduler.save!
- end
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;#{total_jobs} background jobs in queue")
- Delayed::Job.delete_all
- travel_back
-
- dir = Rails.root.join('tmp/unprocessable_mail')
- FileUtils.mkdir_p(dir)
- FileUtils.touch("#{dir}/test.eml")
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq('Channel: Email::Notification out ;unprocessable mails: 1')
-
- Setting.set('ldap_integration', true)
- ImportJob.create(
- name: 'Import::Ldap',
- started_at: Time.zone.now,
- finished_at: Time.zone.now,
- result: {
- error: 'Some bad error'
- }
- )
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;unprocessable mails: 1;Failed to run import backend 'Import::Ldap'. Cause: Some bad error")
- stuck_updated_at_timestamp = 15.minutes.ago
- ImportJob.create(
- name: 'Import::Ldap',
- started_at: Time.zone.now,
- finished_at: nil,
- updated_at: stuck_updated_at_timestamp,
- )
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;unprocessable mails: 1;Failed to run import backend 'Import::Ldap'. Cause: Some bad error;Stuck import backend 'Import::Ldap' detected. Last update: #{stuck_updated_at_timestamp}")
- privacy_stuck_updated_at_timestamp = 30.minutes.ago
- task = create(:data_privacy_task, deletable: customer)
- task.update(updated_at: privacy_stuck_updated_at_timestamp)
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Channel: Email::Notification out ;unprocessable mails: 1;Failed to run import backend 'Import::Ldap'. Cause: Some bad error;Stuck import backend 'Import::Ldap' detected. Last update: #{stuck_updated_at_timestamp};Stuck data privacy task (ID #{task.id}) detected. Last update: #{privacy_stuck_updated_at_timestamp}")
- Setting.set('ldap_integration', false)
- end
- it 'does check restart_failed_jobs' do
- authenticated_as(admin)
- post '/api/v1/monitoring/restart_failed_jobs', params: {}, as: :json
- expect(response).to have_http_status(:ok)
- end
- it 'does check failed delayed job', db_strategy: :reset do
-
- prev_es_config = Setting.get('es_url')
- Setting.set('es_url', 'http://127.0.0.1:92001')
-
-
- Delayed::Job.destroy_all
-
- object = create(:object_manager_attribute_text, name: 'test4')
- migration = ObjectManager::Attribute.migration_execute
- expect(true).to eq(migration)
- authenticated_as(admin)
- post "/api/v1/object_manager_attributes/#{object.id}", params: {}, as: :json
- token = @response.headers['CSRF-TOKEN']
-
- params = {
- name: 'test4',
- object: 'Ticket',
- display: 'Test 4',
- active: true,
- data_type: 'input',
- data_option: {
- default: 'test',
- type: 'text',
- maxlength: 120
- },
- screens: {
- create_middle: {
- 'ticket.customer': {
- shown: true,
- item_class: 'column'
- },
- 'ticket.agent': {
- shown: true,
- item_class: 'column'
- }
- },
- edit: {
- 'ticket.customer': {
- shown: true
- },
- 'ticket.agent': {
- shown: true
- }
- }
- },
- id: 'c-196'
- }
-
- put "/api/v1/object_manager_attributes/#{object.id}", params: params, as: :json
- migration = ObjectManager::Attribute.migration_execute
- expect(true).to eq(migration)
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_truthy
- expect(json_response['data_option']['null']).to be_truthy
- expect('test4').to eq(json_response['name'])
- expect('Test 4').to eq(json_response['display'])
- 4.times do
- Delayed::Worker.new.work_off
- end
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #2 'SearchIndexJob' 1 time(s) with 1 attempt(s).")
-
- manual_added = SearchIndexJob.perform_later('Ticket', 1)
- Delayed::Job.find(manual_added.provider_job_id).update!(attempts: 10)
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect( json_response['message']).to eq("Failed to run background job #1 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #2 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
-
- dummy_class = Class.new(ApplicationJob) do
- def perform
- puts 'work work'
- end
- end
- manual_added = Delayed::Job.enqueue( dummy_class.new )
- manual_added.update!(attempts: 5)
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("Failed to run background job #1 'Object' 1 time(s) with 5 attempt(s).;Failed to run background job #2 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #3 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
-
- Setting.set('es_url', prev_es_config)
-
- 10.times do
- manual_added = Delayed::Job.enqueue( dummy_class.new )
- manual_added.update!(attempts: 5)
- end
-
- get "/api/v1/monitoring/health_check?token=#{token}", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['message']).to be_truthy
- expect(json_response['issues']).to be_truthy
- expect(json_response['healthy']).to eq(false)
- expect(json_response['message']).to eq("14 failing background jobs;Failed to run background job #1 'Object' 7 time(s) with 35 attempt(s).;Failed to run background job #2 'SearchIndexAssociationsJob' 1 time(s) with 1 attempt(s).;Failed to run background job #3 'SearchIndexJob' 2 time(s) with 11 attempt(s).")
-
- Delayed::Job.delete_all
- end
- it 'does check amount' do
- Ticket.destroy_all
-
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response.key?('state')).to eq(false)
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(0)
- Ticket.destroy_all
- (1..6).each do |i|
- create(:ticket, title: "Ticket-#{i}")
- travel 10.seconds
- end
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&min_warning=10&min_critical=8", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('critical')
- expect(json_response['message']).to eq('The minimum of 8 was undercut by 6 in the last 1h')
- expect(json_response['count']).to eq(6)
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&min_warning=7&min_critical=2", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('warning')
- expect(json_response['message']).to eq('The minimum of 7 was undercut by 6 in the last 1h')
- expect(json_response['count']).to eq(6)
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('ok')
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(6)
- (1..6).each do |i|
- create(:ticket, title: "Ticket-#{i}")
- travel 1.second
- end
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('warning')
- expect(json_response['message']).to eq('The limit of 10 was exceeded with 12 in the last 1h')
- expect(json_response['count']).to eq(12)
- (1..10).each do |i|
- create(:ticket, title: "Ticket-#{i}")
- travel 1.second
- end
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=10&max_critical=20", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('critical')
- expect(json_response['message']).to eq('The limit of 20 was exceeded with 22 in the last 1h')
- expect(json_response['count']).to eq(22)
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('ok')
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(22)
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response.key?('state')).to eq(false)
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(22)
- travel 2.hours
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h&max_warning=30", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response['state']).to eq('ok')
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(0)
- get "/api/v1/monitoring/amount_check?token=#{token}&periode=1h", params: {}, as: :json
- expect(response).to have_http_status(:ok)
- expect(json_response).to be_a_kind_of(Hash)
- expect(json_response.key?('state')).to eq(false)
- expect(json_response.key?('message')).to eq(false)
- expect(json_response['count']).to eq(0)
- end
- end
- end
|