twilio_receive_text_agent_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'rails_helper'
  2. # Twilio Params
  3. # https://www.twilio.com/docs/api/twiml/sms/twilio_request
  4. # url: https://b924379f.ngrok.io/users/1/web_requests/7/sms-endpoint
  5. # params: {"ToCountry"=>"US", "ToState"=>"NY", "SmsMessageSid"=>"SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "NumMedia"=>"0", "ToCity"=>"NEW YORK", "FromZip"=>"48342", "SmsSid"=>"SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "FromState"=>"MI", "SmsStatus"=>"received", "FromCity"=>"PONTIAC", "Body"=>"Lol", "FromCountry"=>"US", "To"=>"+1347555555", "ToZip"=>"10016", "NumSegments"=>"1", "MessageSid"=>"SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "AccountSid"=>"ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "From"=>"+12485551111", "ApiVersion"=>"2010-04-01"}
  6. # signature: K29NMD9+v5/QLzbdGZW/DRGyxNU=
  7. describe Agents::TwilioReceiveTextAgent do
  8. before do
  9. stub.any_instance_of(Twilio::Util::RequestValidator).validate { true }
  10. end
  11. let(:payload) {
  12. {
  13. "ToCountry"=>"US",
  14. "ToState"=>"NY",
  15. "SmsMessageSid"=>"SMxxxxxxxxxxxxxxxx",
  16. "NumMedia"=>"0",
  17. "ToCity"=>"NEW YORK",
  18. "FromZip"=>"48342",
  19. "SmsSid"=>"SMxxxxxxxxxxxxxxxx",
  20. "FromState"=>"MI",
  21. "SmsStatus"=>"received",
  22. "FromCity"=>"PONTIAC",
  23. "Body"=>"Hy ",
  24. "FromCountry"=>"US",
  25. "To"=>"+1347555555",
  26. "ToZip"=>"10016",
  27. "NumSegments"=>"1",
  28. "MessageSid"=>"SMxxxxxxxxxxxxxxxx",
  29. "AccountSid"=>"ACxxxxxxxxxxxxxxxx",
  30. "From"=>"+12485551111",
  31. "ApiVersion"=>"2010-04-01"}
  32. }
  33. describe 'receive_twilio_text_message' do
  34. before do
  35. @agent = Agents::TwilioReceiveTextAgent.new(
  36. :name => 'twilioreceive',
  37. :options => { :account_sid => 'x',
  38. :auth_token => 'x',
  39. :server_url => 'http://example.com',
  40. :expected_receive_period_in_days => 1
  41. }
  42. )
  43. @agent.user = users(:bob)
  44. @agent.save!
  45. end
  46. it 'should create event upon receiving request' do
  47. request = ActionDispatch::Request.new({
  48. 'action_dispatch.request.request_parameters' => payload.merge({"secret" => "sms-endpoint"}),
  49. 'REQUEST_METHOD' => "POST",
  50. 'HTTP_ACCEPT' => 'application/xml',
  51. 'HTTP_X_TWILIO_SIGNATURE' => "HpS7PBa1Agvt4OtO+wZp75IuQa0="
  52. })
  53. out = nil
  54. expect {
  55. out = @agent.receive_web_request(request)
  56. }.to change { Event.count }.by(1)
  57. expect(out).to eq(["<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response></Response>", 201, "text/xml"])
  58. expect(Event.last.payload).to eq(payload)
  59. end
  60. end
  61. describe 'receive_twilio_text_message and send a response' do
  62. before do
  63. @agent = Agents::TwilioReceiveTextAgent.new(
  64. :name => 'twilioreceive',
  65. :options => { :account_sid => 'x',
  66. :auth_token => 'x',
  67. :server_url => 'http://example.com',
  68. :reply_text => "thanks!",
  69. :expected_receive_period_in_days => 1
  70. }
  71. )
  72. @agent.user = users(:bob)
  73. @agent.save!
  74. end
  75. it 'should create event and send back TwiML Message if reply_text is set' do
  76. out = nil
  77. request = ActionDispatch::Request.new({
  78. 'action_dispatch.request.request_parameters' => payload.merge({"secret" => "sms-endpoint"}),
  79. 'REQUEST_METHOD' => "POST",
  80. 'HTTP_ACCEPT' => 'application/xml',
  81. 'HTTP_X_TWILIO_SIGNATURE' => "HpS7PBa1Agvt4OtO+wZp75IuQa0="
  82. })
  83. expect {
  84. out = @agent.receive_web_request(request)
  85. }.to change { Event.count }.by(1)
  86. expect(out).to eq(["<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Message>thanks!</Message></Response>", 201, "text/xml"])
  87. expect(Event.last.payload).to eq(payload)
  88. end
  89. end
  90. end