Skip to content
Extraits de code Groupes Projets
user_spec.rb 17,3 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
Raphael's avatar
Raphael a validé
  it 'should have a key' do
    alice.encryption_key.should_not be nil
Raphael's avatar
Raphael a validé
  end

  describe 'overwriting people' do
    it 'does not overwrite old users with factory' do
        new_user = Factory.create(:user, :id => alice.id)
      }.should raise_error ActiveRecord::RecordNotUnique
danielgrippi's avatar
danielgrippi a validé

    it 'does not overwrite old users with create' do
          params = {:username => "ohai",
                    :email => "ohai@example.com",
                    :password => "password",
                    :password_confirmation => "password",
                    :person =>
                      {:profile =>
                        {:first_name => "O",
                         :last_name => "Hai"}
                      }
          }
          params[:id] = alice.id
      new_user = User.build(params)
      new_user.save
      new_user.persisted?.should be_true
      new_user.id.should_not == alice.id
    describe "of associated person" do
      it "fails if person is not valid" do
danielgrippi's avatar
danielgrippi a validé
        user = alice
        user.should be_valid

        user.person.serialized_public_key = nil
        user.person.should_not be_valid
        user.should_not be_valid

        user.errors.full_messages.count.should == 1
Raphael's avatar
Raphael a validé
        user.errors.full_messages.first.should =~ /Person is invalid/i
      it "requires presence" do
danielgrippi's avatar
danielgrippi a validé
        alice.username = nil
        alice.should_not be_valid
      it "requires uniqueness" do
danielgrippi's avatar
danielgrippi a validé
        alice.username = eve.username
        alice.should_not be_valid
      it "downcases username" do
        user = Factory.build(:user, :username => "WeIrDcAsE")
        user.should be_valid
        user.username.should == "weirdcase"
      it "fails if the requested username is only different in case from an existing username" do
danielgrippi's avatar
danielgrippi a validé
        alice.username = eve.username.upcase
        alice.should_not be_valid
Sarah Mei's avatar
Sarah Mei a validé

      it "strips leading and trailing whitespace" do
danielgrippi's avatar
danielgrippi a validé
        user = Factory.build(:user, :username => "      janie   ")
Sarah Mei's avatar
Sarah Mei a validé
        user.should be_valid
        user.username.should == "janie"
      end
Sarah Mei's avatar
Sarah Mei a validé

      it "fails if there's whitespace in the middle" do
danielgrippi's avatar
danielgrippi a validé
        alice.username = "bobby tables"
        alice.should_not be_valid
Sarah Mei's avatar
Sarah Mei a validé
      end

      it 'can not contain non url safe characters' do
danielgrippi's avatar
danielgrippi a validé
        alice.username = "kittens;"
        alice.should_not be_valid
      end

      it 'should not contain periods' do
danielgrippi's avatar
danielgrippi a validé
        alice.username = "kittens."
        alice.should_not be_valid
      it "can be 32 characters long" do
danielgrippi's avatar
danielgrippi a validé
        alice.username = "hexagoooooooooooooooooooooooooon"
        alice.should be_valid
      it "cannot be 33 characters" do
danielgrippi's avatar
danielgrippi a validé
        alice.username =  "hexagooooooooooooooooooooooooooon"
        alice.should_not be_valid

    describe "of email" do
      it "requires email address" do
danielgrippi's avatar
danielgrippi a validé
        alice.email = nil
        alice.should_not be_valid
      end

      it "requires a unique email address" do
danielgrippi's avatar
danielgrippi a validé
        alice.email = eve.email
        alice.should_not be_valid
      after do
        I18n.locale = :en
      end
danielgrippi's avatar
danielgrippi a validé

      it "requires availability" do
danielgrippi's avatar
danielgrippi a validé
        alice.language = 'some invalid language'
        alice.should_not be_valid
      end

      it "should save with current language if blank" do
        I18n.locale = :fr
        user = Factory(:user, :language => nil)
        user.language.should == 'fr'
      end
    end
danielgrippi's avatar
danielgrippi a validé
  end
  describe ".build" do
    context 'with valid params' do
      before do
        params = {:username => "ohai",
                  :email => "ohai@example.com",
                  :password => "password",
                  :password_confirmation => "password",
                  :person =>
                    {:profile =>
                      {:first_name => "O",
                       :last_name => "Hai"}
                    }
        }
        @user = User.build(params)
      end
danielgrippi's avatar
danielgrippi a validé

      it "does not save" do
        @user.persisted?.should be_false
        @user.person.persisted?.should be_false
        User.find_by_username("ohai").should be_nil
      end
