Creating notifier and first attempt to send instructions to confirm by email. Some minor refactoring.
This commit is contained in:
parent
bb336150fe
commit
3e6d1ffe43
|
@ -1,3 +1,5 @@
|
|||
require 'devise/authenticable'
|
||||
require 'devise/confirmable'
|
||||
|
||||
require 'devise/notifier'
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ module Devise
|
|||
extend ClassMethods
|
||||
|
||||
before_create :generate_confirmation_token
|
||||
after_create :send_confirmation_instructions
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -35,8 +36,21 @@ module Devise
|
|||
self.confirmation_token = secure_digest(Time.now.utc, random_string, password)
|
||||
end
|
||||
|
||||
# Send confirmation instructions by email
|
||||
def send_confirmation_instructions
|
||||
# ::Devise::Notifier.deliver_confirmation_instructions(self)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
# Hook default authenticate to provide test whether the account is confirmed
|
||||
# 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?
|
||||
#end
|
||||
|
||||
# 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
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
module Devise
|
||||
class Notifier < ::ActionMailer::Base
|
||||
self.view_paths.unshift(File.join(File.dirname(__FILE__), '..', '..', 'views'))
|
||||
|
||||
def confirmation_instructions(record)
|
||||
#
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -105,12 +105,19 @@ class AuthenticableTest < ActiveSupport::TestCase
|
|||
|
||||
test 'should authenticate a valid user with email and password and return it' do
|
||||
user = create_user
|
||||
authenticated_user = User.authenticate('test@email.com', '12345')
|
||||
authenticated_user = User.authenticate(user.email, user.password)
|
||||
assert_equal authenticated_user, user
|
||||
end
|
||||
|
||||
test 'should return nil when authenticating an invalid user' do
|
||||
authenticated_user = User.authenticate('another.email@email.com', '12345')
|
||||
test 'should return nil when authenticating an invalid user by email' do
|
||||
user = create_user
|
||||
authenticated_user = User.authenticate('another.email@email.com', user.password)
|
||||
assert_nil authenticated_user
|
||||
end
|
||||
|
||||
test 'should return nil when authenticating an invalid user by password' do
|
||||
user = create_user
|
||||
authenticated_user = User.authenticate(user.email, 'another_password')
|
||||
assert_nil authenticated_user
|
||||
end
|
||||
end
|
||||
|
|
|
@ -87,5 +87,25 @@ class ConfirmableTest < ActiveSupport::TestCase
|
|||
assert confirmed_user.confirmed?
|
||||
assert confirmed_user.errors[:email]
|
||||
end
|
||||
|
||||
# test 'should not authenticate a user not confirmed' do
|
||||
# user = create_user
|
||||
# authenticated_user = User.authenticate(user.email, user.password)
|
||||
# assert_nil authenticated_user
|
||||
# end
|
||||
|
||||
# test 'should authenticate a confirmed user' do
|
||||
# user = create_user
|
||||
# user.confirm!
|
||||
# authenticated_user = User.authenticate(user.email, user.password)
|
||||
# assert_not_nil authenticated_user
|
||||
# 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
|
||||
end
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
require 'test_helper'
|
||||
|
||||
class NotifierTest < ActiveSupport::TestCase
|
||||
|
||||
def setup
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
# TODO
|
||||
end
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
RAILS_ENV = ENV["RAILS_ENV"] = "test"
|
||||
|
||||
require 'test/unit'
|
||||
require 'rubygems'
|
||||
require 'active_support'
|
||||
require 'active_support/test_case'
|
||||
require 'action_mailer'
|
||||
require 'active_record'
|
||||
require 'mocha'
|
||||
|
||||
require File.join(File.dirname(__FILE__), '..', 'lib', 'devise')
|
||||
|
||||
|
@ -24,6 +26,11 @@ class User < ::ActiveRecord::Base
|
|||
include ::Devise::Confirmable
|
||||
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
|
||||
|
@ -40,8 +47,14 @@ class ActiveSupport::TestCase
|
|||
|
||||
# Helpers for creating new users
|
||||
#
|
||||
def generate_unique_email
|
||||
@@email_count ||= 0
|
||||
@@email_count += 1
|
||||
"test#{@@email_count}@email.com"
|
||||
end
|
||||
|
||||
def valid_attributes(attributes={})
|
||||
{ :email => 'test@email.com',
|
||||
{ :email => generate_unique_email,
|
||||
:password => '12345',
|
||||
:password_confirmation => '12345' }.update(attributes)
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue