hipchat_agent_spec.rb 2.5KB

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