exception_notification 4.3.0 → 4.4.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.
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,181 +1,148 @@
1
1
  require 'test_helper'
2
2
  require 'action_mailer'
3
+ require 'action_controller'
3
4
 
4
5
  class EmailNotifierTest < ActiveSupport::TestCase
5
6
  setup do
6
7
  Time.stubs(:current).returns('Sat, 20 Apr 2013 20:58:55 UTC +00:00')
7
- @email_notifier = ExceptionNotifier.registered_exception_notifier(:email)
8
- begin
9
- 1/0
10
- rescue => e
11
- @exception = e
12
- @mail = @email_notifier.create_email(@exception,
13
- :data => {:job => 'DivideWorkerJob', :payload => '1/0', :message => 'My Custom Message'})
14
- end
15
- end
16
8
 
17
- test "should call pre/post_callback if specified" do
18
- assert_equal @email_notifier.options[:pre_callback_called], 1
19
- assert_equal @email_notifier.options[:post_callback_called], 1
20
- end
9
+ @exception = ZeroDivisionError.new('divided by 0')
10
+ @exception.set_backtrace(['test/exception_notifier/email_notifier_test.rb:20'])
11
+
12
+ @email_notifier = ExceptionNotifier::EmailNotifier.new(
13
+ email_prefix: '[Dummy ERROR] ',
14
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
15
+ exception_recipients: %w[dummyexceptions@example.com],
16
+ email_headers: { 'X-Custom-Header' => 'foobar' },
17
+ sections: %w[new_section request session environment backtrace],
18
+ background_sections: %w[new_bkg_section backtrace data],
19
+ pre_callback: proc { |_opts, _notifier, _backtrace, _message, _message_opts| @pre_callback_called = true },
20
+ post_callback: proc { |_opts, _notifier, _backtrace, _message, _message_opts| @post_callback_called = true },
21
+ smtp_settings: {
22
+ user_name: 'Dummy user_name',
23
+ password: 'Dummy password'
24
+ }
25
+ )
21
26
 
22
- test "should have default sender address overridden" do
23
- assert_equal @email_notifier.sender_address, %("Dummy Notifier" <dummynotifier@example.com>)
27
+ @mail = @email_notifier.call(
28
+ @exception,
29
+ data: { job: 'DivideWorkerJob', payload: '1/0', message: 'My Custom Message' }
30
+ )
24
31
  end
25
32
 
26
- test "should have default exception recipients overridden" do
27
- assert_equal @email_notifier.exception_recipients, %w(dummyexceptions@example.com)
33
+ test 'should call pre/post_callback if specified' do
34
+ assert @pre_callback_called
35
+ assert @post_callback_called
28
36
  end
29
37
 
30
- test "should have default email prefix overridden" do
31
- assert_equal @email_notifier.email_prefix, "[Dummy ERROR] "
32
- end
38
+ test 'sends mail with correct content' do
39
+ assert_equal %("Dummy Notifier" <dummynotifier@example.com>), @mail[:from].value
40
+ assert_equal %w[dummyexceptions@example.com], @mail.to
41
+ assert_equal '[Dummy ERROR] (ZeroDivisionError) "divided by 0"', @mail.subject
42
+ assert_equal 'foobar', @mail['X-Custom-Header'].value
43
+ assert_equal 'text/plain; charset=UTF-8', @mail.content_type
44
+ assert_equal [], @mail.attachments
45
+ assert_equal 'Dummy user_name', @mail.delivery_method.settings[:user_name]
46
+ assert_equal 'Dummy password', @mail.delivery_method.settings[:password]
33
47
 
34
- test "should have default email headers overridden" do
35
- assert_equal @email_notifier.email_headers, { "X-Custom-Header" => "foobar"}
36
- end
48
+ body = <<-BODY.gsub(/^ /, '')
49
+ A ZeroDivisionError occurred in background at Sat, 20 Apr 2013 20:58:55 UTC +00:00 :
37
50
 
38
- test "should have default sections overridden" do
39
- for section in %w(new_section request session environment backtrace)
40
- assert_includes @email_notifier.sections, section
41
- end
42
- end
51
+ divided by 0
52
+ test/exception_notifier/email_notifier_test.rb:20
43
53
 
44
- test "should have default background sections" do
45
- for section in %w(new_bkg_section backtrace data)
46
- assert_includes @email_notifier.background_sections, section
47
- end
48
- end
54
+ -------------------------------
55
+ New bkg section:
56
+ -------------------------------
49
57
 