danielgrippi's avatar
danielgrippi a validé

        @user.should be_valid
        @user.persisted?.should be_true
        @user.person.persisted?.should be_true
        User.find_by_username("ohai").should == @user
      end
danielgrippi's avatar
danielgrippi a validé

    describe "with invalid params" do
      before do
        @invalid_params = {
          :username => "ohai",
          :email => "ohai@example.com",
          :password => "password",
          :password_confirmation => "wrongpasswordz",
          :person => {:profile => {:first_name => "", :last_name => ""}}}
      end
danielgrippi's avatar
danielgrippi a validé

      it "raises no error" do
        lambda { User.build(@invalid_params) }.should_not raise_error
danielgrippi's avatar
danielgrippi a validé

      it "does not save" do
        User.build(@invalid_params).save.should be_false
danielgrippi's avatar
danielgrippi a validé

Raphael's avatar
Raphael a validé
      it 'does not save a person' do
        lambda { User.build(@invalid_params) }.should_not change(Person, :count)
      end
danielgrippi's avatar
danielgrippi a validé

Raphael's avatar
Raphael a validé
      it 'does not generate a key' do
        User.should_receive(:generate_key).exactly(0).times
        User.build(@invalid_params)
      end
danielgrippi's avatar
danielgrippi a validé

    describe "with malicious params" do
      let(:person) {Factory.create :person}
      before do
        @invalid_params = {:username => "ohai",
                  :email => "ohai@example.com",
                  :password => "password",
                  :password_confirmation => "password",
Raphael's avatar
Raphael a validé
                    {:id => person.id,
                      :profile =>
                      {:first_name => "O",
                       :last_name => "Hai"}
                    }
        }
      end
danielgrippi's avatar
danielgrippi a validé

      it "does not assign it to the person" do
        User.build(@invalid_params).person.id.should_not == person.id
      end
    end
  describe "#can_add?" do
    it "returns true if there is no existing connection" do
      alice.can_add?(eve.person).should be_true
    end

    it "returns false if the user and the person are the same" do
      alice.can_add?(alice.person).should be_false
    end

    it "returns false if the users are already connected" do
      alice.can_add?(bob.person).should be_false
    end
    
    it "returns false if the user has already sent a request to that person" do
      alice.share_with(eve.person, alice.aspects.first)
      alice.reload
      eve.reload
      alice.can_add?(eve.person).should be_false
    end
  end

  describe 'update_user_preferences' do
    before do
      @pref_count = UserPreference::VALID_EMAIL_TYPES.count
    end

    it 'unsets disable mail and makes the right amount of prefs' do
      alice.disable_mail = true
      proc {
        alice.update_user_preferences({})
      }.should change(alice.user_preferences, :count).by(@pref_count)
danielgrippi's avatar
danielgrippi a validé

    it 'still sets new prefs to false on update' do
      alice.disable_mail = true
      proc {
        alice.update_user_preferences({'mentioned' => false})
      }.should change(alice.user_preferences, :count).by(@pref_count-1)
      alice.reload.disable_mail.should be_false
  describe ".find_for_authentication" do
Raphael's avatar
Raphael a validé
    it 'finds a user' do
      User.find_for_authentication(:username => alice.username).should == alice
Raphael's avatar
Raphael a validé
    end
danielgrippi's avatar
danielgrippi a validé

Raphael's avatar
Raphael a validé
    it "does not preserve case" do
      User.find_for_authentication(:username => alice.username.upcase).should == alice
