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