hipchat_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. require 'rails_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. expect(@checker).to be_valid
  23. end
  24. it "should require the basecamp username" do
  25. @checker.options['auth_token'] = nil
  26. expect(@checker).not_to be_valid
  27. end
  28. it "should require the basecamp password" do
  29. @checker.options['room_name'] = nil
  30. expect(@checker).not_to 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. expect(@checker).to be_valid
  36. end
  37. it "should also allow a credential" do
  38. @checker.options['auth_token'] = nil
  39. expect(@checker).not_to be_valid
  40. @checker.user.user_credentials.create :credential_name => 'hipchat_auth_token', :credential_value => 'something'
  41. expect(@checker.reload).to be_valid
  42. end
  43. end
  44. describe "#validate_auth_token" do
  45. it "should return true when valid" do
  46. any_instance_of(HipChat::Client) do |klass|
  47. stub(klass).rooms { true }
  48. end
  49. expect(@checker.validate_auth_token).to be true
  50. end
  51. it "should return false when invalid" do
  52. any_instance_of(HipChat::Client) do |klass|
  53. stub(klass).rooms { raise HipChat::UnknownResponseCode.new }
  54. end
  55. expect(@checker.validate_auth_token).to be false
  56. end
  57. end
  58. describe "#complete_room_name" do
  59. it "should return a array of hashes" do
  60. any_instance_of(HipChat::Client) do |klass|
  61. stub(klass).rooms { [OpenStruct.new(name: 'test'), OpenStruct.new(name: 'test1')] }
  62. end
  63. expect(@checker.complete_room_name).to eq [{text: 'test', id: 'test'},{text: 'test1', id: 'test1'}]
  64. end
  65. end
  66. describe "#receive" do
  67. it "send a message to the hipchat" do
  68. any_instance_of(HipChat::Room) do |obj|
  69. mock(obj).send(@event.payload[:username][0..14], @event.payload[:message], {:notify => false, :color => 'yellow'})
  70. end
  71. @checker.receive([@event])
  72. end
  73. end
  74. describe "#working?" do
  75. it "should not be working until the first event was received" do
  76. expect(@checker).not_to be_working
  77. @checker.last_receive_at = Time.now
  78. expect(@checker).to be_working
  79. end
  80. it "should not be working when the last error occured after the last received event" do
  81. @checker.last_receive_at = Time.now - 1.minute
  82. @checker.last_error_log_at = Time.now
  83. expect(@checker).not_to be_working
  84. end
  85. it "should be working when the last received event occured after the last error" do
  86. @checker.last_receive_at = Time.now
  87. @checker.last_error_log_at = Time.now - 1.minute
  88. expect(@checker).to be_working
  89. end
  90. end
  91. end