renderer_spec.rb 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe NotificationFactory::Renderer do
  4. # rubocop:disable Lint/InterpolationCheck
  5. describe 'render' do
  6. before { @user = User.where(firstname: 'Nicole').first }
  7. it 'correctly renders a blank template' do
  8. renderer = build(:notification_factory_renderer)
  9. expect(renderer.render).to eq ''
  10. end
  11. context 'when rendering templates with ERB tags' do
  12. let(:template) { '<%% <%= "<%" %> %%>' }
  13. it 'ignores pre-existing ERB tags in an untrusted template' do
  14. renderer = build(:notification_factory_renderer, template: template)
  15. expect(renderer.render).to eq '<% <%= "<%" %> %%>'
  16. end
  17. it 'executes pre-existing ERB tags in a trusted template' do
  18. renderer = build(:notification_factory_renderer, template: template, trusted: true)
  19. expect(renderer.render).to eq '<% <% %%>'
  20. end
  21. end
  22. describe 'escaping' do
  23. let(:ticket) { create(:ticket, title: '< + some % special " characters') }
  24. let(:objects) { { ticket: ticket } }
  25. let(:renderer) { build(:notification_factory_renderer, objects: objects, template: template, escape: escape, url_encode: url_encode) }
  26. let(:escape) { false }
  27. let(:url_encode) { false }
  28. let(:template) { 'embedded #{ ticket.title } value' }
  29. context 'without escaping' do
  30. it 'renders correctly' do
  31. expect(renderer.render).to eq "embedded #{ticket.title} value"
  32. end
  33. end
  34. context 'with HTML escaping' do
  35. let(:escape) { true }
  36. it 'renders correctly' do
  37. expect(renderer.render).to eq 'embedded &lt; + some % special &quot; characters value'
  38. end
  39. end
  40. context 'with link encoding' do
  41. let(:url_encode) { true }
  42. it 'renders correctly' do
  43. expect(renderer.render).to eq 'embedded %3C%20%2B%20some%20%25%20special%20%22%20characters value'
  44. end
  45. end
  46. end
  47. describe 'interpolation error handling' do
  48. let(:renderer) { build(:notification_factory_renderer, objects: {}, template: template) }
  49. let(:template) { '#{ ticket.title }' }
  50. context 'with debug_errors' do
  51. it 'renders an debug message' do
  52. expect(renderer.render).to eq "\#{ticket / no such object}"
  53. end
  54. end
  55. context 'without debug_errors' do
  56. it 'renders a dash' do
  57. expect(renderer.render(debug_errors: false)).to eq '-'
  58. end
  59. end
  60. end
  61. it 'correctly renders chained object references' do
  62. user = User.where(firstname: 'Nicole').first
  63. ticket = create(:ticket, customer: user)
  64. renderer = build(:notification_factory_renderer,
  65. objects: { ticket: ticket },
  66. template: '#{ticket.customer.firstname.downcase}')
  67. expect(renderer.render).to eq 'nicole'
  68. end
  69. it 'correctly renders multiple value calls' do
  70. ticket = create(:ticket, customer: @user)
  71. renderer = build(:notification_factory_renderer,
  72. objects: { ticket: ticket },
  73. template: '#{ticket.created_at.value.value.value.value.to_s.first}')
  74. expect(renderer.render).to eq '2'
  75. end
  76. it 'raises a StandardError when rendering a template with a broken syntax' do
  77. renderer = build(:notification_factory_renderer, template: 'test <% if %>', objects: {}, trusted: true)
  78. expect { renderer.render }.to raise_error(StandardError)
  79. end
  80. it 'raises a StandardError when rendering a template calling a non existant method' do
  81. renderer = build(:notification_factory_renderer, template: 'test <% Ticket.non_existant_method %>', objects: {}, trusted: true)
  82. expect { renderer.render }.to raise_error(StandardError)
  83. end
  84. it 'raises a StandardError when rendering a template referencing a non existant object' do
  85. renderer = build(:notification_factory_renderer, template: 'test <% NonExistantObject.first %>', objects: {}, trusted: true)
  86. expect { renderer.render }.to raise_error(StandardError)
  87. end
  88. context 'with different article variables' do
  89. let(:customer) { create(:customer, firstname: 'Nicole') }
  90. let(:ticket) { create(:ticket, customer: customer) }
  91. let(:objects) do
  92. last_article = nil
  93. last_internal_article = nil
  94. last_external_article = nil
  95. all_articles = ticket.articles
  96. if article.nil?
  97. last_article = all_articles.last
  98. last_internal_article = all_articles.reverse.find(&:internal?)
  99. last_external_article = all_articles.reverse.find { |a| !a.internal? }
  100. else
  101. last_article = article
  102. last_internal_article = article.internal? ? article : all_articles.reverse.find(&:internal?)
  103. last_external_article = article.internal? ? all_articles.reverse.find { |a| !a.internal? } : article
  104. end
  105. {
  106. ticket: ticket,
  107. article: last_article,
  108. last_article: last_article,
  109. last_internal_article: last_internal_article,
  110. last_external_article: last_external_article,
  111. created_article: article,
  112. created_internal_article: article&.internal? ? article : nil,
  113. created_external_article: article&.internal? ? nil : article,
  114. }
  115. end
  116. let(:renderer) do
  117. build(:notification_factory_renderer,
  118. objects: objects,
  119. template: template)
  120. end
  121. let(:body) { 'test' }
  122. let(:article) { create(:ticket_article, ticket: ticket, body: body) }
  123. context 'with ticket.tags as template' do
  124. let(:template) { '#{ticket.tags}' }
  125. before do
  126. ticket.tag_add('Tag1', customer.id)
  127. end
  128. it 'correctly renders ticket tags references' do
  129. expect(renderer.render).to eq 'Tag1'
  130. end
  131. end
  132. %w[article last_article last_internal_article last_external_article
  133. created_article created_internal_article created_external_article].each do |tag|
  134. context "with #{tag}.body as template" do
  135. let(:template) { "\#{#{tag}.body}" }
  136. let(:article) do
  137. create(
  138. :ticket_article,
  139. ticket: ticket,
  140. body: body,
  141. internal: tag.match?('internal')
  142. )
  143. end
  144. it "renders an #{tag} body with quote" do
  145. expect(renderer.render).to eq "&gt; #{body}<br>"
  146. end
  147. end
  148. end
  149. end
  150. context 'when handling ObjectManager::Attribute usage', db_strategy: :reset do
  151. before do
  152. create_object_manager_attribute
  153. ObjectManager::Attribute.migration_execute
  154. end
  155. let(:renderer) do
  156. build(:notification_factory_renderer,
  157. objects: { ticket: ticket },
  158. template: template)
  159. end
  160. shared_examples 'correctly rendering the attributes' do
  161. it 'correctly renders the attributes' do
  162. expect(renderer.render).to eq expected_render
  163. end
  164. end
  165. context 'with a simple select attribute' do
  166. let(:create_object_manager_attribute) do
  167. create(:object_manager_attribute_select, name: 'select')
  168. end
  169. let(:ticket) { create(:ticket, customer: @user, select: 'key_1') }
  170. let(:template) { '#{ticket.select} _SEPERATOR_ #{ticket.select.value}' }
  171. let(:expected_render) { 'key_1 _SEPERATOR_ value_1' }
  172. it_behaves_like 'correctly rendering the attributes'
  173. end
  174. context 'with select attribute on chained user object' do
  175. let(:create_object_manager_attribute) do
  176. create(:object_manager_attribute_select,
  177. object_lookup_id: ObjectLookup.by_name('User'),
  178. name: 'select')
  179. end
  180. let(:user) do
  181. user = User.where(firstname: 'Nicole').first
  182. user.select = 'key_2'
  183. user.save
  184. user
  185. end
  186. let(:ticket) { create(:ticket, customer: user) }
  187. let(:template) { '#{ticket.customer.select} _SEPERATOR_ #{ticket.customer.select.value}' }
  188. let(:expected_render) { 'key_2 _SEPERATOR_ value_2' }
  189. it_behaves_like 'correctly rendering the attributes'
  190. end
  191. context 'with select attribute on chained group object' do
  192. let(:create_object_manager_attribute) do
  193. create(:object_manager_attribute_select,
  194. object_lookup_id: ObjectLookup.by_name('Group'),
  195. name: 'select')
  196. end
  197. let(:template) { '#{ticket.group.select} _SEPERATOR_ #{ticket.group.select.value}' }
  198. let(:expected_render) { 'key_3 _SEPERATOR_ value_3' }
  199. let(:ticket) { create(:ticket, customer: @user) }
  200. before do
  201. group = ticket.group
  202. group.select = 'key_3'
  203. group.save
  204. end
  205. it_behaves_like 'correctly rendering the attributes'
  206. end
  207. context 'with select attribute on chained organization object' do
  208. let(:create_object_manager_attribute) do
  209. create(:object_manager_attribute_select,
  210. object_lookup_id: ObjectLookup.by_name('Organization'),
  211. name: 'select')
  212. end
  213. let(:user) do
  214. @user.organization.select = 'key_2'
  215. @user.organization.save
  216. @user
  217. end
  218. let(:ticket) { create(:ticket, customer: user) }
  219. let(:template) { '#{ticket.customer.organization.select} _SEPERATOR_ #{ticket.customer.organization.select.value}' }
  220. let(:expected_render) { 'key_2 _SEPERATOR_ value_2' }
  221. it_behaves_like 'correctly rendering the attributes'
  222. end
  223. context 'with multiselect', mariadb: true do
  224. context 'with a simple multiselect attribute' do
  225. let(:create_object_manager_attribute) do
  226. create(:object_manager_attribute_multiselect, name: 'multiselect')
  227. end
  228. let(:ticket) { create(:ticket, customer: @user, multiselect: ['key_1']) }
  229. let(:template) { '#{ticket.multiselect} _SEPERATOR_ #{ticket.multiselect.value}' }
  230. let(:expected_render) { 'key_1 _SEPERATOR_ value_1' }
  231. it_behaves_like 'correctly rendering the attributes'
  232. end
  233. context 'with single multiselect attribute on chained user object' do
  234. let(:create_object_manager_attribute) do
  235. create(:object_manager_attribute_multiselect,
  236. object_lookup_id: ObjectLookup.by_name('User'),
  237. name: 'multiselect')
  238. end
  239. let(:user) do
  240. user = User.where(firstname: 'Nicole').first
  241. user.multiselect = ['key_2']
  242. user.save
  243. user
  244. end
  245. let(:ticket) { create(:ticket, customer: user) }
  246. let(:template) { '#{ticket.customer.multiselect} _SEPERATOR_ #{ticket.customer.multiselect.value}' }
  247. let(:expected_render) { 'key_2 _SEPERATOR_ value_2' }
  248. it_behaves_like 'correctly rendering the attributes'
  249. end
  250. context 'with single multiselect attribute on chained group object' do
  251. let(:create_object_manager_attribute) do
  252. create(:object_manager_attribute_multiselect,
  253. object_lookup_id: ObjectLookup.by_name('Group'),
  254. name: 'multiselect')
  255. end
  256. let(:template) { '#{ticket.group.multiselect} _SEPERATOR_ #{ticket.group.multiselect.value}' }
  257. let(:expected_render) { 'key_3 _SEPERATOR_ value_3' }
  258. let(:ticket) { create(:ticket, customer: @user) }
  259. before do
  260. group = ticket.group
  261. group.multiselect = ['key_3']
  262. group.save
  263. end
  264. it_behaves_like 'correctly rendering the attributes'
  265. end
  266. context 'with single multiselect attribute on chained organization object' do
  267. let(:create_object_manager_attribute) do
  268. create(:object_manager_attribute_multiselect,
  269. object_lookup_id: ObjectLookup.by_name('Organization'),
  270. name: 'multiselect')
  271. end
  272. let(:user) do
  273. @user.organization.multiselect = ['key_2']
  274. @user.organization.save
  275. @user
  276. end
  277. let(:ticket) { create(:ticket, customer: user) }
  278. let(:template) { '#{ticket.customer.organization.multiselect} _SEPERATOR_ #{ticket.customer.organization.multiselect.value}' }
  279. let(:expected_render) { 'key_2 _SEPERATOR_ value_2' }
  280. it_behaves_like 'correctly rendering the attributes'
  281. end
  282. context 'with a multiple multiselect attribute' do
  283. let(:create_object_manager_attribute) do
  284. create(:object_manager_attribute_multiselect, name: 'multiselect')
  285. end
  286. let(:ticket) { create(:ticket, customer: @user, multiselect: %w[key_1 key_2]) }
  287. let(:template) { '#{ticket.multiselect} _SEPERATOR_ #{ticket.multiselect.value}' }
  288. let(:expected_render) { 'key_1, key_2 _SEPERATOR_ value_1, value_2' }
  289. it_behaves_like 'correctly rendering the attributes'
  290. end
  291. context 'with multiple multiselect attribute on chained user object' do
  292. let(:create_object_manager_attribute) do
  293. create(:object_manager_attribute_multiselect,
  294. object_lookup_id: ObjectLookup.by_name('User'),
  295. name: 'multiselect')
  296. end
  297. let(:user) do
  298. user = User.where(firstname: 'Nicole').first
  299. user.multiselect = %w[key_2 key_3]
  300. user.save
  301. user
  302. end
  303. let(:ticket) { create(:ticket, customer: user) }
  304. let(:template) { '#{ticket.customer.multiselect} _SEPERATOR_ #{ticket.customer.multiselect.value}' }
  305. let(:expected_render) { 'key_2, key_3 _SEPERATOR_ value_2, value_3' }
  306. it_behaves_like 'correctly rendering the attributes'
  307. end
  308. context 'with multiple multiselect attribute on chained group object' do
  309. let(:create_object_manager_attribute) do
  310. create(:object_manager_attribute_multiselect,
  311. object_lookup_id: ObjectLookup.by_name('Group'),
  312. name: 'multiselect')
  313. end
  314. let(:template) { '#{ticket.group.multiselect} _SEPERATOR_ #{ticket.group.multiselect.value}' }
  315. let(:expected_render) { 'key_3, key_1 _SEPERATOR_ value_3, value_1' }
  316. let(:ticket) { create(:ticket, customer: @user) }
  317. before do
  318. group = ticket.group
  319. group.multiselect = %w[key_3 key_1]
  320. group.save
  321. end
  322. it_behaves_like 'correctly rendering the attributes'
  323. end
  324. context 'with select (custom sorted) attribute on chained group object' do
  325. let(:create_object_manager_attribute) do
  326. create(:object_manager_attribute_select,
  327. object_lookup_id: ObjectLookup.by_name('Group'),
  328. name: 'select',
  329. data_option_options: [{ name: 'value_1', value: 'key_1' }, { name: 'value_2', value: 'key_2' }, { name: 'value_3', value: 'key_3' }])
  330. end
  331. let(:template) { '#{ticket.group.select} _SEPERATOR_ #{ticket.group.select.value}' }
  332. let(:expected_render) { 'key_3 _SEPERATOR_ value_3' }
  333. let(:ticket) { create(:ticket, customer: @user) }
  334. before do
  335. group = ticket.group
  336. group.select = 'key_3'
  337. group.save
  338. end
  339. it_behaves_like 'correctly rendering the attributes'
  340. end
  341. context 'with multiple multiselect (custom sorted) attribute on chained group object' do
  342. let(:create_object_manager_attribute) do
  343. create(:object_manager_attribute_multiselect,
  344. object_lookup_id: ObjectLookup.by_name('Group'),
  345. name: 'multiselect',
  346. data_option_options: [{ name: 'value_1', value: 'key_1' }, { name: 'value_2', value: 'key_2' }, { name: 'value_3', value: 'key_3' }])
  347. end
  348. let(:template) { '#{ticket.group.multiselect} _SEPERATOR_ #{ticket.group.multiselect.value}' }
  349. let(:expected_render) { 'key_3, key_1 _SEPERATOR_ value_3, value_1' }
  350. let(:ticket) { create(:ticket, customer: @user) }
  351. before do
  352. group = ticket.group
  353. group.multiselect = %w[key_3 key_1]
  354. group.save
  355. end
  356. it_behaves_like 'correctly rendering the attributes'
  357. end
  358. context 'with multiple multiselect attribute on chained organization object' do
  359. let(:create_object_manager_attribute) do
  360. create(:object_manager_attribute_multiselect,
  361. object_lookup_id: ObjectLookup.by_name('Organization'),
  362. name: 'multiselect')
  363. end
  364. let(:user) do
  365. @user.organization.multiselect = %w[key_2 key_1]
  366. @user.organization.save
  367. @user
  368. end
  369. let(:ticket) { create(:ticket, customer: user) }
  370. let(:template) { '#{ticket.customer.organization.multiselect} _SEPERATOR_ #{ticket.customer.organization.multiselect.value}' }
  371. let(:expected_render) { 'key_2, key_1 _SEPERATOR_ value_2, value_1' }
  372. it_behaves_like 'correctly rendering the attributes'
  373. end
  374. context 'with external data source attribute on chained group object', db_adapter: :postgresql do
  375. let(:create_object_manager_attribute) do
  376. create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  377. object_lookup_id: ObjectLookup.by_name('Group'),
  378. name: 'external_data_source')
  379. end
  380. let(:template) { '#{ticket.group.external_data_source} _SEPERATOR_ #{ticket.group.external_data_source.value}' }
  381. let(:expected_render) { '1234 _SEPERATOR_ Example' }
  382. let(:ticket) { create(:ticket, customer: @user) }
  383. before do
  384. group = ticket.group
  385. group.external_data_source = {
  386. value: 1234,
  387. label: 'Example'
  388. }
  389. group.save
  390. end
  391. it_behaves_like 'correctly rendering the attributes'
  392. end
  393. end
  394. context 'with a tree select attribute' do
  395. let(:create_object_manager_attribute) do
  396. create(:object_manager_attribute_tree_select, name: 'tree_select')
  397. end
  398. let(:ticket) { create(:ticket, customer: @user, tree_select: 'Incident::Hardware::Laptop') }
  399. let(:template) { '#{ticket.tree_select} _SEPERATOR_ #{ticket.tree_select.value}' }
  400. let(:expected_render) { 'Incident::Hardware::Laptop _SEPERATOR_ Incident::Hardware::Laptop' }
  401. it_behaves_like 'correctly rendering the attributes'
  402. end
  403. end
  404. end
  405. # rubocop:enable Lint/InterpolationCheck
  406. context 'with user avatar' do
  407. let(:base64_img) { 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' }
  408. let(:decoded_img) { Base64.decode64(base64_img) }
  409. let(:mime_type) { 'image/png' }
  410. let(:avatar) do
  411. Avatar.add(
  412. object: 'User',
  413. o_id: owner.id,
  414. full: {
  415. content: decoded_img,
  416. mime_type: mime_type,
  417. },
  418. resize: {
  419. content: decoded_img,
  420. mime_type: mime_type,
  421. },
  422. source: "upload #{Time.zone.now}",
  423. deletable: true,
  424. created_by_id: owner.id,
  425. updated_by_id: owner.id,
  426. )
  427. end
  428. let(:owner) { create(:user, group_ids: Group.pluck(:id)) }
  429. let(:ticket) { create(:ticket, owner: owner, group: Group.first) }
  430. context 'with an avatar' do
  431. before do
  432. owner.update!(image: avatar.store_hash)
  433. end
  434. it 'returns a <img> tag' do
  435. renderer = build(:notification_factory_renderer, template: 'Avatar test #{ticket.owner.avatar(150, 150)}', objects: { ticket: ticket }, trusted: true) # rubocop:disable Lint/InterpolationCheck
  436. expect(renderer.render).to eq "Avatar test <img src='data:#{mime_type};base64,#{base64_img}' width='150' height='150' />"
  437. end
  438. end
  439. context 'without an avatar' do
  440. it 'returns empty string' do
  441. renderer = build(:notification_factory_renderer, template: 'Avatar test #{ticket.owner.avatar(150, 150)}', objects: { ticket: ticket }, trusted: true) # rubocop:disable Lint/InterpolationCheck
  442. expect(renderer.render).to eq 'Avatar test '
  443. end
  444. end
  445. end
  446. end