Skip to content
Extraits de code Groupes Projets
user_spec.rb 30 ko
Newer Older
danielgrippi's avatar
danielgrippi a validé
#   Copyright (c) 2010-2011, 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
  describe "private key" do
    it 'has a key' do
      alice.encryption_key.should_not be nil
    end
    it 'marshalls the key to and from the db correctly' do
      user = User.build(:username => 'max', :email => 'foo@bar.com', :password => 'password', :password_confirmation => 'password')
      user.serialized_private_key.should be_present
      }.to_not raise_error
  describe 'yearly_actives' do
    it 'returns list which includes users who latest signed in within last year' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 1.month
      user.save
      User.yearly_actives.should include user
    end

    it 'returns list which does not include users who did not sign in within last year' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 2.year
      user.save
      User.yearly_actives.should_not include user
    end
  end

  describe 'monthly_actives' do
    it 'returns list which includes users who latest signed in within last month' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 1.day
      user.save
      User.monthly_actives.should include user
    end

     it 'returns list which does not include users who did not sign in within last month' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 2.month
      user.save
      User.monthly_actives.should_not include user
    end
  end

  describe 'daily_actives' do
    it 'returns list which includes users who latest signed in within last day' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 1.hour
      user.save
      User.daily_actives.should include(user)
    end

    it 'returns list which does not include users who did not sign in within last day' do
      user = FactoryGirl.build(:user)
      user.last_sign_in_at = Time.now - 2.day
      user.save
      User.daily_actives.should_not include(user)
    end
  end

  context 'callbacks' do
    describe '#save_person!' do
      it 'saves the corresponding user if it has changed' do
        alice.person.url = "http://stuff.com"
        Person.any_instance.should_receive(:save)
        alice.save
      end

      it 'does not save the corresponding user if it has not changed' do
        Person.any_instance.should_not_receive(:save)
        alice.save
      end
    end
  end
  describe 'hidden_shareables' do
    before do
Jonne Haß's avatar
Jonne Haß a validé
      @sm = FactoryGirl.create(:status_message)
      @sm_id = @sm.id.to_s
      @sm_class = @sm.class.base_class.to_s
    end

    it 'is a hash' do
      alice.hidden_shareables.should == {}
    end

    describe '#add_hidden_shareable' do
      it 'adds the share id to an array which is keyed by the objects class' do
        alice.add_hidden_shareable(@sm_class, @sm_id)
        alice.hidden_shareables['Post'].should == [@sm_id]
      end

      it 'handles having multiple posts' do
Jonne Haß's avatar
Jonne Haß a validé
        sm2 = FactoryGirl.build(:status_message)
        alice.add_hidden_shareable(@sm_class, @sm_id)
        alice.add_hidden_shareable(sm2.class.base_class.to_s, sm2.id.to_s)

        alice.hidden_shareables['Post'].should =~ [@sm_id, sm2.id.to_s]
      end

      it 'handles having multiple shareable types' do
Jonne Haß's avatar
Jonne Haß a validé
        photo = FactoryGirl.create(:photo)
        alice.add_hidden_shareable(photo.class.base_class.to_s, photo.id.to_s)
        alice.add_hidden_shareable(@sm_class, @sm_id)

        alice.hidden_shareables['Photo'].should == [photo.id.to_s]
      end
    end

    describe '#remove_hidden_shareable' do
      it 'removes the id from the hash if it is there'  do
        alice.add_hidden_shareable(@sm_class, @sm_id)
        alice.remove_hidden_shareable(@sm_class, @sm_id)
        alice.hidden_shareables['Post'].should == []
      end
    end

    describe 'toggle_hidden_shareable' do
      it 'calls add_hidden_shareable if the key does not exist, and returns true' do
        alice.should_receive(:add_hidden_shareable).with(@sm_class, @sm_id)
        alice.toggle_hidden_shareable(@sm).should be_true
      end

      it 'calls remove_hidden_shareable if the key exists' do
        alice.should_receive(:remove_hidden_shareable).with(@sm_class, @sm_id)
        alice.add_hidden_shareable(@sm_class, @sm_id)
        alice.toggle_hidden_shareable(@sm).should be_false
      end
    end

    describe '#is_shareable_hidden?' do
      it 'returns true if the shareable is hidden' do
Jonne Haß's avatar
Jonne Haß a validé
        post = FactoryGirl.create(:status_message)
        bob.toggle_hidden_shareable(post)
        bob.is_shareable_hidden?(post).should be_true
      end

      it 'returns false if the shareable is not present' do
