Skip to content

Commit

Permalink
fix: use message occurrence for new project verification test
Browse files Browse the repository at this point in the history
  • Loading branch information
waltjones committed Jul 15, 2020
1 parent 1b739f0 commit c415250
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 130 deletions.
2 changes: 1 addition & 1 deletion lib/rollbar/rake_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

namespace :rollbar do
desc 'Verify your gem installation by sending a test exception to Rollbar'
desc 'Verify your gem installation by sending a test message to Rollbar'
task :test => [:environment] do
rollbar_dir = Gem.loaded_specs['rollbar'].full_gem_path
require "#{rollbar_dir}/lib/rollbar/rollbar_test"
Expand Down
127 changes: 6 additions & 121 deletions lib/rollbar/rollbar_test.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,16 @@
require 'rollbar'
begin
require 'rack/mock'
rescue LoadError
puts 'Cannot load rack/mock'
end
require 'logger'

# Module to inject into the Rails controllers or rack apps
module RollbarTest # :nodoc:
def test_rollbar
puts 'Raising RollbarTestingException to simulate app failure.'

raise RollbarTestingException.new, ::RollbarTest.success_message
end

def self.run
return unless confirmed_token?

configure_rails if defined?(Rails)

puts 'Testing manual report...'
Rollbar.error('Test error from rollbar:test')

return unless defined?(Rack::MockRequest)

protocol, app = setup_app

puts 'Processing...'
env = Rack::MockRequest.env_for("#{protocol}:https://www.example.com/verify", 'REMOTE_ADDR' => '127.0.0.1')

# Needed for Rails 6.x ActionDispatch::HostAuthorization (DNS rebinding protection)
env['HTTP_HOST'] = 'localhost'

status, = app.call(env)

puts error_message unless status.to_i == 500
end
puts 'Test sending to Rollbar...'
result = Rollbar.info('Test message from rollbar:test')

def self.configure_rails
Rails.logger = if defined?(ActiveSupport::TaggedLogging)
ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
else
Logger.new(STDOUT)
end

Rails.logger.level = Logger::DEBUG
Rollbar.preconfigure do |config|
config.logger = Rails.logger
if result == 'error'
puts error_message
else
puts success_message
end
end

Expand All @@ -58,69 +22,6 @@ def self.confirmed_token?
false
end

def self.authlogic_config
# from http:https://stackoverflow.com/questions/5270835/authlogic-activation-problems
return unless defined?(Authlogic)

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
end

def self.setup_app
puts 'Setting up the test app.'

if defined?(Rails)
app = rails_app

draw_rails_route(app)

authlogic_config

[rails_protocol(app), app]
else
['http', rack_app]
end
end

def self.rails_app
# The setup below is needed for Rails 5.x, but not for Rails 4.x and below.
# (And fails on Rails 4.x in various ways depending on the exact version.)
return Rails.application if Rails.version < '5.0.0'

# Spring now runs by default in development on all new Rails installs. This causes
# the new `/verify` route to not get picked up if `config.cache_classes == false`
# which is also a default in development env.
#
# `config.cache_classes` needs to be set, but the only possible time is at app load,
# so here we clone the default app with an updated config.
#
config = Rails.application.config
config.cache_classes = true

# Make a copy of the app, so the config can be updated.
Rails.application.class.name.constantize.new(:config => config)
end

def self.draw_rails_route(app)
app.routes_reloader.execute_if_updated
app.routes.draw do
get 'verify' => 'rollbar_test#verify', :as => 'verify'
end
end

def self.rails_protocol(app)
defined?(app.config.force_ssl && app.config.force_ssl) ? 'https' : 'http'
end

def self.rack_app
Class.new do
include RollbarTest

def self.call(_env)
new.test_rollbar
end
end
end

def self.token_error_message
'Rollbar needs an access token configured. Check the README for instructions.'
end
Expand All @@ -133,19 +34,3 @@ def self.success_message
'Testing rollbar with "rake rollbar:test". If you can see this, it works.'
end
end

class RollbarTestingException < RuntimeError; end

if defined?(Rails)
class RollbarTestController < ActionController::Base # :nodoc:
include RollbarTest

def verify
test_rollbar
end

def logger
nil
end
end
end
22 changes: 14 additions & 8 deletions spec/rollbar/rake_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@
reconfigure_notifier
end

after do
# Rails <= 4.x needs this, since we modify the default RouteSet in RollbarTest#run.
Rails.application.reload_routes!
end

it 'raises the test exception and exits with success message' do
expect { subject.run }.to raise_exception(RollbarTestingException) \
.with_message(Regexp.new(subject.success_message))
expect { subject.run }.to output(Regexp.new(subject.success_message)).to_stdout
end
end

context 'when rollbar is not configured' do
it 'exits with error message' do
it 'exits with token error message' do
expect { subject.run }.to output(Regexp.new(subject.token_error_message)).to_stdout
end
end

context 'when the occurrence fails' do
before do
reset_configuration
reconfigure_notifier
allow(Rollbar.notifier).to receive(:report).and_raise(StandardError)
end

it 'exits with error message' do
expect { subject.run }.to output(Regexp.new(subject.error_message)).to_stdout
end
end
end
end

0 comments on commit c415250

Please sign in to comment.