hipchat_agent_spec.rb 2.3KB

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