jam-cloud/spec/jam_ruby/models/user_spec.rb

258 lines
7.4 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe User do
before do
@user = User.new(first_name: "Example", last_name: "User", email: "user@example.com",
2013-03-15 04:22:31 +00:00
password: "foobar", password_confirmation: "foobar", city: "Apex", state: "NC", country: "USA", terms_of_service: true, musician: true)
@user.musician_instruments << FactoryGirl.build(:musician_instrument, user: @user)
end
subject { @user }
2012-11-11 04:23:38 +00:00
it { should respond_to(:first_name) }
it { should respond_to(:last_name) }
it { should respond_to(:email) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:admin) }
2013-03-15 04:22:31 +00:00
it { should respond_to(:valid_password?) }
it { should respond_to(:can_invite) }
it { should be_valid }
it { should_not be_admin }
describe "accessible attributes" do
it "should not allow access to admin" do
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
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 = " " }
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 }
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
2012-12-14 03:32:23 +00:00
describe "set_password" do
before do
@user.confirm_email!
end
it "setting a new password should work" do
@user.set_password("foobar", "newpassword", "newpassword")
User.authenticate(@user.email, "newpassword").should_not be_nil
end
2012-12-13 17:15:47 +00:00
2012-12-14 03:32:23 +00:00
it "setting a new password should fail if old one doesnt match" do
2012-12-14 09:07:14 +00:00
expect { @user.set_password("wrongold", "newpassword", "newpassword") }.to raise_error
2012-12-14 03:32:23 +00:00
end
it "setting a new password should fail if new ones dont match" do
@user.set_password("foobar", "newpassword", "newpassword2")
User.authenticate(@user.email, "newpassword").should be_nil
end
2012-12-13 17:15:47 +00:00
2012-12-14 03:32:23 +00:00
it "setting a new password should fail if new one doesnt validate" do
@user.set_password("foobar", "a", "a")
User.authenticate(@user.email, "newpassword").should be_nil
end
2012-12-13 17:15:47 +00:00
end
2012-12-14 03:32:23 +00:00
2012-12-22 00:56:16 +00:00
describe "reset_password" do
before do
@user.confirm_email!
@user.save
end
it "fails if the provided email address is unrecognized" do
expect { User.reset_password("invalidemail@invalid.com") }.to raise_error
end
2012-12-13 17:15:47 +00:00
2012-12-22 00:56:16 +00:00
it "assigns a reset_token and reset_token_created on reset" do
User.reset_password(@user.email)
@user.reload
@user.reset_password_token.should_not be_nil
@user.reset_password_token_created.should_not be_nil
@user.reset_password_token_created.should <= Time.now
@user.reset_password_token_created.should >= Time.now - 1.minute
end
it "errors if the wrong token is comes in" do
User.reset_password(@user.email)
@user.reload
expect { User.set_password_from_token(@user.email, "wrongtoken", "newpassword", "newpassword") }.to raise_error
end
it "changes the password if the token is right" do
User.reset_password(@user.email)
@user.reload
User.set_password_from_token(@user.email, @user.reset_password_token, "newpassword", "newpassword")
User.authenticate(@user.email, "newpassword").should_not be_nil
@user.reload
end
end
2012-12-13 17:15:47 +00:00
describe "return value of authenticate method" do
before { @user.save }
let(:found_user) { User.find_by_email(@user.email) }
describe "with valid password" do
2013-03-15 04:22:31 +00:00
it { found_user.valid_password?(@user.password).should be_true }
end
describe "with invalid password" do
2013-03-15 04:22:31 +00:00
let(:user_for_invalid_password) { found_user.valid_password?("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
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-12-09 20:56:35 +00:00
describe "create_dev_user" do
before { @dev_user = User.create_dev_user("Seth", "Call", "seth@jamkazam.com", "Jam123", "Austin", "Texas", "USA", nil, nil) }
subject { @dev_user }
describe "creates a valid record" do
it { should be_valid }
end
describe "should not be a new record" do
it { should be_persisted }
end
describe "updates record" do
before { @dev_user = User.create_dev_user("Seth", "Call2", "seth@jamkazam.com", "Jam123", "Austin", "Texas", "USA", nil, nil) }
it { should be_valid }
its(:last_name) { should == "Call2" }
end
end
2012-12-22 00:56:16 +00:00
end