exception_notification 4.3.0 → 4.5.0

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