slack_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require 'rails_helper'
  2. RSpec.describe NotificationFactory::Slack do
  3. describe '.template' do
  4. subject(:template) do
  5. described_class.template(
  6. template: action,
  7. locale: 'en-us',
  8. timezone: 'Europe/Berlin',
  9. objects: {
  10. ticket: ticket,
  11. article: article,
  12. recipient: agent,
  13. current_user: current_user,
  14. changes: changes,
  15. }
  16. )
  17. end
  18. let(:ticket) { article.ticket }
  19. let(:article) { create(:ticket_article) }
  20. let(:agent) { create(:agent) }
  21. let(:current_user) { create(:agent) }
  22. context 'for "ticket_create", with an empty "changes" hash' do
  23. let(:action) { 'ticket_create' }
  24. let(:changes) { {} }
  25. it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
  26. expect(template).to include(subject: "# #{ticket.title}")
  27. end
  28. it 'returns a hash with body: <article author & body>' do
  29. expect(template[:body])
  30. .to match(/Created by #{current_user.fullname}/)
  31. .and match(/#{article.body}\z/)
  32. end
  33. end
  34. context 'for "ticket_update", with a populated "changes" hash' do
  35. let(:action) { 'ticket_update' }
  36. let(:changes) do
  37. {
  38. state: %w[aaa bbb],
  39. group: %w[xxx yyy],
  40. pending_time: [Time.zone.parse('2019-04-01T10:00:00Z0'), Time.zone.parse('2019-04-01T23:00:00Z0')],
  41. }
  42. end
  43. it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
  44. expect(template).to include(subject: "# #{ticket.title}")
  45. end
  46. it 'returns a hash with body: <article editor, changes, & body>' do
  47. expect(template[:body])
  48. .to match(/Updated by #{current_user.fullname}/)
  49. .and match(/state: aaa -> bbb/)
  50. .and match(/group: xxx -> yyy/)
  51. .and match(%r{pending_time: 04/01/2019 12:00 \(Europe/Berlin\) -> 04/02/2019 01:00 \(Europe/Berlin\)})
  52. .and match(/#{article.body}\z/)
  53. end
  54. end
  55. context 'for "ticket_escalate"' do
  56. subject(:template) do
  57. described_class.template(
  58. template: 'ticket_escalation',
  59. locale: 'en-us',
  60. timezone: 'Europe/Berlin',
  61. objects: {
  62. ticket: ticket,
  63. article: article,
  64. recipient: agent,
  65. }
  66. )
  67. end
  68. before { ticket.escalation_at = escalation_time }
  69. let(:escalation_time) { Time.zone.parse('2019-04-01T10:00:00Z') }
  70. it 'returns a hash with subject: <ticket title> (as Markdown heading)' do
  71. expect(template).to include(subject: "# #{ticket.title}")
  72. end
  73. it 'returns a hash with body: <ticket customer, escalation time, & body>' do
  74. expect(template[:body])
  75. .to match(/A ticket \(#{ticket.title}\) from "#{ticket.customer.fullname}"/)
  76. .and match(%r{is escalated since "04/01/2019 12:00 \(Europe/Berlin\)"!})
  77. .and match(/#{article.body}\z/)
  78. end
  79. end
  80. end
  81. end