hipchat_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = {
  18. room_name: 'test room',
  19. message: 'Looks like its going to rain',
  20. username: "Huggin user "
  21. }
  22. @event.save!
  23. end
  24. describe "validating" do
  25. before do
  26. expect(@checker).to be_valid
  27. end
  28. it "should require the basecamp username" do
  29. @checker.options['auth_token'] = nil
  30. expect(@checker).not_to be_valid
  31. end
  32. it "should require the basecamp password" do
  33. @checker.options['room_name'] = nil
  34. expect(@checker).not_to be_valid
  35. end
  36. it "should require the basecamp user_id" do
  37. @checker.options['room_name'] = nil
  38. @checker.options['room_name_path'] = 'jsonpath'
  39. expect(@checker).to be_valid
  40. end
  41. it "should also allow a credential" do
  42. @checker.options['auth_token'] = nil
  43. expect(@checker).not_to be_valid
  44. @checker.user.user_credentials.create credential_name: 'hipchat_auth_token', credential_value: 'something'
  45. expect(@checker.reload).to be_valid
  46. end
  47. end
  48. describe "#validate_auth_token" do
  49. it "should return true when valid" do
  50. allow_any_instance_of(HipChat::Client).to receive(:rooms) { true }
  51. expect(@checker.validate_auth_token).to be true
  52. end
  53. it "should return false when invalid" do
  54. allow_any_instance_of(HipChat::Client).to receive(:rooms).and_raise(HipChat::UnknownResponseCode, '403')
  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. allow_any_instance_of(HipChat::Client).to receive(:rooms) {
  61. [OpenStruct.new(name: 'test'), OpenStruct.new(name: 'test1')]
  62. }
  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. expect_any_instance_of(HipChat::Room).to receive(:send).with(@event.payload[:username][0..14],
  69. @event.payload[:message], { notify: false, color: 'yellow', message_format: 'html' })
  70. @checker.receive([@event])
  71. end
  72. end
  73. describe "#working?" do
  74. it "should not be working until the first event was received" do
  75. expect(@checker).not_to be_working
  76. @checker.last_receive_at = Time.now
  77. expect(@checker).to be_working
  78. end
  79. it "should not be working when the last error occured after the last received event" do
  80. @checker.last_receive_at = Time.now - 1.minute
  81. @checker.last_error_log_at = Time.now
  82. expect(@checker).not_to be_working
  83. end
  84. it "should be working when the last received event occured after the last error" do
  85. @checker.last_receive_at = Time.now
  86. @checker.last_error_log_at = Time.now - 1.minute
  87. expect(@checker).to be_working
  88. end
  89. end
  90. end