12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- require 'spec_helper'
- require 'models/concerns/liquid_interpolatable'
- describe Agents::JabberAgent do
- it_behaves_like LiquidInterpolatable
- let(:sent) { [] }
- let(:config) {
- {
- jabber_server: '127.0.0.1',
- jabber_port: '5222',
- jabber_sender: 'foo@localhost',
- jabber_receiver: 'bar@localhost',
- jabber_password: 'password',
- message: 'Warning! {{title}} - {{url}}',
- expected_receive_period_in_days: '2'
- }
- }
- let(:agent) do
- Agents::JabberAgent.new(name: 'Jabber Agent', options: config).tap do |a|
- a.user = users(:bob)
- a.save!
- end
- end
- let(:event) do
- Event.new.tap do |e|
- e.agent = agents(:bob_weather_agent)
- e.payload = { :title => 'Weather Alert!', :url => 'http://www.weather.com/' }
- e.save!
- end
- end
- before do
- stub.any_instance_of(Agents::JabberAgent).deliver { |message| sent << message }
- end
- describe "#working?" do
- it "checks if events have been received within the expected receive period" do
- agent.should_not be_working
- Agents::JabberAgent.async_receive agent.id, [event.id]
- agent.reload.should be_working
- two_days_from_now = 2.days.from_now
- stub(Time).now { two_days_from_now }
- agent.reload.should_not be_working
- end
- end
- describe "validation" do
- before do
- agent.should be_valid
- end
- it "should validate presence of of jabber_server" do
- agent.options[:jabber_server] = ""
- agent.should_not be_valid
- end
- it "should validate presence of jabber_sender" do
- agent.options[:jabber_sender] = ""
- agent.should_not be_valid
- end
- it "should validate presence of jabber_receiver" do
- agent.options[:jabber_receiver] = ""
- agent.should_not be_valid
- end
- end
- describe "receive" do
- it "should send an IM for each event" do
- event2 = Event.new.tap do |e|
- e.agent = agents(:bob_weather_agent)
- e.payload = { :title => 'Another Weather Alert!', :url => 'http://www.weather.com/we-are-screwed' }
- e.save!
- end
- agent.receive([event, event2])
- sent.should == [ 'Warning! Weather Alert! - http://www.weather.com/',
- 'Warning! Another Weather Alert! - http://www.weather.com/we-are-screwed']
- end
- end
- end
|