Jonne Haß's avatar
Jonne Haß a validé
        post = FactoryGirl.create(:status_message)
        bob.is_shareable_hidden?(post).should be_false
      end
    end
  end


  describe 'overwriting people' do
    it 'does not overwrite old users with factory' do
Jonne Haß's avatar
Jonne Haß a validé
        new_user = FactoryGirl.create(:user, :id => alice.id)
      }.should raise_error ActiveRecord::StatementInvalid
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.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 'requires uniqueness also amount Person objects with diaspora handle' do
Jonne Haß's avatar
Jonne Haß a validé
        p = FactoryGirl.create(:person, :diaspora_handle => "jimmy#{User.diaspora_id_host}")
      it "downcases username" do
Jonne Haß's avatar
Jonne Haß a validé
        user = FactoryGirl.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
Jonne Haß's avatar
Jonne Haß a validé
        user = FactoryGirl.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
Paul Spieker's avatar
Paul Spieker a validé
      it "cannot be one of the blacklist names" do
        ['hostmaster', 'postmaster', 'root', 'webmaster'].each do |username|
          alice.username =  username
          alice.should_not be_valid
        end
      end

    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
      it "requires a vaild email address" do
        alice.email = "somebody@anywhere"
        alice.should_not be_valid
      end
    describe "of unconfirmed_email" do
      it "unconfirmed_email address can be nil/blank" do
        alice.unconfirmed_email = nil
        alice.should be_valid
        alice.unconfirmed_email = ""
        alice.should be_valid
      end

      it "does NOT require a unique unconfirmed_email address" do
        eve.update_attribute :unconfirmed_email, "new@email.com"
        alice.unconfirmed_email = "new@email.com"
        alice.should be_valid
      end
      it "requires a vaild unconfirmed_email address" do
        alice.unconfirmed_email = "somebody@anywhere"
        alice.should_not be_valid
      end
      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
alda519's avatar
alda519 a validé
        user = User.build(:username => 'max', :email => 'foo@bar.com', :password => 'password', :password_confirmation => 'password')
        user.language.should == 'fr'
      end
alda519's avatar
alda519 a validé

      it "should save with language what is set" do
        I18n.locale = :fr
        user = User.build(:username => 'max', :email => 'foo@bar.com', :password => 'password', :password_confirmation => 'password', :language => 'de')
        user.language.should == 'de'
      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
Jonne Haß's avatar
Jonne Haß a validé
      let(:person) {FactoryGirl.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 '#process_invite_acceptence' do
    it 'sets the inviter on user' do
      inv = InvitationCode.create(:user => bob)
Jonne Haß's avatar
Jonne Haß a validé
      user = FactoryGirl.build(:user)
      user.process_invite_acceptence(inv)
      user.invited_by_id.should == bob.id
  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
        alice.update_user_preferences({})
      }.to 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
        alice.update_user_preferences({'mentioned' => false})
      }.to change(alice.user_preferences, :count).by(@pref_count-1)
      alice.reload.disable_mail.should be_false
  describe ".find_for_database_authentication" do
Raphael's avatar
Raphael a validé
    it 'finds a user' do
      User.find_for_database_authentication(:username => alice.username).should == alice
    end

    it 'finds a user by email' do
      User.find_for_database_authentication(:username => alice.email).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_database_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_database_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::Dispatcher.build(alice, Profile.new)
      Postzord::Dispatcher.should_receive(:build).and_return(mailman)
      alice.update_profile(@params).should be_true
    end
danielgrippi's avatar
danielgrippi a validé

    it 'sends a profile to their contacts' do
      mailman = Postzord::Dispatcher.build(alice, Profile.new)
      Postzord::Dispatcher.should_receive(:build).and_return(mailman)
      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 'should dispatch post' do
      photo = alice.build_post(:photo, :user_file => uploaded_photo, :text => "hello", :to => alice.aspects.first.id)
      alice.should_receive(:dispatch_post).with(photo)
      alice.update_post(photo, :text => 'hellp')
