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

@ -5,40 +5,47 @@
require 'spec_helper' require 'spec_helper'
describe User do describe User do
let(:user) { Factory(:user) } let(:user) { Factory(:user) }
let(:aspect) { user.aspect(:name => 'heroes') } let(:aspect) { user.aspect(:name => 'heroes') }
let(:user2) { Factory(:user) } let(:user2) { Factory(:user) }
let(:aspect2) { user2.aspect(:name => 'stuff') } let(:aspect2) { user2.aspect(:name => 'stuff') }
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
it "requires a username" do
user = Factory.build(:user, :username => nil)
user.should_not be_valid
end
it "downcases the username" do
user = Factory.build(:user, :username => "ALLUPPERCASE")
user.valid?
user.username.should == "alluppercase"
user = Factory.build(:user, :username => "someUPPERCASE") describe "of passwords" do
user.valid? it "fails if password doesn't match confirmation" do
user.username.should == "someuppercase" 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 end
it "confirms the password" do describe "of username" do
pending "I cannot figure out why this doesn't work. --Raphael" it "requires a username" do
user = User.instantiate!( user = Factory.build(:user, :username => nil)
:email => "tom@tom.joindiaspora.com", user.should_not be_valid
:username => "tom", end
:password => "evankorth",
:password_confirmation => "potatoes", it "requires a unique username" do
:person => Person.new( duplicate_user = Factory.build(:user, :username => user.username)
:profile => Profile.new( :first_name => "Alexander", :last_name => "Hamiltom" )) duplicate_user.should_not be_valid
) end
user.created_at.should be_nil
user.valid?.should be_false it "downcases the username" do
user = Factory.build(:user, :username => "ALLUPPERCASE")
user.valid?
user.username.should == "alluppercase"
user = Factory.build(:user, :username => "someUPPERCASE")
user.valid?
user.username.should == "someuppercase"
end
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
@ -80,27 +87,27 @@ describe User do
friend_users(user, aspect, user2, aspect2) friend_users(user, aspect, user2, aspect2)
friend_users(user, aspect, user3, aspect3) friend_users(user, aspect, user3, aspect3)
end end
it 'should unfriend everyone' do it 'should unfriend everyone' do
user.should_receive(:unfriend_everyone) user.should_receive(:unfriend_everyone)
user.destroy user.destroy
end end
it 'should remove person' do it 'should remove person' do
user.should_receive(:remove_person) user.should_receive(:remove_person)
user.destroy user.destroy
end end
it 'should remove all aspects' do it 'should remove all aspects' 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
describe '#remove_person' do describe '#remove_person' do
it 'should remove the person object' do it 'should remove the person object' do
person = user.person person = user.person
@ -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
@ -128,7 +135,7 @@ describe User do
user.destroy user.destroy
end end
it 'should unfriend local people' do it 'should unfriend local people' do
user2.friends.count.should be 1 user2.friends.count.should be 1
user.destroy user.destroy
user2.reload user2.reload