Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.dirname(__FILE__) + '/../spec_helper'
- def valid_user_attributes
- {
- :name => "master",
- :email => "zookeeperson@glasshanded.net",
- :pass => "agarina"
- }
- end
- describe User do
- before(:each) do
- @user = User.new
- end
- it "should have many memberships and have many sites through those memberships" do
- User.should have_many(:memberships).with_dependent(:destroy)
- User.should have_many(:sites).through(:memberships)
- end
- it "should not be valid without a unique name between 2 and 20 characters long" do
- @user.attributes = valid_user_attributes.except(:name)
- @user.should_not be_valid
- @user.errors_on(:name).should == ["can't be blank"]
- {
- "I" => "can't be less than 2 characters"
- }.each do |name, message|
- @user.name = name
- @user.should_not be_valid
- @user.errors_on(:name).should == [message]
- end
- @user.name = "master"
- @user.should be_valid
- end
- it "should not be valid without an email which at least looks valid and is less than 100 characters" do
- @user.attributes = valid_user_attributes.except(:email)
- @user.should_not be_valid
- @user.errors_on(:email).should == ["can't be blank"]
- %w{foo foo@bar bar.com}.each do |bad_email|
- @user.email = bad_email
- @user.should_not be_valid
- @user.errors_on(:email).should == ["doesn't seem to be a valid email address"]
- end
- @user.email = "#{'foo' * 40}@bar.com"
- @user.should_not be_valid
- @user.errors_on(:email).should == ["can't be more than 100 characters"]
- %w{foo@bar.com foo@bar.baz.com foo_bar@baz.com foo.bar@baz.com foo+bar@baz.com}.each do |good_email|
- @user.email = good_email
- @user.should be_valid
- end
- end
- it "should not be valid if either the name or email is already in use" do
- @previous_user = User.create!(valid_user_attributes)
- @user.attributes = valid_user_attributes
- @user.should_not be_valid
- @user.errors_on(:name).should == ["is already taken"]
- @user.errors_on(:email).should == ["is already registered"]
- end
- it "should encrypt the password and save it as salty_pass" do
- @user.attributes = valid_user_attributes
- @user.pass.should == "agarina"
- @user.salty_pass.should be_blank
- @user.save!
- @user.salty_pass.should_not be_blank
- @user.salty_pass.should_not == "agarina"
- end
- it "should create a random password if one is not provided" do
- @user.attributes = valid_user_attributes.except(:pass)
- @user.pass.should be_blank
- @user.salty_pass.should be_blank
- @user.save!
- @user.salty_pass.should_not be_blank
- end
- it "should not be valid unless the new pass matches a confirmation" do
- @user = User.create!(valid_user_attributes)
- @user.pass = "updated"
- @user.pass_confirmation = nil
- @user.should_not be_valid
- @user.errors_on(:pass_confirmation).should == ["can't be blank"]
- @user.pass_confirmation = "unmatched"
- @user.should_not be_valid
- @user.errors_on(:pass).should == ["doesn't match the confirmation"]
- end
- it "should authenticate users by name and password, returning the user on success and false on failure" do
- @user.attributes = valid_user_attributes.merge(:pass => "agarina")
- @user.save!
- User.authenticate("master", "agarina").should == @user
- User.authenticate("whoever", "whatever").should_not be_true
- end
- it "should use text_callbacks to generate the html attributes of display_name" do
- @user.attributes = valid_user_attributes
- @user.should generate_html_attributes_via_callbacks(:display_name)
- end
- end
Add Comment
Please, Sign In to add comment