From 0b452cbd29779509d9679be218e6cf8a9ff1a71b Mon Sep 17 00:00:00 2001 From: "Carlos A. da Silva" Date: Thu, 17 Sep 2009 21:29:13 -0300 Subject: [PATCH] Setup confirmation instructions notification and tests green. --- lib/devise/confirmable.rb | 8 +++++--- lib/devise/notifier.rb | 5 +++-- test/confirmable_test.rb | 22 ++++++++++++++++------ test/notifier_test.rb | 6 +----- test/test_helper.rb | 14 ++++++++------ 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/lib/devise/confirmable.rb b/lib/devise/confirmable.rb index babc361f..5bf199b1 100644 --- a/lib/devise/confirmable.rb +++ b/lib/devise/confirmable.rb @@ -37,15 +37,16 @@ module Devise end # Send confirmation instructions by email + # def send_confirmation_instructions - # ::Devise::Notifier.deliver_confirmation_instructions(self) + ::Devise::Notifier.deliver_confirmation_instructions(self) end module ClassMethods - # Hook default authenticate to provide test whether the account is confirmed + # Hook default authenticate to test whether the account is confirmed or not # Returns the authenticated_user if it's confirmed, otherwise returns nil - # TODO + # def authenticate(email, password) confirmable = super confirmable if confirmable.confirmed? unless confirmable.nil? @@ -54,6 +55,7 @@ module Devise # Find a user by it's confirmation token and try to confirm it. # If no user is found, returns a new user # If the user is already confirmed, create an error for the user + # def find_and_confirm(confirmation_token) confirmable = find_or_initialize_by_confirmation_token(confirmation_token) unless confirmable.new_record? diff --git a/lib/devise/notifier.rb b/lib/devise/notifier.rb index 8867b5f9..523005e2 100644 --- a/lib/devise/notifier.rb +++ b/lib/devise/notifier.rb @@ -1,10 +1,11 @@ module Devise class Notifier < ::ActionMailer::Base - self.view_paths.unshift(File.join(File.dirname(__FILE__), '..', 'views')) def confirmation_instructions(record) - # + # TODO: configure email end end end +Devise::Notifier.template_root = File.join(File.dirname(__FILE__), '..', 'views') + diff --git a/test/confirmable_test.rb b/test/confirmable_test.rb index 0c7d48aa..3fbc047b 100644 --- a/test/confirmable_test.rb +++ b/test/confirmable_test.rb @@ -3,7 +3,9 @@ require 'test_helper' class ConfirmableTest < ActiveSupport::TestCase def setup - User.send :include, ::Devise::Confirmable + # Todo: refactor this! + User.send :include, ::Devise::Confirmable unless User.included_modules.include?(::Devise::Confirmable) + setup_mailer end test 'should not have confirmation code accessible' do @@ -106,10 +108,18 @@ class ConfirmableTest < ActiveSupport::TestCase assert_equal authenticated_user, user end -# test 'should send confirmation instructions by email' do -# assert_difference 'ActionMailer::Base.deliveries.size' do -# create_user -# end -# end + test 'should send confirmation instructions by email' do + assert_difference 'ActionMailer::Base.deliveries.size' do + create_user + end + end + + test 'should not send confirmation when trying to save an invalid user' do + assert_no_difference 'ActionMailer::Base.deliveries.size' do + user = new_user + user.stubs(:valid?).returns(false) + user.save + end + end end diff --git a/test/notifier_test.rb b/test/notifier_test.rb index d1766eaa..b1bfb5bd 100644 --- a/test/notifier_test.rb +++ b/test/notifier_test.rb @@ -1,10 +1,6 @@ require 'test_helper' -class NotifierTest < ActiveSupport::TestCase - - def setup - ActionMailer::Base.deliveries = [] - end +class NotifierTest < ActionMailer::TestCase # TODO end diff --git a/test/test_helper.rb b/test/test_helper.rb index 0c95c0c0..7a091eaf 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,4 @@ RAILS_ENV = ENV["RAILS_ENV"] = "test" - require 'test/unit' require 'rubygems' require 'active_support' @@ -9,6 +8,10 @@ require 'active_record' require File.join(File.dirname(__FILE__), '..', 'lib', 'devise') +ActionMailer::Base.delivery_method = :test +ActionMailer::Base.perform_deliveries = true + +ActiveRecord::Migration.verbose = false ActiveRecord::Base.logger = Logger.new(nil) ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:") ActiveRecord::Schema.define(:version => 1) do @@ -25,11 +28,6 @@ class User < ::ActiveRecord::Base include ::Devise::Authenticable end -ActionMailer::Base.delivery_method = :test -#ActionMailer::Base.delivery_method = :test -#ActionMailer::Base.perform_deliveries = true -#ActionMailer::Base.deliveries = [] - class ActiveSupport::TestCase def assert_not(assertion) assert !assertion @@ -44,6 +42,10 @@ class ActiveSupport::TestCase end alias :assert_present :assert_not_blank + def setup_mailer + ActionMailer::Base.deliveries = [] + end + # Helpers for creating new users # def generate_unique_email