Permalink
Browse files

remove duplucation about password generation

add a password_generate on user
  • Loading branch information...
1 parent 7afaa04 commit 6ff4e17060a7c1ca9805e1c2c4acc9bcd415fc9f Yannick Francois committed Nov 21, 2012
@@ -55,7 +55,7 @@ def signup
@user = User.new(params[:user])
if request.post?
- @user.password = generate_password
+ @user.generate_password!
session[:tmppass] = @user.password
@user.name = @user.login
if @user.save
@@ -74,7 +74,7 @@ def recover_password
@user = User.find(:first, :conditions => ["login = ? or email = ?", params[:user][:login], params[:user][:login]])
if @user
- @user.password = generate_password
+ @user.generate_password!
@user.save
flash[:notice] = _("An email has been successfully sent to your address with your new password")
redirect_to :action => 'login'
@@ -95,12 +95,6 @@ def logout
end
private
- def generate_password
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- newpass = ""
- 1.upto(7) { |i| newpass << chars[rand(chars.size-1)] }
- return newpass
- end
def verify_users
redirect_to(:controller => "accounts", :action => "signup") if User.count == 0
@@ -9,7 +9,7 @@ def index
this_blog.base_url = blog_base_url
@user = User.new(:login => 'admin', :email => params[:setting][:email])
- @user.password = generate_password
+ @user.generate_password!
@user.name = @user.login
unless this_blog.valid? and @user.valid?
@@ -55,13 +55,6 @@ def update_or_create_first_post_with_user user
end
end
- def generate_password
- chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
- newpass = ""
- 1.upto(7) { |i| newpass << chars[rand(chars.size-1)] }
- return newpass
- end
-
def check_config
return unless this_blog.configured?
redirect_to :controller => 'articles', :action => 'index'
View
@@ -164,6 +164,13 @@ def admin?
profile.label == Profile::ADMIN
end
+ def generate_password!
+ chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
+ newpass = ""
+ 1.upto(7) { |i| newpass << chars[rand(chars.size-1)] }
+ self.password = newpass
+ end
+
protected
# Apply SHA1 encryption to the supplied password.
@@ -290,8 +290,7 @@ def make_request
User.stub!(:count).and_return(0)
@user = mock_model(User)
@user.stub!(:login).and_return('newbob')
- @user.stub!(:password=).and_return(true)
- @user.stub!(:password).and_return('foo')
+ @user.stub!(:generate_password!).and_return(true)
@user.stub!(:name=).and_return(true)
User.stub!(:new).and_return(@user)
User.stub!(:authenticate).and_return(@user)
View
@@ -242,4 +242,17 @@ def set_password(newpass)
article.user.should eq user
end
end
+
+ describe "generate_password!" do
+ it "respond to generate_password!" do
+ User.new.should respond_to(:generate_password!)
+ end
+
+ it "set a 7 char length password" do
+ user = User.new
+ user.should_receive(:rand).exactly(7).times.and_return(0)
+ user.should_receive(:password=).with('a' * 7)
+ user.generate_password!
+ end
+ end
end

0 comments on commit 6ff4e17

Please sign in to comment.