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