-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added password and encrypted_password attributes to the User model
- Loading branch information
Showing
4 changed files
with
80 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 |