dans mon controller, l'envoi d'un email peut lever une erreur (serveur SMTP off, busy, etc...)
ex :
def reset_password
respond_to do |format|
format.html {
@user = User.find_by_email(params[:email])
flash[:notice] = "Email address unknown: '#{CGI.escapeHTML params[:email]}'. Did you type it correctly?" if params[:email] and not @user
redirect_to login_path and return unless @user
@user.reset_activation_code
begin
UserMailer.deliver_password_key_notification(@user)
rescue Errno::ECONNREFUSED, Net::SMTPFatalError => e
flash[:notice] = "Could not send email to: '#{CGI.escapeHTML @user.email}'. Mail Server error."
render :action => 'new' and return false
rescue Net::SMTPServerBusy, Net::SMTPUnknownError, Net::SMTPSyntaxError, TimeoutError => e
flash[:notice] = "Could not send email to '#{CGI.escapeHTML @user.email}' Please, try later."
render :action => 'new' and return false
end
flash[:notice] = "Access key sent by email to >> #{CGI.escapeHTML @user.email}"
redirect_back_or_default('/')
}
end
pour tester l'envoi de l'email dans le controller_test
fixtures :users
def setup
@controller = SessionController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
# for testing action mailer
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.deliveries = []
@emails = ActionMailer::Base.deliveries
@emails.clear
UserMailer.class_eval do
unless method_defined? :password_key_notification_response
def password_key_notification_with_failure(user)
case password_key_notification_response
when :fatal
raise Net::SMTPFatalError.new('error!')
when :server_busy
raise Net::SMTPServerBusy.new('error!')
else
password_key_notification_without_failure(user)
end
end
alias_method_chain :password_key_notification, :failure
cattr_accessor :password_key_notification_response
end
end
end
....
TEST de L'ENVOI
def test_should_reset_password
users(:quentin).update_attribute :activation_code, nil
post :reset_password, :login => 'quentin', :email => 'quentin@example.com'
assert_equal 1, @emails.length
assert(@emails.first.subject =~ /Forgotten password/)
assert(@emails.first.body =~ /Hello #{assigns(:user).login}, click on the following link to access your profile and change your password./)
assert_equal "Access key sent by email to >> #{CGI.escapeHTML assigns(:user).email}", flash[:notice]
assert_response( :redirect )
end
TEST D'UNE ERREUR ADRESSE EMAIL
def test_wrong_email_should_fail_reset_password
users(:quentin).update_attribute :activation_code, nil
post :reset_password, :login => 'quentin', :email => 'bingo@example.com'
assert_nil assigns[:user]
assert_equal "Email address unknown: 'bingo@example.com'. Did you type it correctly?", flash[:notice]
assert_response( :redirect )
end
TEST D'UNE ERREUR SERVEUR SMTP
def test_smtp_error_should_fail_reset_password
UserMailer.password_key_notification_response = :fatal
users(:quentin).update_attribute :activation_code, nil
post :reset_password, :login => 'quentin', :email => 'quentin@example.com'
assert_equal "Could not send email to: '#{CGI.escapeHTML assigns(:user).email}'. Mail Server error.", flash[:notice]
assert_template( 'new' )
end
|
il y a 11 heures 9 min
il y a 13 heures 31 min
il y a 1 jour 22 heures
il y a 4 jours 5 heures
il y a 6 jours 18 heures
il y a 6 jours 18 heures
il y a 6 jours 19 heures
il y a 6 jours 23 heures
il y a 1 semaine 17 heures
il y a 1 semaine 17 heures