danielgrippi's avatar
danielgrippi a validé

    it 'errors out when passed a non-hash' do
      lambda {
        User.find_for_authentication(alice.username)
      }.should raise_error
    end
  describe '#update_profile' do
    before do
      @params = {
        :first_name => 'bob',
        :last_name => 'billytown',
danielgrippi's avatar
danielgrippi a validé

    it 'dispatches the profile when tags are set' do
      @params = {:tags => '#what #hey'}
      mailman = Postzord::Dispatch.new(alice, Profile.new)
      Postzord::Dispatch.should_receive(:new).and_return(mailman)
      mailman.should_receive(:deliver_to_local)
      alice.update_profile(@params).should be_true
    end
danielgrippi's avatar
danielgrippi a validé

    it 'sends a profile to their contacts' do
      mailman = Postzord::Dispatch.new(alice, Profile.new)
maxwell's avatar
maxwell a validé
      Postzord::Dispatch.should_receive(:new).and_return(mailman)
      mailman.should_receive(:deliver_to_local)
      alice.update_profile(@params).should be_true
danielgrippi's avatar
danielgrippi a validé

      alice.update_profile(@params).should be_true
      alice.reload.profile.first_name.should == 'bob'
danielgrippi's avatar
danielgrippi a validé

    it 'updates image_url' do
      params = {:image_url => "http://clown.com"}

      alice.update_profile(params).should be_true
      alice.reload.profile.image_url.should == "http://clown.com"
danielgrippi's avatar
danielgrippi a validé

    context 'passing in a photo' do
      before do
        fixture_filename  = 'button.png'
        fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', fixture_filename)
        image = File.open(fixture_name)
        @photo = Photo.diaspora_initialize(:author => alice.person, :user_file => image)
        @photo.save!
        @params = {:photo => @photo}
      end
danielgrippi's avatar
danielgrippi a validé

        alice.update_profile(@params).should be_true
        alice.reload

        alice.profile.image_url.should =~ Regexp.new(@photo.url(:thumb_large))
        alice.profile.image_url_medium.should =~ Regexp.new(@photo.url(:thumb_medium))
        alice.profile.image_url_small.should =~ Regexp.new(@photo.url(:thumb_small))
danielgrippi's avatar
danielgrippi a validé

      it 'unpends the photo' do
        @photo.pending = true
        @photo.save!
        @photo.reload
        alice.update_profile(@params).should be true
        @photo.reload.pending.should be_false
      end
maxwell's avatar
maxwell a validé
  end
danielvincent's avatar
danielvincent a validé

maxwell's avatar
maxwell a validé
  describe '#update_post' do
    it 'sends a notification to aspects' do
maxwell's avatar
maxwell a validé
      m = mock()
      m.should_receive(:post)
      Postzord::Dispatch.should_receive(:new).and_return(m)
      photo = alice.build_post(:photo, :user_file => uploaded_photo, :text => "hello", :to => alice.aspects.first.id)
      alice.update_post(photo, :text => 'hellp')
  describe '#notify_if_mentioned' do
    before do
      @post = Factory.create(:status_message, :author => bob.person)
    end

    it 'notifies the user if the incoming post mentions them' do
      @post.should_receive(:mentions?).with(alice.person).and_return(true)
      @post.should_receive(:notify_person).with(alice.person)

      alice.notify_if_mentioned(@post)
    end

    it 'does not notify the user if the incoming post does not mention them' do
      @post.should_receive(:mentions?).with(alice.person).and_return(false)
      @post.should_not_receive(:notify_person)

      alice.notify_if_mentioned(@post)
    end

    it 'does not notify the user if the post author is not a contact' do
      @post = Factory.create(:status_message, :author => eve.person)
      @post.stub(:mentions?).and_return(true)
      @post.should_not_receive(:notify_person)

      alice.notify_if_mentioned(@post)
    end
  end

Raphael's avatar
Raphael a validé
  describe 'account removal' do
    it 'should disconnect everyone' do
      alice.should_receive(:disconnect_everyone)
      alice.destroy
ilya's avatar
ilya a validé
    end
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it 'removes invitations from the user' do
      alice.invite_user alice.aspects.first.id, 'email', 'blah@blah.blah'
      lambda {
        alice.destroy
      }.should change {alice.invitations_from_me(true).count }.by(-1)
    end

    it 'removes invitations to the user' do
      Invitation.create(:sender_id => eve.id, :recipient_id => alice.id, :aspect_id => eve.aspects.first.id)
      lambda {
        alice.destroy
      }.should change {alice.invitations_to_me(true).count }.by(-1)
    end

    it 'should remove mentions' do
      alice.should_receive(:remove_mentions)
      alice.destroy
    end

ilya's avatar
ilya a validé
    it 'should remove person' do
      alice.should_receive(:remove_person)
      alice.destroy
ilya's avatar
ilya a validé
    end

    it 'should remove all aspects' do
        alice.destroy
      }.should change{ alice.aspects(true).count }.by(-1)
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it 'removes all contacts' do
      lambda {
        alice.destroy
      }.should change {
        alice.contacts.count
      }.by(-1)
Raphael Sofaer's avatar
Raphael Sofaer a validé
    end

    it 'removes all service connections' do
      Services::Facebook.create(:access_token => 'what', :user_id => alice.id)
      lambda {
        alice.destroy
      }.should change {
        alice.services.count
      }.by(-1)
ilya's avatar
ilya a validé
    describe '#remove_person' do
      it 'should remove the person object' do
        person = alice.person
        alice.destroy
ilya's avatar
ilya a validé
        person.reload
        person.should be nil
      end

      it 'should remove the posts' do
        message = alice.post(:status_message, :text => "hi", :to => alice.aspects.first.id)
        alice.reload
        alice.destroy
Raphael's avatar
Raphael a validé
        proc { message.reload }.should raise_error ActiveRecord::RecordNotFound
