Skip to content
Extraits de code Groupes Projets
user_spec.rb 37,5 ko
Newer Older
  • Learn to ignore specific revisions
  • 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, :type => :model do
    
      context "relations" do
        context "#conversations" do
          it "doesn't find anything when there is nothing to find" do
            u = FactoryGirl.create(:user)
    
            expect(u.conversations).to be_empty
    
          end
    
          it "finds the users conversations" do
            c = FactoryGirl.create(:conversation, { author: alice.person })
    
    
            expect(alice.conversations).to include c
    
          end
    
          it "doesn't find other users conversations" do
            c1 = FactoryGirl.create(:conversation)
            c2 = FactoryGirl.create(:conversation)
            c_own = FactoryGirl.create(:conversation, { author: alice.person })
    
    
            expect(alice.conversations).to include c_own
            expect(alice.conversations).not_to include c1
            expect(alice.conversations).not_to include c2
    
          expect(alice.encryption_key).not_to be nil
    
        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')
    
          expect(user.serialized_private_key).to be_present
    
          }.to_not raise_error
    
        it 'returns list which includes users within last year' do
    
          user.last_seen = Time.now - 1.month
    
          expect(User.yearly_actives).to include user
    
        it 'returns list which does not include users seen within last year' do
    
          user.last_seen = Time.now - 2.year
    
          expect(User.yearly_actives).not_to include user
    
        it 'returns list which includes users seen within last month' do
    
          user.last_seen = Time.now - 1.day
    
          expect(User.monthly_actives).to include user
    
         it 'returns list which does not include users seen within last month' do
    
          user.last_seen = Time.now - 2.month
    
          expect(User.monthly_actives).not_to include user
    
        it 'returns list which includes users seen within last day' do
    
          user.last_seen = Time.now - 1.hour
    
          expect(User.daily_actives).to include(user)
    
        it 'returns list which does not include users seen within last day' do
    
          user.last_seen = Time.now - 2.day
    
          expect(User.daily_actives).not_to include(user)
    
        it 'returns list which includes users seen within half a year' do
    
          user.last_seen = Time.now - 4.month
    
          expect(User.halfyear_actives).to include user
    
         it 'returns list which does not include users seen within the last half a year' do
    
          user.last_seen = Time.now - 7.month
    
          expect(User.halfyear_actives).not_to include user
    
      context 'callbacks' do
        describe '#save_person!' do
          it 'saves the corresponding user if it has changed' do
            alice.person.url = "http://stuff.com"
    
            expect_any_instance_of(Person).to receive(:save)
    
            alice.save
          end
    
          it 'does not save the corresponding user if it has not changed' do
    
            expect_any_instance_of(Person).not_to receive(:save)
    
      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
    
          expect(alice.hidden_shareables).to eq({})
    
        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)
    
            expect(alice.hidden_shareables['Post']).to eq([@sm_id])
    
    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)
    
    
            expect(alice.hidden_shareables['Post']).to match_array([@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)
    
    
            expect(alice.hidden_shareables['Photo']).to eq([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)
    
            expect(alice.hidden_shareables['Post']).to eq([])
    
          end
        end
    
        describe 'toggle_hidden_shareable' do
          it 'calls add_hidden_shareable if the key does not exist, and returns true' do
    
            expect(alice).to receive(:add_hidden_shareable).with(@sm_class, @sm_id)
            expect(alice.toggle_hidden_shareable(@sm)).to be true
    
          end
    
          it 'calls remove_hidden_shareable if the key exists' do
    
            expect(alice).to receive(:remove_hidden_shareable).with(@sm_class, @sm_id)
    
            alice.add_hidden_shareable(@sm_class, @sm_id)
    
            expect(alice.toggle_hidden_shareable(@sm)).to 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)
    
            expect(bob.is_shareable_hidden?(post)).to 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)
    
            expect(bob.is_shareable_hidden?(post)).to be false
    
      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)
    
          }.to raise_error ActiveRecord::StatementInvalid
    
        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
    
          expect(new_user.persisted?).to be true
          expect(new_user.id).not_to eq(alice.id)
    
        describe "of associated person" do
          it "fails if person is not valid" do
    
    danielgrippi's avatar
    danielgrippi a validé
            user = alice
    
            expect(user).to be_valid
    
            user.person.serialized_public_key = nil
    
            expect(user.person).not_to be_valid
            expect(user).not_to be_valid
    
            expect(user.errors.full_messages.count).to eq(1)
            expect(user.errors.full_messages.first).to match(/Person is invalid/i)
    
          it "requires presence" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = nil
    
            expect(alice).not_to be_valid
    
          it "requires uniqueness" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = eve.username
    
            expect(alice).not_to 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}")
    
            expect(alice).not_to be_valid
    
          it "downcases username" do
    
    Jonne Haß's avatar
    Jonne Haß a validé
            user = FactoryGirl.build(:user, :username => "WeIrDcAsE")
    
            expect(user).to be_valid
            expect(user.username).to eq("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
    
            expect(alice).not_to 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   ")
    
            expect(user).to be_valid
            expect(user.username).to eq("janie")
    
    Sarah Mei's avatar
    Sarah Mei a validé
          end
    
    
          it "fails if there's whitespace in the middle" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = "bobby tables"
    
            expect(alice).not_to be_valid
    
    
          it 'can not contain non url safe characters' do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = "kittens;"
    
            expect(alice).not_to be_valid
    
          end
    
          it 'should not contain periods' do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = "kittens."
    
            expect(alice).not_to be_valid
    
          it "can be 32 characters long" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username = "hexagoooooooooooooooooooooooooon"
    
            expect(alice).to be_valid
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.username =  "hexagooooooooooooooooooooooooooon"
    
            expect(alice).not_to 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
    
              expect(alice).not_to be_valid
    
    Paul Spieker's avatar
    Paul Spieker a validé
            end
          end
    
    
        describe "of email" do
          it "requires email address" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.email = nil
    
            expect(alice).not_to be_valid
    
          end
    
          it "requires a unique email address" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.email = eve.email
    
            expect(alice).not_to be_valid
    
    Jonne Haß's avatar
    Jonne Haß a validé
          it "requires a valid email address" do
    
            alice.email = "somebody@anywhere"
    
            expect(alice).not_to be_valid
    
        describe "of unconfirmed_email" do
          it "unconfirmed_email address can be nil/blank" do
            alice.unconfirmed_email = nil
    
            expect(alice).to be_valid
    
            alice.unconfirmed_email = ""
    
            expect(alice).to 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"
    
            expect(alice).to be_valid
    
    Jonne Haß's avatar
    Jonne Haß a validé
          it "requires a valid unconfirmed_email address" do
    
            alice.unconfirmed_email = "somebody@anywhere"
    
            expect(alice).not_to be_valid
    
          after do
            I18n.locale = :en
          end
    
          it "requires availability" do
    
    danielgrippi's avatar
    danielgrippi a validé
            alice.language = 'some invalid language'
    
            expect(alice).not_to 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')
    
            expect(user.language).to eq('fr')
    
    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')
    
            expect(user.language).to eq('de')
    
    alda519's avatar
    alda519 a validé
          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
    
          it "does not save" do
    
            expect(@user.persisted?).to be false
            expect(@user.person.persisted?).to be false
            expect(User.find_by_username("ohai")).to be_nil
    
            expect(@user).to be_valid
            expect(@user.save).to be true
            expect(@user.persisted?).to be true
            expect(@user.person.persisted?).to be true
            expect(User.find_by_username("ohai")).to eq(@user)
    
        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
    
            expect { User.build(@invalid_params) }.not_to raise_error
    
            expect(User.build(@invalid_params).save).to be false
    
    Raphael's avatar
    Raphael a validé
          it 'does not save a person' do
    
            expect { User.build(@invalid_params) }.not_to change(Person, :count)
    
    Raphael's avatar
    Raphael a validé
          end
    
    Raphael's avatar
    Raphael a validé
          it 'does not generate a key' do
    
            expect(User).to receive(:generate_key).exactly(0).times
    
    Raphael's avatar
    Raphael a validé
            User.build(@invalid_params)
          end
    
        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",
    
                        {:id => person.id,
    
                          :profile =>
                          {:first_name => "O",
    
                           :last_name => "Hai"}
                        }
            }
          end
    
          it "does not assign it to the person" do
    
            expect(User.build(@invalid_params).person.id).not_to eq(person.id)
    
      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)
    
          expect(user.invited_by_id).to eq(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)
    
        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)
    
          expect(alice.reload.disable_mail).to be false
    
      describe ".find_for_database_authentication" do
    
    Raphael's avatar
    Raphael a validé
        it 'finds a user' do
    
          expect(User.find_for_database_authentication(:username => alice.username)).to eq(alice)
    
          expect(User.find_for_database_authentication(:username => alice.email)).to eq(alice)
    
    Raphael's avatar
    Raphael a validé
        end
    
    Raphael's avatar
    Raphael a validé
        it "does not preserve case" do
    
          expect(User.find_for_database_authentication(:username => alice.username.upcase)).to eq(alice)
    
        it 'errors out when passed a non-hash' do
    
            User.find_for_database_authentication(alice.username)
    
          }.to raise_error
    
      describe '#update_profile' do
        before do
          @params = {
    
            :first_name => 'bob',
            :last_name => 'billytown',
    
        it 'dispatches the profile when tags are set' do
    
          @params = {:tag_string => '#what #hey'}
    
          mailman = Postzord::Dispatcher.build(alice, Profile.new)
    
          expect(Postzord::Dispatcher).to receive(:build).and_return(mailman)
          expect(alice.update_profile(@params)).to be true
    
        it 'sends a profile to their contacts' do
    
          mailman = Postzord::Dispatcher.build(alice, Profile.new)
    
          expect(Postzord::Dispatcher).to receive(:build).and_return(mailman)
          expect(alice.update_profile(@params)).to be true
    
          expect(alice.update_profile(@params)).to be true
          expect(alice.reload.profile.first_name).to eq('bob')
    
        it 'updates image_url' do
          params = {:image_url => "http://clown.com"}
    
    
          expect(alice.update_profile(params)).to be true
          expect(alice.reload.profile.image_url).to eq("http://clown.com")
    
        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
    
            expect(alice.update_profile(@params)).to be true
    
            expect(alice.profile.image_url).to match(Regexp.new(@photo.url(:thumb_large)))
            expect(alice.profile.image_url_medium).to match(Regexp.new(@photo.url(:thumb_medium)))
            expect(alice.profile.image_url_small).to match(Regexp.new(@photo.url(:thumb_small)))
    
          it 'unpends the photo' do
            @photo.pending = true
            @photo.save!
            @photo.reload
    
            expect(alice.update_profile(@params)).to be true
            expect(@photo.reload.pending).to be false
    
    maxwell's avatar
    maxwell a validé
      end
    
    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)
    
          expect(alice).to 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
    
          expect(@post).to receive(:mentions?).with(alice.person).and_return(true)
          expect(@post).to 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
    
          expect(@post).to receive(:mentions?).with(alice.person).and_return(false)
          expect(@post).not_to 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)
    
          allow(@post).to receive(:mentions?).and_return(true)
          expect(@post).not_to receive(:notify_person)
    
      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)
    
            }.to change {alice.invitations_from_me(true).count }.by(-1)
    
          it 'removes invitations to the user' do
    
            Invitation.new(:sender => eve, :recipient => alice, :identifier => alice.email, :aspect => eve.aspects.first).save(:validate => false)
    
            }.to change {alice.invitations_to_me(true).count }.by(-1)
    
          it 'removes all service connections' do
            Services::Facebook.create(:access_token => 'what', :user_id => alice.id)
    
            }.to change {
    
              alice.services.count
            }.by(-1)
          end
    
    ilya's avatar
    ilya a validé
      end
    
    
      describe '#mail' do
        it 'enqueues a mail job' do
    
          alice.disable_mail = false
          alice.save
    
          expect(Workers::Mail::StartedSharing).to receive(:perform_async).with(alice.id, 'contactrequestid').once
    
    Jonne Haß's avatar
    Jonne Haß a validé
          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')
    
          expect(Workers::Mail::StartedSharing).not_to receive(:perform_async)
    
    Jonne Haß's avatar
    Jonne Haß a validé
          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
    
           expect(Workers::Mail::StartedSharing).not_to receive(:perform_async)
    
    Jonne Haß's avatar
    Jonne Haß a validé
          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)
    
            }.to 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
    
            expect(alice.add_contact_to_aspect(@contact, @original_aspect)).to be true
    
    danielgrippi's avatar
    danielgrippi a validé
          end
        end
      end
    
      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
    
    
        describe '#like_for' do
          it 'returns the correct like' do
    
            expect(alice.like_for(@message)).to eq(@like)
            expect(bob.like_for(@message)).to eq(@like2)
    
          it "returns nil if there's no like" do
    
            expect(alice.like_for(@message2)).to be_nil
    
        describe '#liked?' do
          it "returns true if there's a like" do
    
            expect(alice.liked?(@message)).to be true
            expect(bob.liked?(@message)).to be true
    
          it "returns false if there's no like" do
    
            expect(alice.liked?(@message2)).to be false
    
    
      context 'change email' do
        let(:user){ alice }
    
        describe "#unconfirmed_email" do
          it "is nil by default" do
    
            expect(user.unconfirmed_email).to eql(nil)
    
          end
    
          it "forces blank to nil" do
            user.unconfirmed_email = ""
            user.save!
    
            expect(user.unconfirmed_email).to eql(nil)
    
          end
    
          it "is ignored if it equals email" do
            user.unconfirmed_email = user.email
            user.save!
    
            expect(user.unconfirmed_email).to eql(nil)
    
          end
    
          it "allows change to valid new email" do
            user.unconfirmed_email = "alice@newmail.com"
            user.save!
    
            expect(user.unconfirmed_email).to eql("alice@newmail.com")
    
    
          it "downcases the unconfirmed email" do
            user.unconfirmed_email = "AlIce@nEwmaiL.Com"
            user.save!
            expect(user.unconfirmed_email).to eql("alice@newmail.com")
          end
    
        end
    
        describe "#confirm_email_token" do
          it "is nil by default" do
    
            expect(user.confirm_email_token).to eql(nil)
    
          end
    
          it "is autofilled when unconfirmed_email is set to new email" do
            user.unconfirmed_email = "alice@newmail.com"
            user.save!
    
            expect(user.confirm_email_token).not_to be_blank
            expect(user.confirm_email_token.size).to eql(30)
    
          end
    
          it "is set back to nil when unconfirmed_email is empty" do
            user.unconfirmed_email = "alice@newmail.com"
            user.save!
    
            expect(user.confirm_email_token).not_to be_blank
    
            user.unconfirmed_email = nil
            user.save!
    
            expect(user.confirm_email_token).to 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!
    
            expect(user.confirm_email_token).not_to eql(first_token)
            expect(user.confirm_email_token.size).to eql(30)
    
        describe "#send_confirm_email" do
          it "enqueues a mail job on user with unconfirmed email" do
    
            user.update_attribute(:unconfirmed_email, "alice@newmail.com")
    
            expect(Workers::Mail::ConfirmEmail).to receive(:perform_async).with(alice.id).once
    
            alice.send_confirm_email
    
          it "enqueues NO mail job on user without unconfirmed email" do
    
            expect(Workers::Mail::ConfirmEmail).not_to receive(:perform_async).with(alice.id)
    
            alice.send_confirm_email
    
    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
    
              expect(user.confirm_email(user.confirm_email_token)).to eql(true)
              expect(user.email).to eql("alice@newmail.com")
              expect(user.unconfirmed_email).to eql(nil)
              expect(user.confirm_email_token).to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
    
            it 'returns false and does not change anything on wrong token' do
    
              expect(user.confirm_email(user.confirm_email_token.reverse)).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).not_to eql(nil)
              expect(user.confirm_email_token).not_to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
    
    Sebastian's avatar
    Sebastian a validé
            it 'returns false and does not change anything on blank token' do
    
              expect(user.confirm_email("")).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).not_to eql(nil)
              expect(user.confirm_email_token).not_to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
    
    Sebastian's avatar
    Sebastian a validé
            it 'returns false and does not change anything on blank token' do
    
              expect(user.confirm_email(nil)).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).not_to eql(nil)
              expect(user.confirm_email_token).not_to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
          end
    
          context 'on user without unconfirmed email' do
            it 'returns false and does not change anything on any token' do
    
              expect(user.confirm_email("12345"*6)).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).to eql(nil)
              expect(user.confirm_email_token).to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
    
    Sebastian's avatar
    Sebastian a validé
            it 'returns false and does not change anything on blank token' do
    
              expect(user.confirm_email("")).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).to eql(nil)
              expect(user.confirm_email_token).to eql(nil)
    
    Sebastian's avatar
    Sebastian a validé
            end
    
    Sebastian's avatar
    Sebastian a validé
            it 'returns false and does not change anything on blank token' do
    
              expect(user.confirm_email(nil)).to eql(false)
              expect(user.email).not_to eql("alice@newmail.com")
              expect(user.unconfirmed_email).to eql(nil)
              expect(user.confirm_email_token).to eql(nil)
    
      describe '#retract' do
        before do
    
          @retraction = double
    
    Jonne Haß's avatar
    Jonne Haß a validé
          @post = FactoryGirl.build(:status_message, :author => bob.person, :public => true)
    
          before do
    
            allow(SignedRetraction).to receive(:build).and_return(@retraction)
            allow(@retraction).to receive(:perform)
    
          end
    
          it 'sends a retraction' do
    
            dispatcher = double
    
            expect(Postzord::Dispatcher).to receive(:build).with(bob, @retraction, anything()).and_return(dispatcher)
            expect(dispatcher).to 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 = double
    
            expect(Postzord::Dispatcher).to receive(:build).with(bob, @retraction, {:additional_subscribers => [person], :services => anything}).and_return(dispatcher)
            expect(dispatcher).to receive(:post)
    
    
            bob.retract(@post)
          end
        end
      end
    
    
      describe "#send_reset_password_instructions" do
        it "queues up a job to send the reset password instructions" do
    
    Jonne Haß's avatar
    Jonne Haß a validé
          user = FactoryGirl.create :user
    
          expect(Workers::ResetPassword).to 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
    
              expect(user.aspects.find_by_name(aspect_name)).not_to be_nil
    
          context "with autofollow sharing enabled" do
            it "should start sharing with autofollow account" do
              AppConfig.settings.autofollow_on_join = true
              AppConfig.settings.autofollow_on_join_user = 'one'
    
              wf_double = double
    
              expect(wf_double).to receive(:fetch)
              expect(Webfinger).to receive(:new).with('one').and_return(wf_double)
    
    
              user.seed_aspects
            end
          end
    
          context "with sharing with diasporahq enabled" do
            it "should not start sharing with the diasporahq account" do
    
              AppConfig.settings.autofollow_on_join = false
    
              expect(Webfinger).not_to receive(:new)
    
      describe "#send_welcome_message" do
        let(:user) { FactoryGirl.create(:user) }
        let(:podmin) { FactoryGirl.create(:user) }
    
        context "with welcome message enabled" do
          before do
            AppConfig.settings.welcome_message.enabled = true
          end
    
          it "should send welcome message from podmin account" do
            AppConfig.admins.account = podmin.username
            expect {
              user.send_welcome_message
            }.to change(user.conversations, :count).by(1)
            expect(user.conversations.first.author.owner.username).to eq podmin.username
          end
    
          it "should send welcome message text from config" do
            AppConfig.admins.account = podmin.username
            AppConfig.settings.welcome_message.text = "Hello %{username}, welcome!"
            user.send_welcome_message
            expect(user.conversations.first.messages.first.text).to eq "Hello #{user.username}, welcome!"
          end
    
          it "should use subject from config" do
            AppConfig.settings.welcome_message.subject = "Welcome Message"
            AppConfig.admins.account = podmin.username
            user.send_welcome_message
            expect(user.conversations.first.subject).to eq "Welcome Message"
          end
    
          it "should send no welcome message if no podmin is specified" do
            AppConfig.admins.account = ""
            user.send_welcome_message
            expect(user.conversations.count).to eq 0
          end
        end
    
        context "with welcome message disabled" do
          it "shouldn't send a welcome message" do
            AppConfig.settings.welcome_message.enabled = false
            AppConfig.admins.account = podmin.username
            user.send_welcome_message
            expect(user.conversations.count).to eq 0
          end
        end
      end
    
    
      context "close account" do
        before do
          @user = bob
        end
    
        describe "#close_account!" do
    
            expect(@user.reload.access_locked?).to be true
    
          end
    
          it 'creates an account deletion' do
            expect{
              @user.close_account!
            }.to change(AccountDeletion, :count).by(1)
    
            expect(@user.person).to receive(:lock_access!)