jabber_agent_spec.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. require 'rails_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. allow_any_instance_of(Agents::JabberAgent).to receive(:deliver) { |agent, 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. allow(Time).to receive(: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. context "#start_worker?" do
  42. it "starts when connect_to_receiver is truthy" do
  43. agent.options[:connect_to_receiver] = 'true'
  44. expect(agent.start_worker?).to be_truthy
  45. end
  46. it "does not starts when connect_to_receiver is not truthy" do
  47. expect(agent.start_worker?).to be_falsy
  48. end
  49. end
  50. describe "validation" do
  51. before do
  52. expect(agent).to be_valid
  53. end
  54. it "should validate presence of of jabber_server" do
  55. agent.options[:jabber_server] = ""
  56. expect(agent).not_to be_valid
  57. end
  58. it "should validate presence of jabber_sender" do
  59. agent.options[:jabber_sender] = ""
  60. expect(agent).not_to be_valid
  61. end
  62. it "should validate presence of jabber_receiver" do
  63. agent.options[:jabber_receiver] = ""
  64. expect(agent).not_to be_valid
  65. end
  66. end
  67. describe "receive" do
  68. it "should send an IM for each event" do
  69. event2 = Event.create!(
  70. agent: agents(:bob_weather_agent),
  71. payload: { title: 'Another Weather Alert!', url: 'http://www.weather.com/we-are-screwed' },
  72. )
  73. agent.receive([event, event2])
  74. expect(sent).to eq([
  75. 'Warning! Weather Alert! - http://www.weather.com/',
  76. 'Warning! Another Weather Alert! - http://www.weather.com/we-are-screwed'
  77. ])
  78. end
  79. end
  80. describe Agents::JabberAgent::Worker do
  81. before(:each) do
  82. @worker = Agents::JabberAgent::Worker.new(agent: agent)
  83. @worker.setup
  84. allow_any_instance_of(Jabber::Client).to receive(:connect)
  85. allow_any_instance_of(Jabber::Client).to receive(:auth)
  86. end
  87. it "runs" do
  88. agent.options[:jabber_receiver] = 'someJID'
  89. expect_any_instance_of(Jabber::MUC::SimpleMUCClient).to receive(:join).with('someJID')
  90. @worker.run
  91. end
  92. it "stops" do
  93. @worker.instance_variable_set(:@client, @worker.client)
  94. expect_any_instance_of(Jabber::Client).to receive(:close)
  95. expect_any_instance_of(Jabber::Client).to receive(:stop)
  96. expect(@worker).to receive(:thread) { double(terminate: nil) }
  97. @worker.stop
  98. end
  99. context "#message_handler" do
  100. it "it ignores messages for the first seconds" do
  101. @worker.instance_variable_set(:@started_at, Time.now)
  102. expect { @worker.message_handler(:on_message, [123456, 'nick', 'hello']) }
  103. .to change { agent.events.count }.by(0)
  104. end
  105. it "creates events" do
  106. @worker.instance_variable_set(:@started_at, Time.now - 10.seconds)
  107. expect { @worker.message_handler(:on_message, [123456, 'nick', 'hello']) }
  108. .to change { agent.events.count }.by(1)
  109. event = agent.events.last
  110. expect(event.payload).to eq({'event' => 'on_message', 'time' => 123456, 'nick' => 'nick', 'message' => 'hello'})
  111. end
  112. end
  113. context "#normalize_args" do
  114. it "handles :on_join and :on_leave" do
  115. time, nick, message = @worker.send(:normalize_args, :on_join, [123456, 'nick'])
  116. expect(time).to eq(123456)
  117. expect(nick).to eq('nick')
  118. expect(message).to be_nil
  119. end
  120. it "handles :on_message and :on_leave" do
  121. time, nick, message = @worker.send(:normalize_args, :on_message, [123456, 'nick', 'hello'])
  122. expect(time).to eq(123456)
  123. expect(nick).to eq('nick')
  124. expect(message).to eq('hello')
  125. end
  126. it "handles :on_room_message" do
  127. time, nick, message = @worker.send(:normalize_args, :on_room_message, [123456, 'hello'])
  128. expect(time).to eq(123456)
  129. expect(nick).to be_nil
  130. expect(message).to eq('hello')
  131. end
  132. end
  133. end
  134. end