exception_notification 4.3.0 → 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Appraisals +2 -2
- data/CHANGELOG.rdoc +14 -0
- data/CONTRIBUTING.md +18 -0
- data/Gemfile +1 -1
- data/README.md +64 -935
- data/Rakefile +2 -2
- data/docs/notifiers/campfire.md +50 -0
- data/docs/notifiers/custom.md +42 -0
- data/docs/notifiers/datadog.md +51 -0
- data/docs/notifiers/email.md +195 -0
- data/docs/notifiers/google_chat.md +31 -0
- data/docs/notifiers/hipchat.md +66 -0
- data/docs/notifiers/irc.md +97 -0
- data/docs/notifiers/mattermost.md +115 -0
- data/docs/notifiers/slack.md +161 -0
- data/docs/notifiers/sns.md +37 -0
- data/docs/notifiers/teams.md +54 -0
- data/docs/notifiers/webhook.md +60 -0
- data/examples/sample_app.rb +54 -0
- data/examples/sinatra/Gemfile +6 -6
- data/examples/sinatra/config.ru +1 -1
- data/examples/sinatra/sinatra_app.rb +14 -10
- data/exception_notification.gemspec +27 -22
- data/gemfiles/rails4_0.gemfile +3 -3
- data/gemfiles/rails4_1.gemfile +3 -3
- data/gemfiles/rails4_2.gemfile +3 -3
- data/gemfiles/rails5_0.gemfile +3 -3
- data/gemfiles/rails5_1.gemfile +3 -3
- data/gemfiles/rails5_2.gemfile +7 -0
- data/gemfiles/rails6_0.gemfile +7 -0
- data/lib/exception_notification.rb +1 -0
- data/lib/exception_notification/rack.rb +8 -21
- data/lib/exception_notification/resque.rb +8 -10
- data/lib/exception_notification/sidekiq.rb +8 -12
- data/lib/exception_notification/version.rb +3 -0
- data/lib/exception_notifier.rb +20 -3
- data/lib/exception_notifier/base_notifier.rb +2 -3
- data/lib/exception_notifier/campfire_notifier.rb +12 -13
- data/lib/exception_notifier/datadog_notifier.rb +153 -0
- data/lib/exception_notifier/email_notifier.rb +64 -87
- data/lib/exception_notifier/google_chat_notifier.rb +25 -119
- data/lib/exception_notifier/hipchat_notifier.rb +11 -12
- data/lib/exception_notifier/irc_notifier.rb +32 -30
- data/lib/exception_notifier/mattermost_notifier.rb +47 -140
- data/lib/exception_notifier/modules/backtrace_cleaner.rb +0 -2
- data/lib/exception_notifier/modules/error_grouping.rb +5 -5
- data/lib/exception_notifier/modules/formatter.rb +118 -0
- data/lib/exception_notifier/notifier.rb +5 -6
- data/lib/exception_notifier/slack_notifier.rb +63 -40
- data/lib/exception_notifier/sns_notifier.rb +17 -11
- data/lib/exception_notifier/teams_notifier.rb +58 -44
- data/lib/exception_notifier/views/exception_notifier/_backtrace.html.erb +1 -1
- data/lib/exception_notifier/views/exception_notifier/_environment.text.erb +1 -1
- data/lib/exception_notifier/views/exception_notifier/_request.text.erb +1 -1
- data/lib/exception_notifier/views/exception_notifier/exception_notification.html.erb +2 -2
- data/lib/exception_notifier/views/exception_notifier/exception_notification.text.erb +2 -2
- data/lib/exception_notifier/webhook_notifier.rb +14 -11
- data/lib/generators/exception_notification/install_generator.rb +5 -5
- data/lib/generators/exception_notification/templates/{exception_notification.rb → exception_notification.rb.erb} +13 -11
- data/test/exception_notification/rack_test.rb +27 -11
- data/test/exception_notification/resque_test.rb +52 -0
- data/test/exception_notifier/campfire_notifier_test.rb +42 -42
- data/test/exception_notifier/datadog_notifier_test.rb +151 -0
- data/test/exception_notifier/email_notifier_test.rb +269 -153
- data/test/exception_notifier/google_chat_notifier_test.rb +154 -101
- data/test/exception_notifier/hipchat_notifier_test.rb +78 -81
- data/test/exception_notifier/irc_notifier_test.rb +34 -34
- data/test/exception_notifier/mattermost_notifier_test.rb +164 -67
- data/test/exception_notifier/modules/error_grouping_test.rb +39 -40
- data/test/exception_notifier/modules/formatter_test.rb +150 -0
- data/test/exception_notifier/sidekiq_test.rb +6 -6
- data/test/exception_notifier/slack_notifier_test.rb +61 -60
- data/test/exception_notifier/sns_notifier_test.rb +27 -32
- data/test/exception_notifier/teams_notifier_test.rb +23 -26
- data/test/exception_notifier/webhook_notifier_test.rb +48 -46
- data/test/exception_notifier_test.rb +41 -38
- data/test/{dummy/app → support}/views/exception_notifier/_new_bkg_section.html.erb +0 -0
- data/test/{dummy/app → support}/views/exception_notifier/_new_bkg_section.text.erb +0 -0
- data/test/{dummy/app → support}/views/exception_notifier/_new_section.html.erb +0 -0
- data/test/{dummy/app → support}/views/exception_notifier/_new_section.text.erb +0 -0
- data/test/test_helper.rb +11 -14
- metadata +136 -166
- data/test/dummy/.gitignore +0 -4
- data/test/dummy/Rakefile +0 -7
- data/test/dummy/app/controllers/application_controller.rb +0 -3
- data/test/dummy/app/controllers/posts_controller.rb +0 -30
- data/test/dummy/app/helpers/application_helper.rb +0 -2
- data/test/dummy/app/helpers/posts_helper.rb +0 -2
- data/test/dummy/app/models/post.rb +0 -2
- data/test/dummy/app/views/layouts/application.html.erb +0 -14
- data/test/dummy/app/views/posts/_form.html.erb +0 -0
- data/test/dummy/app/views/posts/new.html.erb +0 -0
- data/test/dummy/app/views/posts/show.html.erb +0 -0
- data/test/dummy/config.ru +0 -4
- data/test/dummy/config/application.rb +0 -42
- data/test/dummy/config/boot.rb +0 -6
- data/test/dummy/config/database.yml +0 -22
- data/test/dummy/config/environment.rb +0 -17
- data/test/dummy/config/environments/development.rb +0 -25
- data/test/dummy/config/environments/production.rb +0 -50
- data/test/dummy/config/environments/test.rb +0 -35
- data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/test/dummy/config/initializers/inflections.rb +0 -10
- data/test/dummy/config/initializers/mime_types.rb +0 -5
- data/test/dummy/config/initializers/secret_token.rb +0 -8
- data/test/dummy/config/initializers/session_store.rb +0 -8
- data/test/dummy/config/locales/en.yml +0 -5
- data/test/dummy/config/routes.rb +0 -3
- data/test/dummy/db/migrate/20110729022608_create_posts.rb +0 -15
- data/test/dummy/db/schema.rb +0 -24
- data/test/dummy/db/seeds.rb +0 -7
- data/test/dummy/lib/tasks/.gitkeep +0 -0
- data/test/dummy/public/404.html +0 -26
- data/test/dummy/public/422.html +0 -26
- data/test/dummy/public/500.html +0 -26
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/images/rails.png +0 -0
- data/test/dummy/public/index.html +0 -239
- data/test/dummy/public/javascripts/application.js +0 -2
- data/test/dummy/public/javascripts/controls.js +0 -965
- data/test/dummy/public/javascripts/dragdrop.js +0 -974
- data/test/dummy/public/javascripts/effects.js +0 -1123
- data/test/dummy/public/javascripts/prototype.js +0 -6001
- data/test/dummy/public/javascripts/rails.js +0 -191
- data/test/dummy/public/robots.txt +0 -5
- data/test/dummy/public/stylesheets/.gitkeep +0 -0
- data/test/dummy/public/stylesheets/scaffold.css +0 -56
- data/test/dummy/script/rails +0 -6
- data/test/dummy/test/functional/posts_controller_test.rb +0 -237
- data/test/dummy/test/test_helper.rb +0 -7
@@ -1,128 +1,181 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'rack'
|
2
3
|
require 'httparty'
|
4
|
+
require 'timecop'
|
5
|
+
require 'json'
|
3
6
|
|
4
7
|
class GoogleChatNotifierTest < ActiveSupport::TestCase
|
8
|
+
URL = 'http://localhost:8000'.freeze
|
5
9
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
}
|
10
|
-
google_chat_notifier = ExceptionNotifier::GoogleChatNotifier.new
|
11
|
-
google_chat_notifier.httparty = FakeHTTParty.new
|
12
|
-
|
13
|
-
options = google_chat_notifier.call ArgumentError.new("foo"), options
|
10
|
+
def setup
|
11
|
+
Timecop.freeze('2018-12-09 12:07:16 UTC')
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
def teardown
|
15
|
+
Timecop.return
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
assert_equal 'An *ArgumentError* occured.', text[2]
|
22
|
-
assert_equal '*foo*', text[5]
|
18
|
+
test 'should send notification if properly configured' do
|
19
|
+
HTTParty.expects(:post).with(URL, post_opts("#{header}\n#{body}"))
|
20
|
+
notifier.call ArgumentError.new('foo')
|
23
21
|
end
|
24
22
|
|
25
|
-
test
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
test 'shoud use errors count if accumulated_errors_count is provided' do
|
24
|
+
text = [
|
25
|
+
'',
|
26
|
+
"Application: *#{app_name}*",
|
27
|
+
'5 *ArgumentError* occurred.',
|
28
|
+
'',
|
29
|
+
body
|
30
|
+
].join("\n")
|
31
|
+
|
32
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
30
33
|
|
31
|
-
|
34
|
+
notifier.call(ArgumentError.new('foo'), accumulated_errors_count: 5)
|
32
35
|
end
|
33
36
|
|
34
|
-
test
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
test 'Message request should be formatted as hash' do
|
38
|
+
text = [
|
39
|
+
header,
|
40
|
+
body,
|
41
|
+
'',
|
42
|
+
'*Request:*',
|
43
|
+
'```',
|
44
|
+
'* url : http://test.address/?id=foo',
|
45
|
+
'* http_method : GET',
|
46
|
+
'* ip_address : 127.0.0.1',
|
47
|
+
'* parameters : {"id"=>"foo"}',
|
48
|
+
'* timestamp : 2018-12-09 12:07:16 UTC',
|
49
|
+
'```'
|
50
|
+
].join("\n")
|
51
|
+
|
52
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
53
|
+
|
54
|
+
notifier.call(ArgumentError.new('foo'), env: test_env)
|
55
|
+
end
|
39
56
|
|
40
|
-
|
57
|
+
test 'backtrace with less than 3 lines should be displayed fully' do
|
58
|
+
text = [
|
59
|
+
header,
|
60
|
+
body,
|
61
|
+
'',
|
62
|
+
'*Backtrace:*',
|
63
|
+
'```',
|
64
|
+
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
|
65
|
+
"* app/controllers/my_controller.rb:34:in `update'",
|
66
|
+
'```'
|
67
|
+
].join("\n")
|
68
|
+
|
69
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
70
|
+
|
71
|
+
exception = ArgumentError.new('foo')
|
72
|
+
exception.set_backtrace([
|
73
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
74
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
75
|
+
])
|
76
|
+
|
77
|
+
notifier.call(exception)
|
41
78
|
end
|
42
79
|
|
43
|
-
test
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
80
|
+
test 'backtrace with more than 3 lines should display only top 3 lines' do
|
81
|
+
text = [
|
82
|
+
header,
|
83
|
+
body,
|
84
|
+
'',
|
85
|
+
'*Backtrace:*',
|
86
|
+
'```',
|
87
|
+
"* app/controllers/my_controller.rb:99:in `specific_function'",
|
88
|
+
"* app/controllers/my_controller.rb:70:in `specific_param'",
|
89
|
+
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
|
90
|
+
'```'
|
91
|
+
].join("\n")
|
92
|
+
|
93
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
94
|
+
|
95
|
+
exception = ArgumentError.new('foo')
|
96
|
+
exception.set_backtrace([
|
97
|
+
"app/controllers/my_controller.rb:99:in `specific_function'",
|
98
|
+
"app/controllers/my_controller.rb:70:in `specific_param'",
|
99
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
100
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
101
|
+
])
|
102
|
+
|
103
|
+
notifier.call(exception)
|
58
104
|
end
|
59
105
|
|
60
|
-
test '
|
61
|
-
|
106
|
+
test 'Get text with backtrace and request info' do
|
107
|
+
text = [
|
108
|
+
header,
|
109
|
+
body,
|
110
|
+
'',
|
111
|
+
'*Request:*',
|
112
|
+
'```',
|
113
|
+
'* url : http://test.address/?id=foo',
|
114
|
+
'* http_method : GET',
|
115
|
+
'* ip_address : 127.0.0.1',
|
116
|
+
'* parameters : {"id"=>"foo"}',
|
117
|
+
'* timestamp : 2018-12-09 12:07:16 UTC',
|
118
|
+
'```',
|
119
|
+
'',
|
120
|
+
'*Backtrace:*',
|
121
|
+
'```',
|
122
|
+
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
|
123
|
+
"* app/controllers/my_controller.rb:34:in `update'",
|
124
|
+
'```'
|
125
|
+
].join("\n")
|
126
|
+
|
127
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
128
|
+
|
129
|
+
exception = ArgumentError.new('foo')
|
130
|
+
exception.set_backtrace([
|
131
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
132
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
133
|
+
])
|
134
|
+
|
135
|
+
notifier.call(exception, env: test_env)
|
136
|
+
end
|
62
137
|
|
63
|
-
|
64
|
-
google_chat_notifier.instance_variable_set(:@backtrace, backtrace)
|
138
|
+
private
|
65
139
|
|
66
|
-
|
67
|
-
|
68
|
-
assert_includes message_backtrace, "* app/controllers/my_controller.rb:34:in `update'"
|
140
|
+
def notifier
|
141
|
+
ExceptionNotifier::GoogleChatNotifier.new(webhook_url: URL)
|
69
142
|
end
|
70
143
|
|
71
|
-
|
72
|
-
|
144
|
+
def post_opts(text)
|
145
|
+
{
|
146
|
+
body: { text: text }.to_json,
|
147
|
+
headers: { 'Content-Type' => 'application/json' }
|
148
|
+
}
|
149
|
+
end
|
73
150
|
|
74
|
-
|
75
|
-
|
151
|
+
def test_env
|
152
|
+
Rack::MockRequest.env_for(
|
153
|
+
'/',
|
154
|
+
'HTTP_HOST' => 'test.address',
|
155
|
+
'REMOTE_ADDR' => '127.0.0.1',
|
156
|
+
'HTTP_USER_AGENT' => 'Rails Testing',
|
157
|
+
params: { id: 'foo' }
|
158
|
+
)
|
159
|
+
end
|
76
160
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
161
|
+
def header
|
162
|
+
[
|
163
|
+
'',
|
164
|
+
"Application: *#{app_name}*",
|
165
|
+
'An *ArgumentError* occurred.',
|
166
|
+
''
|
167
|
+
].join("\n")
|
82
168
|
end
|
83
169
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
timestamp: Time.parse('2018-08-13 12:13:24 UTC') }
|
95
|
-
google_chat_notifier.instance_variable_set(:@request_items, request_items)
|
96
|
-
|
97
|
-
google_chat_notifier.instance_variable_set(:@options, {accumulated_errors_count: 0})
|
98
|
-
|
99
|
-
google_chat_notifier.instance_variable_set(:@application_name, 'dummy')
|
100
|
-
|
101
|
-
exception = ArgumentError.new("foo")
|
102
|
-
google_chat_notifier.instance_variable_set(:@exception, exception)
|
103
|
-
|
104
|
-
text = google_chat_notifier.send(:exception_text)
|
105
|
-
expected_text = %q(
|
106
|
-
Application: *dummy*
|
107
|
-
An *ArgumentError* occured.
|
108
|
-
|
109
|
-
⚠️ Error 500 in test ⚠️
|
110
|
-
*foo*
|
111
|
-
|
112
|
-
*Request:*
|
113
|
-
```
|
114
|
-
* url : http://test.address
|
115
|
-
* http_method : get
|
116
|
-
* ip_address : 127.0.0.1
|
117
|
-
* parameters : {"id"=>"foo"}
|
118
|
-
* timestamp : 2018-08-13 12:13:24 UTC
|
119
|
-
```
|
120
|
-
|
121
|
-
*Backtrace:*
|
122
|
-
```
|
123
|
-
* app/controllers/my_controller.rb:53:in `my_controller_params'
|
124
|
-
* app/controllers/my_controller.rb:34:in `update'
|
125
|
-
```)
|
126
|
-
assert_equal text, expected_text
|
170
|
+
def body
|
171
|
+
if defined?(::Rails) && ::Rails.respond_to?(:env)
|
172
|
+
"⚠️ Error occurred in test ⚠️\n*foo*"
|
173
|
+
else
|
174
|
+
"⚠️ Error occurred ⚠️\n*foo*"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
def app_name
|
179
|
+
'dummy' if defined?(::Rails) && ::Rails.respond_to?(:application)
|
127
180
|
end
|
128
181
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'rack'
|
2
3
|
|
3
4
|
# silence_warnings trick around require can be removed once
|
4
5
|
# https://github.com/hipchat/hipchat-rb/pull/174
|
@@ -8,31 +9,31 @@ silence_warnings do
|
|
8
9
|
end
|
9
10
|
|
10
11
|
class HipchatNotifierTest < ActiveSupport::TestCase
|
11
|
-
|
12
|
-
test "should send hipchat notification if properly configured" do
|
12
|
+
test 'should send hipchat notification if properly configured' do
|
13
13
|
options = {
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:
|
14
|
+
api_token: 'good_token',
|
15
|
+
room_name: 'room_name',
|
16
|
+
color: 'yellow'
|
17
17
|
}
|
18
18
|
|
19
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body,
|
19
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body, color: 'yellow')
|
20
20
|
|
21
21
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
22
22
|
hipchat.call(fake_exception)
|
23
23
|
end
|
24
24
|
|
25
|
-
test
|
26
|
-
pre_callback_called
|
25
|
+
test 'should call pre/post_callback if specified' do
|
26
|
+
pre_callback_called = 0
|
27
|
+
post_callback_called = 0
|
27
28
|
options = {
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
29
|
+
api_token: 'good_token',
|
30
|
+
room_name: 'room_name',
|
31
|
+
color: 'yellow',
|
32
|
+
pre_callback: proc { |*| pre_callback_called += 1 },
|
33
|
+
post_callback: proc { |*| post_callback_called += 1 }
|
33
34
|
}
|
34
35
|
|
35
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body, { :
|
36
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body, { color: 'yellow' }.merge(options.except(:api_token, :room_name)))
|
36
37
|
|
37
38
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
38
39
|
hipchat.call(fake_exception)
|
@@ -40,150 +41,150 @@ class HipchatNotifierTest < ActiveSupport::TestCase
|
|
40
41
|
assert_equal(1, post_callback_called)
|
41
42
|
end
|
42
43
|
|
43
|
-
test
|
44
|
+
test 'should send hipchat notification without backtrace info if properly configured' do
|
44
45
|
options = {
|
45
|
-
:
|
46
|
-
:
|
47
|
-
:
|
46
|
+
api_token: 'good_token',
|
47
|
+
room_name: 'room_name',
|
48
|
+
color: 'yellow'
|
48
49
|
}
|
49
50
|
|
50
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body_without_backtrace,
|
51
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body_without_backtrace, color: 'yellow')
|
51
52
|
|
52
53
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
53
54
|
hipchat.call(fake_exception_without_backtrace)
|
54
55
|
end
|
55
56
|
|
56
|
-
test
|
57
|
+
test 'should allow custom from value if set' do
|
57
58
|
options = {
|
58
|
-
:
|
59
|
-
:
|
60
|
-
:
|
59
|
+
api_token: 'good_token',
|
60
|
+
room_name: 'room_name',
|
61
|
+
from: 'TrollFace'
|
61
62
|
}
|
62
63
|
|
63
|
-
HipChat::Room.any_instance.expects(:send).with('TrollFace', fake_body,
|
64
|
+
HipChat::Room.any_instance.expects(:send).with('TrollFace', fake_body, color: 'red')
|
64
65
|
|
65
66
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
66
67
|
hipchat.call(fake_exception)
|
67
68
|
end
|
68
69
|
|
69
|
-
test
|
70
|
+
test 'should not send hipchat notification if badly configured' do
|
70
71
|
wrong_params = {
|
71
|
-
:
|
72
|
-
:
|
72
|
+
api_token: 'bad_token',
|
73
|
+
room_name: 'test_room'
|
73
74
|
}
|
74
75
|
|
75
|
-
HipChat::Client.stubs(:new).with('bad_token',
|
76
|
+
HipChat::Client.stubs(:new).with('bad_token', api_version: 'v1').returns(nil)
|
76
77
|
|
77
78
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
78
79
|
assert_nil hipchat.room
|
79
80
|
end
|
80
81
|
|
81
|
-
test
|
82
|
-
wrong_params
|
82
|
+
test 'should not send hipchat notification if api_key is missing' do
|
83
|
+
wrong_params = { room_name: 'test_room' }
|
83
84
|
|
84
|
-
HipChat::Client.stubs(:new).with(nil,
|
85
|
+
HipChat::Client.stubs(:new).with(nil, api_version: 'v1').returns(nil)
|
85
86
|
|
86
87
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
87
88
|
assert_nil hipchat.room
|
88
89
|
end
|
89
90
|
|
90
|
-
test
|
91
|
-
wrong_params
|
91
|
+
test 'should not send hipchat notification if room_name is missing' do
|
92
|
+
wrong_params = { api_token: 'good_token' }
|
92
93
|
|
93
|
-
HipChat::Client.stubs(:new).with('good_token',
|
94
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v1').returns({})
|
94
95
|
|
95
96
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
96
97
|
assert_nil hipchat.room
|
97
98
|
end
|
98
99
|
|
99
|
-
test
|
100
|
+
test 'should send hipchat notification with message_template' do
|
100
101
|
options = {
|
101
|
-
:
|
102
|
-
:
|
103
|
-
:
|
104
|
-
:
|
102
|
+
api_token: 'good_token',
|
103
|
+
room_name: 'room_name',
|
104
|
+
color: 'yellow',
|
105
|
+
message_template: ->(exception, _) { "This is custom message: '#{exception.message}'" }
|
105
106
|
}
|
106
107
|
|
107
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', "This is custom message: '#{fake_exception.message}'",
|
108
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', "This is custom message: '#{fake_exception.message}'", color: 'yellow')
|
108
109
|
|
109
110
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
110
111
|
hipchat.call(fake_exception)
|
111
112
|
end
|
112
113
|
|
113
|
-
test
|
114
|
+
test 'should send hipchat notification exclude accumulated errors count' do
|
114
115
|
options = {
|
115
|
-
:
|
116
|
-
:
|
117
|
-
:
|
116
|
+
api_token: 'good_token',
|
117
|
+
room_name: 'room_name',
|
118
|
+
color: 'yellow'
|
118
119
|
}
|
119
120
|
|
120
|
-
HipChat::Room.any_instance.expects(:send).with{ |_, msg, _| msg.start_with?(
|
121
|
+
HipChat::Room.any_instance.expects(:send).with { |_, msg, _| msg.start_with?('A new exception occurred:') }
|
121
122
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
122
123
|
hipchat.call(fake_exception)
|
123
124
|
end
|
124
125
|
|
125
|
-
test
|
126
|
+
test 'should send hipchat notification include accumulated errors count' do
|
126
127
|
options = {
|
127
|
-
:
|
128
|
-
:
|
129
|
-
:
|
128
|
+
api_token: 'good_token',
|
129
|
+
room_name: 'room_name',
|
130
|
+
color: 'yellow'
|
130
131
|
}
|
131
132
|
|
132
|
-
HipChat::Room.any_instance.expects(:send).with{ |_, msg, _| msg.start_with?(
|
133
|
+
HipChat::Room.any_instance.expects(:send).with { |_, msg, _| msg.start_with?('The exception occurred 3 times:') }
|
133
134
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
134
|
-
hipchat.call(fake_exception,
|
135
|
+
hipchat.call(fake_exception, accumulated_errors_count: 3)
|
135
136
|
end
|
136
137
|
|
137
|
-
test
|
138
|
+
test 'should send hipchat notification with HTML-escaped meessage if using default message_template' do
|
138
139
|
options = {
|
139
|
-
:
|
140
|
-
:
|
141
|
-
:
|
140
|
+
api_token: 'good_token',
|
141
|
+
room_name: 'room_name',
|
142
|
+
color: 'yellow'
|
142
143
|
}
|
143
144
|
|
144
145
|
exception = fake_exception_with_html_characters
|
145
146
|
body = "A new exception occurred: '#{Rack::Utils.escape_html(exception.message)}' on '#{exception.backtrace.first}'"
|
146
147
|
|
147
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', body,
|
148
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', body, color: 'yellow')
|
148
149
|
|
149
150
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
150
151
|
hipchat.call(exception)
|
151
152
|
end
|
152
153
|
|
153
|
-
test
|
154
|
+
test 'should use APIv1 if api_version is not specified' do
|
154
155
|
options = {
|
155
|
-
:
|
156
|
-
:
|
156
|
+
api_token: 'good_token',
|
157
|
+
room_name: 'room_name'
|
157
158
|
}
|
158
159
|
|
159
|
-
HipChat::Client.stubs(:new).with('good_token',
|
160
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v1').returns({})
|
160
161
|
|
161
162
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
162
163
|
hipchat.call(fake_exception)
|
163
164
|
end
|
164
165
|
|
165
|
-
test
|
166
|
+
test 'should use APIv2 when specified' do
|
166
167
|
options = {
|
167
|
-
:
|
168
|
-
:
|
169
|
-
:
|
168
|
+
api_token: 'good_token',
|
169
|
+
room_name: 'room_name',
|
170
|
+
api_version: 'v2'
|
170
171
|
}
|
171
172
|
|
172
|
-
HipChat::Client.stubs(:new).with('good_token',
|
173
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v2').returns({})
|
173
174
|
|
174
175
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
175
176
|
hipchat.call(fake_exception)
|
176
177
|
end
|
177
178
|
|
178
|
-
test
|
179
|
+
test 'should allow server_url value (for a self-hosted HipChat Server) if set' do
|
179
180
|
options = {
|
180
|
-
:
|
181
|
-
:
|
182
|
-
:
|
183
|
-
:
|
181
|
+
api_token: 'good_token',
|
182
|
+
room_name: 'room_name',
|
183
|
+
api_version: 'v2',
|
184
|
+
server_url: 'https://domain.com'
|
184
185
|
}
|
185
186
|
|
186
|
-
HipChat::Client.stubs(:new).with('good_token',
|
187
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v2', server_url: 'https://domain.com').returns({})
|
187
188
|
|
188
189
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
189
190
|
hipchat.call(fake_exception)
|
@@ -196,19 +197,15 @@ class HipchatNotifierTest < ActiveSupport::TestCase
|
|
196
197
|
end
|
197
198
|
|
198
199
|
def fake_exception
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
e
|
203
|
-
end
|
200
|
+
5 / 0
|
201
|
+
rescue StandardError => e
|
202
|
+
e
|
204
203
|
end
|
205
204
|
|
206
205
|
def fake_exception_with_html_characters
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
e
|
211
|
-
end
|
206
|
+
raise StandardError, 'an error with <html> characters'
|
207
|
+
rescue StandardError => e
|
208
|
+
e
|
212
209
|
end
|
213
210
|
|
214
211
|
def fake_body_without_backtrace
|