exception_notification 4.3.0 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/Appraisals +4 -2
- data/CHANGELOG.rdoc +47 -0
- data/CONTRIBUTING.md +18 -0
- data/Gemfile +3 -1
- data/README.md +97 -945
- data/Rakefile +4 -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 +56 -0
- data/examples/sinatra/Gemfile +8 -6
- data/examples/sinatra/config.ru +3 -1
- data/examples/sinatra/sinatra_app.rb +19 -11
- data/exception_notification.gemspec +30 -24
- data/gemfiles/{rails4_0.gemfile → rails5_2.gemfile} +2 -2
- data/gemfiles/{rails4_1.gemfile → rails6_0.gemfile} +2 -2
- data/gemfiles/{rails4_2.gemfile → rails6_1.gemfile} +2 -2
- data/gemfiles/{rails5_0.gemfile → rails7_0.gemfile} +2 -2
- data/lib/exception_notification/rack.rb +28 -30
- data/lib/exception_notification/rails.rb +2 -0
- data/lib/exception_notification/resque.rb +10 -10
- data/lib/exception_notification/sidekiq.rb +10 -12
- data/lib/exception_notification/version.rb +5 -0
- data/lib/exception_notification.rb +3 -0
- data/lib/exception_notifier/base_notifier.rb +10 -5
- data/lib/exception_notifier/datadog_notifier.rb +156 -0
- data/lib/exception_notifier/email_notifier.rb +73 -88
- data/lib/exception_notifier/google_chat_notifier.rb +27 -119
- data/lib/exception_notifier/hipchat_notifier.rb +13 -12
- data/lib/exception_notifier/irc_notifier.rb +36 -33
- data/lib/exception_notifier/mattermost_notifier.rb +54 -137
- data/lib/exception_notifier/modules/backtrace_cleaner.rb +2 -2
- data/lib/exception_notifier/modules/error_grouping.rb +24 -13
- data/lib/exception_notifier/modules/formatter.rb +125 -0
- data/lib/exception_notifier/notifier.rb +9 -6
- data/lib/exception_notifier/slack_notifier.rb +65 -40
- data/lib/exception_notifier/sns_notifier.rb +23 -13
- data/lib/exception_notifier/teams_notifier.rb +67 -46
- 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 +17 -14
- data/lib/exception_notifier.rb +65 -10
- data/lib/generators/exception_notification/install_generator.rb +11 -5
- data/lib/generators/exception_notification/templates/{exception_notification.rb → exception_notification.rb.erb} +13 -11
- data/test/exception_notification/rack_test.rb +75 -13
- data/test/exception_notification/resque_test.rb +54 -0
- data/test/exception_notifier/datadog_notifier_test.rb +153 -0
- data/test/exception_notifier/email_notifier_test.rb +275 -153
- data/test/exception_notifier/google_chat_notifier_test.rb +158 -101
- data/test/exception_notifier/hipchat_notifier_test.rb +84 -81
- data/test/exception_notifier/irc_notifier_test.rb +36 -34
- data/test/exception_notifier/mattermost_notifier_test.rb +213 -67
- data/test/exception_notifier/modules/error_grouping_test.rb +41 -40
- data/test/exception_notifier/modules/formatter_test.rb +152 -0
- data/test/exception_notifier/sidekiq_test.rb +9 -17
- data/test/exception_notifier/slack_notifier_test.rb +66 -63
- data/test/exception_notifier/sns_notifier_test.rb +84 -32
- data/test/exception_notifier/teams_notifier_test.rb +25 -26
- data/test/exception_notifier/webhook_notifier_test.rb +52 -48
- data/test/exception_notifier_test.rb +150 -41
- data/test/support/exception_notifier_helper.rb +14 -0
- 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 +14 -13
- metadata +134 -175
- data/gemfiles/rails5_1.gemfile +0 -7
- data/lib/exception_notifier/campfire_notifier.rb +0 -40
- 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/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/config.ru +0 -4
- 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
- data/test/exception_notifier/campfire_notifier_test.rb +0 -120
|
@@ -1,128 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'test_helper'
|
|
4
|
+
require 'rack'
|
|
2
5
|
require 'httparty'
|
|
6
|
+
require 'timecop'
|
|
7
|
+
require 'json'
|
|
3
8
|
|
|
4
9
|
class GoogleChatNotifierTest < ActiveSupport::TestCase
|
|
10
|
+
URL = 'http://localhost:8000'
|
|
5
11
|
|
|
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
|
|
12
|
+
def setup
|
|
13
|
+
Timecop.freeze('2018-12-09 12:07:16 UTC')
|
|
14
|
+
end
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
def teardown
|
|
17
|
+
Timecop.return
|
|
18
|
+
end
|
|
17
19
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
assert_equal 'An *ArgumentError* occured.', text[2]
|
|
22
|
-
assert_equal '*foo*', text[5]
|
|
20
|
+
test 'should send notification if properly configured' do
|
|
21
|
+
HTTParty.expects(:post).with(URL, post_opts("#{header}\n#{body}"))
|
|
22
|
+
notifier.call ArgumentError.new('foo')
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
test
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
test 'shoud use errors count if accumulated_errors_count is provided' do
|
|
26
|
+
text = [
|
|
27
|
+
'',
|
|
28
|
+
"Application: *#{app_name}*",
|
|
29
|
+
'5 *ArgumentError* occurred.',
|
|
30
|
+
'',
|
|
31
|
+
body
|
|
32
|
+
].join("\n")
|
|
33
|
+
|
|
34
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
|
30
35
|
|
|
31
|
-
|
|
36
|
+
notifier.call(ArgumentError.new('foo'), accumulated_errors_count: 5)
|
|
32
37
|
end
|
|
33
38
|
|
|
34
|
-
test
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
test 'Message request should be formatted as hash' do
|
|
40
|
+
text = [
|
|
41
|
+
header,
|
|
42
|
+
body,
|
|
43
|
+
'',
|
|
44
|
+
'*Request:*',
|
|
45
|
+
'```',
|
|
46
|
+
'* url : http://test.address/?id=foo',
|
|
47
|
+
'* http_method : GET',
|
|
48
|
+
'* ip_address : 127.0.0.1',
|
|
49
|
+
'* parameters : {"id"=>"foo"}',
|
|
50
|
+
'* timestamp : 2018-12-09 12:07:16 UTC',
|
|
51
|
+
'```'
|
|
52
|
+
].join("\n")
|
|
53
|
+
|
|
54
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
|
55
|
+
|
|
56
|
+
notifier.call(ArgumentError.new('foo'), env: test_env)
|
|
57
|
+
end
|
|
39
58
|
|
|
40
|
-
|
|
59
|
+
test 'backtrace with less than 3 lines should be displayed fully' do
|
|
60
|
+
text = [
|
|
61
|
+
header,
|
|
62
|
+
body,
|
|
63
|
+
'',
|
|
64
|
+
backtrace
|
|
65
|
+
].join("\n")
|
|
66
|
+
|
|
67
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
|
68
|
+
|
|
69
|
+
exception = ArgumentError.new('foo')
|
|
70
|
+
exception.set_backtrace([
|
|
71
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
|
72
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
|
73
|
+
])
|
|
74
|
+
|
|
75
|
+
notifier.call(exception)
|
|
41
76
|
end
|
|
42
77
|
|
|
43
|
-
test
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
78
|
+
test 'backtrace with more than 3 lines should display only top 3 lines' do
|
|
79
|
+
text = [
|
|
80
|
+
header,
|
|
81
|
+
body,
|
|
82
|
+
'',
|
|
83
|
+
'*Backtrace:*',
|
|
84
|
+
'```',
|
|
85
|
+
"* app/controllers/my_controller.rb:99:in `specific_function'",
|
|
86
|
+
"* app/controllers/my_controller.rb:70:in `specific_param'",
|
|
87
|
+
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
|
|
88
|
+
'```'
|
|
89
|
+
].join("\n")
|
|
90
|
+
|
|
91
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
|
92
|
+
|
|
93
|
+
exception = ArgumentError.new('foo')
|
|
94
|
+
exception.set_backtrace([
|
|
95
|
+
"app/controllers/my_controller.rb:99:in `specific_function'",
|
|
96
|
+
"app/controllers/my_controller.rb:70:in `specific_param'",
|
|
97
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
|
98
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
|
99
|
+
])
|
|
100
|
+
|
|
101
|
+
notifier.call(exception)
|
|
58
102
|
end
|
|
59
103
|
|
|
60
|
-
test '
|
|
61
|
-
|
|
104
|
+
test 'Get text with backtrace and request info' do
|
|
105
|
+
text = [
|
|
106
|
+
header,
|
|
107
|
+
body,
|
|
108
|
+
'',
|
|
109
|
+
'*Request:*',
|
|
110
|
+
'```',
|
|
111
|
+
'* url : http://test.address/?id=foo',
|
|
112
|
+
'* http_method : GET',
|
|
113
|
+
'* ip_address : 127.0.0.1',
|
|
114
|
+
'* parameters : {"id"=>"foo"}',
|
|
115
|
+
'* timestamp : 2018-12-09 12:07:16 UTC',
|
|
116
|
+
'```',
|
|
117
|
+
'',
|
|
118
|
+
backtrace
|
|
119
|
+
].join("\n")
|
|
120
|
+
|
|
121
|
+
HTTParty.expects(:post).with(URL, post_opts(text))
|
|
122
|
+
|
|
123
|
+
exception = ArgumentError.new('foo')
|
|
124
|
+
exception.set_backtrace([
|
|
125
|
+
"app/controllers/my_controller.rb:53:in `my_controller_params'",
|
|
126
|
+
"app/controllers/my_controller.rb:34:in `update'"
|
|
127
|
+
])
|
|
128
|
+
|
|
129
|
+
notifier.call(exception, env: test_env)
|
|
130
|
+
end
|
|
62
131
|
|
|
63
|
-
|
|
64
|
-
google_chat_notifier.instance_variable_set(:@backtrace, backtrace)
|
|
132
|
+
private
|
|
65
133
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
assert_includes message_backtrace, "* app/controllers/my_controller.rb:34:in `update'"
|
|
134
|
+
def notifier
|
|
135
|
+
ExceptionNotifier::GoogleChatNotifier.new(webhook_url: URL)
|
|
69
136
|
end
|
|
70
137
|
|
|
71
|
-
|
|
72
|
-
|
|
138
|
+
def post_opts(text)
|
|
139
|
+
{
|
|
140
|
+
body: { text: text }.to_json,
|
|
141
|
+
headers: { 'Content-Type' => 'application/json' }
|
|
142
|
+
}
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_env
|
|
146
|
+
Rack::MockRequest.env_for(
|
|
147
|
+
'/',
|
|
148
|
+
'HTTP_HOST' => 'test.address',
|
|
149
|
+
'REMOTE_ADDR' => '127.0.0.1',
|
|
150
|
+
'HTTP_USER_AGENT' => 'Rails Testing',
|
|
151
|
+
params: { id: 'foo' }
|
|
152
|
+
)
|
|
153
|
+
end
|
|
73
154
|
|
|
74
|
-
|
|
75
|
-
|
|
155
|
+
def header
|
|
156
|
+
[
|
|
157
|
+
'',
|
|
158
|
+
"Application: *#{app_name}*",
|
|
159
|
+
'An *ArgumentError* occurred.',
|
|
160
|
+
''
|
|
161
|
+
].join("\n")
|
|
162
|
+
end
|
|
76
163
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
164
|
+
def body
|
|
165
|
+
if defined?(::Rails) && ::Rails.respond_to?(:env)
|
|
166
|
+
"⚠️ Error occurred in test ⚠️\n*foo*"
|
|
167
|
+
else
|
|
168
|
+
"⚠️ Error occurred ⚠️\n*foo*"
|
|
169
|
+
end
|
|
82
170
|
end
|
|
83
171
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
|
172
|
+
def app_name
|
|
173
|
+
'dummy' if defined?(::Rails) && ::Rails.respond_to?(:application)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def backtrace
|
|
177
|
+
[
|
|
178
|
+
'*Backtrace:*',
|
|
179
|
+
'```',
|
|
180
|
+
"* app/controllers/my_controller.rb:53:in `my_controller_params'",
|
|
181
|
+
"* app/controllers/my_controller.rb:34:in `update'",
|
|
182
|
+
'```'
|
|
183
|
+
]
|
|
127
184
|
end
|
|
128
185
|
end
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'test_helper'
|
|
4
|
+
require 'rack'
|
|
2
5
|
|
|
3
6
|
# silence_warnings trick around require can be removed once
|
|
4
7
|
# https://github.com/hipchat/hipchat-rb/pull/174
|
|
@@ -8,31 +11,33 @@ silence_warnings do
|
|
|
8
11
|
end
|
|
9
12
|
|
|
10
13
|
class HipchatNotifierTest < ActiveSupport::TestCase
|
|
11
|
-
|
|
12
|
-
test "should send hipchat notification if properly configured" do
|
|
14
|
+
test 'should send hipchat notification if properly configured' do
|
|
13
15
|
options = {
|
|
14
|
-
:
|
|
15
|
-
:
|
|
16
|
-
:
|
|
16
|
+
api_token: 'good_token',
|
|
17
|
+
room_name: 'room_name',
|
|
18
|
+
color: 'yellow'
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body,
|
|
21
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body, color: 'yellow')
|
|
20
22
|
|
|
21
23
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
22
24
|
hipchat.call(fake_exception)
|
|
23
25
|
end
|
|
24
26
|
|
|
25
|
-
test
|
|
26
|
-
pre_callback_called
|
|
27
|
+
test 'should call pre/post_callback if specified' do
|
|
28
|
+
pre_callback_called = 0
|
|
29
|
+
post_callback_called = 0
|
|
27
30
|
options = {
|
|
28
|
-
:
|
|
29
|
-
:
|
|
30
|
-
:
|
|
31
|
-
:
|
|
32
|
-
:
|
|
31
|
+
api_token: 'good_token',
|
|
32
|
+
room_name: 'room_name',
|
|
33
|
+
color: 'yellow',
|
|
34
|
+
pre_callback: proc { |*| pre_callback_called += 1 },
|
|
35
|
+
post_callback: proc { |*| post_callback_called += 1 }
|
|
33
36
|
}
|
|
34
37
|
|
|
35
|
-
HipChat::Room.any_instance
|
|
38
|
+
HipChat::Room.any_instance
|
|
39
|
+
.expects(:send)
|
|
40
|
+
.with('Exception', fake_body, { color: 'yellow' }.merge(options.except(:api_token, :room_name)))
|
|
36
41
|
|
|
37
42
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
38
43
|
hipchat.call(fake_exception)
|
|
@@ -40,150 +45,152 @@ class HipchatNotifierTest < ActiveSupport::TestCase
|
|
|
40
45
|
assert_equal(1, post_callback_called)
|
|
41
46
|
end
|
|
42
47
|
|
|
43
|
-
test
|
|
48
|
+
test 'should send hipchat notification without backtrace info if properly configured' do
|
|
44
49
|
options = {
|
|
45
|
-
:
|
|
46
|
-
:
|
|
47
|
-
:
|
|
50
|
+
api_token: 'good_token',
|
|
51
|
+
room_name: 'room_name',
|
|
52
|
+
color: 'yellow'
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body_without_backtrace,
|
|
55
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', fake_body_without_backtrace, color: 'yellow')
|
|
51
56
|
|
|
52
57
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
53
58
|
hipchat.call(fake_exception_without_backtrace)
|
|
54
59
|
end
|
|
55
60
|
|
|
56
|
-
test
|
|
61
|
+
test 'should allow custom from value if set' do
|
|
57
62
|
options = {
|
|
58
|
-
:
|
|
59
|
-
:
|
|
60
|
-
:
|
|
63
|
+
api_token: 'good_token',
|
|
64
|
+
room_name: 'room_name',
|
|
65
|
+
from: 'TrollFace'
|
|
61
66
|
}
|
|
62
67
|
|
|
63
|
-
HipChat::Room.any_instance.expects(:send).with('TrollFace', fake_body,
|
|
68
|
+
HipChat::Room.any_instance.expects(:send).with('TrollFace', fake_body, color: 'red')
|
|
64
69
|
|
|
65
70
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
66
71
|
hipchat.call(fake_exception)
|
|
67
72
|
end
|
|
68
73
|
|
|
69
|
-
test
|
|
74
|
+
test 'should not send hipchat notification if badly configured' do
|
|
70
75
|
wrong_params = {
|
|
71
|
-
:
|
|
72
|
-
:
|
|
76
|
+
api_token: 'bad_token',
|
|
77
|
+
room_name: 'test_room'
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
HipChat::Client.stubs(:new).with('bad_token',
|
|
80
|
+
HipChat::Client.stubs(:new).with('bad_token', api_version: 'v1').returns(nil)
|
|
76
81
|
|
|
77
82
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
|
78
83
|
assert_nil hipchat.room
|
|
79
84
|
end
|
|
80
85
|
|
|
81
|
-
test
|
|
82
|
-
wrong_params
|
|
86
|
+
test 'should not send hipchat notification if api_key is missing' do
|
|
87
|
+
wrong_params = { room_name: 'test_room' }
|
|
83
88
|
|
|
84
|
-
HipChat::Client.stubs(:new).with(nil,
|
|
89
|
+
HipChat::Client.stubs(:new).with(nil, api_version: 'v1').returns(nil)
|
|
85
90
|
|
|
86
91
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
|
87
92
|
assert_nil hipchat.room
|
|
88
93
|
end
|
|
89
94
|
|
|
90
|
-
test
|
|
91
|
-
wrong_params
|
|
95
|
+
test 'should not send hipchat notification if room_name is missing' do
|
|
96
|
+
wrong_params = { api_token: 'good_token' }
|
|
92
97
|
|
|
93
|
-
HipChat::Client.stubs(:new).with('good_token',
|
|
98
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v1').returns({})
|
|
94
99
|
|
|
95
100
|
hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params)
|
|
96
101
|
assert_nil hipchat.room
|
|
97
102
|
end
|
|
98
103
|
|
|
99
|
-
test
|
|
104
|
+
test 'should send hipchat notification with message_template' do
|
|
100
105
|
options = {
|
|
101
|
-
:
|
|
102
|
-
:
|
|
103
|
-
:
|
|
104
|
-
:
|
|
106
|
+
api_token: 'good_token',
|
|
107
|
+
room_name: 'room_name',
|
|
108
|
+
color: 'yellow',
|
|
109
|
+
message_template: ->(exception, _) { "This is custom message: '#{exception.message}'" }
|
|
105
110
|
}
|
|
106
111
|
|
|
107
|
-
HipChat::Room.any_instance
|
|
112
|
+
HipChat::Room.any_instance
|
|
113
|
+
.expects(:send)
|
|
114
|
+
.with('Exception', "This is custom message: '#{fake_exception.message}'", color: 'yellow')
|
|
108
115
|
|
|
109
116
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
110
117
|
hipchat.call(fake_exception)
|
|
111
118
|
end
|
|
112
119
|
|
|
113
|
-
test
|
|
120
|
+
test 'should send hipchat notification exclude accumulated errors count' do
|
|
114
121
|
options = {
|
|
115
|
-
:
|
|
116
|
-
:
|
|
117
|
-
:
|
|
122
|
+
api_token: 'good_token',
|
|
123
|
+
room_name: 'room_name',
|
|
124
|
+
color: 'yellow'
|
|
118
125
|
}
|
|
119
126
|
|
|
120
|
-
HipChat::Room.any_instance.expects(:send).with{ |_, msg, _| msg.start_with?(
|
|
127
|
+
HipChat::Room.any_instance.expects(:send).with { |_, msg, _| msg.start_with?('A new exception occurred:') }
|
|
121
128
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
122
129
|
hipchat.call(fake_exception)
|
|
123
130
|
end
|
|
124
131
|
|
|
125
|
-
test
|
|
132
|
+
test 'should send hipchat notification include accumulated errors count' do
|
|
126
133
|
options = {
|
|
127
|
-
:
|
|
128
|
-
:
|
|
129
|
-
:
|
|
134
|
+
api_token: 'good_token',
|
|
135
|
+
room_name: 'room_name',
|
|
136
|
+
color: 'yellow'
|
|
130
137
|
}
|
|
131
138
|
|
|
132
|
-
HipChat::Room.any_instance.expects(:send).with{ |_, msg, _| msg.start_with?(
|
|
139
|
+
HipChat::Room.any_instance.expects(:send).with { |_, msg, _| msg.start_with?('The exception occurred 3 times:') }
|
|
133
140
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
134
|
-
hipchat.call(fake_exception,
|
|
141
|
+
hipchat.call(fake_exception, accumulated_errors_count: 3)
|
|
135
142
|
end
|
|
136
143
|
|
|
137
|
-
test
|
|
144
|
+
test 'should send hipchat notification with HTML-escaped meessage if using default message_template' do
|
|
138
145
|
options = {
|
|
139
|
-
:
|
|
140
|
-
:
|
|
141
|
-
:
|
|
146
|
+
api_token: 'good_token',
|
|
147
|
+
room_name: 'room_name',
|
|
148
|
+
color: 'yellow'
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
exception = fake_exception_with_html_characters
|
|
145
152
|
body = "A new exception occurred: '#{Rack::Utils.escape_html(exception.message)}' on '#{exception.backtrace.first}'"
|
|
146
153
|
|
|
147
|
-
HipChat::Room.any_instance.expects(:send).with('Exception', body,
|
|
154
|
+
HipChat::Room.any_instance.expects(:send).with('Exception', body, color: 'yellow')
|
|
148
155
|
|
|
149
156
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
150
157
|
hipchat.call(exception)
|
|
151
158
|
end
|
|
152
159
|
|
|
153
|
-
test
|
|
160
|
+
test 'should use APIv1 if api_version is not specified' do
|
|
154
161
|
options = {
|
|
155
|
-
:
|
|
156
|
-
:
|
|
162
|
+
api_token: 'good_token',
|
|
163
|
+
room_name: 'room_name'
|
|
157
164
|
}
|
|
158
165
|
|
|
159
|
-
HipChat::Client.stubs(:new).with('good_token',
|
|
166
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v1').returns({})
|
|
160
167
|
|
|
161
168
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
162
169
|
hipchat.call(fake_exception)
|
|
163
170
|
end
|
|
164
171
|
|
|
165
|
-
test
|
|
172
|
+
test 'should use APIv2 when specified' do
|
|
166
173
|
options = {
|
|
167
|
-
:
|
|
168
|
-
:
|
|
169
|
-
:
|
|
174
|
+
api_token: 'good_token',
|
|
175
|
+
room_name: 'room_name',
|
|
176
|
+
api_version: 'v2'
|
|
170
177
|
}
|
|
171
178
|
|
|
172
|
-
HipChat::Client.stubs(:new).with('good_token',
|
|
179
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v2').returns({})
|
|
173
180
|
|
|
174
181
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
175
182
|
hipchat.call(fake_exception)
|
|
176
183
|
end
|
|
177
184
|
|
|
178
|
-
test
|
|
185
|
+
test 'should allow server_url value (for a self-hosted HipChat Server) if set' do
|
|
179
186
|
options = {
|
|
180
|
-
:
|
|
181
|
-
:
|
|
182
|
-
:
|
|
183
|
-
:
|
|
187
|
+
api_token: 'good_token',
|
|
188
|
+
room_name: 'room_name',
|
|
189
|
+
api_version: 'v2',
|
|
190
|
+
server_url: 'https://domain.com'
|
|
184
191
|
}
|
|
185
192
|
|
|
186
|
-
HipChat::Client.stubs(:new).with('good_token',
|
|
193
|
+
HipChat::Client.stubs(:new).with('good_token', api_version: 'v2', server_url: 'https://domain.com').returns({})
|
|
187
194
|
|
|
188
195
|
hipchat = ExceptionNotifier::HipchatNotifier.new(options)
|
|
189
196
|
hipchat.call(fake_exception)
|
|
@@ -196,19 +203,15 @@ class HipchatNotifierTest < ActiveSupport::TestCase
|
|
|
196
203
|
end
|
|
197
204
|
|
|
198
205
|
def fake_exception
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
e
|
|
203
|
-
end
|
|
206
|
+
5 / 0
|
|
207
|
+
rescue StandardError => e
|
|
208
|
+
e
|
|
204
209
|
end
|
|
205
210
|
|
|
206
211
|
def fake_exception_with_html_characters
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
e
|
|
211
|
-
end
|
|
212
|
+
raise StandardError, 'an error with <html> characters'
|
|
213
|
+
rescue StandardError => e
|
|
214
|
+
e
|
|
212
215
|
end
|
|
213
216
|
|
|
214
217
|
def fake_body_without_backtrace
|