custom_payload_spec.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe TriggerWebhookJob::CustomPayload do
  4. # rubocop:disable Lint/InterpolationCheck
  5. describe '.generate' do
  6. subject(:generate) { described_class.generate(record, { ticket:, article:, notification: }) }
  7. let(:ticket) { create(:ticket) }
  8. let(:article) { create(:ticket_article, body: "Text with\nnew line.") }
  9. let(:event) do
  10. {
  11. type: 'info',
  12. execution: 'trigger',
  13. changes: { 'state' => %w[open closed] },
  14. user_id: 1,
  15. }
  16. end
  17. let(:notification) { TriggerWebhookJob::CustomPayload::Track::Notification.generate({ ticket:, article: }, { event: }) }
  18. context 'when the payload is empty' do
  19. let(:record) { {}.to_json }
  20. let(:json_data) { {} }
  21. it 'returns an empty JSON object' do
  22. expect(generate).to eq(json_data)
  23. end
  24. end
  25. context 'when the placeholder is empty' do
  26. let(:record) { { 'ticket' => '#{}' }.to_json }
  27. let(:json_data) { { 'ticket' => '#{}' } }
  28. it 'returns the placeholder' do
  29. expect(generate).to eq(json_data)
  30. end
  31. end
  32. context 'when the placeholder is invalid' do
  33. let(:record) { { 'ticket' => '#{ticket.title', 'article' => '#{article.id article.note}' }.to_json }
  34. let(:json_data) { { 'ticket' => '#{ticket.title', 'article' => '#{article.id article.note}' } }
  35. it 'returns the placeholder' do
  36. expect(generate).to eq(json_data)
  37. end
  38. end
  39. context 'when the placeholder base object ticket or article is missing' do
  40. let(:record) { { 'ticket' => '#{.title}' }.to_json }
  41. let(:json_data) { { 'ticket' => '#{no object provided}' } }
  42. it 'returns the placeholder reporting "no object provided"' do
  43. expect(generate).to eq(json_data)
  44. end
  45. end
  46. context 'when the placeholder base object is other than ticket or article' do
  47. let(:record) { { 'user' => '#{user}' }.to_json }
  48. let(:json_data) { { 'user' => '#{user / no such object}' } }
  49. it 'returns the placehodler reporting "no such object"' do
  50. expect(generate).to eq(json_data)
  51. end
  52. end
  53. context 'when the placeholder contains only base object ticket or article' do
  54. let(:record) { { 'ticket' => '#{ticket}', 'Article' => '#{article}' }.to_json }
  55. let(:json_data) { { 'ticket' => '#{ticket / missing method}', 'Article' => '#{article / missing method}' } }
  56. it 'returns the placeholder reporting "missing method"' do
  57. expect(generate).to eq(json_data)
  58. end
  59. end
  60. context 'when the placeholder contains denied method' do
  61. let(:record) { { 'ticket' => '#{ticket.articles}' }.to_json }
  62. let(:json_data) { { 'ticket' => '#{ticket.articles / no such method}' } }
  63. it 'returns the placeholder reporting "no such method"' do
  64. expect(generate).to eq(json_data)
  65. end
  66. end
  67. context 'when the placeholder contains denied attribute' do
  68. let(:record) { { 'ticket.owner' => '#{ticket.owner.password}' }.to_json }
  69. let(:json_data) { { 'ticket.owner' => '#{ticket.owner.password / no such method}' } }
  70. it 'returns the placeholder reporting "no such method"' do
  71. expect(generate).to eq(json_data)
  72. end
  73. end
  74. context 'when the placeholder contains danger method' do
  75. let(:record) { { 'ticket.owner' => '#{ticket.destroy!}' }.to_json }
  76. let(:json_data) { { 'ticket.owner' => '#{ticket.destroy! / no such method}' } }
  77. it 'returns the placeholder reporting "no such method"' do
  78. expect(generate).to eq(json_data)
  79. end
  80. end
  81. context 'when the placeholder ends with complex object' do
  82. let(:record) { { 'ticket' => '#{ticket.group}' }.to_json }
  83. let(:json_data) { { 'ticket' => '#{ticket.group / no such method}' } }
  84. it 'returns the placeholder reporting "no such method"' do
  85. expect(generate).to eq(json_data)
  86. end
  87. end
  88. context 'when the placeholder contains valid object and method' do
  89. let(:record) { { 'ticket.id' => '#{ticket.id}' }.to_json }
  90. let(:json_data) { { 'ticket.id' => ticket.id.to_s } }
  91. it 'returns the determined value' do
  92. expect(generate).to eq(json_data)
  93. end
  94. end
  95. context 'when the placeholder contains valid object and method, but the value is nil' do
  96. let(:record) do
  97. {
  98. 'ticket.organization.name' => '#{ticket.organization.name}',
  99. 'ticket.title' => '#{ticket.title}'
  100. }.to_json
  101. end
  102. let(:json_data) do
  103. {
  104. 'ticket.organization.name' => '',
  105. 'ticket.title' => ticket.title
  106. }
  107. end
  108. it 'returns an empty string' do
  109. expect(generate).to eq(json_data)
  110. end
  111. end
  112. context 'when the placeholder contains multiple valid object and method' do
  113. let(:record) do
  114. {
  115. 'ticket' => { 'owner' => '#{ticket.owner.fullname}' },
  116. 'article' => { 'created_at' => '#{article.created_at}' }
  117. }.to_json
  118. end
  119. let(:json_data) do
  120. {
  121. 'ticket' => { 'owner' => ticket.owner.fullname.to_s },
  122. 'article' => { 'created_at' => article.created_at.to_s }
  123. }
  124. end
  125. it 'returns the determined value' do
  126. expect(generate).to eq(json_data)
  127. end
  128. end
  129. context 'when the placeholder contains multiple attributes' do
  130. let(:record) { { 'my_field' => '#{ticket.id} // #{ticket.group.name}' }.to_json }
  131. let(:json_data) do
  132. {
  133. 'my_field' => "#{ticket.id} // #{ticket.group.name}",
  134. }
  135. end
  136. it 'returns the placeholder reporting "no such method"' do
  137. expect(generate).to eq(json_data)
  138. end
  139. end
  140. context 'when the payload contains a complex structure' do
  141. let(:record) do
  142. {
  143. 'current_user' => '#{current_user.fullname}',
  144. 'ticket' => {
  145. 'id' => '#{ticket.id}',
  146. 'owner' => '#{ticket.owner.fullname}',
  147. 'group' => '#{ticket.group.name}',
  148. 'article' => {
  149. 'id' => '#{article.id}',
  150. 'created_at' => '#{article.created_at}',
  151. 'subject' => '#{article.subject}',
  152. 'body' => '#{article.body}',
  153. 'attachments' => '#{article.attachments}'
  154. }
  155. }
  156. }.to_json
  157. end
  158. let(:json_data) do
  159. {
  160. 'current_user' => '#{current_user / no such object}',
  161. 'ticket' => {
  162. 'id' => ticket.id.to_s,
  163. 'owner' => ticket.owner.fullname.to_s,
  164. 'group' => ticket.group.name.to_s,
  165. 'article' => {
  166. 'id' => article.id.to_s,
  167. 'created_at' => article.created_at.to_s,
  168. 'subject' => article.subject.to_s,
  169. 'body' => article.body.to_s,
  170. 'attachments' => '#{article.attachments / no such method}',
  171. }
  172. }
  173. }
  174. end
  175. it 'returns a valid JSON payload' do
  176. expect(generate).to eq(json_data)
  177. end
  178. end
  179. context 'when the replacement value contains double quotes' do
  180. let(:ticket) { create(:ticket, title: 'Test "Title"') }
  181. let(:record) { { 'ticket.title' => '#{ticket.title}' }.to_json }
  182. let(:json_data) { { 'ticket.title' => 'Test "Title"' } }
  183. it 'returns the determined value' do
  184. expect(generate).to eq(json_data)
  185. end
  186. end
  187. describe "when the placeholder contains object 'notification'" do
  188. let(:record) do
  189. {
  190. 'subject' => '#{notification.subject}',
  191. 'message' => '#{notification.message}',
  192. 'changes' => '#{notification.changes}',
  193. 'body' => '#{notification.body}',
  194. 'link' => '#{notification.link}',
  195. }.to_json
  196. end
  197. context "when the event is of the type 'create'" do
  198. let(:event) do
  199. {
  200. type: 'create',
  201. execution: 'trigger',
  202. user_id: 1,
  203. }
  204. end
  205. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  206. expect(generate['subject']).to eq(ticket.title)
  207. expect(generate['body']).to eq(article.body_as_text)
  208. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  209. expect(generate['message']).to include('Created by')
  210. expect(generate['changes']).to include('State: new')
  211. end
  212. end
  213. context "when the event is of the type 'update'" do
  214. let(:event) do
  215. {
  216. type: 'update',
  217. execution: 'trigger',
  218. changes: { 'state' => %w[open closed] },
  219. user_id: 1,
  220. }
  221. end
  222. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  223. expect(generate['subject']).to eq(ticket.title)
  224. expect(generate['body']).to eq(article.body_as_text)
  225. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  226. expect(generate['message']).to include('Updated by')
  227. expect(generate['changes']).to include('state: open -> closed')
  228. end
  229. context 'without changes' do
  230. let(:event) do
  231. {
  232. type: 'update',
  233. execution: 'trigger',
  234. user_id: 1,
  235. }
  236. end
  237. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  238. expect(generate['subject']).to eq(ticket.title)
  239. expect(generate['body']).to eq(article.body_as_text)
  240. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  241. expect(generate['message']).to include('Updated by')
  242. end
  243. end
  244. end
  245. context "when the event is of the type 'info'" do
  246. let(:event) do
  247. {
  248. type: 'info',
  249. execution: 'trigger',
  250. changes: { 'state' => %w[open closed] },
  251. user_id: 1,
  252. }
  253. end
  254. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  255. expect(generate['subject']).to eq(ticket.title)
  256. expect(generate['body']).to eq(article.body_as_text)
  257. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  258. expect(generate['message']).to include('Last updated at')
  259. end
  260. end
  261. context "when the event is of the type 'escalation'" do
  262. let(:event) do
  263. {
  264. type: 'escalation',
  265. execution: 'trigger',
  266. user_id: 1,
  267. }
  268. end
  269. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  270. expect(generate['subject']).to eq(ticket.title)
  271. expect(generate['body']).to eq(article.body_as_text)
  272. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  273. expect(generate['message']).to include('Escalated at')
  274. expect(generate['changes']).to include('has been escalated since')
  275. end
  276. end
  277. context "when the event is of the type 'escalation warning'" do
  278. let(:event) do
  279. {
  280. type: 'escalation_warning',
  281. execution: 'trigger',
  282. user_id: 1,
  283. }
  284. end
  285. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  286. expect(generate['subject']).to eq(ticket.title)
  287. expect(generate['body']).to eq(article.body_as_text)
  288. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  289. expect(generate['message']).to include('Will escalate at')
  290. expect(generate['changes']).to include('will escalate at')
  291. end
  292. end
  293. context "when the event is of the type 'reminder reached'" do
  294. let(:event) do
  295. {
  296. type: 'reminder_reached',
  297. execution: 'trigger',
  298. user_id: 1,
  299. }
  300. end
  301. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  302. expect(generate['subject']).to eq(ticket.title)
  303. expect(generate['body']).to eq(article.body_as_text)
  304. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  305. expect(generate['message']).to include('Reminder reached!')
  306. expect(generate['changes']).to include('reminder reached for')
  307. end
  308. end
  309. context "when the event is triggered by a 'job'" do
  310. let(:event) do
  311. {
  312. type: '',
  313. execution: 'job',
  314. changes: { 'state' => %w[open closed] },
  315. user_id: 1,
  316. }
  317. end
  318. let(:article) { nil }
  319. it 'returns a valid json with a notification factory generated information"', :aggregate_failures do
  320. expect(generate['subject']).to eq(ticket.title)
  321. expect(generate['body']).to be_empty
  322. expect(generate['link']).to match(%r{http.*#ticket/zoom/#{ticket.id}$})
  323. expect(generate['message']).to include('Last updated at')
  324. end
  325. end
  326. end
  327. describe 'when the payload is a pre-defined webhook' do
  328. subject(:generate) { described_class.generate(record, { ticket:, article:, notification:, webhook: struct_webhook }) }
  329. let(:webhook) { create(:mattermost_webhook) }
  330. let(:struct_webhook) { TriggerWebhookJob::CustomPayload::Track::PreDefinedWebhook.generate({ ticket:, article: }, { event:, webhook: }) }
  331. let(:record) { TriggerWebhookJob::CustomPayload::Track::PreDefinedWebhook.payload('Mattermost') }
  332. it 'returns a valid json with webhook information"', :aggregate_failures do
  333. info = webhook.preferences[:pre_defined_webhook]
  334. expect(generate[:channel]).to eq(info[:channel])
  335. expect(generate[:icon_url]).to eq(info[:icon_url])
  336. end
  337. context 'when event has no changes' do
  338. let(:event) do
  339. {
  340. type: 'info',
  341. execution: 'trigger',
  342. changes: { 'state' => %w[open closed] },
  343. user_id: 1,
  344. }
  345. end
  346. it "returns a valid json with webhook information without 'attachments'", :aggregate_failures do
  347. info = webhook.preferences[:pre_defined_webhook]
  348. expect(generate[:channel]).to eq(info[:channel])
  349. expect(generate[:icon_url]).to eq(info[:icon_url])
  350. expect(generate).to not_include(:attachments)
  351. end
  352. end
  353. context 'when pre-defined webhook has no additional values' do
  354. let(:webhook) { create(:slack_webhook) }
  355. let(:record) { TriggerWebhookJob::CustomPayload::Track::PreDefinedWebhook.payload('Slack') }
  356. it 'returns a valid json with webhook information"', :aggregate_failures do
  357. expect(generate['text']).to eq("# #{ticket.title}")
  358. end
  359. end
  360. end
  361. end
  362. # rubocop:enable Lint/InterpolationCheck
  363. describe '.replacements' do
  364. subject(:replacements) { described_class.replacements(pre_defined_webhook_type: 'Mattermost') }
  365. it 'returns a hash with the replacement variables', :aggregate_failures do
  366. expect(replacements).to be_a(Hash)
  367. expect(replacements.keys).to include(:article, :ticket, :notification, :webhook)
  368. end
  369. end
  370. end