twilio_agent_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. @messages = []
  20. @calls = []
  21. allow(Twilio::REST::Client).to receive(:new) do
  22. instance_double(Twilio::REST::Client).tap { |c|
  23. allow(c).to receive(:calls) do
  24. double.tap { |l|
  25. allow(l).to receive(:create) do |message|
  26. @calls << message
  27. end
  28. }
  29. end
  30. allow(c).to receive(:messages) do
  31. double.tap { |l|
  32. allow(l).to receive(:create) do |message|
  33. @messages << message
  34. end
  35. }
  36. end
  37. }
  38. end
  39. end
  40. describe '#receive' do
  41. it 'should make sure multiple events are being received' do
  42. event1 = Event.new
  43. event1.agent = agents(:bob_rain_notifier_agent)
  44. event1.payload = {message: 'Some message', to: 12345}
  45. event1.save!
  46. event2 = Event.new
  47. event2.agent = agents(:bob_weather_agent)
  48. event2.payload = {message: 'Some other message', to: 987654}
  49. event2.save!
  50. @checker.receive([@event,event1,event2])
  51. expect(@messages).to eq [
  52. {from: "x", to: "54321", body: "Looks like its going to rain"},
  53. {from: "x", to: "12345", body: "Some message"},
  54. {from: "x", to: "987654", body: "Some other message"}
  55. ]
  56. end
  57. it 'should check if receive_text is working fine' do
  58. @checker.options[:receive_text] = 'false'
  59. @checker.receive([@event])
  60. expect(@messages).to be_empty
  61. end
  62. it 'should check if receive_call is working fine' do
  63. @checker.options[:receive_call] = 'true'
  64. @checker.receive([@event])
  65. expect(@checker.memory[:pending_calls]).not_to eq({})
  66. end
  67. end
  68. describe '#working?' do
  69. it 'checks if events have been received within the expected receive period' do
  70. expect(@checker).not_to be_working # No events received
  71. Agents::TwilioAgent.async_receive @checker.id, [@event.id]
  72. expect(@checker.reload).to be_working # Just received events
  73. two_days_from_now = 2.days.from_now
  74. allow(Time).to receive(:now) { two_days_from_now }
  75. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  76. end
  77. end
  78. describe "validation" do
  79. before do
  80. expect(@checker).to be_valid
  81. end
  82. it "should validate presence of of account_sid" do
  83. @checker.options[:account_sid] = ""
  84. expect(@checker).not_to be_valid
  85. end
  86. it "should validate presence of auth_token" do
  87. @checker.options[:auth_token] = ""
  88. expect(@checker).not_to be_valid
  89. end
  90. it "should validate presence of receiver_cell" do
  91. @checker.options[:receiver_cell] = ""
  92. expect(@checker).not_to be_valid
  93. end
  94. it "should validate presence of sender_cell" do
  95. @checker.options[:sender_cell] = ""
  96. expect(@checker).not_to be_valid
  97. end
  98. it "should make sure filling sure filling server_url is not necessary" do
  99. @checker.options[:server_url] = ""
  100. expect(@checker).to be_valid
  101. end
  102. end
  103. end