notification_factory_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'rails_helper'
  2. RSpec.describe NotificationFactory do
  3. # WARNING: This spec relies on the presence of
  4. # *actual* view templates in the app/ directory.
  5. # Deleting them from the repo will break the tests!
  6. describe '::template_read' do
  7. let(:rendered_locale) { 'en' }
  8. let(:parsed_template) { { subject: template_lines.first, body: template_lines.drop(1).join } }
  9. let(:template_lines) { File.readlines(template_path) }
  10. let(:template_path) { Rails.root.join("app/views/mailer/signup/#{rendered_locale}.html.erb") }
  11. let(:read_params) do
  12. { type: 'mailer', template: 'signup', locale: 'en', format: 'html' }
  13. end
  14. it 'returns template file content as { subject: <first line>, body: <rest of file> }' do
  15. expect(described_class.template_read(read_params))
  16. .to eq(parsed_template)
  17. end
  18. context 'when selecting a template file to render' do
  19. # see https://github.com/zammad/zammad/issues/845#issuecomment-395084348
  20. context 'and file with ‘.custom’ suffix is available' do
  21. let(:template_path) { Rails.root.to_s + "/app/views/mailer/signup/#{rendered_locale}.html.erb.custom" }
  22. it 'uses that file' do
  23. File.write(template_path, "Subject\nBody\nbody\n")
  24. expect(described_class.template_read(read_params))
  25. .to eq({ subject: "Subject\n", body: "Body\nbody\n" })
  26. ensure
  27. File.delete(template_path)
  28. end
  29. end
  30. context 'if no locale given in arguments, and no default locale is set' do
  31. before { Setting.set('locale_default', nil) }
  32. it 'renders en-us template' do
  33. expect(described_class.template_read(read_params.except(:locale)))
  34. .to eq(parsed_template)
  35. end
  36. end
  37. context 'if no locale given in arguments, but default locale is set' do
  38. before { Setting.set('locale_default', 'de-de') }
  39. let(:rendered_locale) { 'de' }
  40. it 'tries template for default locale' do
  41. expect(described_class.template_read(read_params.except(:locale)))
  42. .to eq(parsed_template)
  43. end
  44. context 'and no such template exists' do
  45. before { Setting.set('locale_default', 'xx') }
  46. let(:rendered_locale) { 'en' }
  47. it 'falls back to en template' do
  48. expect(described_class.template_read(read_params.except(:locale)))
  49. .to eq(parsed_template)
  50. end
  51. end
  52. end
  53. context 'if locale given in arguments' do
  54. let(:rendered_locale) { 'de' }
  55. it 'tries template for given locale' do
  56. expect(described_class.template_read(read_params.merge(locale: 'de-de')))
  57. .to eq(parsed_template)
  58. end
  59. context 'and no such template exists' do
  60. let(:rendered_locale) { 'en' }
  61. it 'falls back to en template' do
  62. expect(described_class.template_read(read_params.merge(locale: 'xx')))
  63. .to eq(parsed_template)
  64. end
  65. end
  66. end
  67. end
  68. end
  69. describe '::application_template_read' do
  70. let(:read_params) { { type: 'mailer', format: 'html' } }
  71. let(:template_path) { Rails.root.join('app/views/mailer/application.html.erb') }
  72. it 'returns template file content as string' do
  73. expect(described_class.application_template_read(read_params))
  74. .to eq(File.read(template_path))
  75. end
  76. end
  77. end