Jonne Haß's avatar
Jonne Haß a validé
      @post = FactoryGirl.build(: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
Jonne Haß's avatar
Jonne Haß a validé
      @post = FactoryGirl.build(:status_message, :author => eve.person)
      @post.stub(:mentions?).and_return(true)
      @post.should_not_receive(:notify_person)

      alice.notify_if_mentioned(@post)
    end
  end

  describe 'account deletion' do
    describe '#destroy' do
      it 'removes invitations from the user' do
Jonne Haß's avatar
Jonne Haß a validé
        FactoryGirl.create(:invitation, :sender => alice)
        lambda {
          alice.destroy
        }.should change {alice.invitations_from_me(true).count }.by(-1)
      end
      it 'removes invitations to the user' do
        Invitation.new(:sender => eve, :recipient => alice, :identifier => alice.email, :aspect => eve.aspects.first).save(:validate => false)
        lambda {
          alice.destroy
        }.should change {alice.invitations_to_me(true).count }.by(-1)
      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)
      end
Raphael Sofaer's avatar
Raphael Sofaer a validé
    end
ilya's avatar
ilya a validé
  end

  describe '#mail' do
    it 'enqueues a mail job' do
      alice.disable_mail = false
      alice.save
Jonne Haß's avatar
Jonne Haß a validé
      Workers::Mail::StartedSharing.should_receive(:perform_async).with(alice.id, 'contactrequestid').once
      alice.mail(Workers::Mail::StartedSharing, alice.id, 'contactrequestid')
Jonne Haß's avatar
Jonne Haß a validé
    it 'does not enqueue a mail job if the correct corresponding job has a preference entry' do
      alice.user_preferences.create(:email_type => 'started_sharing')
Jonne Haß's avatar
Jonne Haß a validé
      Workers::Mail::StartedSharing.should_not_receive(:perform_async)
      alice.mail(Workers::Mail::StartedSharing, 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
Jonne Haß's avatar
Jonne Haß a validé
       Workers::Mail::StartedSharing.should_not_receive(:perform_async)
      alice.mail(Workers::Mail::StartedSharing, alice.id, 'contactrequestid')
danielgrippi's avatar
danielgrippi a validé
  context "aspect management" do
    before do
      @contact = alice.contact_for(bob.person)
      @original_aspect = alice.aspects.where(:name => "generic").first
      @new_aspect = alice.aspects.create(:name => 'two')
danielgrippi's avatar
danielgrippi a validé
    end

    describe "#add_contact_to_aspect" do
      it 'adds the contact to the aspect' do
          alice.add_contact_to_aspect(@contact, @new_aspect)
        }.should change(@new_aspect.contacts, :count).by(1)
danielgrippi's avatar
danielgrippi a validé
      end

      it 'returns true if they are already in the aspect' do
        alice.add_contact_to_aspect(@contact, @original_aspect).should be_true
danielgrippi's avatar
danielgrippi a validé
      end
    end
  end
MrZYX's avatar
MrZYX a validé
  context 'likes' do
    before do
      alices_aspect = alice.aspects.where(:name => "generic").first
danielgrippi's avatar
danielgrippi a validé
      @bobs_aspect = bob.aspects.where(:name => "generic").first
      @message = alice.post(:status_message, :text => "cool", :to => alices_aspect)
danielgrippi's avatar
danielgrippi a validé
      @message2 = bob.post(:status_message, :text => "uncool", :to => @bobs_aspect)
      @like = alice.like!(@message)
      @like2 = bob.like!(@message)
    end

MrZYX's avatar
MrZYX a validé
    describe '#like_for' do
      it 'returns the correct like' do
        alice.like_for(@message).should == @like
        bob.like_for(@message).should == @like2
MrZYX's avatar
MrZYX a validé
      end
MrZYX's avatar
MrZYX a validé
      it "returns nil if there's no like" do
        alice.like_for(@message2).should be_nil
      end
    end
MrZYX's avatar
MrZYX a validé
    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