50
- test "should have email format by default" do
51
- assert_equal @email_notifier.email_format, :text
52
- end
53
-
54
- test "should have verbose subject by default" do
55
- assert @email_notifier.verbose_subject
56
- end
58
+ * New background section for testing
57
59
 
58
- test "should have normalize_subject false by default" do
59
- refute @email_notifier.normalize_subject
60
- end
61
-
62
- test "should have delivery_method nil by default" do
63
- assert_nil @email_notifier.delivery_method
64
- end
60
+ -------------------------------
61
+ Backtrace:
62
+ -------------------------------
65
63
 
66
- test "should have mailer_settings nil by default" do
67
- assert_nil @email_notifier.mailer_settings
68
- end
64
+ test/exception_notifier/email_notifier_test.rb:20
69
65
 
70
- test "should have mailer_parent by default" do
71
- assert_equal @email_notifier.mailer_parent, 'ActionMailer::Base'
72
- end
66
+ -------------------------------
67
+ Data:
68
+ -------------------------------
73
69
 
74
- test "should have template_path by default" do
75
- assert_equal @email_notifier.template_path, 'exception_notifier'
76
- end
70
+ * data: {:job=>"DivideWorkerJob", :payload=>"1/0", :message=>"My Custom Message"}
77
71
 
78
- test "should normalize multiple digits into one N" do
79
- assert_equal 'N foo N bar N baz N',
80
- ExceptionNotifier::EmailNotifier.normalize_digits('1 foo 12 bar 123 baz 1234')
81
- end
82
72
 
83
- test "mail should be plain text and UTF-8 enconded by default" do
84
- assert_equal @mail.content_type, "text/plain; charset=UTF-8"
85
- end
73
+ BODY
86
74
 
87
- test "should have raised an exception" do
88
- refute_nil @exception
75
+ assert_equal body, @mail.decode_body
89
76
  end
90
77
 
91
- test "should have generated a notification email" do
92
- refute_nil @mail
93
- end
94
-
95
- test "mail should have a from address set" do
96
- assert_equal @mail.from, ["dummynotifier@example.com"]
97
- end
98
-
99
- test "mail should have a to address set" do
100
- assert_equal @mail.to, ["dummyexceptions@example.com"]
101
- end
102
-
103
- test "mail should have a descriptive subject" do
104
- assert_match(/^\[Dummy ERROR\]\s+\(ZeroDivisionError\) "divided by 0"$/, @mail.subject)
105
- end
106
-
107
- test "mail should say exception was raised in background at show timestamp" do
108
- assert_includes @mail.encoded, "A ZeroDivisionError occurred in background at #{Time.current}"
78
+ test 'should normalize multiple digits into one N' do
79
+ assert_equal 'N foo N bar N baz N',
80
+ ExceptionNotifier::EmailNotifier.normalize_digits('1 foo 12 bar 123 baz 1234')
109
81
  end
110
82
 
111
83
  test "mail should prefix exception class with 'an' instead of 'a' when it starts with a vowel" do
112
84
  begin
113
- raise ActiveRecord::RecordNotFound
114
- rescue => e
85
+ raise ArgumentError
86
+ rescue StandardError => e
115
87
  @vowel_exception = e
116
- @vowel_mail = @email_notifier.create_email(@vowel_exception)
88
+ @vowel_mail = @email_notifier.call(@vowel_exception)
117
89
  end
118
90
 
119
- assert_includes @vowel_mail.encoded, "An ActiveRecord::RecordNotFound occurred in background at #{Time.current}"
120
- end
121
-
122
- test "mail should contain backtrace in body" do
123
- assert @mail.encoded.include?("test/exception_notifier/email_notifier_test.rb:9"), "\n#{@mail.inspect}"
124
- end
125
-
126
- test "mail should contain data in body" do
127
- assert_includes @mail.encoded, '* data:'
128
- assert_includes @mail.encoded, ':payload=>"1/0"'
129
- assert_includes @mail.encoded, ':job=>"DivideWorkerJob"'
130
- assert_includes @mail.encoded, "My Custom Message"
131
- end
132
-
133
- test "mail should not contain any attachments" do
134
- assert_equal @mail.attachments, []
91
+ assert_includes @vowel_mail.encoded, "An ArgumentError occurred in background at #{Time.current}"
135
92
  end
136
93
 
137
- test "should not send notification if one of ignored exceptions" do
94
+ test 'should not send notification if one of ignored exceptions' do
138
95
  begin
139
- raise ActiveRecord::RecordNotFound
140
- rescue => e
96
+ raise AbstractController::ActionNotFound
97
+ rescue StandardError => e
141
98
  @ignored_exception = e
