<commit_before>#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe Person do
  before do
    @user = Factory.create(:user)
    @user2 = Factory.create(:user)
    @person = Factory.create(:person)
    @aspect = @user.aspect(:name => "Dudes")
    @aspect2 = @user2.aspect(:name => "Abscence of Babes")
  end

  describe '#diaspora_handle' do
    context 'local people' do
      it 'uses the pod config url to set the diaspora_handle' do
        @user.person.diaspora_handle.should == @user.username + "@" + APP_CONFIG[:terse_pod_url]
      end
    end

    context 'remote people' do
      it 'stores the diaspora_handle in the database' do
        @person.diaspora_handle.include?(APP_CONFIG[:terse_pod_url]).should be false
      end
    end
  end

  it 'should not allow two people with the same diaspora_handle' do
    person_two = Factory.build(:person, :url => @person.diaspora_handle)
    person_two.valid?.should == false
  end

  describe 'xml' do
    before do
      @xml = @person.to_xml.to_s
    end

    it 'should serialize to xml' do
      @xml.include?("person").should == true
    end

    it 'should have a profile in its xml' do
      @xml.include?("first_name").should == true
    end
  end

  it 'should know when a post belongs to it' do
    person_message = Factory.create(:status_message, :person => @person)
    person_two =     Factory.create(:person)

    @person.owns?(person_message).should be true
    person_two.owns?(person_message).should be false
  end

  it 'should delete all of user except comments upon user deletion' do
    person = Factory.create(:person)

    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)

    status_message = Factory.create(:status_message, :person => @person)

    Factory.create(:comment, :person_id => person.id,  :text => "yes i do",       :post => status_message)
    Factory.create(:comment, :person_id => person.id,  :text => "i love you",     :post => status_message)
    Factory.create(:comment, :person_id => person.id,  :text => "hello",          :post => status_message)
    Factory.create(:comment, :person_id => @person.id, :text => "you are creepy", :post => status_message)

    person.destroy

    Post.count.should == 1
    Comment.all.count.should == 4
    status_message.comments.count.should == 4
  end

  describe "unfriending" do
    it 'should not delete an orphaned friend' do
      request = @user.send_friend_request_to @person, @aspect

      @user.activate_friend(@person, @aspect)
      @user.reload

      Person.all.count.should    == 3
      @user.friends.count.should == 1
      @user.unfriend(@person)
      @user.reload
      @user.friends.count.should == 0
      Person.all.count.should    == 3
    end

    it 'should not delete an un-orphaned friend' do
      request = @user.send_friend_request_to @person, @aspect
      request2 = @user2.send_friend_request_to @person, @aspect2

      @user.activate_friend(@person, @aspect)
      @user2.activate_friend(@person, @aspect2)

      @user.reload
      @user2.reload

      Person.all.count.should     == 3
      @user.friends.count.should  == 1
      @user2.friends.count.should == 1

      @user.unfriend(@person)
      @user.reload
      @user2.reload
      @user.friends.count.should  == 0
      @user2.friends.count.should == 1

      Person.all.count.should     == 3
    end
  end

  describe 'searching' do
    before do
      @friend_one   = Factory.create(:person)
      @friend_two   = Factory.create(:person)
      @friend_three = Factory.create(:person)
      @friend_four  = Factory.create(:person)

      @friend_one.profile.first_name = "Robert"
      @friend_one.profile.last_name  = "Grimm"
      @friend_one.profile.save

      @friend_two.profile.first_name = "Eugene"
      @friend_two.profile.last_name  = "Weinstein"
      @friend_two.save

      @friend_three.profile.first_name = "Yevgeniy"
      @friend_three.profile.last_name  = "Dodis"
      @friend_three.save

      @friend_four.profile.first_name = "Casey"
      @friend_four.profile.last_name  = "Grippi"
      @friend_four.save
    end

    it 'should yield search results on partial names' do
      people = Person.search("Eu")
      people.include?(@friend_two).should   == true
      people.include?(@friend_one).should   == false
      people.include?(@friend_three).should == false
      people.include?(@friend_four).should  == false

      people = Person.search("wEi")
      people.include?(@friend_two).should   == true
      people.include?(@friend_one).should   == false
      people.include?(@friend_three).should == false
      people.include?(@friend_four).should  == false

      people = Person.search("gri")
      people.include?(@friend_one).should   == true
      people.include?(@friend_four).should  == true
      people.include?(@friend_two).should   == false
      people.include?(@friend_three).should == false
    end

    it 'should search by diaspora_handle exactly' do
      stub_success("<EMAIL>")
      Person.by_webfinger(@friend_one.diaspora_handle).should == @friend_one
    end

    it 'should create a stub for a remote user' do
      stub_success("<EMAIL>")
      tom = Person.by_webfinger('<EMAIL>')
      tom.real_name.include?("Hamiltom").should be true
    end

  end