ilya's avatar
ilya a validé
      end
    end

    describe '#remove_mentions' do
      it 'should remove the mentions' do
        person = alice.person
        sm =  Factory(:status_message)
        mention  = Mention.create(:person => person, :post=> sm)
        alice.reload
        alice.destroy
        proc { mention.reload }.should raise_error ActiveRecord::RecordNotFound
      end
    end

    describe '#disconnect_everyone' do
      it 'has no error on a local friend who has deleted his account' do
        alice.destroy
        lambda {
          bob.destroy
        }.should_not raise_error
      end

      it 'has no error when the user has sent local requests' do
        alice.share_with(eve.person, alice.aspects.first)
        lambda {
          alice.destroy
        }.should_not raise_error
      end

ilya's avatar
ilya a validé
      it 'should send retractions to remote poeple' do
        person = eve.person
        eve.delete
danielgrippi's avatar
danielgrippi a validé
        alice.contacts.create(:person => person, :aspects => [alice.aspects.first])
Raphael's avatar
Raphael a validé

        alice.should_receive(:disconnect).once
        alice.destroy
ilya's avatar
ilya a validé
      end

      it 'should disconnect local people' do
          alice.destroy
        }.should change{bob.reload.contacts.count}.by(-1)
ilya's avatar
ilya a validé
      end
    end
  end

  describe '#mail' do
    it 'enqueues a mail job' do
      alice.disable_mail = false
      alice.save
      Resque.should_receive(:enqueue).with(Job::MailStartedSharing, alice.id, 'contactrequestid').once
      alice.mail(Job::MailStartedSharing, alice.id, 'contactrequestid')
    it 'does not enqueue a mail job if the correct corresponding job has a prefrence entry' do
      alice.user_preferences.create(:email_type => 'started_sharing')
      Resque.should_not_receive(:enqueue)
      alice.mail(Job::MailStartedSharing, alice.id, 'contactrequestid')

    it 'does not send a mail if disable_mail is set to true' do
       alice.disable_mail = true
       alice.save
       alice.reload
       Resque.should_not_receive(:enqueue)
      alice.mail(Job::MailStartedSharing, alice.id, 'contactrequestid')
danielgrippi's avatar
danielgrippi a validé
  context "aspect management" do
    before do
      @contact = alice.contact_for(bob.person)
      @aspect1 = alice.aspects.create(:name => 'two')
    end

    describe "#add_contact_to_aspect" do
      it 'adds the contact to the aspect' do
        lambda { 
          alice.add_contact_to_aspect(@contact, @aspect1)
        }.should change(@aspect1.contacts, :count).by(1)
      end

      it 'returns true if they are already in the aspect' do
        alice.add_contact_to_aspect(@contact, alice.aspects.first).should be_true
      end
    end

    context 'moving and removing posts' do
      describe 'User#move_contact' do
        it 'should be able to move a contact from one of users existing aspects to another' do
          alice.move_contact(bob.person, @aspect1, alice.aspects.first)

          alice.aspects.first.contacts(true).include?(@contact).should be_false
          @aspect1.contacts(true).include?(@contact).should be_true
        end

        it "should not move a person who is not a contact" do
          non_contact = eve.person

          proc{
            alice.move_contact(non_contact, @aspect1, alice.aspects.first)
          }.should raise_error

          alice.aspects.first.contacts.where(:person_id => non_contact.id).should be_empty
          @aspect1.contacts.where(:person_id => non_contact.id).should be_empty
        end

        it 'does not try to delete if add person did not go through' do
          alice.should_receive(:add_contact_to_aspect).and_return(false)
          alice.should_not_receive(:delete_person_from_aspect)
          alice.move_contact(bob.person, @aspect1, alice.aspects.first)
        end
      end
    end
  end
MrZYX's avatar
MrZYX a validé
  
  context 'likes' do
    before do
      @message = alice.post(:status_message, :text => "cool", :to => alice.aspects.first)
      @message2 = bob.post(:status_message, :text => "uncool", :to => bob.aspects.first)
      @like = alice.like(true, :on => @message)
      @dislike = bob.like(false, :on => @message)
    end
    
    describe '#like_for' do
      it 'returns the correct like' do
        alice.like_for(@message).should == @like
        bob.like_for(@message).should == @dislike
      end
      
      it "returns nil if there's no like" do
        alice.like_for(@message2).should be_nil
      end
    end
  
    describe '#liked?' do
      it "returns true if there's a like" do
        alice.liked?(@message).should be_true
        bob.liked?(@message).should be_true
      end
    
      it "returns false if there's no like" do
        alice.liked?(@message2).should be_false
      end
    end
  end