MrZYX's avatar
MrZYX a validé
      it "returns false if there's no like" do
        alice.liked?(@message2).should be_false
      end
    end
  end

  context 'change email' do
    let(:user){ alice }

    describe "#unconfirmed_email" do
      it "is nil by default" do
        user.unconfirmed_email.should eql(nil)
      end

      it "forces blank to nil" do
        user.unconfirmed_email = ""
        user.save!
        user.unconfirmed_email.should eql(nil)
      end

      it "is ignored if it equals email" do
        user.unconfirmed_email = user.email
        user.save!
        user.unconfirmed_email.should eql(nil)
      end

      it "allows change to valid new email" do
        user.unconfirmed_email = "alice@newmail.com"
        user.save!
        user.unconfirmed_email.should eql("alice@newmail.com")
      end
    end

    describe "#confirm_email_token" do
      it "is nil by default" do
        user.confirm_email_token.should eql(nil)
      end

      it "is autofilled when unconfirmed_email is set to new email" do
        user.unconfirmed_email = "alice@newmail.com"
        user.save!
        user.confirm_email_token.should_not be_blank
        user.confirm_email_token.size.should eql(30)
      end

      it "is set back to nil when unconfirmed_email is empty" do
        user.unconfirmed_email = "alice@newmail.com"
        user.save!
        user.confirm_email_token.should_not be_blank
        user.unconfirmed_email = nil
        user.save!
        user.confirm_email_token.should eql(nil)
      end

      it "generates new token on every new unconfirmed_email" do
        user.unconfirmed_email = "alice@newmail.com"
        user.save!
        first_token = user.confirm_email_token
        user.unconfirmed_email = "alice@andanotherone.com"
        user.save!
        user.confirm_email_token.should_not eql(first_token)
        user.confirm_email_token.size.should eql(30)
      end
    end
    describe '#mail_confirm_email' do
      it 'enqueues a mail job on user with unconfirmed email' do
        user.update_attribute(:unconfirmed_email, "alice@newmail.com")
Jonne Haß's avatar
Jonne Haß a validé
        Workers::Mail::ConfirmEmail.should_receive(:perform_async).with(alice.id).once
        alice.mail_confirm_email.should eql(true)
      end

      it 'enqueues NO mail job on user without unconfirmed email' do
Jonne Haß's avatar
Jonne Haß a validé
        Workers::Mail::ConfirmEmail.should_not_receive(:perform_async).with(alice.id)
        alice.mail_confirm_email.should eql(false)
      end
    end
Sebastian's avatar
Sebastian a validé
    describe '#confirm_email' do
      context 'on user with unconfirmed email' do
        before do
          user.update_attribute(:unconfirmed_email, "alice@newmail.com")
        end

        it 'confirms email and set the unconfirmed_email to email on valid token' do
          user.confirm_email(user.confirm_email_token).should eql(true)
          user.email.should eql("alice@newmail.com")
          user.unconfirmed_email.should eql(nil)
          user.confirm_email_token.should eql(nil)
        end

        it 'returns false and does not change anything on wrong token' do
          user.confirm_email(user.confirm_email_token.reverse).should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should_not eql(nil)
          user.confirm_email_token.should_not eql(nil)
        end
Sebastian's avatar
Sebastian a validé
        it 'returns false and does not change anything on blank token' do
          user.confirm_email("").should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should_not eql(nil)
          user.confirm_email_token.should_not eql(nil)
        end
Sebastian's avatar
Sebastian a validé
        it 'returns false and does not change anything on blank token' do
          user.confirm_email(nil).should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should_not eql(nil)
          user.confirm_email_token.should_not eql(nil)
        end
      end

      context 'on user without unconfirmed email' do
        it 'returns false and does not change anything on any token' do
          user.confirm_email("12345"*6).should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should eql(nil)
          user.confirm_email_token.should eql(nil)
        end
Sebastian's avatar
Sebastian a validé
        it 'returns false and does not change anything on blank token' do
          user.confirm_email("").should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should eql(nil)
          user.confirm_email_token.should eql(nil)
        end
Sebastian's avatar
Sebastian a validé
        it 'returns false and does not change anything on blank token' do
          user.confirm_email(nil).should eql(false)
          user.email.should_not eql("alice@newmail.com")
          user.unconfirmed_email.should eql(nil)
          user.confirm_email_token.should eql(nil)
        end
      end
    end
  describe '#retract' do
    before do
      @retraction = mock
Jonne Haß's avatar
Jonne Haß a validé
      @post = FactoryGirl.build(:status_message, :author => bob.person, :public => true)
      before do
        SignedRetraction.stub(:build).and_return(@retraction)
        @retraction.stub(:perform)
      end

      it 'sends a retraction' do
        dispatcher = mock
        Postzord::Dispatcher.should_receive(:build).with(bob, @retraction, anything()).and_return(dispatcher)
        dispatcher.should_receive(:post)

        bob.retract(@post)
      end

      it 'adds resharers of target post as additional subsctibers' do
Jonne Haß's avatar
Jonne Haß a validé
        person = FactoryGirl.create(:person)
        reshare = FactoryGirl.create(:reshare, :root => @post, :author => person)
        @post.reshares << reshare

        dispatcher = mock
        Postzord::Dispatcher.should_receive(:build).with(bob, @retraction, {:additional_subscribers => [person], :services => anything}).and_return(dispatcher)
        dispatcher.should_receive(:post)

        bob.retract(@post)
      end
    end
  end

  describe "#send_reset_password_instructions" do
    it "generates a reset password token if it's supposed to" do
      user = User.new
