Skip to content

Instantly share code, notes, and snippets.

@gma
Forked from jc00ke/gist:234265
Created November 14, 2009 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gma/234446 to your computer and use it in GitHub Desktop.
Save gma/234446 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module UserSpecHelper
def valid_user_attributes
{ :username => 'jc00ke',
:email => 'jc00ke@example.com',
:password => 'p@ssw0rd' }
end
def invalid_usernames
# TODO this list of usernames needs to go in a config file
%w(contact terms privacy what faqs signup login logout admin)
end
end
describe "A User" do
include UserSpecHelper
let(:user) { User.new }
setup do
User.delete_all
end
it "should be valid" do
user.attributes = valid_user_attributes
user.should be_valid
end
it "should not allow @ signs in the username" do
user.attributes = valid_user_attributes.except(:username)
user.username = '@jc00ke'
user.should_not be_valid
end
it "should allow plus signs before the @ sign in an email address" do
user.attributes = valid_user_attributes.except(:email)
user.email = 'jc00ke+springee@example.com'
user.should be_valid
end
it "should require a valid email address" do
user.attributes = valid_user_attributes.except(:email)
user.email = 'asdf'
user.should_not be_valid
end
it "shouldn't allow certain usernames" do
user.attributes = valid_user_attributes.except(:username)
# make sure none of the invalid usernames make it through
all_disallowed = invalid_usernames.all? { |u|
user.username = u
user.should_not be_valid
}
all_disallowed.should be_true
end
it "shouldn't allow passwords less than 6 characters" do
user.attributes = valid_user_attributes.except(:password)
lambda { user.password = '12345' }.should raise_error(ArgumentError, 'Password must have at least 6 characters')
end
it "should be found by either username or email" do
user.attributes = valid_user_attributes
user.save
by_username = User.find_by_username_or_email(user.username)
by_email = User.find_by_username_or_email(user.email)
by_username.id.should == by_email.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment