2012-08-06 03:01:00 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
|
|
|
|
|
|
before do
|
2012-11-11 04:23:38 +00:00
|
|
|
@user = User.new(first_name: "Example", last_name: "User", name: "test", email: "user@example.com",
|
2012-08-06 03:01:00 +00:00
|
|
|
password: "foobar", password_confirmation: "foobar")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
subject { @user }
|
|
|
|
|
|
2012-11-11 04:23:38 +00:00
|
|
|
it { should respond_to(:first_name) }
|
|
|
|
|
it { should respond_to(:last_name) }
|
2012-08-06 03:01:00 +00:00
|
|
|
it { should respond_to(:email) }
|
|
|
|
|
it { should respond_to(:password_digest) }
|
|
|
|
|
it { should respond_to(:password) }
|
|
|
|
|
it { should respond_to(:password_confirmation) }
|
|
|
|
|
it { should respond_to(:remember_token) }
|
|
|
|
|
it { should respond_to(:admin) }
|
|
|
|
|
it { should respond_to(:authenticate) }
|
|
|
|
|
|
|
|
|
|
it { should be_valid }
|
|
|
|
|
it { should_not be_admin }
|
|
|
|
|
|
|
|
|
|
describe "accessible attributes" do
|
|
|
|
|
it "should not allow access to admin" do
|
2012-08-18 18:48:43 +00:00
|
|
|
userish = User.new(admin: true)
|
|
|
|
|
userish.admin.should == false # the .new style above will be ignored
|
|
|
|
|
userish.admin = true # but deliberate property setting will work
|
2012-08-06 03:01:00 +00:00
|
|
|
userish.admin.should == true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with admin attribute set to 'true'" do
|
|
|
|
|
before do
|
|
|
|
|
@user.save!
|
|
|
|
|
@user.toggle!(:admin)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { should be_admin }
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-11 04:23:38 +00:00
|
|
|
describe "when first name is not present" do
|
|
|
|
|
before { @user.first_name = " " }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when last name is not present" do
|
|
|
|
|
before { @user.last_name = " " }
|
2012-08-06 03:01:00 +00:00
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when email is not present" do
|
|
|
|
|
before { @user.email = " " }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
2012-11-11 04:23:38 +00:00
|
|
|
describe "when first name is too long" do
|
|
|
|
|
before { @user.first_name = "a" * 51 }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when last name is too long" do
|
|
|
|
|
before { @user.last_name = "a" * 51 }
|
2012-08-06 03:01:00 +00:00
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when email format is invalid" do
|
|
|
|
|
it "should be invalid" do
|
|
|
|
|
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
|
|
|
|
|
addresses.each do |invalid_address|
|
|
|
|
|
@user.email = invalid_address
|
|
|
|
|
@user.should_not be_valid
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when email format is valid" do
|
|
|
|
|
it "should be valid" do
|
|
|
|
|
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
|
|
|
|
|
addresses.each do |valid_address|
|
|
|
|
|
@user.email = valid_address
|
|
|
|
|
@user.should be_valid
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when email address is already taken" do
|
|
|
|
|
before do
|
|
|
|
|
user_with_same_email = @user.dup
|
|
|
|
|
user_with_same_email.email = @user.email.upcase
|
|
|
|
|
user_with_same_email.save
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "email address with mixed case" do
|
|
|
|
|
let(:mixed_case_email) { "Foo@ExAMPle.CoM" }
|
|
|
|
|
|
|
|
|
|
it "should be saved as all lower-case" do
|
|
|
|
|
@user.email = mixed_case_email
|
|
|
|
|
@user.save
|
|
|
|
|
@user.reload.email.should == mixed_case_email.downcase
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when password is not present" do
|
|
|
|
|
before { @user.password = @user.password_confirmation = " " }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when password doesn't match confirmation" do
|
|
|
|
|
before { @user.password_confirmation = "mismatch" }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "when password confirmation is nil" do
|
|
|
|
|
before { @user.password_confirmation = nil }
|
|
|
|
|
it { should_not be_valid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with a password that's too short" do
|
|
|
|
|
before { @user.password = @user.password_confirmation = "a" * 5 }
|
|
|
|
|
it { should be_invalid }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "return value of authenticate method" do
|
|
|
|
|
before { @user.save }
|
|
|
|
|
let(:found_user) { User.find_by_email(@user.email) }
|
|
|
|
|
|
|
|
|
|
describe "with valid password" do
|
|
|
|
|
it { should == found_user.authenticate(@user.password) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with invalid password" do
|
|
|
|
|
let(:user_for_invalid_password) { found_user.authenticate("invalid") }
|
|
|
|
|
|
|
|
|
|
it { should_not == user_for_invalid_password }
|
|
|
|
|
specify { user_for_invalid_password.should be_false }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "remember token" do
|
|
|
|
|
before { @user.save }
|
|
|
|
|
its(:remember_token) { should_not be_blank }
|
|
|
|
|
end
|
2012-11-14 05:37:18 +00:00
|
|
|
|
|
|
|
|
describe "authenticate (class-instance)" do
|
|
|
|
|
before { @user.email_confirmed=true; @user.save }
|
|
|
|
|
|
|
|
|
|
describe "with valid password" do
|
|
|
|
|
it { should == User.authenticate(@user.email, @user.password) }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with invalid password" do
|
|
|
|
|
it { User.authenticate(@user.email, "invalid").should be_nil }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with invalid email" do
|
|
|
|
|
it { User.authenticate("junk", "invalid").should be_nil }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with nil args" do
|
|
|
|
|
it { User.authenticate(nil, nil).should be_nil }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "with empty args" do
|
|
|
|
|
it { User.authenticate("", "").should be_nil }
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-08-06 03:01:00 +00:00
|
|
|
end
|