notification_factory_spec.rb 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. begin
  24. File.write(template_path, "Subject\nBody\nbody\n")
  25. expect(described_class.template_read(read_params))
  26. .to eq({ subject: "Subject\n", body: "Body\nbody\n" })
  27. ensure
  28. File.delete(template_path)
  29. end
  30. end
  31. end
  32. context 'if no locale given in arguments, and no default locale is set' do
  33. before { Setting.set('locale_default', nil) }
  34. it 'renders en-us template' do
  35. expect(described_class.template_read(read_params.except(:locale)))
  36. .to eq(parsed_template)
  37. end
  38. end
  39. context 'if no locale given in arguments, but default locale is set' do
  40. before { Setting.set('locale_default', 'de-de') }
  41. let(:rendered_locale) { 'de' }
  42. it 'tries template for default locale' do
  43. expect(described_class.template_read(read_params.except(:locale)))
  44. .to eq(parsed_template)
  45. end
  46. context 'and no such template exists' do
  47. before { Setting.set('locale_default', 'xx') }
  48. let(:rendered_locale) { 'en' }
  49. it 'falls back to en template' do
  50. expect(described_class.template_read(read_params.except(:locale)))
  51. .to eq(parsed_template)
  52. end
  53. end
  54. end
  55. context 'if locale given in arguments' do
  56. let(:rendered_locale) { 'de' }
  57. it 'tries template for given locale' do
  58. expect(described_class.template_read(read_params.merge(locale: 'de-de')))
  59. .to eq(parsed_template)
  60. end
  61. context 'and no such template exists' do
  62. let(:rendered_locale) { 'en' }
  63. it 'falls back to en template' do
  64. expect(described_class.template_read(read_params.merge(locale: 'xx')))
  65. .to eq(parsed_template)
  66. end
  67. end
  68. end
  69. end
  70. end
  71. describe '::application_template_read' do
  72. let(:read_params) { { type: 'mailer', format: 'html' } }
  73. let(:template_path) { Rails.root.join('app', 'views', 'mailer', 'application.html.erb') }
  74. it 'returns template file content as string' do
  75. expect(described_class.application_template_read(read_params))
  76. .to eq(File.read(template_path))
  77. end
  78. end
  79. end