Skip to content
Extraits de code Groupes Projets
user_spec.rb 6,56 ko
Newer Older
  • Learn to ignore specific revisions
  • Raphael's avatar
    Raphael a validé
    #   Copyright (c) 2010, Diaspora Inc.  This file is
    
    Raphael's avatar
    Raphael a validé
    #   licensed under the Affero General Public License version 3 or later.  See
    
    Raphael's avatar
    Raphael a validé
    #   the COPYRIGHT file.
    
    require 'spec_helper'
    
    
    describe User do
    
      let(:user) { Factory(:user) }
    
    ilya's avatar
    ilya a validé
      let(:aspect) { user.aspect(:name => 'heroes') }
    
      let(:user2) { Factory(:user) }
    
    ilya's avatar
    ilya a validé
      let(:aspect2) { user2.aspect(:name => 'stuff') }
    
      let(:user3) { Factory(:user) }
    
    ilya's avatar
    ilya a validé
      let(:aspect3) { user3.aspect(:name => 'stuff') }
    
        describe "of associated person" do
          it "fails if person is not valid" do
            user = Factory.build(:user)
            user.should be_valid
    
            user.person.update_attribute(:serialized_public_key, nil)
            user.person.should_not be_valid
            user.should_not be_valid
    
            user.errors.full_messages.count.should == 1
            user.errors.full_messages.first.should =~ /serialized public key/i
          end
        end
    
    
        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
    
          it "requires presence" do
    
            user = Factory.build(:user, :username => nil)
            user.should_not be_valid
          end
    
    
          it "requires uniqueness" do
    
            duplicate_user = Factory.build(:user, :username => user.username)
            duplicate_user.should_not be_valid
          end
    
    
          it "keeps the original case" do
            user = Factory.build(:user, :username => "WeIrDcAsE")
            user.should be_valid
            user.username.should == "WeIrDcAsE"
          end
    
          it "fails if the requested username is only different in case from an existing username" do
            duplicate_user = Factory.build(:user, :username => user.username.upcase)
            duplicate_user.should_not be_valid
    
    Sarah Mei's avatar
    Sarah Mei a validé
    
          it "strips leading and trailing whitespace" do
            user = Factory.build(:user, :username => "    janie    ")
            user.should be_valid
            user.username.should == "janie"
          end
    
    
          it "fails if there's whitespace in the middle" do
            user = Factory.build(:user, :username => "bobby tables")
            user.should_not be_valid
          end
    
    
        describe "of email" do
          it "requires email address" do
            user = Factory.build(:user, :email => nil)
            user.should_not be_valid
          end
    
          it "requires a unique email address" do
            duplicate_user = Factory.build(:user, :email => user.email)
            duplicate_user.should_not be_valid
          end
        end
    
      describe ".instantiate!" do
        it "creates the user if params are valid" do
          User.find_by_username("ohai").should be_nil
          user = User.instantiate!({
                                     :username => "ohai",
                                     :email => "ohai@example.com",
                                     :password => "password",
                                     :password_confirmation => "password",
                                     :person => {:profile => {:first_name => "O", :last_name => "Hai"}}})
          user.should be_valid
          User.find_by_username("ohai").should == user
        end
        describe "with invalid params" do
          before do
            @invalid_params = {
              :username => "ohai",
              :email => "ohai@example.com",
              :password => "password",
              :password_confirmation => "password",
              :person => {:profile => {:first_name => "", :last_name => ""}}}
          end
          it "raises an error" do
            lambda { User.instantiate!(@invalid_params) }.should raise_error
          end
          it "does not create the user" do
            User.find_by_username("ohai").should be_nil
            begin
              User.instantiate!(@invalid_params)
            rescue
            end
            User.find_by_username("ohai").should be_nil
          end
        end
      end
    
    
      describe ".find_for_authentication" do
        it "preserves case" do
          User.find_for_authentication(:username => user.username).should == user
          User.find_for_authentication(:username => user.username.upcase).should be_nil
        end
      end
    
    
      describe '#diaspora_handle' do
    
        it 'uses the pod config url to set the diaspora_handle' do
    
          user.diaspora_handle.should == user.username + "@" + APP_CONFIG[:terse_pod_url]
    
    danielvincent's avatar
    danielvincent a validé
      context 'profiles' do
    
        it 'should be able to update their profile and send it to their friends' do
    
          updated_profile = {:profile => {
            :first_name => 'bob',
            :last_name => 'billytown',
            :image_url => "http://clown.com"}}
    
    danielvincent's avatar
    danielvincent a validé
          user.update_profile(updated_profile).should be true
          user.profile.image_url.should == "http://clown.com"
    
    maxwell's avatar
    maxwell a validé
      end
    
    danielvincent's avatar
    danielvincent a validé
      context 'aspects' do
        it 'should delete an empty aspect' do
          user.drop_aspect(aspect)
          user.aspects.include?(aspect).should == false
    
    danielvincent's avatar
    danielvincent a validé
        end
    
        it 'should not delete an aspect with friends' do
    
    ilya's avatar
    ilya a validé
          friend_users(user, aspect, user2, aspect2)
    
    danielvincent's avatar
    danielvincent a validé
          aspect.reload
    
          proc { user.drop_aspect(aspect) }.should raise_error /Aspect not empty/
    
    danielvincent's avatar
    danielvincent a validé
          user.aspects.include?(aspect).should == true
    
    danielvincent's avatar
    danielvincent a validé
        end
      end
    
    ilya's avatar
    ilya a validé
      context 'account removal' do
        before do
          friend_users(user, aspect, user2, aspect2)
          friend_users(user, aspect, user3, aspect3)
        end
    
    ilya's avatar
    ilya a validé
        it 'should unfriend everyone' do
          user.should_receive(:unfriend_everyone)
          user.destroy
        end
    
    ilya's avatar
    ilya a validé
        it 'should remove person' do
          user.should_receive(:remove_person)
          user.destroy
        end
    
    
        it 'should remove all aspects' do
          aspects = user.aspects
    
          aspects.count.should > 0
    
          aspects.reload
          aspects.count.should == 0
    
    ilya's avatar
    ilya a validé
        describe '#remove_person' do
          it 'should remove the person object' do
            person = user.person
            user.destroy
            person.reload
            person.should be nil
          end
    
          it 'should remove the posts' do
            message = user.post(:status_message, :message => "hi", :to => aspect.id)
            user.reload
            user.destroy
    
            proc { message.reload }.should raise_error /does not exist/
    
    ilya's avatar
    ilya a validé
          end
        end
    
        describe '#unfriend_everyone' do
          before do
            user3.delete
          end
    
          it 'should send retractions to remote poeple' do
            user.should_receive(:unfriend).once
            user.destroy
          end
    
    
          it 'should unfriend local people' do
    
    ilya's avatar
    ilya a validé
            user2.friends.count.should be 1
            user.destroy
            user2.reload
            user2.friends.count.should be 0
          end
        end
      end