1
0

twilio_agent_spec.rb 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. require 'spec_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 => 'x',
  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' }
  18. @event.save!
  19. @sent_messages = []
  20. stub.any_instance_of(Agents::TwilioAgent).send_message { |message| @sent_messages << message}
  21. stub.any_instance_of(Agents::TwilioAgent).make_call {}
  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' }
  28. event1.save!
  29. event2 = Event.new
  30. event2.agent = agents(:bob_weather_agent)
  31. event2.payload = { :message => 'Some other message' }
  32. event2.save!
  33. @checker.receive([@event,event1,event2])
  34. expect(@sent_messages).to eq(['Looks like its going to rain','Some message','Some other message'])
  35. end
  36. it 'should check if receive_text is working fine' do
  37. @checker.options[:receive_text] = 'false'
  38. @checker.receive([@event])
  39. expect(@sent_messages).to be_empty
  40. end
  41. it 'should check if receive_call is working fine' do
  42. @checker.options[:receive_call] = 'true'
  43. @checker.receive([@event])
  44. expect(@checker.memory[:pending_calls]).not_to eq({})
  45. end
  46. end
  47. describe '#working?' do
  48. it 'checks if events have been received within the expected receive period' do
  49. expect(@checker).not_to be_working # No events received
  50. Agents::TwilioAgent.async_receive @checker.id, [@event.id]
  51. expect(@checker.reload).to be_working # Just received events
  52. two_days_from_now = 2.days.from_now
  53. stub(Time).now { two_days_from_now }
  54. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  55. end
  56. end
  57. describe "validation" do
  58. before do
  59. expect(@checker).to be_valid
  60. end
  61. it "should validate presence of of account_sid" do
  62. @checker.options[:account_sid] = ""
  63. expect(@checker).not_to be_valid
  64. end
  65. it "should validate presence of auth_token" do
  66. @checker.options[:auth_token] = ""
  67. expect(@checker).not_to be_valid
  68. end
  69. it "should validate presence of receiver_cell" do
  70. @checker.options[:receiver_cell] = ""
  71. expect(@checker).not_to be_valid
  72. end
  73. it "should validate presence of sender_cell" do
  74. @checker.options[:sender_cell] = ""
  75. expect(@checker).not_to be_valid
  76. end
  77. it "should make sure filling sure filling server_url is not necessary" do
  78. @checker.options[:server_url] = ""
  79. expect(@checker).to be_valid
  80. end
  81. end
  82. end