Skip to content
Extraits de code Groupes Projets
user_spec.rb 3,89 ko
Newer Older
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
danielvincent's avatar
danielvincent a validé
  let(:user)   { Factory(:user) }
ilya's avatar
ilya a validé
  let(:aspect) { user.aspect(:name => 'heroes') }
  let(:user2)   { Factory(:user) }
  let(:aspect2) { user2.aspect(:name => 'stuff') }
  let(:user3)   { Factory(:user) }
  let(:aspect3) { user3.aspect(:name => 'stuff') }

  describe "validations" 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"
Stoyan Gaydarov's avatar
Stoyan Gaydarov a validé

      user = Factory.build(:user, :username => "someUPPERCASE")
      user.valid?
      user.username.should == "someuppercase"

    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
  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
Jamie Wilkinson's avatar
Jamie Wilkinson a validé
    it 'should be able to update their profile and send it to their friends' do
danielvincent's avatar
danielvincent a validé
      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é

danielvincent's avatar
danielvincent a validé
  context 'aspects' do
danielvincent's avatar
danielvincent a validé

danielvincent's avatar
danielvincent a validé
    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/
      user.aspects.include?(aspect).should == true
danielvincent's avatar
danielvincent a validé
    end
  end
danielvincent's avatar
danielvincent a validé

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
    
    it 'should unfriend everyone' do
      user.should_receive(:unfriend_everyone)
      user.destroy
    end
    
    it 'should remove person' do
      user.should_receive(:remove_person)
      user.destroy
    end

    
    it 'should remove all aspects' do
      pending "this should use :dependant => :destroy on the many assoc...but that screws this test suite..."
      aspects = user.aspects
      user.destroy
      proc{ aspects.reload }.should raise_error /does not exist/

    end

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/
      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 
        user2.friends.count.should be 1
        user.destroy
        user2.reload
        user2.friends.count.should be 0
      end
    end
  end