location_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Whatsapp::Webhook::Message::Location, :aggregate_failures, current_user_id: 1 do
  4. describe '#process' do
  5. let(:channel) { create(:whatsapp_channel, welcome: 'Hey there!') }
  6. let(:location_name) { 'Langenbach Arena' }
  7. let(:from) do
  8. {
  9. phone: Faker::PhoneNumber.cell_phone_in_e164.delete('+'),
  10. name: Faker::Name.unique.name,
  11. }
  12. end
  13. let(:json) do
  14. {
  15. object: 'whatsapp_business_account',
  16. entry: [{
  17. id: '222259550976437',
  18. changes: [{
  19. value: {
  20. messaging_product: 'whatsapp',
  21. metadata: {
  22. display_phone_number: '15551340563',
  23. phone_number_id: channel.options[:phone_number_id],
  24. },
  25. contacts: [{
  26. profile: {
  27. name: from[:name],
  28. },
  29. wa_id: from[:phone],
  30. }],
  31. messages: [{
  32. from: from[:phone],
  33. id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  34. timestamp: '1707921703',
  35. location: {
  36. latitude: 50.697254180908,
  37. longitude: 7.9327116012573,
  38. name: location_name,
  39. url: 'https://foursquare.com/v/4fddbd3ee4b06434e8dc7504'
  40. },
  41. type: 'location',
  42. }],
  43. },
  44. field: 'messages',
  45. }],
  46. }],
  47. }.to_json
  48. end
  49. let(:data) { JSON.parse(json).deep_symbolize_keys }
  50. context 'when all data is valid' do
  51. it 'creates a user' do
  52. expect { described_class.new(data:, channel:).process }.to change(User, :count).by(1)
  53. end
  54. it 'creates a ticket + an article' do
  55. described_class.new(data:, channel:).process
  56. expect(Ticket.last).to have_attributes(
  57. title: "#{from[:name]} (+#{from[:phone]}) via WhatsApp",
  58. group_id: channel.group_id,
  59. )
  60. expect(Ticket.last.preferences).to include(
  61. channel_id: channel.id,
  62. channel_area: channel.area,
  63. whatsapp: {
  64. from: {
  65. phone_number: from[:phone],
  66. display_name: from[:name],
  67. },
  68. timestamp_incoming: '1707921703',
  69. },
  70. )
  71. expect(Ticket::Article.second_to_last.body).to include('Langenbach Arena')
  72. .and include('https://www.openstreetmap.org')
  73. .and include('50.697254180908')
  74. .and include('7.9327116012573')
  75. expect(Ticket::Article.second_to_last).to have_attributes(
  76. content_type: 'text/html',
  77. )
  78. expect(Ticket::Article.second_to_last.preferences).to include(
  79. whatsapp: {
  80. entry_id: '222259550976437',
  81. message_id: 'wamid.HBgNNDkxNTE1NjA4MDY5OBUCABIYFjNFQjBDMUM4M0I5NDRFNThBMUQyMjYA',
  82. type: 'location',
  83. }
  84. )
  85. # Welcome article
  86. expect(Ticket::Article.last).to have_attributes(
  87. body: 'Hey there!',
  88. content_type: 'text/plain',
  89. )
  90. end
  91. context 'when location has no name set' do
  92. let(:location_name) { nil }
  93. it 'uses fallback text for the link' do
  94. described_class.new(data:, channel:).process
  95. expect(Ticket::Article.second_to_last.body).to include('Location')
  96. .and include('target="_blank"')
  97. end
  98. end
  99. end
  100. end
  101. end