slack_agent_spec.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. require 'rails_helper'
  2. describe Agents::SlackAgent do
  3. before(:each) do
  4. @fallback = "Its going to rain"
  5. @attachments = [{'fallback' => "{{fallback}}"}]
  6. @valid_params = {
  7. 'webhook_url' => 'https://hooks.slack.com/services/random1/random2/token',
  8. 'channel' => '#random',
  9. 'username' => "{{username}}",
  10. 'message' => "{{message}}",
  11. 'attachments' => @attachments
  12. }
  13. @checker = Agents::SlackAgent.new(:name => "slacker", :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 = { :channel => '#random', :message => 'Looks like its going to rain', username: "Huggin user", fallback: @fallback}
  19. @event.save!
  20. end
  21. describe "validating" do
  22. before do
  23. expect(@checker).to be_valid
  24. end
  25. it "should require a webhook_url" do
  26. @checker.options['webhook_url'] = nil
  27. expect(@checker).not_to be_valid
  28. end
  29. it "should require a channel" do
  30. @checker.options['channel'] = nil
  31. expect(@checker).not_to be_valid
  32. end
  33. it "should allow an icon" do
  34. @checker.options['icon_emoji'] = nil
  35. expect(@checker).to be_valid
  36. @checker.options['icon_emoji'] = ":something:"
  37. expect(@checker).to be_valid
  38. @checker.options['icon_url'] = "http://something.com/image.png"
  39. expect(@checker).to be_valid
  40. @checker.options['icon_emoji'] = "something"
  41. expect(@checker).to be_valid
  42. end
  43. it "should allow attachments" do
  44. @checker.options['attachments'] = nil
  45. expect(@checker).to be_valid
  46. @checker.options['attachments'] = []
  47. expect(@checker).to be_valid
  48. @checker.options['attachments'] = @attachments
  49. expect(@checker).to be_valid
  50. end
  51. end
  52. describe "#receive" do
  53. it "receive an event without errors" do
  54. expect_any_instance_of(Slack::Notifier).to receive(:ping).with(@event.payload[:message],
  55. attachments: [{'fallback' => @fallback}],
  56. channel: @event.payload[:channel],
  57. username: @event.payload[:username]
  58. )
  59. expect { @checker.receive([@event]) }.not_to raise_error
  60. end
  61. end
  62. describe "#working?" do
  63. it "should call received_event_without_error?" do
  64. expect(@checker).to receive(:received_event_without_error?)
  65. @checker.working?
  66. end
  67. end
  68. end