growl_agent_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. require 'spec_helper'
  2. describe Agents::GrowlAgent do
  3. before do
  4. @checker = Agents::GrowlAgent.new(:name => 'a growl agent',
  5. :options => { :growl_server => 'localhost',
  6. :growl_app_name => 'HuginnGrowlApp',
  7. :growl_password => 'mypassword',
  8. :growl_notification_name => 'Notification',
  9. :expected_receive_period_in_days => '1' })
  10. @checker.user = users(:bob)
  11. @checker.save!
  12. stub.any_instance_of(Growl).notify
  13. @event = Event.new
  14. @event.agent = agents(:bob_weather_agent)
  15. @event.payload = { :subject => 'Weather Alert!', :message => 'Looks like its going to rain' }
  16. @event.save!
  17. end
  18. describe "#working?" do
  19. it "checks if events have been received within the expected receive period" do
  20. expect(@checker).not_to be_working # No events received
  21. Agents::GrowlAgent.async_receive @checker.id, [@event.id]
  22. expect(@checker.reload).to be_working # Just received events
  23. two_days_from_now = 2.days.from_now
  24. stub(Time).now { two_days_from_now }
  25. expect(@checker.reload).not_to be_working # More time has passed than the expected receive period without any new events
  26. end
  27. end
  28. describe "validation" do
  29. before do
  30. expect(@checker).to be_valid
  31. end
  32. it "should validate presence of of growl_server" do
  33. @checker.options[:growl_server] = ""
  34. expect(@checker).not_to be_valid
  35. end
  36. it "should validate presence of expected_receive_period_in_days" do
  37. @checker.options[:expected_receive_period_in_days] = ""
  38. expect(@checker).not_to be_valid
  39. end
  40. end
  41. describe "register_growl" do
  42. it "should set the password for the Growl connection from the agent options" do
  43. @checker.register_growl
  44. expect(@checker.growler.password).to eql(@checker.options[:growl_password])
  45. end
  46. it "should add a notification to the Growl connection" do
  47. called = false
  48. any_instance_of(Growl) do |obj|
  49. called = true
  50. mock(obj).add_notification(@checker.options[:growl_notification_name])
  51. end
  52. @checker.register_growl
  53. expect(called).to be_truthy
  54. end
  55. end
  56. describe "notify_growl" do
  57. before do
  58. @checker.register_growl
  59. end
  60. it "should call Growl.notify with the correct notification name, subject, and message" do
  61. message = "message"
  62. subject = "subject"
  63. called = false
  64. any_instance_of(Growl) do |obj|
  65. called = true
  66. mock(obj).notify(@checker.options[:growl_notification_name],subject,message)
  67. end
  68. @checker.notify_growl(subject,message)
  69. expect(called).to be_truthy
  70. end
  71. end
  72. describe "receive" do
  73. def generate_events_array
  74. events = []
  75. (2..rand(7)).each do
  76. events << @event
  77. end
  78. return events
  79. end
  80. it "should call register_growl once regardless of number of events received" do
  81. mock.proxy(@checker).register_growl.once
  82. @checker.receive(generate_events_array)
  83. end
  84. it "should call notify_growl one time for each event received" do
  85. events = generate_events_array
  86. events.each do |event|
  87. mock.proxy(@checker).notify_growl(event.payload['subject'], event.payload['message'])
  88. end
  89. @checker.receive(events)
  90. end
  91. it "should not call notify_growl if message or subject are missing" do
  92. event_without_a_subject = Event.new
  93. event_without_a_subject.agent = agents(:bob_weather_agent)
  94. event_without_a_subject.payload = { :message => 'Looks like its going to rain' }
  95. event_without_a_subject.save!
  96. event_without_a_message = Event.new
  97. event_without_a_message.agent = agents(:bob_weather_agent)
  98. event_without_a_message.payload = { :subject => 'Weather Alert YO!' }
  99. event_without_a_message.save!
  100. mock.proxy(@checker).notify_growl.never
  101. @checker.receive([event_without_a_subject,event_without_a_message])
  102. end
  103. end
  104. end