monitoring_controller_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class MonitoringControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # set token
  8. @token = SecureRandom.urlsafe_base64(64)
  9. Setting.set('monitoring_token', @token)
  10. # create agent
  11. roles = Role.where(name: %w(Admin Agent))
  12. groups = Group.all
  13. # channel cleanup
  14. Channel.where.not(area: 'Email::Notification').destroy_all
  15. Channel.all.each { |channel|
  16. channel.status_in = 'ok'
  17. channel.status_out = 'ok'
  18. channel.last_log_in = nil
  19. channel.last_log_out = nil
  20. channel.save!
  21. }
  22. dir = "#{Rails.root}/tmp/unprocessable_mail"
  23. Dir.glob("#{dir}/*.eml") do |entry|
  24. File.delete(entry)
  25. end
  26. Scheduler.where(active: true).each { |scheduler|
  27. scheduler.last_run = Time.zone.now
  28. scheduler.save!
  29. }
  30. permission = Permission.find_by(name: 'admin.monitoring')
  31. permission.active = true
  32. permission.save!
  33. UserInfo.current_user_id = 1
  34. @admin = User.create_or_update(
  35. login: 'monitoring-admin',
  36. firstname: 'Monitoring',
  37. lastname: 'Admin',
  38. email: 'monitoring-admin@example.com',
  39. password: 'adminpw',
  40. active: true,
  41. roles: roles,
  42. groups: groups,
  43. )
  44. # create agent
  45. roles = Role.where(name: 'Agent')
  46. @agent = User.create_or_update(
  47. login: 'monitoring-agent@example.com',
  48. firstname: 'Monitoring',
  49. lastname: 'Agent',
  50. email: 'monitoring-agent@example.com',
  51. password: 'agentpw',
  52. active: true,
  53. roles: roles,
  54. groups: groups,
  55. )
  56. # create customer without org
  57. roles = Role.where(name: 'Customer')
  58. @customer_without_org = User.create_or_update(
  59. login: 'monitoring-customer1@example.com',
  60. firstname: 'Monitoring',
  61. lastname: 'Customer1',
  62. email: 'monitoring-customer1@example.com',
  63. password: 'customer1pw',
  64. active: true,
  65. roles: roles,
  66. )
  67. end
  68. test '01 monitoring without token' do
  69. # health_check
  70. get '/api/v1/monitoring/health_check', {}, @headers
  71. assert_response(401)
  72. result = JSON.parse(@response.body)
  73. assert_equal(Hash, result.class)
  74. assert_not(result['healthy'])
  75. assert_equal('Not authorized', result['error'])
  76. # status
  77. get '/api/v1/monitoring/status', {}, @headers
  78. assert_response(401)
  79. result = JSON.parse(@response.body)
  80. assert_equal(Hash, result.class)
  81. assert_not(result['agents'])
  82. assert_not(result['last_login'])
  83. assert_not(result['counts'])
  84. assert_not(result['last_created_at'])
  85. assert_equal('Not authorized', result['error'])
  86. # token
  87. post '/api/v1/monitoring/token', {}, @headers
  88. assert_response(401)
  89. result = JSON.parse(@response.body)
  90. assert_equal(Hash, result.class)
  91. assert_not(result['token'])
  92. assert_equal('authentication failed', result['error'])
  93. end
  94. test '02 monitoring with wrong token' do
  95. # health_check
  96. get '/api/v1/monitoring/health_check?token=abc', {}, @headers
  97. assert_response(401)
  98. result = JSON.parse(@response.body)
  99. assert_equal(Hash, result.class)
  100. assert_not(result['healthy'])
  101. assert_equal('Not authorized', result['error'])
  102. # status
  103. get '/api/v1/monitoring/status?token=abc', {}, @headers
  104. assert_response(401)
  105. result = JSON.parse(@response.body)
  106. assert_equal(Hash, result.class)
  107. assert_not(result['agents'])
  108. assert_not(result['last_login'])
  109. assert_not(result['counts'])
  110. assert_not(result['last_created_at'])
  111. assert_equal('Not authorized', result['error'])
  112. # token
  113. post '/api/v1/monitoring/token', { token: 'abc' }.to_json, @headers
  114. assert_response(401)
  115. result = JSON.parse(@response.body)
  116. assert_equal(Hash, result.class)
  117. assert_not(result['token'])
  118. assert_equal('authentication failed', result['error'])
  119. end
  120. test '03 monitoring with correct token' do
  121. # health_check
  122. get "/api/v1/monitoring/health_check?token=#{@token}", {}, @headers
  123. assert_response(200)
  124. result = JSON.parse(@response.body)
  125. assert_equal(Hash, result.class)
  126. assert_not(result['error'])
  127. assert_equal(true, result['healthy'])
  128. assert_equal('success', result['message'])
  129. # status
  130. get "/api/v1/monitoring/status?token=#{@token}", {}, @headers
  131. assert_response(200)
  132. result = JSON.parse(@response.body)
  133. assert_equal(Hash, result.class)
  134. assert_not(result['error'])
  135. assert(result.key?('agents'))
  136. assert(result.key?('last_login'))
  137. assert(result.key?('counts'))
  138. assert(result.key?('last_created_at'))
  139. # token
  140. post '/api/v1/monitoring/token', { token: @token }.to_json, @headers
  141. assert_response(401)
  142. result = JSON.parse(@response.body)
  143. assert_equal(Hash, result.class)
  144. assert_not(result['token'])
  145. assert_equal('authentication failed', result['error'])
  146. end
  147. test '04 monitoring with admin user' do
  148. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  149. # health_check
  150. get '/api/v1/monitoring/health_check', {}, @headers.merge('Authorization' => credentials)
  151. assert_response(200)
  152. result = JSON.parse(@response.body)
  153. assert_equal(Hash, result.class)
  154. assert_not(result['error'])
  155. assert_equal(true, result['healthy'])
  156. assert_equal('success', result['message'])
  157. # status
  158. get '/api/v1/monitoring/status', {}, @headers.merge('Authorization' => credentials)
  159. assert_response(200)
  160. result = JSON.parse(@response.body)
  161. assert_equal(Hash, result.class)
  162. assert_not(result['error'])
  163. assert(result.key?('agents'))
  164. assert(result.key?('last_login'))
  165. assert(result.key?('counts'))
  166. assert(result.key?('last_created_at'))
  167. # token
  168. post '/api/v1/monitoring/token', { token: @token }.to_json, @headers.merge('Authorization' => credentials)
  169. assert_response(201)
  170. result = JSON.parse(@response.body)
  171. assert_equal(Hash, result.class)
  172. assert(result['token'])
  173. @token = result['token']
  174. assert_not(result['error'])
  175. end
  176. test '05 monitoring with agent user' do
  177. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-agent@example.com', 'agentpw')
  178. # health_check
  179. get '/api/v1/monitoring/health_check', {}, @headers.merge('Authorization' => credentials)
  180. assert_response(401)
  181. result = JSON.parse(@response.body)
  182. assert_equal(Hash, result.class)
  183. assert_not(result['healthy'])
  184. assert_equal('Not authorized (user)!', result['error'])
  185. # status
  186. get '/api/v1/monitoring/status', {}, @headers.merge('Authorization' => credentials)
  187. assert_response(401)
  188. result = JSON.parse(@response.body)
  189. assert_equal(Hash, result.class)
  190. assert_not(result['agents'])
  191. assert_not(result['last_login'])
  192. assert_not(result['counts'])
  193. assert_not(result['last_created_at'])
  194. assert_equal('Not authorized (user)!', result['error'])
  195. # token
  196. post '/api/v1/monitoring/token', { token: @token }.to_json, @headers.merge('Authorization' => credentials)
  197. assert_response(401)
  198. result = JSON.parse(@response.body)
  199. assert_equal(Hash, result.class)
  200. assert_not(result['token'])
  201. assert_equal('Not authorized (user)!', result['error'])
  202. end
  203. test '06 monitoring with admin user and invalid permission' do
  204. permission = Permission.find_by(name: 'admin.monitoring')
  205. permission.active = false
  206. permission.save!
  207. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('monitoring-admin@example.com', 'adminpw')
  208. # health_check
  209. get '/api/v1/monitoring/health_check', {}, @headers.merge('Authorization' => credentials)
  210. assert_response(401)
  211. result = JSON.parse(@response.body)
  212. assert_equal(Hash, result.class)
  213. assert_not(result['healthy'])
  214. assert_equal('Not authorized (user)!', result['error'])
  215. # status
  216. get '/api/v1/monitoring/status', {}, @headers.merge('Authorization' => credentials)
  217. assert_response(401)
  218. result = JSON.parse(@response.body)
  219. assert_equal(Hash, result.class)
  220. assert_not(result['agents'])
  221. assert_not(result['last_login'])
  222. assert_not(result['counts'])
  223. assert_not(result['last_created_at'])
  224. assert_equal('Not authorized (user)!', result['error'])
  225. # token
  226. post '/api/v1/monitoring/token', { token: @token }.to_json, @headers.merge('Authorization' => credentials)
  227. assert_response(401)
  228. result = JSON.parse(@response.body)
  229. assert_equal(Hash, result.class)
  230. assert_not(result['token'])
  231. assert_equal('Not authorized (user)!', result['error'])
  232. permission.active = true
  233. permission.save!
  234. end
  235. test '07 monitoring with correct token and invalid permission' do
  236. permission = Permission.find_by(name: 'admin.monitoring')
  237. permission.active = false
  238. permission.save!
  239. # health_check
  240. get "/api/v1/monitoring/health_check?token=#{@token}", {}, @headers
  241. assert_response(200)
  242. result = JSON.parse(@response.body)
  243. assert_equal(Hash, result.class)
  244. assert_not(result['error'])
  245. assert_equal(true, result['healthy'])
  246. assert_equal('success', result['message'])
  247. # status
  248. get "/api/v1/monitoring/status?token=#{@token}", {}, @headers
  249. assert_response(200)
  250. result = JSON.parse(@response.body)
  251. assert_equal(Hash, result.class)
  252. assert_not(result['error'])
  253. assert(result.key?('agents'))
  254. assert(result.key?('last_login'))
  255. assert(result.key?('counts'))
  256. assert(result.key?('last_created_at'))
  257. # token
  258. post '/api/v1/monitoring/token', { token: @token }.to_json, @headers
  259. assert_response(401)
  260. result = JSON.parse(@response.body)
  261. assert_equal(Hash, result.class)
  262. assert_not(result['token'])
  263. assert_equal('authentication failed', result['error'])
  264. permission.active = true
  265. permission.save!
  266. end
  267. test '08 check health false' do
  268. channel = Channel.find_by(active: true)
  269. channel.status_in = 'ok'
  270. channel.status_out = 'error'
  271. channel.last_log_in = nil
  272. channel.last_log_out = nil
  273. channel.save!
  274. # health_check
  275. get "/api/v1/monitoring/health_check?token=#{@token}", {}, @headers
  276. assert_response(200)
  277. result = JSON.parse(@response.body)
  278. assert_equal(Hash, result.class)
  279. assert(result['message'])
  280. assert(result['issues'])
  281. assert_equal(false, result['healthy'])
  282. assert_equal('Channel: Email::Notification out ', result['message'])
  283. scheduler = Scheduler.where(active: true).last
  284. scheduler.last_run = Time.zone.now - 1.day
  285. scheduler.period = 600
  286. scheduler.save!
  287. # health_check
  288. get "/api/v1/monitoring/health_check?token=#{@token}", {}, @headers
  289. assert_response(200)
  290. result = JSON.parse(@response.body)
  291. assert_equal(Hash, result.class)
  292. assert(result['message'])
  293. assert(result['issues'])
  294. assert_equal(false, result['healthy'])
  295. assert_equal('Channel: Email::Notification out ;scheduler not running', result['message'])
  296. dir = "#{Rails.root}/tmp/unprocessable_mail"
  297. FileUtils.mkdir_p(dir)
  298. FileUtils.touch("#{dir}/test.eml")
  299. # health_check
  300. get "/api/v1/monitoring/health_check?token=#{@token}", {}, @headers
  301. assert_response(200)
  302. result = JSON.parse(@response.body)
  303. assert_equal(Hash, result.class)
  304. assert(result['message'])
  305. assert(result['issues'])
  306. assert_equal(false, result['healthy'])
  307. assert_equal('Channel: Email::Notification out ;unprocessable mails: 1;scheduler not running', result['message'])
  308. end
  309. end