twilio_agent_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require 'rails_helper'
  2. describe Agents::TwilioAgent do
  3. before do
  4. @checker = Agents::TwilioAgent.new(:name => 'somename',
  5. :options => { :account_sid => 'x',
  6. :auth_token => 'x',
  7. :sender_cell => 'x',
  8. :receiver_cell => '{{to}}',
  9. :server_url => 'http://somename.com:3000',
  10. :receive_text => 'true',
  11. :receive_call => 'true',
  12. :expected_receive_period_in_days => '1' })
  13. @checker.user = users(:bob)
  14. @checker.save!
  15. @event = Event.new
  16. @event.agent = agents(:bob_weather_agent)
  17. @event.payload = {message: 'Looks like its going to rain', to: 54321}
  18. @event.save!
  19. @message_calls = []
  20. stub.any_instance_of(Twilio::REST::Messages).create { |message| @message_calls << message }
  21. stub.any_instance_of(Twilio::REST::Calls).create
  22. end
  23. describe '#receive' do
  24. it 'should make sure multiple events are being received' do
  25. event1 = Event.new
  26. event1.agent = agents(:bob_rain_notifier_agent)
  27. event1.payload = {message: 'Some message', to: 12345}
  28. event1.save!
  29. event2 = Event.new
  30. event2.agent = agents(:bob_weather_agent)
  31. event2.payload = {message: 'Some other message', to: 987654}
  32. event2.save!
  33. @checker.receive([@event,event1,event2])
  34. expect(@message_calls).to eq([{from: "x", to: "54321", body: "Looks like its going to rain"},
  35. {from: "x", to: "12345", body: "Some message"},
  36. {from: "x", to: "987654", body: "Some other message"}])
  37. end
  38. it 'should check if receive_text is working fine' do
  39. @checker.options[:receive_text] = 'false'
  40. @checker.receive([@event])
  41. expect(@message_calls).to be_empty
  42. end
  43. it 'should check if receive_call is working fine' do
  44. @checker.options[:receive_call] = 'true'
  45. @checker.receive([@event])
  46. expect(@checker.memory[:pending_calls]).not_to eq({})
  47. end
  48. end
  49. describe '#working?' do
  50. it 'checks if events have been received within the expected receive period' do
  51. expect(@checker).not_to be_working # No events received
  52. Agents::TwilioAgent.async_receive @checker.id, [@event.id]
  53. expect(@checker.reload).to be_working # Just received events
  54. two_days_from_now = 2.days.from_now
  55. stub(Time).now { two_days_from_now }
  56. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  57. end
  58. end
  59. describe "validation" do
  60. before do
  61. expect(@checker).to be_valid
  62. end
  63. it "should validate presence of of account_sid" do
  64. @checker.options[:account_sid] = ""
  65. expect(@checker).not_to be_valid
  66. end
  67. it "should validate presence of auth_token" do
  68. @checker.options[:auth_token] = ""
  69. expect(@checker).not_to be_valid
  70. end
  71. it "should validate presence of receiver_cell" do
  72. @checker.options[:receiver_cell] = ""
  73. expect(@checker).not_to be_valid
  74. end
  75. it "should validate presence of sender_cell" do
  76. @checker.options[:sender_cell] = ""
  77. expect(@checker).not_to be_valid
  78. end
  79. it "should make sure filling sure filling server_url is not necessary" do
  80. @checker.options[:server_url] = ""
  81. expect(@checker).to be_valid
  82. end
  83. end
  84. end