growl_agent_spec.rb 4.4 KB

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