Jonne Haß's avatar
Jonne Haß a validé
      user.stub!(:should_generate_reset_token?).and_return(true)
      user.should_receive(:generate_reset_password_token)
      user.send_reset_password_instructions
    end

    it "does not generate a reset password token if it's not supposed to" do
      user = User.new
Jonne Haß's avatar
Jonne Haß a validé
      user.stub!(:should_generate_reset_token?).and_return(false)
      user.should_not_receive(:generate_reset_password_token)
      user.send_reset_password_instructions
    end
    it "queues up a job to send the reset password instructions" do
Jonne Haß's avatar
Jonne Haß a validé
      user = FactoryGirl.create :user
Jonne Haß's avatar
Jonne Haß a validé
      Workers::ResetPassword.should_receive(:perform_async).with(user.id)
      user.send_reset_password_instructions
  describe "#seed_aspects" do
    describe "create aspects" do
      let(:user) {
        user = FactoryGirl.create(:user)
        user.seed_aspects
        user
      }

      [I18n.t('aspects.seed.family'), I18n.t('aspects.seed.friends'),
       I18n.t('aspects.seed.work'), I18n.t('aspects.seed.acquaintances')].each do |aspect_name|
        it "creates an aspect named #{aspect_name} for the user" do
          user.aspects.find_by_name(aspect_name).should_not be_nil
        end
      end
    end

    describe "diasporahq sharing" do
      let(:user) {
        FactoryGirl.create(:user)
      }

      before(:each) do
        @old_followhq_value = AppConfig.settings.follow_diasporahq?
      end

      after(:each) do
        AppConfig.settings.follow_diasporahq = @old_followhq_value
      end

      context "with sharing with diasporahq enabled" do
        it "should start sharing with the diasporahq account" do
          AppConfig.settings.follow_diasporahq = true

          wf_mock = mock
          wf_mock.should_receive(:fetch)
          Webfinger.should_receive(:new).and_return(wf_mock)

          user.seed_aspects
        end
      end

      context "with sharing with diasporahq enabled" do
        it "should not start sharing with the diasporahq account" do
          AppConfig.settings.follow_diasporahq = false

          Webfinger.should_not_receive(:new)

          user.seed_aspects
        end
      end
    end
  end

danielgrippi's avatar
danielgrippi a validé
  context "close account" do
    before do
      @user = bob
    end

    describe "#close_account!" do
      it 'locks the user out' do
        @user.close_account!
        @user.reload.access_locked?.should be_true
      end

      it 'creates an account deletion' do
        expect{
          @user.close_account!
        }.to change(AccountDeletion, :count).by(1)

      it 'calls person#lock_access!' do
        @user.person.should_receive(:lock_access!)
        @user.close_account!
      end
    end

    describe "#clear_account!" do
danielgrippi's avatar
danielgrippi a validé
      it 'resets the password to a random string' do
        random_pass = "12345678909876543210"
        SecureRandom.should_receive(:hex).and_return(random_pass)
danielgrippi's avatar
danielgrippi a validé
        @user.valid_password?(random_pass)
      end

      it 'clears all the clearable fields' do
danielgrippi's avatar
danielgrippi a validé
        attributes = @user.send(:clearable_fields)
danielgrippi's avatar
danielgrippi a validé
        attributes.each do |attr|
          @user.send(attr.to_sym).should be_blank
        end
      end
    end

    describe "#clearable_attributes" do
Jonne Haß's avatar
Jonne Haß a validé
        user = FactoryGirl.create :user
danielgrippi's avatar
danielgrippi a validé
        user.send(:clearable_fields).sort.should == %w{
          language
          invitation_token
          invitation_sent_at
Jonne Haß's avatar
Jonne Haß a validé
          reset_password_sent_at
danielgrippi's avatar
danielgrippi a validé
          reset_password_token
          remember_created_at
          sign_in_count
          current_sign_in_at
          last_sign_in_at
          current_sign_in_ip
danielgrippi's avatar
danielgrippi a validé
          last_sign_in_ip
          invitation_service
          invitation_identifier
          invitation_limit
          invited_by_id
          invited_by_type
          authentication_token
          auto_follow_back
          auto_follow_back_aspect_id
danielgrippi's avatar
danielgrippi a validé
          unconfirmed_email
          confirm_email_token
        }.sort
      end