Skip to content

Commit

Permalink
Specs for GrowlAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
snicker committed Mar 3, 2014
1 parent b944eb0 commit bd206d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/agents/growl_agent.rb
Expand Up @@ -49,7 +49,7 @@ def receive(incoming_events)
register_growl
message = (event.payload['message'] || event.payload['text']).to_s
subject = event.payload['subject'].to_s
if message != "" && subject != ""
if message.present? && subject.present?
log "Sending Growl notification '#{subject}': '#{message}' to #{options['growlserver']} with event #{event.id}"
notify_growl(subject,message)
else
Expand Down
50 changes: 50 additions & 0 deletions spec/models/agents/growl_agent_spec.rb
@@ -0,0 +1,50 @@
require 'spec_helper'

describe Agents::GrowlAgent do
before do
@checker = Agents::GrowlAgent.new(:name => 'a growl agent',
:options => { :growlserver => 'localhost',
:growlappname => 'HuginnGrowlApp',
:growlnotificationname => 'Notification',
:expected_receive_period_in_days => '1' })
@checker.user = users(:bob)
@checker.save!

class Agents::GrowlAgent #this should really be done with RSpec-Mocks
def notify_growl(message,subject)
end
end

@event = Event.new
@event.agent = agents(:bob_weather_agent)
@event.payload = { :subject => 'Weather Alert!', :message => 'Looks like its going to rain' }
@event.save!
end

describe "#working?" do
it "checks if events have been received within the expected receive period" do
@checker.should_not be_working # No events received
Agents::GrowlAgent.async_receive @checker.id, [@event.id]
@checker.reload.should be_working # Just received events
two_days_from_now = 2.days.from_now
stub(Time).now { two_days_from_now }
@checker.reload.should_not be_working # More time has passed than the expected receive period without any new events
end
end

describe "validation" do
before do
@checker.should be_valid
end

it "should validate presence of of growlserver" do
@checker.options[:growlserver] = ""
@checker.should_not be_valid
end

it "should validate presence of expected_receive_period_in_days" do
@checker.options[:expected_receive_period_in_days] = ""
@checker.should_not be_valid
end
end
end

0 comments on commit bd206d0

Please sign in to comment.