template_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe NotificationFactory::Template do
  4. subject(:template) do
  5. described_class.new(template_string, escape)
  6. end
  7. describe '#to_s' do
  8. context 'for empty input template (incl. whitespace-only)' do
  9. let(:template_string) { "\#{ }" }
  10. context 'with escape = true' do
  11. let(:escape) { true }
  12. it 'returns an ERB template with the #d helper, and passes escape arg as string' do
  13. expect(template.to_s).to eq('<%= d "", true %>')
  14. end
  15. end
  16. context 'with escape = false' do
  17. let(:escape) { false }
  18. it 'returns an ERB template with the #d helper, and passes escape arg as string' do
  19. expect(template.to_s).to eq('<%= d "", false %>')
  20. end
  21. end
  22. end
  23. context 'for input template using #t helper' do
  24. let(:template_string) { "\#{t('some text')}" }
  25. let(:escape) { false }
  26. it 'returns an ERB template with the #t helper, and passes escape arg as string' do
  27. expect(template.to_s).to eq('<%= t "some text", false %>')
  28. end
  29. context 'with double-quotes in argument' do
  30. let(:template_string) { "\#{t('some \"text\"')}" }
  31. it 'adds backslash-escaping' do
  32. expect(template.to_s).to eq('<%= t "some \"text\"", false %>')
  33. end
  34. end
  35. end
  36. # Regression test for https://github.com/zammad/zammad/issues/385
  37. context 'with HTML auto-injected by browser' do
  38. let(:escape) { true }
  39. context 'for <a> tags wrapped around "ticket.id"' do
  40. let(:template_string) { <<~'TEMPLATE'.chomp }
  41. #{<a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id</a>}
  42. TEMPLATE
  43. it 'strips tag from resulting ERB template' do
  44. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  45. end
  46. end
  47. context 'for <a> tags wrapped around "config.fqdn"' do
  48. let(:template_string) { <<~'TEMPLATE'.chomp }
  49. #{<a href="http://config.fqdn" title="http://config.fqdn" target="_blank">config.fqdn</a>}
  50. TEMPLATE
  51. it 'strips tag from resulting ERB template' do
  52. expect(template.to_s).to eq('<%= c "fqdn", true %>')
  53. end
  54. end
  55. context 'for <a> tags surrounded by whitespace' do
  56. let(:template_string) { <<~'TEMPLATE'.chomp }
  57. #{ <a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id </a> }
  58. TEMPLATE
  59. it 'strips tag and spaces from template' do
  60. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  61. end
  62. end
  63. context 'for unpaired <a> tag and trailing whitespace' do
  64. let(:template_string) { <<~'TEMPLATE'.chomp }
  65. #{<a href="http://ticket.id" title="http://ticket.id" target="_blank">ticket.id }
  66. TEMPLATE
  67. it 'strips tag and spaces from template' do
  68. expect(template.to_s).to eq('<%= d "ticket.id", true %>')
  69. end
  70. end
  71. end
  72. end
  73. end