sms_spec.rb 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Trigger do
  4. describe 'sms' do
  5. before do
  6. Translation.sync_locale_from_po(locale)
  7. Setting.set('locale_default', locale)
  8. Setting.set('timezone_default', time_zone)
  9. end
  10. let(:time_zone) { 'Europe/Vilnius' }
  11. let(:locale) { 'de-de' }
  12. context 'sends interpolated, html-free SMS' do
  13. before do
  14. another_agent = create(:admin, mobile: '+37061010000')
  15. Group.lookup(id: 1).users << another_agent
  16. create(:channel, area: 'Sms::Notification')
  17. create(:trigger,
  18. disable_notification: false,
  19. perform: {
  20. 'notification.sms': {
  21. recipient: 'ticket_agents',
  22. body: message_body,
  23. }
  24. })
  25. end
  26. let(:message_body) { 'space&nbsp;between #{ticket.title} #{ticket.created_at}' } # rubocop:disable Lint/InterpolationCheck
  27. let(:agent) { create(:agent) }
  28. let(:ticket) do
  29. ticket = create(:ticket, group: Group.lookup(id: 1), created_by_id: agent.id)
  30. TransactionDispatcher.commit
  31. ticket
  32. end
  33. let(:triggered_article) do
  34. ticket.articles.last
  35. end
  36. it 'renders HTML chars' do
  37. expect(triggered_article.body).to match(%r{space between})
  38. end
  39. it 'interpolates ticket properties' do
  40. expect(triggered_article.body).to match(ticket.title)
  41. end
  42. it 'interpolates time in selected time zone' do
  43. time_in_zone = triggered_article.ticket.created_at.in_time_zone(time_zone)
  44. expect(triggered_article.body).to match(time_in_zone.strftime('%H:%M'))
  45. end
  46. it 'interpolates date in selected locale format' do
  47. time_in_zone = triggered_article.ticket.created_at.in_time_zone(time_zone)
  48. expect(triggered_article.body).to match(time_in_zone.strftime('%d.%m.%Y'))
  49. end
  50. end
  51. context 'recipients' do
  52. let(:recipient1) { create(:agent, mobile: '+37061010000', groups: [ticket_group]) }
  53. let(:recipient2) { create(:agent, mobile: '+37061010001', groups: [ticket_group]) }
  54. let(:recipient3) { create(:agent, mobile: '+37061010002', groups: [ticket_group]) }
  55. let(:ticket_group) { create(:group) }
  56. let(:ticket) do
  57. ticket = create(:ticket, group: ticket_group, created_by_id: create(:agent).id)
  58. TransactionDispatcher.commit
  59. ticket
  60. end
  61. before do
  62. create(:channel, area: 'Sms::Notification')
  63. create(:trigger,
  64. disable_notification: false,
  65. perform: {
  66. 'notification.sms': {
  67. recipient: ['ticket_agents', "userid_#{recipient1.id}", "userid_#{recipient2.id}", "userid_#{recipient3.id}"],
  68. body: 'Hello World!',
  69. }
  70. })
  71. end
  72. it 'contains no duplicates' do
  73. expect(ticket.articles.last.preferences['sms_recipients'].sort).to eq([recipient1.mobile, recipient2.mobile, recipient3.mobile].sort)
  74. end
  75. end
  76. end
  77. end