Skip to content

Commit

Permalink
Added password and encrypted_password attributes to the User model
Browse files Browse the repository at this point in the history
  • Loading branch information
mhartl committed Aug 22, 2010
1 parent b244aa0 commit e9dc8af
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
19 changes: 12 additions & 7 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# == Schema Information
# Schema version: 20100821203213
# Schema version: 20100822204528
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
# encrypted_password :string(255)
#

class User < ActiveRecord::Base
attr_accessible :name, :email
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

Expand All @@ -20,4 +22,7 @@ class User < ActiveRecord::Base
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
end
9 changes: 9 additions & 0 deletions db/migrate/20100822204528_add_password_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddPasswordToUsers < ActiveRecord::Migration
def self.up
add_column :users, :encrypted_password, :string
end

def self.down
remove_column :users, :encrypted_password
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100822012431) do
ActiveRecord::Schema.define(:version => 20100822204528) do

create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "encrypted_password"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
Expand Down
58 changes: 57 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
describe User do

before(:each) do
@attr = { :name => "Example User", :email => "[email protected]" }
@attr = {
:name => "Example User",
:email => "[email protected]",
:password => "foobar",
:password_confirmation => "foobar"
}
end

it "should create a new instance given a valid attribute" do
Expand Down Expand Up @@ -54,4 +59,55 @@
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end

describe "passwords" do

before(:each) do
@user = User.new(@attr)
end

it "should have a password attribute" do
@user.should respond_to(:password)
end

it "should have a password confirmation attribute" do
@user.should respond_to(:password_confirmation)
end
end

describe "password validations" do

it "should require a password" do
User.new(@attr.merge(:password => "", :password_confirmation => "")).
should_not be_valid
end

it "should require a matching password confirmation" do
User.new(@attr.merge(:password_confirmation => "invalid")).
should_not be_valid
end

it "should reject short passwords" do
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end

it "should reject long passwords" do
long = "a" * 41
hash = @attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end

describe "password encryption" do

before(:each) do
@user = User.create!(@attr)
end

it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
end
end

0 comments on commit e9dc8af

Please sign in to comment.