end
<commit_msg>Add spec for full name searching<commit_after>#   Copyright (c) 2010, Diaspora Inc.  This file is
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe Person do
  before do
    @user = Factory.create(:user)
    @user2 = Factory.create(:user)
    @person = Factory.create(:person)
    @aspect = @user.aspect(:name => "Dudes")
    @aspect2 = @user2.aspect(:name => "Abscence of Babes")
  end

  describe '#diaspora_handle' do
    context 'local people' do
      it 'uses the pod config url to set the diaspora_handle' do
        @user.person.diaspora_handle.should == @user.username + "@" + APP_CONFIG[:terse_pod_url]
      end
    end

    context 'remote people' do
      it 'stores the diaspora_handle in the database' do
        @person.diaspora_handle.include?(APP_CONFIG[:terse_pod_url]).should be false
      end
    end
  end

  it 'should not allow two people with the same diaspora_handle' do
    person_two = Factory.build(:person, :url => @person.diaspora_handle)
    person_two.valid?.should == false
  end

  describe 'xml' do
    before do
      @xml = @person.to_xml.to_s
    end

    it 'should serialize to xml' do
      @xml.include?("person").should == true
    end

    it 'should have a profile in its xml' do
      @xml.include?("first_name").should == true
    end
  end

  it 'should know when a post belongs to it' do
    person_message = Factory.create(:status_message, :person => @person)
    person_two =     Factory.create(:person)

    @person.owns?(person_message).should be true
    person_two.owns?(person_message).should be false
  end

  it 'should delete all of user except comments upon user deletion' do
    person = Factory.create(:person)

    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)
    Factory.create(:status_message, :person => person)

    status_message = Factory.create(:status_message, :person => @person)

    Factory.create(:comment, :person_id => person.id,  :text => "yes i do",       :post => status_message)
    Factory.create(:comment, :person_id => person.id,  :text => "i love you",     :post => status_message)
    Factory.create(:comment, :person_id => person.id,  :text => "hello",          :post => status_message)
    Factory.create(:comment, :person_id => @person.id, :text => "you are creepy", :post => status_message)

    person.destroy

    Post.count.should == 1
    Comment.all.count.should == 4
    status_message.comments.count.should == 4
  end

  describe "unfriending" do
    it 'should not delete an orphaned friend' do
      request = @user.send_friend_request_to @person, @aspect

      @user.activate_friend(@person, @aspect)
      @user.reload

      Person.all.count.should    == 3
      @user.friends.count.should == 1
      @user.unfriend(@person)
      @user.reload
      @user.friends.count.should == 0
      Person.all.count.should    == 3
    end

    it 'should not delete an un-orphaned friend' do
      request = @user.send_friend_request_to @person, @aspect
      request2 = @user2.send_friend_request_to @person, @aspect2

      @user.activate_friend(@person, @aspect)
      @user2.activate_friend(@person, @aspect2)

      @user.reload
      @user2.reload

      Person.all.count.should     == 3
      @user.friends.count.should  == 1
      @user2.friends.count.should == 1

      @user.unfriend(@person)
      @user.reload
      @user2.reload
      @user.friends.count.should  == 0
      @user2.friends.count.should == 1

      Person.all.count.should     == 3
    end
  end

  describe '::search' do
    before do
      @friend_one   = Factory.create(:person)
      @friend_two   = Factory.create(:person)
      @friend_three = Factory.create(:person)
      @friend_four  = Factory.create(:person)

      @friend_one.profile.first_name = "Robert"
      @friend_one.profile.last_name  = "Grimm"
      @friend_one.profile.save

      @friend_two.profile.first_name = "Eugene"
      @friend_two.profile.last_name  = "Weinstein"
      @friend_two.save

      @friend_three.profile.first_name = "Yevgeniy"
      @friend_three.profile.last_name  = "Dodis"
      @friend_three.save

      @friend_four.profile.first_name = "Casey"
      @friend_four.profile.last_name  = "Grippi"
      @friend_four.save
    end

    it 'should yield search results on partial names' do
      people = Person.search("Eu")
      people.include?(@friend_two).should   == true
      people.include?(@friend_one).should   == false
      people.include?(@friend_three).should == false
      people.include?(@friend_four).should  == false

      people = Person.search("wEi")
      people.include?(@friend_two).should   == true
      people.include?(@friend_one).should   == false
      people.include?(@friend_three).should == false
      people.include?(@friend_four).should  == false

      people = Person.search("gri")
      people.include?(@friend_one).should   == true
      people.include?(@friend_four).should  == true
      people.include?(@friend_two).should   == false
      people.include?(@friend_three).should == false
    end

    it 'should yield results on full names' do
      pending "Not yet implemented."
      people = Person.search("<NAME>")
      people.should == [@friend_four]
    end

    it 'should search by diaspora_handle exactly' do
      stub_success("<EMAIL>")
      Person.by_webfinger(@friend_one.diaspora_handle).should == @friend_one
    end

    it 'should create a stub for a remote user' do
      stub_success("<EMAIL>")
      tom = Person.by_webfinger('<EMAIL>')
      tom.real_name.include?("Hamiltom").should be true
    end

  end
end

{'fraction_non_alphanumeric': 0.120930644019816, 'fraction_numerical': 0.005307855626326964, 'mean_word_length': 2.9764333450580374, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3681609', 'n_tokens_mistral': 3660, 'n_tokens_neox': 3518, 'n_words': 826}