142
99
  unless ExceptionNotifier.ignored_exceptions.include?(@ignored_exception.class.name)
143
- ignored_mail = @email_notifier.create_email(@ignored_exception)
100
+ ignored_mail = @email_notifier.call(@ignored_exception)
144
101
  end
145
102
  end
146
103
 
147
- assert_equal @ignored_exception.class.inspect, "ActiveRecord::RecordNotFound"
104
+ assert_equal @ignored_exception.class.inspect, 'AbstractController::ActionNotFound'
148
105
  assert_nil ignored_mail
149
106
  end
150
107
 
151
- test "should encode environment strings" do
108
+ test 'should encode environment strings' do
152
109
  email_notifier = ExceptionNotifier::EmailNotifier.new(
153
- :sender_address => "<dummynotifier@example.com>",
154
- :exception_recipients => %w{dummyexceptions@example.com},
155
- :deliver_with => :deliver_now
110
+ sender_address: '<dummynotifier@example.com>',
111
+ exception_recipients: %w[dummyexceptions@example.com]
156
112
  )
157
113
 
158
- mail = email_notifier.create_email(
114
+ mail = email_notifier.call(
159
115
  @exception,
160
- :env => {
161
- "REQUEST_METHOD" => "GET",
162
- "rack.input" => "",
163
- "invalid_encoding" => "R\xC3\xA9sum\xC3\xA9".force_encoding(Encoding::ASCII),
164
- },
165
- :email_format => :text
116
+ env: {
117
+ 'REQUEST_METHOD' => 'GET',
118
+ 'rack.input' => '',
119
+ 'invalid_encoding' => "R\xC3\xA9sum\xC3\xA9".force_encoding(Encoding::ASCII)
120
+ }
166
121
  )
167
122
 
168
123
  assert_match(/invalid_encoding\s+: R__sum__/, mail.encoded)
169
124
  end
170
125
 
171
- test "should send email using ActionMailer" do
126
+ test 'should send email using ActionMailer' do
127
+ ActionMailer::Base.deliveries.clear
128
+ @email_notifier.call(@exception)
129
+ assert_equal 1, ActionMailer::Base.deliveries.count
130
+ end
131
+
132
+ test 'should be able to specify ActionMailer::MessageDelivery method' do
172
133
  ActionMailer::Base.deliveries.clear
173
134
 
135
+ deliver_with = if ActionMailer.version < Gem::Version.new('4.2')
136
+ :deliver
137
+ else
138
+ :deliver_now
139
+ end
140
+
174
141
  email_notifier = ExceptionNotifier::EmailNotifier.new(
175
- :email_prefix => '[Dummy ERROR] ',
176
- :sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
177
- :exception_recipients => %w{dummyexceptions@example.com},
178
- :delivery_method => :test
142
+ email_prefix: '[Dummy ERROR] ',
143
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
144
+ exception_recipients: %w[dummyexceptions@example.com],
145
+ deliver_with: deliver_with
179
146
  )
180
147
 
181
148
  email_notifier.call(@exception)
@@ -183,53 +150,202 @@ class EmailNotifierTest < ActiveSupport::TestCase
183
150
  assert_equal 1, ActionMailer::Base.deliveries.count
184
151
  end
185
152
 
186
- test "should be able to specify ActionMailer::MessageDelivery method" do
187
- ActionMailer::Base.deliveries.clear
153
+ test 'should lazily evaluate exception_recipients' do
154
+ exception_recipients = %w[first@example.com second@example.com]
155
+ email_notifier = ExceptionNotifier::EmailNotifier.new(
156
+ email_prefix: '[Dummy ERROR] ',
157
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
158
+ exception_recipients: -> { [exception_recipients.shift] },
159
+ delivery_method: :test
160
+ )
188
161
 
189
- if ActionMailer.version < Gem::Version.new("4.2")
190
- deliver_with = :deliver
191
- else
192
- deliver_with = :deliver_now
193
- end
162
+ mail = email_notifier.call(@exception)
163
+ assert_equal %w[first@example.com], mail.to
164
+ mail = email_notifier.call(@exception)
165
+ assert_equal %w[second@example.com], mail.to
166
+ end
194
167
 
168
+ test 'should prepend accumulated_errors_count in email subject if accumulated_errors_count larger than 1' do
195
169
  email_notifier = ExceptionNotifier::EmailNotifier.new(
196
- :email_prefix => '[Dummy ERROR] ',
197
- :sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
198
- :exception_recipients => %w{dummyexceptions@example.com},
199
- :deliver_with => deliver_with
170
+ email_prefix: '[Dummy ERROR] ',
171
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
172
+ exception_recipients: %w[dummyexceptions@example.com],
173
+ delivery_method: :test
200
174
  )
201
175
 
202
- email_notifier.call(@exception)
203
-
204
- assert_equal 1, ActionMailer::Base.deliveries.count
176
+ mail = email_notifier.call(@exception, accumulated_errors_count: 3)
177
+ assert mail.subject.start_with?('[Dummy ERROR] (3 times) (ZeroDivisionError)')
205
178
  end
206
179
 
207
- test "should lazily evaluate exception_recipients" do
208
- exception_recipients = %w{first@example.com second@example.com}
180
+ test 'should not include exception message in subject when verbose_subject: false' do
209
181
  email_notifier = ExceptionNotifier::EmailNotifier.new(
210
- :email_prefix => '[Dummy ERROR] ',
211
- :sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
212
- :exception_recipients => -> { [ exception_recipients.shift ] },
213
- :delivery_method => :test
182
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
183
+ exception_recipients: %w[dummyexceptions@example.com],
184
+ verbose_subject: false
214
185
  )
215
186
 
216
187
  mail = email_notifier.call(@exception)
217
- assert_equal %w{first@example.com}, mail.to
188
+
189
+ assert_equal '[ERROR] (ZeroDivisionError)', mail.subject
190
+ end
191
+
192
+ test 'should send html email when selected html format' do
193
+ email_notifier = ExceptionNotifier::EmailNotifier.new(
194
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
195
+ exception_recipients: %w[dummyexceptions@example.com],
196
+ email_format: :html
197
+ )
198
+
218
199
  mail = email_notifier.call(@exception)
219
- assert_equal %w{second@example.com}, mail.to
200
+
201
+ assert mail.multipart?
220
202
  end
203
+ end
221
204
 
222
- test "should prepend accumulated_errors_count in email subject if accumulated_errors_count larger than 1" do
223
- ActionMailer::Base.deliveries.clear
205
+ class EmailNotifierWithEnvTest < ActiveSupport::TestCase
206
+ class HomeController < ActionController::Metal
207
+ def index; end
208
+ end
209
+
210
+ setup do
211
+ Time.stubs(:current).returns('Sat, 20 Apr 2013 20:58:55 UTC +00:00')
212
+
213
+ @exception = ZeroDivisionError.new('divided by 0')
214
+ @exception.set_backtrace(['test/exception_notifier/email_notifier_test.rb:20'])
215
+
216
+ @email_notifier = ExceptionNotifier::EmailNotifier.new(
217
+ email_prefix: '[Dummy ERROR] ',
218
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
219
+ exception_recipients: %w[dummyexceptions@example.com],
220
+ email_headers: { 'X-Custom-Header' => 'foobar' },
221
+ sections: %w[new_section request session environment backtrace],
222
+ background_sections: %w[new_bkg_section backtrace data],
223
+ pre_callback: proc { |_opts, _notifier, _backtrace, _message, message_opts| message_opts[:pre_callback_called] = 1 },
224
+ post_callback: proc { |_opts, _notifier, _backtrace, _message, message_opts| message_opts[:post_callback_called] = 1 }
225
+ )
226
+
227
+ @controller = HomeController.new
228
+ @controller.process(:index)
229
+
230
+ @test_env = Rack::MockRequest.env_for(
231
+ '/',
232
+ 'HTTP_HOST' => 'test.address',
233
+ 'REMOTE_ADDR' => '127.0.0.1',
234
+ 'HTTP_USER_AGENT' => 'Rails Testing',
235
+ 'action_dispatch.parameter_filter' => ['secret'],
236
+ 'HTTPS' => 'on',
237
+ 'action_controller.instance' => @controller,
238
+ params: { id: 'foo', secret: 'secret' }
239
+ )
240
+
241
+ @mail = @email_notifier.call(@exception, env: @test_env, data: { message: 'My Custom Message' })
242
+ end
243
+
244
+ test 'sends mail with correct content' do
245
+ assert_equal %("Dummy Notifier" <dummynotifier@example.com>), @mail[:from].value
246
+ assert_equal %w[dummyexceptions@example.com], @mail.to
247
+ assert_equal '[Dummy ERROR] home index (ZeroDivisionError) "divided by 0"', @mail.subject
248
+ assert_equal 'foobar', @mail['X-Custom-Header'].value
249
+ assert_equal 'text/plain; charset=UTF-8', @mail.content_type
250
+ assert_equal [], @mail.attachments
251
+
252
+ body = <<-BODY.gsub(/^ /, '')
253
+ A ZeroDivisionError occurred in home#index:
254
+
255
+ divided by 0
256
+ test/exception_notifier/email_notifier_test.rb:20
257
+
258
+
259
+ -------------------------------
260
+ New section:
261
+ -------------------------------
262
+
263
+ * New text section for testing
264
+
265
+ -------------------------------
266
+ Request:
267
+ -------------------------------
268
+
269
+ * URL : https://test.address/?id=foo&secret=secret
270
+ * HTTP Method: GET
271
+ * IP address : 127.0.0.1
272
+ * Parameters : {\"id\"=>\"foo\", \"secret\"=>\"[FILTERED]\"}
273
+ * Timestamp : Sat, 20 Apr 2013 20:58:55 UTC +00:00
274
+ * Server : #{Socket.gethostname}
275
+ BODY
276
+
277
+ body << " * Rails root : #{Rails.root}\n" if defined?(Rails) && Rails.respond_to?(:root)
278
+
279
+ body << <<-BODY.gsub(/^ /, '')
280
+ * Process: #{Process.pid}
281
+
282
+ -------------------------------
283
+ Session:
284
+ -------------------------------
224
285
 
286
+ * session id: [FILTERED]
287
+ * data: {}
288
+
289
+ -------------------------------
290
+ Environment:
291
+ -------------------------------
292
+
293
+ * CONTENT_LENGTH : 0
294
+ * HTTPS : on
295
+ * HTTP_HOST : test.address
296
+ * HTTP_USER_AGENT : Rails Testing
297
+ * PATH_INFO : /
298
+ * QUERY_STRING : id=foo&secret=secret
299
+ * REMOTE_ADDR : 127.0.0.1
300
+ * REQUEST_METHOD : GET
301
+ * SCRIPT_NAME :
302
+ * SERVER_NAME : example.org
303
+ * SERVER_PORT : 80
304
+ * action_controller.instance : #{@controller}
305
+ * action_dispatch.parameter_filter : [\"secret\"]
306
+ * action_dispatch.request.content_type :
307
+ * action_dispatch.request.parameters : {"id"=>"foo", "secret"=>"[FILTERED]"}
308
+ * action_dispatch.request.path_parameters : {}
309
+ * action_dispatch.request.query_parameters : {"id"=>"foo", "secret"=>"[FILTERED]"}
310
+ * action_dispatch.request.request_parameters: {}
311
+ * rack.errors : #{@test_env['rack.errors']}
312
+ * rack.input : #{@test_env['rack.input']}
313
+ * rack.multiprocess : true
314
+ * rack.multithread : true
315
+ * rack.request.query_hash : {"id"=>"foo", "secret"=>"[FILTERED]"}
316
+ * rack.request.query_string : id=foo&secret=secret
317
+ * rack.run_once : false
318
+ * rack.session : {}
319
+ * rack.url_scheme : http
320
+ * rack.version : #{Rack::VERSION}
321
+
322
+ -------------------------------
323
+ Backtrace:
324
+ -------------------------------
325
+
326
+ test/exception_notifier/email_notifier_test.rb:20
327
+
328
+ -------------------------------
329
+ Data:
330
+ -------------------------------
331
+
332
+ * data: {:message=>\"My Custom Message\"}
333
+
334
+
335
+ BODY
336
+
337
+ assert_equal body, @mail.decode_body
338
+ end
339
+
340
+ test 'should not include controller and action names in subject' do
225
341
  email_notifier = ExceptionNotifier::EmailNotifier.new(
226
- :email_prefix => '[Dummy ERROR] ',
227
- :sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
228
- :exception_recipients => %w{dummyexceptions@example.com},
229
- :delivery_method => :test
342
+ sender_address: %("Dummy Notifier" <dummynotifier@example.com>),
343
+ exception_recipients: %w[dummyexceptions@example.com],
344
+ include_controller_and_action_names_in_subject: false
230
345
  )
231
346
 
232
- mail = email_notifier.call(@exception, { accumulated_errors_count: 3 })
233
- assert mail.subject.start_with?("[Dummy ERROR] (3 times) (ZeroDivisionError)")
347
+ mail = email_notifier.call(@exception, env: @test_env)
348
+
349
+ assert_equal '[ERROR] (ZeroDivisionError) "divided by 0"', mail.subject
234
350
  end
235
351
  end