reaction_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Reaction, :aggregate_failures, current_user_id: 1 do
  4. describe '#process' do
  5. let(:channel) { create(:whatsapp_channel, welcome: 'W' * 120) }
  6. let(:from) do
  7. {
  8. phone: Faker::PhoneNumber.cell_phone_in_e164.delete('+'),
  9. name: Faker::Name.unique.name,
  10. }
  11. end
  12. let(:json) do
  13. {
  14. object: 'whatsapp_business_account',
  15. entry: [{
  16. id: '222259550976437',
  17. changes: [{
  18. value: {
  19. messaging_product: 'whatsapp',
  20. metadata: {
  21. display_phone_number: '15551340563',
  22. phone_number_id: channel.options[:phone_number_id],
  23. },
  24. contacts: [{
  25. profile: {
  26. name: from[:name],
  27. },
  28. wa_id: from[:phone],
  29. }],
  30. messages: [{
  31. from: from[:phone],
  32. id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  33. timestamp: '1707921703',
  34. reaction: {
  35. message_id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABEYEjZEOUYzRTVEMkIzQkExRkE1RQA=',
  36. emoji: '👍'
  37. },
  38. type: 'reaction',
  39. }],
  40. },
  41. field: 'messages',
  42. }],
  43. }],
  44. }.to_json
  45. end
  46. let(:data) { JSON.parse(json).deep_symbolize_keys }
  47. before do
  48. create(:whatsapp_article).tap do |article|
  49. article.message_id = 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABEYEjZEOUYzRTVEMkIzQkExRkE1RQA='
  50. article.save!
  51. end
  52. allow(TransactionJob).to receive(:perform_now)
  53. end
  54. context 'when a reaction was sent' do
  55. it 'updates the related article with the reaction' do
  56. described_class.new(data:, channel:).process
  57. expect(Ticket::Article.last.preferences[:whatsapp][:reaction]).to eq({
  58. 'author' => from[:name],
  59. 'emoji' => '👍',
  60. })
  61. expect(TransactionJob).to have_received(:perform_now).with hash_including(type: 'update.reaction')
  62. end
  63. # Only for PostgreSQL due to emoji storage
  64. context 'when history is written', db_adapter: :postgresql do
  65. it 'contains correct type and emoji' do
  66. described_class.new(data:, channel:).process
  67. expect(History.last.history_type.name).to eq('created')
  68. expect(History.last.value_to).to eq('👍')
  69. end
  70. end
  71. end
  72. context 'when a reaction got removed' do
  73. before do
  74. article = Ticket::Article.last
  75. article.preferences[:whatsapp][:reaction] = '👍'
  76. article.save!
  77. data[:entry].first[:changes].first[:value][:messages].first[:reaction].delete(:emoji)
  78. end
  79. it 'updates the related article with the reaction' do
  80. described_class.new(data:, channel:).process
  81. expect(Ticket::Article.last.preferences[:whatsapp][:reaction]).to eq({
  82. 'author' => from[:name],
  83. 'emoji' => nil,
  84. })
  85. expect(TransactionJob).not_to have_received(:perform_now)
  86. end
  87. context 'when history is written' do
  88. it 'contains correct type and emoji' do
  89. described_class.new(data:, channel:).process
  90. expect(History.last.history_type.name).to eq('removed')
  91. expect(History.last.value_to).to eq('')
  92. end
  93. end
  94. end
  95. end
  96. end