Permalink
Browse files

non stub tests for emailing, better configuration options

  • Loading branch information...
1 parent adf68ee commit 4b8f0b82451956369766f3d0a92c98a4a9c03308 Wesley Beary committed with mattetti Nov 5, 2008
@@ -16,8 +16,10 @@ Configuration goes in your projects `config/init.rb` file inside `Merb::BootLoad
:web_hooks => ['http://example.com'],
:email_addresses => ['hello@exceptions.com', 'user@myapp.com'],
:app_name => "My App Name",
+ :environments => ['production', 'staging'],
:email_from => "exceptions@myapp.com",
- :environments => ['production', 'staging']
+ :mailer_config => nil,
+ :mailer_delivery_method => :sendmail
}
The plugin now automatically includes itself into your Exceptions controller. If you are using an old version of this plugin, you can remove the include from your Exceptions controller.
@@ -40,6 +42,10 @@ Settings
`environments`: Notifications will only be sent for environments in this list, defaults to `production`
+`mailer_delivery_method`: The delivery method for notifications mailer, see merb-mailer documentation.
+
+`mailer_config`: A hash of configuration options for the notifications mailer, see merb-mailer documentation.
+
Advanced usage
--------------
merb-exceptions will deliver exceptions for any unhandled exceptions (exceptions that do not have views defined in the `Exceptions` controller)
@@ -1,20 +1,23 @@
# make sure we're running inside Merb
if defined?(Merb::Plugins)
- # Default configuration
- Merb::Plugins.config[:exceptions] = {
- :web_hooks => [],
- :email_addresses => [],
- :app_name => "Merb awesome Application",
- :email_from => "exceptions@app.com",
- :environments => ['production']
- }.merge(Merb::Plugins.config[:exceptions] || {})
-
Merb::BootLoader.before_app_loads do
end
Merb::BootLoader.after_app_loads do
+
+ # Default configuration
+ Merb::Plugins.config[:exceptions] = {
+ :web_hooks => [],
+ :email_addresses => [],
+ :app_name => "Merb awesome Application",
+ :environments => ['production'],
+ :email_from => "exceptions@myapp.com",
+ :mailer_config => nil,
+ :mailer_delivery_method => :sendmail
+ }.merge(Merb::Plugins.config[:exceptions] || {})
+
if Object.const_defined?(:Exceptions)
Exceptions.send(:include, MerbExceptions::ExceptionsHelper)
end
@@ -1,14 +1,20 @@
require 'net/http'
require 'uri'
-require "erb"
+require 'erb'
+require 'merb-mailer'
module MerbExceptions
class Notification
+
+ class Mailer < Merb::Mailer
+ end
+
attr_reader :details
def initialize(details = nil)
- @details = details || {}
- @config = Merb::Plugins.config[:exceptions]
+ @details = details || []
+ Mailer.config = Merb::Plugins.config[:exceptions][:mailer_config]
+ Mailer.delivery_method = Merb::Plugins.config[:exceptions][:mailer_delivery_method]
end
def deliver!
@@ -17,18 +23,16 @@ def deliver!
end
def deliver_web_hooks!
- return unless should_deliver_notifications?
Merb.logger.info "DELIVERING EXCEPTION WEB HOOKS"
web_hooks.each do |address|
post_hook(address)
end
end
def deliver_emails!
- return unless should_deliver_notifications?
Merb.logger.info "DELIVERING EXCEPTION EMAILS"
email_addresses.each do |address|
- send_notification_email(address)
+ send_email(address)
end
end
@@ -38,21 +42,16 @@ def email_addresses; option_as_array(:email_addresses); end
def environments; option_as_array(:environments); end
-
- def should_deliver_notifications?
- environments.include? Merb.env
- end
-
-
def params
+ @params ||=
{
'request_url' => details['url'],
'request_controller' => details['params'][:controller],
'request_action' => details['params'][:action],
'request_params' => details['params'],
'environment' => details['environment'],
'exceptions' => details['exceptions'],
- 'app_name' => @config[:app_name]
+ 'app_name' => Merb::Plugins.config[:exceptions][:app_name]
}
end
@@ -65,31 +64,29 @@ def post_hook(address)
Net::HTTP.post_form( uri, params ).body
end
- def send_email(address, body)
+ def email_body
+ @body ||= begin
+ path = File.join(File.dirname(__FILE__), 'templates', 'email.erb')
+ template = Erubis::Eruby.new(File.open(path,'r') { |f| f.read })
+ template.result(binding)
+ end
+ end
+
+ def send_email(address)
Merb.logger.info "- emailing to #{address}"
- email = Merb::Mailer.new({
+ email = Mailer.new({
:to => address,
- :from => @config[:email_from],
- :subject => "[#{@config[:app_name]} EXCEPTION]",
- :text => body
+ :from => Merb::Plugins.config[:exceptions][:email_from],
+ :subject => "[#{Merb::Plugins.config[:exceptions][:app_name]} EXCEPTION]",
+ :text => email_body
})
email.deliver!
end
- def send_notification_email(address)
- send_email(address, plain_text_mail_body)
- end
-
- def plain_text_mail_body
- path = File.join(File.dirname(__FILE__), 'templates', 'email.erb')
- template = Erubis::Eruby.new(File.open(path,'r') { |f| f.read })
- template.result(binding)
- end
-
# Used so that we can accept either a single value or array (e.g. of
# webhooks) in our YAML file.
def option_as_array(option)
- value = @config[option]
+ value = Merb::Plugins.config[:exceptions][option]
case value
when Array
value.reject { |e| e.nil? } # Don't accept nil values
@@ -8,37 +8,40 @@
it "should create a new notification without errors" do
lambda { Notification.new(mock_details) }.should_not raise_error
end
-
+
it "should set the detail values to those provided" do
Notification.new(mock_details).details.should == mock_details
end
end
-
+
describe ".deliver!" do
before :each do
@notification = Notification.new(mock_details)
- @notification.stub!('deliver_emails!')
@notification.stub!('deliver_web_hooks!')
end
-
+
+ after :each do
+ Notification::Mailer.deliveries.clear
+ end
+
it "should deliver web hooks" do
@notification.should_receive('deliver_web_hooks!')
@notification.deliver!
end
it "should deliver emails" do
- @notification.should_receive('deliver_emails!')
+ Notification::Mailer.deliveries.length.should == 0
@notification.deliver!
+ Notification::Mailer.deliveries.length.should == 2
end
end
-
+
describe ".deliver_web_hooks!" do
before :each do
- mock_merb_config({:web_hooks => ['http://www.test1.com', 'http://www.test2.com']})
@notification = Notification.new(mock_details)
@notification.stub!(:post_hook)
end
-
+
it "should call post_hook for each url" do
@notification.should_receive(:post_hook).
once.with('http://www.test1.com')
@@ -50,35 +53,17 @@
describe ".deliver_emails!" do
before :each do
- mock_merb_config({:email_addresses => ['user1@test.com', 'user2@test.com']})
@notification = Notification.new(mock_details)
- @notification.stub!(:send_notification_email)
+ Notification::Mailer.deliveries.clear
end
it "should call send_notification_email for each address" do
- @notification.should_receive(:send_notification_email).
- once.with('user1@test.com')
- @notification.should_receive(:send_notification_email).
- once.with('user2@test.com')
@notification.deliver_emails!
+ Notification::Mailer.deliveries.first.to.should include("user1@test.com")
+ Notification::Mailer.deliveries.first.from.should include("exceptions@myapp.com")
+ Notification::Mailer.deliveries.last.to.should include("user2@test.com")
+ Notification::Mailer.deliveries.first.text.should == Notification::Mailer.deliveries.last.text
end
end
-
- # Running tests with test environment
- describe ".should_deliver_notifications?" do
- it "should return true if the current environment is on the config[:environments] list of one item" do
- mock_merb_config({ :environments => 'test' })
- Notification.new(mock_details).should_deliver_notifications?.should be_true
- end
-
- it "should return true if the current environment is on the config[:environments] list as an array" do
- mock_merb_config({ :environments => ['staging', 'test'] })
- Notification.new(mock_details).should_deliver_notifications?.should be_true
- end
- it "should return false if the current environment is not on the config[:environments] list" do
- mock_merb_config({ :environments => ['staging', 'development'] })
- Notification.new(mock_details).should_deliver_notifications?.should be_false
- end
- end
end
@@ -24,7 +24,12 @@ def not_found
end
end
-Merb::Plugins.config[:exceptions][:environments] = 'test'
+Merb::Plugins.config[:exceptions] = {
+ :email_addresses => ['user1@test.com', 'user2@test.com'],
+ :web_hooks => ['http://www.test1.com', 'http://www.test2.com'],
+ :environments => ['test'],
+ :mailer_delivery_method => :test_send
+}
Merb.start :environment => 'test'
module Merb
@@ -81,14 +86,10 @@ def with_level(level)
module NotificationSpecHelper
def mock_details(opts={})
{
- 'exception' => {},
+ 'exceptions' => [],
'params' => { :controller=>'errors', :action=>'show' },
'environment' => { 'key1'=>'value1', 'key2'=>'value2' },
'url' => 'http://www.my-app.com/errors/1'
}.merge(opts)
end
-
- def mock_merb_config(opts={})
- Merb::Plugins.config[:exceptions].merge!(opts)
- end
end

0 comments on commit 4b8f0b8

Please sign in to comment.