message_bird_sms_spec.rb 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Message Bird SMS', performs_jobs: true, type: :request do
  4. describe 'request handling' do
  5. let(:agent) do
  6. create(:agent, groups: Group.all)
  7. end
  8. it 'does basic call' do
  9. # configure twilio channel
  10. group_id = Group.find_by(name: 'Users').id
  11. UserInfo.current_user_id = 1
  12. channel = create(
  13. :channel,
  14. area: 'Sms::Account',
  15. options: {
  16. adapter: 'sms/message_bird',
  17. webhook_token: 'secret_webhook_token',
  18. token: '223',
  19. sender: '333',
  20. },
  21. group_id: nil,
  22. )
  23. # process inbound sms
  24. post '/api/v1/sms_webhook', params: read_message('inbound_sms1'), as: :json
  25. expect(response).to have_http_status(:not_found)
  26. post '/api/v1/sms_webhook/not_existing', params: read_message('inbound_sms1'), as: :json
  27. expect(response).to have_http_status(:not_found)
  28. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms1'), as: :json
  29. expect(response).to have_http_status(:unprocessable_entity)
  30. expect(json_response['error']).to eq('Can\'t use Channel::Driver::Sms::MessageBird: #<Exceptions::UnprocessableEntity: Group needed in channel definition!>')
  31. channel.group_id = Group.first.id
  32. channel.save!
  33. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms1'), as: :json
  34. expect(response).to have_http_status(:ok)
  35. ticket = Ticket.last
  36. article = Ticket::Article.last
  37. customer = User.last
  38. expect(ticket.articles.count).to eq(1)
  39. expect(ticket.title).to eq('Ldfhxhcuffufuf. Fifififig. Fifififiif F...')
  40. expect(ticket.state.name).to eq('new')
  41. expect(ticket.group_id).to eq(group_id)
  42. expect(ticket.customer_id).to eq(customer.id)
  43. expect(ticket.created_by_id).to eq(customer.id)
  44. expect(article.from).to eq('+491710000000')
  45. expect(article.to).to eq('+4915700000000')
  46. expect(article.cc).to be_nil
  47. expect(article.subject).to be_nil
  48. expect(article.body).to eq('Ldfhxhcuffufuf. Fifififig. Fifififiif Fifififiif Fifififiif Fifififiif Fifififiif')
  49. expect(article.created_by_id).to eq(customer.id)
  50. expect(article.sender.name).to eq('Customer')
  51. expect(article.type.name).to eq('sms')
  52. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms2'), as: :json
  53. expect(response).to have_http_status(:ok)
  54. ticket.reload
  55. expect(ticket.articles.count).to eq(2)
  56. expect(ticket.state.name).to eq('new')
  57. article = Ticket::Article.last
  58. expect(article.from).to eq('+491710000000')
  59. expect(article.to).to eq('+4915700000000')
  60. expect(article.cc).to be_nil
  61. expect(article.subject).to be_nil
  62. expect(article.body).to eq('Follow-up')
  63. expect(article.sender.name).to eq('Customer')
  64. expect(article.type.name).to eq('sms')
  65. expect(article.created_by_id).to eq(customer.id)
  66. # check duplicate callbacks
  67. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms2'), as: :json
  68. expect(response).to have_http_status(:ok)
  69. ticket.reload
  70. expect(ticket.articles.count).to eq(2)
  71. expect(ticket.state.name).to eq('new')
  72. expect(article.id).to eq(Ticket::Article.last.id)
  73. # new ticket needs to be created
  74. ticket.state = Ticket::State.find_by(name: 'closed')
  75. ticket.save!
  76. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms3'), as: :json
  77. expect(response).to have_http_status(:ok)
  78. ticket.reload
  79. expect(ticket.articles.count).to eq(2)
  80. expect(ticket.id).not_to eq(Ticket.last.id)
  81. expect(ticket.state.name).to eq('closed')
  82. ticket = Ticket.last
  83. article = Ticket::Article.last
  84. customer = User.last
  85. expect(ticket.articles.count).to eq(1)
  86. expect(ticket.title).to eq('new 2')
  87. expect(ticket.group_id).to eq(group_id)
  88. expect(ticket.customer_id).to eq(customer.id)
  89. expect(ticket.created_by_id).to eq(customer.id)
  90. expect(article.from).to eq('+491710000000')
  91. expect(article.to).to eq('+4915700000000')
  92. expect(article.cc).to be_nil
  93. expect(article.subject).to be_nil
  94. expect(article.body).to eq('new 2')
  95. expect(article.created_by_id).to eq(customer.id)
  96. expect(article.sender.name).to eq('Customer')
  97. expect(article.type.name).to eq('sms')
  98. # reply by agent
  99. params = {
  100. ticket_id: ticket.id,
  101. body: 'some test',
  102. type: 'sms',
  103. }
  104. authenticated_as(agent)
  105. post '/api/v1/ticket_articles', params: params, as: :json
  106. expect(response).to have_http_status(:created)
  107. expect(json_response).to be_a(Hash)
  108. expect(json_response['subject']).to be_nil
  109. expect(json_response['body']).to eq('some test')
  110. expect(json_response['content_type']).to eq('text/plain')
  111. expect(json_response['updated_by_id']).to eq(agent.id)
  112. expect(json_response['created_by_id']).to eq(agent.id)
  113. stub_request(:post, 'https://rest.messagebird.com/messages')
  114. .to_return(status: 200, body: mocked_response_success, headers: {})
  115. expect(article.preferences[:delivery_retry]).to be_nil
  116. expect(article.preferences[:delivery_status]).to be_nil
  117. perform_enqueued_jobs commit_transaction: true
  118. article = Ticket::Article.find(json_response['id'])
  119. expect(article.preferences[:delivery_retry]).to eq(1)
  120. expect(article.preferences[:delivery_status]).to eq('success')
  121. end
  122. it 'does customer based on already existing mobile attibute' do
  123. customer = create(
  124. :customer,
  125. email: 'me@example.com',
  126. mobile: '01710000000',
  127. )
  128. perform_enqueued_jobs commit_transaction: true
  129. UserInfo.current_user_id = 1
  130. create(
  131. :channel,
  132. area: 'Sms::Account',
  133. options: {
  134. adapter: 'sms/twilio',
  135. webhook_token: 'secret_webhook_token',
  136. account_id: '111',
  137. token: '223',
  138. sender: '333',
  139. },
  140. )
  141. post '/api/v1/sms_webhook/secret_webhook_token', params: read_message('inbound_sms1'), as: :json
  142. expect(response).to have_http_status(:ok)
  143. expect(customer.id).to eq(User.last.id)
  144. end
  145. def read_message(file)
  146. JSON.parse(Rails.root.join('test', 'data', 'message_bird', "#{file}.json").read)
  147. end
  148. def mocked_response_success
  149. '{"id":"1e8cc35873d14fe4ab18bd97a412121","href":"https://rest.messagebird.com/messages/1e8cc35873d14fe4ab18bd121212f971a","direction":"mt","type":"sms","originator":"Zammad GmbH","body":"some test","reference":"Foobar","validity":null,"gateway":10,"typeDetails":{},"datacoding":"plain","mclass":1,"scheduledDatetime":null,"createdDatetime":"2021-07-22T13:25:03+00:00","recipients":{"totalCount":1,"totalSentCount":1,"totalDeliveredCount":0,"totalDeliveryFailedCount":0,"items":[{"recipient":491234,"status":"sent","statusDatetime":"2021-07-22T13:25:03+00:00","messagePartCount":1}]}}'
  150. end
  151. end
  152. end