Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed a nasty bug on password reminder mailer introduced by a nasty b…

…ug on restful_autenthicated introduced by ... [#121 state:resolved]
  • Loading branch information...
commit 11e3fde3ae9327cea09ffb78f7fba6143a015397 1 parent 1cffa18
@aitor aitor authored
View
22 Rakefile
@@ -0,0 +1,22 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the tog_user plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the tog_user plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Tog User'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README', 'MIT-LICENCE', 'CHANGELOG')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
7 app/controllers/users_controller.rb
@@ -43,10 +43,9 @@ def activate
def forgot
if request.post?
- user = User.find_by_email(params[:user][:email])
- if user
- user.forgot_password
- flash[:notice] = I18n.t("tog_user.user.password_reset_sent", :email => user.email)
+ if @user = User.find_by_email(params[:user][:email])
+ @user.forgot_password
+ flash[:notice] = I18n.t("tog_user.user.password_reset_sent", :email => @user.email)
else
flash[:error] = I18n.t("tog_user.user.password_reset_not_found", :email => params[:user][:email])
end
View
1  app/models/user.rb
@@ -99,6 +99,7 @@ def forget_me
end
def make_activation_code
+ return if self.activation_code
self.deleted_at = nil
self.activation_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
end
View
11 app/models/user_observer.rb
@@ -1,16 +1,11 @@
class UserObserver < ActiveRecord::Observer
def after_create(user)
- UserMailer.deliver_signup_notification(reload_user(user))
+ UserMailer.deliver_signup_notification(user)
end
def after_save(user)
- u = reload_user(user)
- UserMailer.deliver_activation(u) if u.recently_activated?
- UserMailer.deliver_reset_notification(u) if u.recently_forgot_password?
+ UserMailer.deliver_activation(user) if user.recently_activated?
+ UserMailer.deliver_reset_notification(user) if user.recently_forgot_password?
end
- private
- def reload_user(user)
- User.find(user.id)
- end
end
View
8 test/factories.rb
@@ -0,0 +1,8 @@
+Factory.define :user do |u|
+ u.salt '7e3041ebc2fc05a40c60028e2c4901a81035d3cd'
+ u.crypted_password '00742970dc9e6319f8019fd54864d3ea740f04b1'
+ u.activation_code '8f24789ae988411ccf33ab0c30fe9106fab32e9a'
+ u.state 'pending'
+ u.email {|a| "#{a.login}@example.com".downcase }
+end
+
View
20 test/test_helper.rb
@@ -2,21 +2,15 @@
require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
require 'test_help'
require 'test/unit'
+require 'mocha'
+require 'shoulda'
+require 'factory_girl'
+require File.expand_path(File.dirname(__FILE__) + '/factories')
+
+begin require 'redgreen'; rescue LoadError; end
class Test::Unit::TestCase
self.use_transactional_fixtures = true
self.use_instantiated_fixtures = false
- fixtures :all
-
- def assert_difference(object, method = nil, difference = 1)
- initial_value = object.send(method)
- yield
- assert_equal initial_value + difference, object.send(method), "#{object}##{method} should have a difference of #{difference}"
- end
-
- def assert_no_difference(object, method, &block)
- assert_difference object, method, 0, &block
- end
-
-end
+end
View
30 test/unit/user_test.rb
@@ -0,0 +1,30 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class UserTest < Test::Unit::TestCase
+ context "A User" do
+
+ setup do
+ @chavez = Factory(:user, :login => 'chavez')
+ @chavez.activate!
+ end
+
+ context "that has forgotten his password" do
+ setup do
+ @chavez.forgot_password
+ end
+
+ should "generate a password_reset_code" do
+ assert_not_nil @chavez.password_reset_code
+ end
+
+ should "receive an email with a link to reset the password" do
+ assert_sent_email do |email|
+ email.body.include? @chavez.password_reset_code
+ email.subject.include?(I18n.t("tog_user.mailer.reset_password"))
+ end
+ end
+
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.