File: test_slack.rb

package info (click to toggle)
ruby-god 0.13.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 832 kB
  • sloc: ruby: 6,641; ansic: 237; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require File.dirname(__FILE__) + '/helper'

class TestSlack < Minitest::Test
  def setup
    @slack = God::Contacts::Slack.new

    # new slack webhook url contains three random 'tokens' with length of 9, 9 and 24 characters
    @slack.url = "https://hooks.slack.com/services/ABCABCABC/DEFDEFDEF/ABCDEFABCDEFABCDEFABCDEF"

    @sample_data = {
      :message => "a sample message",
      :time => "2038-01-01 00:00:00",
      :priority => "High",
      :category => "Test",
      :host => "example.com"
    }
  end

  def test_api_url
    assert_equal @slack.url, @slack.api_url.to_s
  end

  def test_notify
    Net::HTTP.any_instance.expects(:request).returns(Net::HTTPSuccess.new('a', 'b', 'c'))

    @slack.channel = "#ops"

    @slack.notify('msg', Time.now, 'prio', 'cat', 'host')
    assert_equal "successfully notified slack on channel #ops", @slack.info
  end

  def test_default_channel
    Net::HTTP.any_instance.expects(:request).returns(Net::HTTPSuccess.new('a', 'b', 'c'))

    @slack.notify('msg', Time.now, 'prio', 'cat', 'host')
    assert_equal "successfully notified slack on channel #general", @slack.info
  end

  def test_default_formatting
    text = @slack.text(@sample_data)
    assert_equal "High alert on example.com: a sample message (Test, 2038-01-01 00:00:00)", text
  end

  def test_custom_formatting
    @slack.format = "%{host}: %{message}"
    text = @slack.text(@sample_data)
    assert_equal "example.com: a sample message", text
  end

  def test_notify_channel
    @slack.notify_channel = true
    @slack.format = ""
    text = @slack.text(@sample_data)
    assert_equal "<!channel> ", text
  end
end