Error message for duplicate username; refactor User validation specs.

This commit is contained in:
Sarah Mei 2010-10-16 22:36:26 -07:00
parent d3a62c7a45
commit 83b23a0ffe
3 changed files with 54 additions and 40 deletions

View file

@ -6,6 +6,13 @@
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. # See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en: en:
activemodel:
errors:
models:
user:
attributes:
username:
taken: "is already taken."
hello: "Hello world" hello: "Hello world"
application: application:
helper: helper:

View file

@ -32,7 +32,7 @@ Factory.define :user do |u|
u.password_confirmation "bluepin7" u.password_confirmation "bluepin7"
u.serialized_private_key OpenSSL::PKey::RSA.generate(1024).export u.serialized_private_key OpenSSL::PKey::RSA.generate(1024).export
u.after_build do |user| u.after_build do |user|
user.person = Factory(:person, :owner_id => user._id, user.person = Factory.build(:person, :owner_id => user._id,
:serialized_public_key => user.encryption_key.public_key.export, :serialized_public_key => user.encryption_key.public_key.export,
:diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}") :diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}")
end end

View file

@ -12,11 +12,31 @@ describe User do
let(:user3) { Factory(:user) } let(:user3) { Factory(:user) }
let(:aspect3) { user3.aspect(:name => 'stuff') } let(:aspect3) { user3.aspect(:name => 'stuff') }
describe "validations" do describe "validation" do
describe "of passwords" do
it "fails if password doesn't match confirmation" do
user = Factory.build(:user, :password => "password", :password_confirmation => "nope")
user.should_not be_valid
end
it "succeeds if password matches confirmation" do
user = Factory.build(:user, :password => "password", :password_confirmation => "password")
user.should be_valid
end
end
describe "of username" do
it "requires a username" do it "requires a username" do
user = Factory.build(:user, :username => nil) user = Factory.build(:user, :username => nil)
user.should_not be_valid user.should_not be_valid
end end
it "requires a unique username" do
duplicate_user = Factory.build(:user, :username => user.username)
duplicate_user.should_not be_valid
end
it "downcases the username" do it "downcases the username" do
user = Factory.build(:user, :username => "ALLUPPERCASE") user = Factory.build(:user, :username => "ALLUPPERCASE")
user.valid? user.valid?
@ -26,19 +46,6 @@ describe User do
user.valid? user.valid?
user.username.should == "someuppercase" user.username.should == "someuppercase"
end end
it "confirms the password" do
pending "I cannot figure out why this doesn't work. --Raphael"
user = User.instantiate!(
:email => "tom@tom.joindiaspora.com",
:username => "tom",
:password => "evankorth",
:password_confirmation => "potatoes",
:person => Person.new(
:profile => Profile.new( :first_name => "Alexander", :last_name => "Hamiltom" ))
)
user.created_at.should be_nil
user.valid?.should be_false
end end
end end
@ -50,10 +57,10 @@ describe User do
context 'profiles' do context 'profiles' do
it 'should be able to update their profile and send it to their friends' do it 'should be able to update their profile and send it to their friends' do
updated_profile = { :profile => { updated_profile = {:profile => {
:first_name => 'bob', :first_name => 'bob',
:last_name => 'billytown', :last_name => 'billytown',
:image_url => "http://clown.com"} } :image_url => "http://clown.com"}}
user.update_profile(updated_profile).should be true user.update_profile(updated_profile).should be true
user.profile.image_url.should == "http://clown.com" user.profile.image_url.should == "http://clown.com"
@ -70,7 +77,7 @@ describe User do
it 'should not delete an aspect with friends' do it 'should not delete an aspect with friends' do
friend_users(user, aspect, user2, aspect2) friend_users(user, aspect, user2, aspect2)
aspect.reload aspect.reload
proc{user.drop_aspect(aspect)}.should raise_error /Aspect not empty/ proc { user.drop_aspect(aspect) }.should raise_error /Aspect not empty/
user.aspects.include?(aspect).should == true user.aspects.include?(aspect).should == true
end end
end end
@ -96,7 +103,7 @@ describe User do
pending "this should use :dependant => :destroy on the many assoc...but that screws this test suite..." pending "this should use :dependant => :destroy on the many assoc...but that screws this test suite..."
aspects = user.aspects aspects = user.aspects
user.destroy user.destroy
proc{ aspects.reload }.should raise_error /does not exist/ proc { aspects.reload }.should raise_error /does not exist/
end end
@ -113,7 +120,7 @@ describe User do
message = user.post(:status_message, :message => "hi", :to => aspect.id) message = user.post(:status_message, :message => "hi", :to => aspect.id)
user.reload user.reload
user.destroy user.destroy
proc{ message.reload }.should raise_error /does not exist/ proc { message.reload }.should raise_error /does not exist/
end end
end end