jabber_agent_spec.rb 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. require 'spec_helper'
  2. require 'models/concerns/liquid_interpolatable'
  3. describe Agents::JabberAgent do
  4. it_behaves_like LiquidInterpolatable
  5. let(:sent) { [] }
  6. let(:config) {
  7. {
  8. jabber_server: '127.0.0.1',
  9. jabber_port: '5222',
  10. jabber_sender: 'foo@localhost',
  11. jabber_receiver: 'bar@localhost',
  12. jabber_password: 'password',
  13. message: 'Warning! {{title}} - {{url}}',
  14. expected_receive_period_in_days: '2'
  15. }
  16. }
  17. let(:agent) do
  18. Agents::JabberAgent.new(name: 'Jabber Agent', options: config).tap do |a|
  19. a.user = users(:bob)
  20. a.save!
  21. end
  22. end
  23. let(:event) do
  24. Event.new.tap do |e|
  25. e.agent = agents(:bob_weather_agent)
  26. e.payload = { :title => 'Weather Alert!', :url => 'http://www.weather.com/' }
  27. e.save!
  28. end
  29. end
  30. before do
  31. stub.any_instance_of(Agents::JabberAgent).deliver { |message| sent << message }
  32. end
  33. describe "#working?" do
  34. it "checks if events have been received within the expected receive period" do
  35. agent.should_not be_working # No events received
  36. Agents::JabberAgent.async_receive agent.id, [event.id]
  37. agent.reload.should be_working # Just received events
  38. two_days_from_now = 2.days.from_now
  39. stub(Time).now { two_days_from_now }
  40. agent.reload.should_not be_working # More time has passed than the expected receive period without any new events
  41. end
  42. end
  43. describe "validation" do
  44. before do
  45. agent.should be_valid
  46. end
  47. it "should validate presence of of jabber_server" do
  48. agent.options[:jabber_server] = ""
  49. agent.should_not be_valid
  50. end
  51. it "should validate presence of jabber_sender" do
  52. agent.options[:jabber_sender] = ""
  53. agent.should_not be_valid
  54. end
  55. it "should validate presence of jabber_receiver" do
  56. agent.options[:jabber_receiver] = ""
  57. agent.should_not be_valid
  58. end
  59. end
  60. describe "receive" do
  61. it "should send an IM for each event" do
  62. event2 = Event.new.tap do |e|
  63. e.agent = agents(:bob_weather_agent)
  64. e.payload = { :title => 'Another Weather Alert!', :url => 'http://www.weather.com/we-are-screwed' }
  65. e.save!
  66. end
  67. agent.receive([event, event2])
  68. sent.should == [ 'Warning! Weather Alert! - http://www.weather.com/',
  69. 'Warning! Another Weather Alert! - http://www.weather.com/we-are-screwed']
  70. end
  71. end
  72. end