jabber_agent_spec.rb 2.3 KB

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