check_mk_spec.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Integration Check MK', type: :request do
  4. let(:customer) do
  5. create(:customer)
  6. end
  7. let(:group) do
  8. create(:group)
  9. end
  10. before do
  11. token = SecureRandom.urlsafe_base64(16)
  12. Setting.set('check_mk_token', token)
  13. Setting.set('check_mk_integration', true)
  14. end
  15. describe 'request handling' do
  16. it 'does fail without a token' do
  17. post '/api/v1/integration/check_mk/', params: {}
  18. expect(response).to have_http_status(:not_found)
  19. end
  20. it 'does fail with invalid token and feature enabled' do
  21. post '/api/v1/integration/check_mk/invalid_token', params: {}
  22. expect(response).to have_http_status(:unprocessable_entity)
  23. expect(json_response).to be_a(Hash)
  24. expect(json_response['error']).to eq('The provided token is invalid.')
  25. end
  26. context 'service check' do
  27. it 'does create ticket with customer email' do
  28. params = {
  29. event_id: '123',
  30. state: 'down',
  31. host: 'some host',
  32. service: 'some service',
  33. customer: customer.email,
  34. }
  35. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  36. expect(response).to have_http_status(:ok)
  37. expect(json_response).to be_a(Hash)
  38. expect(json_response['result']).to be_truthy
  39. expect(json_response['ticket_id']).to be_truthy
  40. expect(json_response['ticket_number']).to be_truthy
  41. ticket = Ticket.find(json_response['ticket_id'])
  42. expect(ticket.state.name).to eq('new')
  43. expect(ticket.customer.email).to eq(customer.email)
  44. expect(ticket.articles.count).to eq(1)
  45. end
  46. it 'does create ticket with customer login' do
  47. params = {
  48. event_id: '123',
  49. state: 'down',
  50. host: 'some host',
  51. service: 'some service',
  52. customer: customer.login,
  53. }
  54. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  55. expect(response).to have_http_status(:ok)
  56. expect(json_response).to be_a(Hash)
  57. expect(json_response['result']).to be_truthy
  58. expect(json_response['ticket_id']).to be_truthy
  59. expect(json_response['ticket_number']).to be_truthy
  60. ticket = Ticket.find(json_response['ticket_id'])
  61. expect(ticket.state.name).to eq('new')
  62. expect(ticket.customer.email).to eq(customer.email)
  63. expect(ticket.articles.count).to eq(1)
  64. end
  65. it 'does create ticket with group' do
  66. params = {
  67. event_id: '123',
  68. state: 'down',
  69. host: 'some host',
  70. service: 'some service',
  71. group: group.name,
  72. }
  73. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  74. expect(response).to have_http_status(:ok)
  75. expect(json_response).to be_a(Hash)
  76. expect(json_response['result']).to be_truthy
  77. expect(json_response['ticket_id']).to be_truthy
  78. expect(json_response['ticket_number']).to be_truthy
  79. ticket = Ticket.find(json_response['ticket_id'])
  80. expect(ticket.state.name).to eq('new')
  81. expect(ticket.group.name).to eq(group.name)
  82. expect(ticket.articles.count).to eq(1)
  83. end
  84. it 'does create ticket with extra field', db_strategy: :reset do
  85. create(:object_manager_attribute_text, name: 'text1')
  86. ObjectManager::Attribute.migration_execute
  87. params = {
  88. event_id: '123',
  89. state: 'down',
  90. host: 'some host',
  91. service: 'some service',
  92. text1: 'some text'
  93. }
  94. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  95. expect(response).to have_http_status(:ok)
  96. expect(json_response).to be_a(Hash)
  97. expect(json_response['result']).to be_truthy
  98. expect(json_response['ticket_id']).to be_truthy
  99. expect(json_response['ticket_number']).to be_truthy
  100. ticket = Ticket.find(json_response['ticket_id'])
  101. expect(ticket.state.name).to eq('new')
  102. expect(ticket.text1).to eq('some text')
  103. end
  104. it 'does create and close a ticket' do
  105. params = {
  106. event_id: '123',
  107. state: 'down',
  108. host: 'some host',
  109. service: 'some service',
  110. }
  111. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  112. expect(response).to have_http_status(:ok)
  113. expect(json_response).to be_a(Hash)
  114. expect(json_response['result']).to be_truthy
  115. expect(json_response['ticket_id']).to be_truthy
  116. expect(json_response['ticket_number']).to be_truthy
  117. ticket = Ticket.find(json_response['ticket_id'])
  118. expect(ticket.state.name).to eq('new')
  119. expect(ticket.articles.count).to eq(1)
  120. params = {
  121. event_id: '123',
  122. state: 'up',
  123. host: 'some host',
  124. service: 'some service',
  125. }
  126. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  127. expect(response).to have_http_status(:ok)
  128. expect(json_response).to be_a(Hash)
  129. expect(json_response['result']).not_to be_empty
  130. expect(json_response['ticket_ids']).to include(ticket.id)
  131. ticket.reload
  132. expect(ticket.state.name).to eq('closed')
  133. expect(ticket.articles.count).to eq(2)
  134. end
  135. it 'does double create and auto close' do
  136. params = {
  137. event_id: '123',
  138. state: 'down',
  139. host: 'some host',
  140. service: 'some service',
  141. }
  142. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  143. expect(response).to have_http_status(:ok)
  144. expect(json_response).to be_a(Hash)
  145. expect(json_response['result']).to be_truthy
  146. expect(json_response['ticket_id']).to be_truthy
  147. expect(json_response['ticket_number']).to be_truthy
  148. ticket = Ticket.find(json_response['ticket_id'])
  149. expect(ticket.state.name).to eq('new')
  150. expect(ticket.articles.count).to eq(1)
  151. params = {
  152. event_id: '123',
  153. state: 'down',
  154. host: 'some host',
  155. service: 'some service',
  156. }
  157. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  158. expect(response).to have_http_status(:ok)
  159. expect(json_response).to be_a(Hash)
  160. expect(json_response['result']).to eq('ticket already open, added note')
  161. expect(json_response['ticket_ids']).to include(ticket.id)
  162. ticket.reload
  163. expect(ticket.state.name).to eq('new')
  164. expect(ticket.articles.count).to eq(2)
  165. params = {
  166. event_id: '123',
  167. state: 'up',
  168. host: 'some host',
  169. service: 'some service',
  170. }
  171. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  172. expect(response).to have_http_status(:ok)
  173. expect(json_response).to be_a(Hash)
  174. expect(json_response['result']).to be_truthy
  175. expect(json_response['ticket_ids']).to include(ticket.id)
  176. ticket.reload
  177. expect(ticket.state.name).to eq('closed')
  178. expect(ticket.articles.count).to eq(3)
  179. end
  180. it 'does ticket close which get ignored' do
  181. params = {
  182. event_id: '123',
  183. state: 'up',
  184. host: 'some host',
  185. service: 'some service',
  186. }
  187. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  188. expect(response).to have_http_status(:ok)
  189. expect(json_response).to be_a(Hash)
  190. expect(json_response['result']).to eq('no open tickets found, ignore action')
  191. end
  192. it 'does double create and no auto close' do
  193. Setting.set('check_mk_auto_close', false)
  194. params = {
  195. event_id: '123',
  196. state: 'down',
  197. host: 'some host',
  198. service: 'some service',
  199. }
  200. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  201. expect(response).to have_http_status(:ok)
  202. expect(json_response).to be_a(Hash)
  203. expect(json_response['result']).to be_truthy
  204. expect(json_response['ticket_id']).to be_truthy
  205. expect(json_response['ticket_number']).to be_truthy
  206. ticket = Ticket.find(json_response['ticket_id'])
  207. expect(ticket.state.name).to eq('new')
  208. expect(ticket.articles.count).to eq(1)
  209. params = {
  210. event_id: '123',
  211. state: 'down',
  212. host: 'some host',
  213. service: 'some service',
  214. }
  215. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  216. expect(response).to have_http_status(:ok)
  217. expect(json_response).to be_a(Hash)
  218. expect(json_response['result']).to eq('ticket already open, added note')
  219. expect(json_response['ticket_ids']).to include(ticket.id)
  220. ticket.reload
  221. expect(ticket.state.name).to eq('new')
  222. expect(ticket.articles.count).to eq(2)
  223. params = {
  224. event_id: '123',
  225. state: 'up',
  226. host: 'some host',
  227. service: 'some service',
  228. }
  229. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  230. expect(response).to have_http_status(:ok)
  231. expect(json_response).to be_a(Hash)
  232. expect(json_response['result']).to eq('ticket already open, added note')
  233. expect(json_response['ticket_ids']).to include(ticket.id)
  234. ticket.reload
  235. expect(ticket.state.name).to eq('new')
  236. expect(ticket.articles.count).to eq(3)
  237. end
  238. it 'does double create and auto close - with host recover' do
  239. params = {
  240. event_id: '123',
  241. state: 'down',
  242. host: 'some host',
  243. service: 'some service',
  244. }
  245. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  246. expect(response).to have_http_status(:ok)
  247. expect(json_response).to be_a(Hash)
  248. expect(json_response['result']).to be_truthy
  249. expect(json_response['ticket_id']).to be_truthy
  250. expect(json_response['ticket_number']).to be_truthy
  251. ticket = Ticket.find(json_response['ticket_id'])
  252. expect(ticket.state.name).to eq('new')
  253. expect(ticket.articles.count).to eq(1)
  254. params = {
  255. event_id: '123',
  256. state: 'up',
  257. host: 'some host',
  258. }
  259. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  260. expect(response).to have_http_status(:ok)
  261. expect(json_response).to be_a(Hash)
  262. expect(json_response['result']).to eq('no open tickets found, ignore action')
  263. expect(json_response['ticket_ids']).to be_nil
  264. ticket.reload
  265. expect(ticket.state.name).to eq('new')
  266. expect(ticket.articles.count).to eq(1)
  267. end
  268. end
  269. context 'host check' do
  270. it 'does create and close a ticket' do
  271. params = {
  272. event_id: '123',
  273. state: 'down',
  274. host: 'some host',
  275. }
  276. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  277. expect(response).to have_http_status(:ok)
  278. expect(json_response).to be_a(Hash)
  279. expect(json_response['result']).to be_truthy
  280. expect(json_response['ticket_id']).to be_truthy
  281. expect(json_response['ticket_number']).to be_truthy
  282. ticket = Ticket.find(json_response['ticket_id'])
  283. expect(ticket.state.name).to eq('new')
  284. expect(ticket.articles.count).to eq(1)
  285. params = {
  286. event_id: '123',
  287. state: 'up',
  288. host: 'some host',
  289. }
  290. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  291. expect(response).to have_http_status(:ok)
  292. expect(json_response).to be_a(Hash)
  293. expect(json_response['result']).not_to be_empty
  294. expect(json_response['ticket_ids']).to include(ticket.id)
  295. ticket.reload
  296. expect(ticket.state.name).to eq('closed')
  297. expect(ticket.articles.count).to eq(2)
  298. end
  299. it 'does double create and auto close' do
  300. params = {
  301. event_id: '123',
  302. state: 'down',
  303. host: 'some host',
  304. }
  305. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  306. expect(response).to have_http_status(:ok)
  307. expect(json_response).to be_a(Hash)
  308. expect(json_response['result']).to be_truthy
  309. expect(json_response['ticket_id']).to be_truthy
  310. expect(json_response['ticket_number']).to be_truthy
  311. ticket = Ticket.find(json_response['ticket_id'])
  312. expect(ticket.state.name).to eq('new')
  313. expect(ticket.articles.count).to eq(1)
  314. params = {
  315. event_id: '123',
  316. state: 'down',
  317. host: 'some host',
  318. }
  319. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  320. expect(response).to have_http_status(:ok)
  321. expect(json_response).to be_a(Hash)
  322. expect(json_response['result']).to eq('ticket already open, added note')
  323. expect(json_response['ticket_ids']).to include(ticket.id)
  324. ticket.reload
  325. expect(ticket.state.name).to eq('new')
  326. expect(ticket.articles.count).to eq(2)
  327. params = {
  328. event_id: '123',
  329. state: 'up',
  330. host: 'some host',
  331. }
  332. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  333. expect(response).to have_http_status(:ok)
  334. expect(json_response).to be_a(Hash)
  335. expect(json_response['result']).to be_truthy
  336. expect(json_response['ticket_ids']).to include(ticket.id)
  337. ticket.reload
  338. expect(ticket.state.name).to eq('closed')
  339. expect(ticket.articles.count).to eq(3)
  340. end
  341. it 'does ticket close which get ignored' do
  342. params = {
  343. event_id: '123',
  344. state: 'up',
  345. host: 'some host',
  346. }
  347. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  348. expect(response).to have_http_status(:ok)
  349. expect(json_response).to be_a(Hash)
  350. expect(json_response['result']).to eq('no open tickets found, ignore action')
  351. end
  352. it 'does double create and no auto close' do
  353. Setting.set('check_mk_auto_close', false)
  354. params = {
  355. event_id: '123',
  356. state: 'down',
  357. host: 'some host',
  358. }
  359. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  360. expect(response).to have_http_status(:ok)
  361. expect(json_response).to be_a(Hash)
  362. expect(json_response['result']).to be_truthy
  363. expect(json_response['ticket_id']).to be_truthy
  364. expect(json_response['ticket_number']).to be_truthy
  365. ticket = Ticket.find(json_response['ticket_id'])
  366. expect(ticket.state.name).to eq('new')
  367. expect(ticket.articles.count).to eq(1)
  368. params = {
  369. event_id: '123',
  370. state: 'down',
  371. host: 'some host',
  372. }
  373. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  374. expect(response).to have_http_status(:ok)
  375. expect(json_response).to be_a(Hash)
  376. expect(json_response['result']).to eq('ticket already open, added note')
  377. expect(json_response['ticket_ids']).to include(ticket.id)
  378. ticket.reload
  379. expect(ticket.state.name).to eq('new')
  380. expect(ticket.articles.count).to eq(2)
  381. params = {
  382. event_id: '123',
  383. state: 'up',
  384. host: 'some host',
  385. }
  386. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  387. expect(response).to have_http_status(:ok)
  388. expect(json_response).to be_a(Hash)
  389. expect(json_response['result']).to eq('ticket already open, added note')
  390. expect(json_response['ticket_ids']).to include(ticket.id)
  391. ticket.reload
  392. expect(ticket.state.name).to eq('new')
  393. expect(ticket.articles.count).to eq(3)
  394. end
  395. it 'does double create and auto close - with service recover' do
  396. params = {
  397. event_id: '123',
  398. state: 'down',
  399. host: 'some host',
  400. }
  401. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  402. expect(response).to have_http_status(:ok)
  403. expect(json_response).to be_a(Hash)
  404. expect(json_response['result']).to be_truthy
  405. expect(json_response['ticket_id']).to be_truthy
  406. expect(json_response['ticket_number']).to be_truthy
  407. ticket = Ticket.find(json_response['ticket_id'])
  408. expect(ticket.state.name).to eq('new')
  409. expect(ticket.articles.count).to eq(1)
  410. params = {
  411. event_id: '123',
  412. state: 'up',
  413. host: 'some host',
  414. service: 'some service',
  415. }
  416. post "/api/v1/integration/check_mk/#{Setting.get('check_mk_token')}", params: params
  417. expect(response).to have_http_status(:ok)
  418. expect(json_response).to be_a(Hash)
  419. expect(json_response['result']).to eq('no open tickets found, ignore action')
  420. expect(json_response['ticket_ids']).to be_nil
  421. ticket.reload
  422. expect(ticket.state.name).to eq('new')
  423. expect(ticket.articles.count).to eq(1)
  424. end
  425. end
  426. end
  427. end