riscfuture-hoptoad_notifier 2.3.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/CHANGELOG +218 -0
  2. data/INSTALL +25 -0
  3. data/MIT-LICENSE +22 -0
  4. data/README.rdoc +394 -0
  5. data/Rakefile +217 -0
  6. data/SUPPORTED_RAILS_VERSIONS +10 -0
  7. data/TESTING.rdoc +8 -0
  8. data/generators/hoptoad/hoptoad_generator.rb +63 -0
  9. data/generators/hoptoad/lib/insert_commands.rb +34 -0
  10. data/generators/hoptoad/lib/rake_commands.rb +24 -0
  11. data/generators/hoptoad/templates/capistrano_hook.rb +6 -0
  12. data/generators/hoptoad/templates/hoptoad_notifier_tasks.rake +25 -0
  13. data/generators/hoptoad/templates/initializer.rb +6 -0
  14. data/lib/hoptoad_notifier.rb +148 -0
  15. data/lib/hoptoad_notifier/backtrace.rb +99 -0
  16. data/lib/hoptoad_notifier/capistrano.rb +20 -0
  17. data/lib/hoptoad_notifier/configuration.rb +236 -0
  18. data/lib/hoptoad_notifier/notice.rb +334 -0
  19. data/lib/hoptoad_notifier/rack.rb +40 -0
  20. data/lib/hoptoad_notifier/rails.rb +39 -0
  21. data/lib/hoptoad_notifier/rails/action_controller_catcher.rb +29 -0
  22. data/lib/hoptoad_notifier/rails/controller_methods.rb +60 -0
  23. data/lib/hoptoad_notifier/rails/error_lookup.rb +33 -0
  24. data/lib/hoptoad_notifier/rails/javascript_notifier.rb +43 -0
  25. data/lib/hoptoad_notifier/rails3_tasks.rb +91 -0
  26. data/lib/hoptoad_notifier/railtie.rb +29 -0
  27. data/lib/hoptoad_notifier/sender.rb +63 -0
  28. data/lib/hoptoad_notifier/tasks.rb +97 -0
  29. data/lib/hoptoad_notifier/version.rb +3 -0
  30. data/lib/hoptoad_tasks.rb +44 -0
  31. data/lib/rails/generators/hoptoad/hoptoad_generator.rb +69 -0
  32. data/lib/templates/javascript_notifier.erb +6 -0
  33. data/lib/templates/rescue.erb +91 -0
  34. data/rails/init.rb +1 -0
  35. data/script/integration_test.rb +38 -0
  36. data/test/backtrace_test.rb +118 -0
  37. data/test/catcher_test.rb +329 -0
  38. data/test/configuration_test.rb +209 -0
  39. data/test/helper.rb +239 -0
  40. data/test/hoptoad_tasks_test.rb +152 -0
  41. data/test/logger_test.rb +85 -0
  42. data/test/notice_test.rb +457 -0
  43. data/test/notifier_test.rb +222 -0
  44. data/test/rack_test.rb +58 -0
  45. data/test/rails_initializer_test.rb +36 -0
  46. data/test/sender_test.rb +123 -0
  47. metadata +197 -0
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class LoggerTest < Test::Unit::TestCase
4
+ def stub_http(response, body = nil)
5
+ response.stubs(:body => body) if body
6
+ @http = stub(:post => response,
7
+ :read_timeout= => nil,
8
+ :open_timeout= => nil,
9
+ :use_ssl= => nil)
10
+ Net::HTTP.stubs(:new).returns(@http)
11
+ end
12
+
13
+ def send_notice
14
+ HoptoadNotifier.sender.send_to_hoptoad('data')
15
+ end
16
+
17
+ def stub_verbose_log
18
+ HoptoadNotifier.stubs(:write_verbose_log)
19
+ end
20
+
21
+ def assert_logged(expected)
22
+ assert_received(HoptoadNotifier, :write_verbose_log) do |expect|
23
+ expect.with {|actual| actual =~ expected }
24
+ end
25
+ end
26
+
27
+ def assert_not_logged(expected)
28
+ assert_received(HoptoadNotifier, :write_verbose_log) do |expect|
29
+ expect.with {|actual| actual =~ expected }.never
30
+ end
31
+ end
32
+
33
+ def configure
34
+ HoptoadNotifier.configure { |config| }
35
+ end
36
+
37
+ should "report that notifier is ready when configured" do
38
+ stub_verbose_log
39
+ configure
40
+ assert_logged /Notifier (.*) ready/
41
+ end
42
+
43
+ should "not report that notifier is ready when internally configured" do
44
+ stub_verbose_log
45
+ HoptoadNotifier.configure(true) { |config| }
46
+ assert_not_logged /.*/
47
+ end
48
+
49
+ should "print environment info a successful notification without a body" do
50
+ reset_config
51
+ stub_verbose_log
52
+ stub_http(Net::HTTPSuccess)
53
+ send_notice
54
+ assert_logged /Environment Info:/
55
+ assert_not_logged /Response from Hoptoad:/
56
+ end
57
+
58
+ should "print environment info on a failed notification without a body" do
59
+ reset_config
60
+ stub_verbose_log
61
+ stub_http(Net::HTTPError)
62
+ send_notice
63
+ assert_logged /Environment Info:/
64
+ assert_not_logged /Response from Hoptoad:/
65
+ end
66
+
67
+ should "print environment info and response on a success with a body" do
68
+ reset_config
69
+ stub_verbose_log
70
+ stub_http(Net::HTTPSuccess, 'test')
71
+ send_notice
72
+ assert_logged /Environment Info:/
73
+ assert_logged /Response from Hoptoad:/
74
+ end
75
+
76
+ should "print environment info and response on a failure with a body" do
77
+ reset_config
78
+ stub_verbose_log
79
+ stub_http(Net::HTTPError, 'test')
80
+ send_notice
81
+ assert_logged /Environment Info:/
82
+ assert_logged /Response from Hoptoad:/
83
+ end
84
+
85
+ end
@@ -0,0 +1,457 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class NoticeTest < Test::Unit::TestCase
4
+
5
+ include DefinesConstants
6
+
7
+ def configure
8
+ returning HoptoadNotifier::Configuration.new do |config|
9
+ config.api_key = 'abc123def456'
10
+ end
11
+ end
12
+
13
+ def build_notice(args = {})
14
+ configuration = args.delete(:configuration) || configure
15
+ HoptoadNotifier::Notice.new(configuration.merge(args))
16
+ end
17
+
18
+ def stub_request(attrs = {})
19
+ stub('request', { :parameters => { 'one' => 'two' },
20
+ :protocol => 'http',
21
+ :host => 'some.host',
22
+ :request_uri => '/some/uri',
23
+ :session => { :to_hash => { 'a' => 'b' } },
24
+ :env => { 'three' => 'four' } }.update(attrs))
25
+ end
26
+
27
+ should "set the api key" do
28
+ api_key = 'key'
29
+ notice = build_notice(:api_key => api_key)
30
+ assert_equal api_key, notice.api_key
31
+ end
32
+
33
+ should "accept a project root" do
34
+ project_root = '/path/to/project'
35
+ notice = build_notice(:project_root => project_root)
36
+ assert_equal project_root, notice.project_root
37
+ end
38
+
39
+ should "accept a component" do
40
+ assert_equal 'users_controller', build_notice(:component => 'users_controller').controller
41
+ end
42
+
43
+ should "alias the component as controller" do
44
+ assert_equal 'users_controller', build_notice(:controller => 'users_controller').component
45
+ assert_equal 'users_controller', build_notice(:component => 'users_controller').controller
46
+ end
47
+
48
+ should "accept a action" do
49
+ assert_equal 'index', build_notice(:action => 'index').action
50
+ end
51
+
52
+ should "accept a url" do
53
+ url = 'http://some.host/uri'
54
+ notice = build_notice(:url => url)
55
+ assert_equal url, notice.url
56
+ end
57
+
58
+ should "accept a backtrace from an exception or hash" do
59
+ array = ["user.rb:34:in `crazy'"]
60
+ exception = build_exception
61
+ exception.set_backtrace array
62
+ backtrace = HoptoadNotifier::Backtrace.parse(array)
63
+ notice_from_exception = build_notice(:exception => exception)
64
+
65
+
66
+ assert_equal backtrace,
67
+ notice_from_exception.backtrace,
68
+ "backtrace was not correctly set from an exception"
69
+
70
+ notice_from_hash = build_notice(:backtrace => array)
71
+ assert_equal backtrace,
72
+ notice_from_hash.backtrace,
73
+ "backtrace was not correctly set from a hash"
74
+ end
75
+
76
+ should "pass its backtrace filters for parsing" do
77
+ backtrace_array = ['my/file/backtrace:3']
78
+ exception = build_exception
79
+ exception.set_backtrace(backtrace_array)
80
+ HoptoadNotifier::Backtrace.expects(:parse).with(backtrace_array, {:filters => 'foo'})
81
+
82
+ notice = HoptoadNotifier::Notice.new({:exception => exception, :backtrace_filters => 'foo'})
83
+ end
84
+
85
+ should "set the error class from an exception or hash" do
86
+ assert_accepts_exception_attribute :error_class do |exception|
87
+ exception.class.name
88
+ end
89
+ end
90
+
91
+ should "set the error message from an exception or hash" do
92
+ assert_accepts_exception_attribute :error_message do |exception|
93
+ "#{exception.class.name}: #{exception.message}"
94
+ end
95
+ end
96
+
97
+ should "accept parameters from a request or hash" do
98
+ parameters = { 'one' => 'two' }
99
+ notice_from_hash = build_notice(:parameters => parameters)
100
+ assert_equal notice_from_hash.parameters, parameters
101
+ end
102
+
103
+ should "accept session data from a session[:data] hash" do
104
+ data = { 'one' => 'two' }
105
+ notice = build_notice(:session => { :data => data })
106
+ assert_equal data, notice.session_data
107
+ end
108
+
109
+ should "accept session data from a session_data hash" do
110
+ data = { 'one' => 'two' }
111
+ notice = build_notice(:session_data => data)
112
+ assert_equal data, notice.session_data
113
+ end
114
+
115
+ should "accept an environment name" do
116
+ assert_equal 'development', build_notice(:environment_name => 'development').environment_name
117
+ end
118
+
119
+ should "accept CGI data from a hash" do
120
+ data = { 'string' => 'value' }
121
+ notice = build_notice(:cgi_data => data)
122
+ assert_equal data, notice.cgi_data, "should take CGI data from a hash"
123
+ end
124
+
125
+ should "accept notifier information" do
126
+ params = { :notifier_name => 'a name for a notifier',
127
+ :notifier_version => '1.0.5',
128
+ :notifier_url => 'http://notifiers.r.us/download' }
129
+ notice = build_notice(params)
130
+ assert_equal params[:notifier_name], notice.notifier_name
131
+ assert_equal params[:notifier_version], notice.notifier_version
132
+ assert_equal params[:notifier_url], notice.notifier_url
133
+ end
134
+
135
+ should "set sensible defaults without an exception" do
136
+ backtrace = HoptoadNotifier::Backtrace.parse(build_backtrace_array)
137
+ notice = build_notice(:backtrace => build_backtrace_array)
138
+
139
+ assert_equal 'Notification', notice.error_message
140
+ assert_array_starts_with backtrace.lines, notice.backtrace.lines
141
+ assert_equal({}, notice.parameters)
142
+ assert_equal({}, notice.session_data)
143
+ end
144
+
145
+ should "use the caller as the backtrace for an exception without a backtrace" do
146
+ filters = HoptoadNotifier::Configuration.new.backtrace_filters
147
+ backtrace = HoptoadNotifier::Backtrace.parse(caller, :filters => filters)
148
+ notice = build_notice(:exception => StandardError.new('error'), :backtrace => nil)
149
+
150
+ assert_array_starts_with backtrace.lines, notice.backtrace.lines
151
+ end
152
+
153
+ should "convert unserializable objects to strings" do
154
+ assert_serializes_hash(:parameters)
155
+ assert_serializes_hash(:cgi_data)
156
+ assert_serializes_hash(:session_data)
157
+ end
158
+
159
+ should "filter parameters" do
160
+ assert_filters_hash(:parameters)
161
+ end
162
+
163
+ should "filter cgi data" do
164
+ assert_filters_hash(:cgi_data)
165
+ end
166
+
167
+ should "filter session" do
168
+ assert_filters_hash(:session_data)
169
+ end
170
+
171
+ should "remove rack.request.form_vars" do
172
+ original = {
173
+ "rack.request.form_vars" => "story%5Btitle%5D=The+TODO+label",
174
+ "abc" => "123"
175
+ }
176
+
177
+ notice = build_notice(:cgi_data => original)
178
+ assert_equal({"abc" => "123"}, notice.cgi_data)
179
+ end
180
+
181
+ context "a Notice turned into XML" do
182
+ setup do
183
+ HoptoadNotifier.configure do |config|
184
+ config.api_key = "1234567890"
185
+ end
186
+
187
+ @exception = build_exception
188
+
189
+ @notice = build_notice({
190
+ :notifier_name => 'a name',
191
+ :notifier_version => '1.2.3',
192
+ :notifier_url => 'http://some.url/path',
193
+ :exception => @exception,
194
+ :controller => "controller",
195
+ :action => "action",
196
+ :url => "http://url.com",
197
+ :parameters => { "paramskey" => "paramsvalue",
198
+ "nestparentkey" => { "nestkey" => "nestvalue" } },
199
+ :session_data => { "sessionkey" => "sessionvalue" },
200
+ :cgi_data => { "cgikey" => "cgivalue" },
201
+ :project_root => "RAILS_ROOT",
202
+ :environment_name => "RAILS_ENV"
203
+ })
204
+
205
+ @xml = @notice.to_xml
206
+
207
+ @document = Nokogiri::XML::Document.parse(@xml)
208
+ end
209
+
210
+ should "validate against the XML schema" do
211
+ assert_valid_notice_document @document
212
+ end
213
+
214
+ should "serialize a Notice to XML when sent #to_xml" do
215
+ assert_valid_node(@document, "//api-key", @notice.api_key)
216
+
217
+ assert_valid_node(@document, "//notifier/name", @notice.notifier_name)
218
+ assert_valid_node(@document, "//notifier/version", @notice.notifier_version)
219
+ assert_valid_node(@document, "//notifier/url", @notice.notifier_url)
220
+
221
+ assert_valid_node(@document, "//error/class", @notice.error_class)
222
+ assert_valid_node(@document, "//error/message", @notice.error_message)
223
+
224
+ assert_valid_node(@document, "//error/backtrace/line/@number", @notice.backtrace.lines.first.number)
225
+ assert_valid_node(@document, "//error/backtrace/line/@file", @notice.backtrace.lines.first.file)
226
+ assert_valid_node(@document, "//error/backtrace/line/@method", @notice.backtrace.lines.first.method)
227
+
228
+ assert_valid_node(@document, "//request/url", @notice.url)
229
+ assert_valid_node(@document, "//request/component", @notice.controller)
230
+ assert_valid_node(@document, "//request/action", @notice.action)
231
+
232
+ assert_valid_node(@document, "//request/params/var/@key", "paramskey")
233
+ assert_valid_node(@document, "//request/params/var", "paramsvalue")
234
+ assert_valid_node(@document, "//request/params/var/@key", "nestparentkey")
235
+ assert_valid_node(@document, "//request/params/var/var/@key", "nestkey")
236
+ assert_valid_node(@document, "//request/params/var/var", "nestvalue")
237
+ assert_valid_node(@document, "//request/session/var/@key", "sessionkey")
238
+ assert_valid_node(@document, "//request/session/var", "sessionvalue")
239
+ assert_valid_node(@document, "//request/cgi-data/var/@key", "cgikey")
240
+ assert_valid_node(@document, "//request/cgi-data/var", "cgivalue")
241
+
242
+ assert_valid_node(@document, "//server-environment/project-root", "RAILS_ROOT")
243
+ assert_valid_node(@document, "//server-environment/environment-name", "RAILS_ENV")
244
+ end
245
+ end
246
+
247
+ should "not send empty request data" do
248
+ notice = build_notice
249
+ assert_nil notice.url
250
+ assert_nil notice.controller
251
+ assert_nil notice.action
252
+
253
+ xml = notice.to_xml
254
+ document = Nokogiri::XML.parse(xml)
255
+ assert_nil document.at('//request/url')
256
+ assert_nil document.at('//request/component')
257
+ assert_nil document.at('//request/action')
258
+
259
+ assert_valid_notice_document document
260
+ end
261
+
262
+ %w(url controller action).each do |var|
263
+ should "send a request if #{var} is present" do
264
+ notice = build_notice(var.to_sym => 'value')
265
+ xml = notice.to_xml
266
+ document = Nokogiri::XML.parse(xml)
267
+ assert_not_nil document.at('//request')
268
+ end
269
+ end
270
+
271
+ %w(parameters cgi_data session_data).each do |var|
272
+ should "send a request if #{var} is present" do
273
+ notice = build_notice(var.to_sym => { 'key' => 'value' })
274
+ xml = notice.to_xml
275
+ document = Nokogiri::XML.parse(xml)
276
+ assert_not_nil document.at('//request')
277
+ end
278
+ end
279
+
280
+ should "not ignore an exception not matching ignore filters" do
281
+ notice = build_notice(:error_class => 'ArgumentError',
282
+ :ignore => ['Argument'],
283
+ :ignore_by_filters => [lambda { |notice| false }])
284
+ assert !notice.ignore?
285
+ end
286
+
287
+ should "ignore an exception with a matching error class" do
288
+ notice = build_notice(:error_class => 'ArgumentError',
289
+ :ignore => [ArgumentError])
290
+ assert notice.ignore?
291
+ end
292
+
293
+ should "ignore an exception with a matching error class name" do
294
+ notice = build_notice(:error_class => 'ArgumentError',
295
+ :ignore => ['ArgumentError'])
296
+ assert notice.ignore?
297
+ end
298
+
299
+ should "ignore an exception with a matching filter" do
300
+ filter = lambda {|notice| notice.error_class == 'ArgumentError' }
301
+ notice = build_notice(:error_class => 'ArgumentError',
302
+ :ignore_by_filters => [filter])
303
+ assert notice.ignore?
304
+ end
305
+
306
+ should "not raise without an ignore list" do
307
+ notice = build_notice(:ignore => nil, :ignore_by_filters => nil)
308
+ assert_nothing_raised do
309
+ notice.ignore?
310
+ end
311
+ end
312
+
313
+ should "ignore RecordNotFound error by default" do
314
+ notice = build_notice(:error_class => 'ActiveRecord::RecordNotFound')
315
+ assert notice.ignore?
316
+ end
317
+
318
+ should "ignore RoutingError error by default" do
319
+ notice = build_notice(:error_class => 'ActionController::RoutingError')
320
+ assert notice.ignore?
321
+ end
322
+
323
+ should "ignore InvalidAuthenticityToken error by default" do
324
+ notice = build_notice(:error_class => 'ActionController::InvalidAuthenticityToken')
325
+ assert notice.ignore?
326
+ end
327
+
328
+ should "ignore TamperedWithCookie error by default" do
329
+ notice = build_notice(:error_class => 'CGI::Session::CookieStore::TamperedWithCookie')
330
+ assert notice.ignore?
331
+ end
332
+
333
+ should "ignore UnknownAction error by default" do
334
+ notice = build_notice(:error_class => 'ActionController::UnknownAction')
335
+ assert notice.ignore?
336
+ end
337
+
338
+ should "act like a hash" do
339
+ notice = build_notice(:error_message => 'some message')
340
+ assert_equal notice.error_message, notice[:error_message]
341
+ end
342
+
343
+ should "return params on notice[:request][:params]" do
344
+ params = { 'one' => 'two' }
345
+ notice = build_notice(:parameters => params)
346
+ assert_equal params, notice[:request][:params]
347
+ end
348
+
349
+ should "ensure #to_hash is called on objects that support it" do
350
+ assert_nothing_raised do
351
+ build_notice(:session => { :object => stub(:to_hash => {}) })
352
+ end
353
+ end
354
+
355
+ should "extract data from a rack environment hash" do
356
+ url = "https://subdomain.happylane.com:100/test/file.rb?var=value&var2=value2"
357
+ parameters = { 'var' => 'value', 'var2' => 'value2' }
358
+ env = Rack::MockRequest.env_for(url)
359
+
360
+ notice = build_notice(:rack_env => env)
361
+
362
+ assert_equal url, notice.url
363
+ assert_equal parameters, notice.parameters
364
+ assert_equal 'GET', notice.cgi_data['REQUEST_METHOD']
365
+ end
366
+
367
+ should "extract data from a rack environment hash with action_dispatch info" do
368
+ params = { 'controller' => 'users', 'action' => 'index', 'id' => '7' }
369
+ env = Rack::MockRequest.env_for('/', { 'action_dispatch.request.parameters' => params })
370
+
371
+ notice = build_notice(:rack_env => env)
372
+
373
+ assert_equal params, notice.parameters
374
+ assert_equal params['controller'], notice.component
375
+ assert_equal params['action'], notice.action
376
+ end
377
+
378
+ should "extract session data from a rack environment" do
379
+ session_data = { 'something' => 'some value' }
380
+ env = Rack::MockRequest.env_for('/', 'rack.session' => session_data)
381
+
382
+ notice = build_notice(:rack_env => env)
383
+
384
+ assert_equal session_data, notice.session_data
385
+ end
386
+
387
+ should "prefer passed session data to rack session data" do
388
+ session_data = { 'something' => 'some value' }
389
+ env = Rack::MockRequest.env_for('/')
390
+
391
+ notice = build_notice(:rack_env => env, :session_data => session_data)
392
+
393
+ assert_equal session_data, notice.session_data
394
+ end
395
+
396
+ def assert_accepts_exception_attribute(attribute, args = {}, &block)
397
+ exception = build_exception
398
+ block ||= lambda { exception.send(attribute) }
399
+ value = block.call(exception)
400
+
401
+ notice_from_exception = build_notice(args.merge(:exception => exception))
402
+
403
+ assert_equal notice_from_exception.send(attribute),
404
+ value,
405
+ "#{attribute} was not correctly set from an exception"
406
+
407
+ notice_from_hash = build_notice(args.merge(attribute => value))
408
+ assert_equal notice_from_hash.send(attribute),
409
+ value,
410
+ "#{attribute} was not correctly set from a hash"
411
+ end
412
+
413
+ def assert_serializes_hash(attribute)
414
+ [File.open(__FILE__), Proc.new { puts "boo!" }, Module.new].each do |object|
415
+ hash = {
416
+ :strange_object => object,
417
+ :sub_hash => {
418
+ :sub_object => object
419
+ },
420
+ :array => [object]
421
+ }
422
+ notice = build_notice(attribute => hash)
423
+ hash = notice.send(attribute)
424
+ assert_equal object.to_s, hash[:strange_object], "objects should be serialized"
425
+ assert_kind_of Hash, hash[:sub_hash], "subhashes should be kept"
426
+ assert_equal object.to_s, hash[:sub_hash][:sub_object], "subhash members should be serialized"
427
+ assert_kind_of Array, hash[:array], "arrays should be kept"
428
+ assert_equal object.to_s, hash[:array].first, "array members should be serialized"
429
+ end
430
+ end
431
+
432
+ def assert_valid_notice_document(document)
433
+ xsd_path = File.join(File.dirname(__FILE__), "hoptoad_2_0.xsd")
434
+ schema = Nokogiri::XML::Schema.new(IO.read(xsd_path))
435
+ errors = schema.validate(document)
436
+ assert errors.empty?, errors.collect{|e| e.message }.join
437
+ end
438
+
439
+ def assert_filters_hash(attribute)
440
+ filters = ["abc", :def]
441
+ original = { 'abc' => "123", 'def' => "456", 'ghi' => "789", 'nested' => { 'abc' => '100' } }
442
+ filtered = { 'abc' => "[FILTERED]",
443
+ 'def' => "[FILTERED]",
444
+ 'ghi' => "789",
445
+ 'nested' => { 'abc' => '[FILTERED]' } }
446
+
447
+ notice = build_notice(:params_filters => filters, attribute => original)
448
+
449
+ assert_equal(filtered,
450
+ notice.send(attribute))
451
+ end
452
+
453
+ def build_backtrace_array
454
+ ["app/models/user.rb:13:in `magic'",
455
+ "app/controllers/users_controller.rb:8:in `index'"]
456
+ end
457
+ end