hipchat_agent_spec.rb 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'spec_helper'
  2. require 'models/concerns/liquid_interpolatable'
  3. describe Agents::HipchatAgent do
  4. it_behaves_like LiquidInterpolatable
  5. before(:each) do
  6. @valid_params = {
  7. 'auth_token' => 'token',
  8. 'room_name' => 'test',
  9. 'username' => "{{username}}",
  10. 'message' => "{{message}}",
  11. 'notify' => false,
  12. 'color' => 'yellow',
  13. }
  14. @checker = Agents::HipchatAgent.new(:name => "somename", :options => @valid_params)
  15. @checker.user = users(:jane)
  16. @checker.save!
  17. @event = Event.new
  18. @event.agent = agents(:bob_weather_agent)
  19. @event.payload = { :room_name => 'test room', :message => 'Looks like its going to rain', username: "Huggin user"}
  20. @event.save!
  21. end
  22. describe "validating" do
  23. before do
  24. @checker.should be_valid
  25. end
  26. it "should require the basecamp username" do
  27. @checker.options['auth_token'] = nil
  28. @checker.should_not be_valid
  29. end
  30. it "should require the basecamp password" do
  31. @checker.options['room_name'] = nil
  32. @checker.should_not be_valid
  33. end
  34. it "should require the basecamp user_id" do
  35. @checker.options['room_name'] = nil
  36. @checker.options['room_name_path'] = 'jsonpath'
  37. @checker.should be_valid
  38. end
  39. end
  40. describe "#receive" do
  41. it "send a message to the hipchat" do
  42. any_instance_of(HipChat::Room) do |obj|
  43. mock(obj).send(@event.payload[:username], @event.payload[:message], {:notify => 0, :color => 'yellow'})
  44. end
  45. @checker.receive([@event])
  46. end
  47. end
  48. describe "#working?" do
  49. it "should not be working until the first event was received" do
  50. @checker.should_not be_working
  51. @checker.last_receive_at = Time.now
  52. @checker.should be_working
  53. end
  54. it "should not be working when the last error occured after the last received event" do
  55. @checker.last_receive_at = Time.now - 1.minute
  56. @checker.last_error_log_at = Time.now
  57. @checker.should_not be_working
  58. end
  59. it "should be working when the last received event occured after the last error" do
  60. @checker.last_receive_at = Time.now
  61. @checker.last_error_log_at = Time.now - 1.minute
  62. @checker.should be_working
  63. end
  64. end
  65. end