Skip to content
Extraits de code Groupes Projets
people_controller_spec.rb 6,61 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 PeopleController do
  render_views

  let!(:aspect) { user.aspects.create(:name => "lame-os") }
Raphael's avatar
Raphael a validé
  describe '#hashes_from_people' do
    before do
      @everyone = []
      10.times do
        @everyone << Factory.create(:person)
      end

      user.send_contact_request_to(@everyone[3], aspect)
      user.send_contact_request_to(@everyone[2], aspect)
      user.activate_contact(@everyone[4], aspect)
      user.activate_contact(@everyone[5], aspect)

      @people = Person.search('eugene')
      @people.length.should == 10
      @hashes = @controller.hashes_for_people(@people, user.aspects)
    end
    it 'has the correct result for no relationship' do
      hash = @hashes.first
      hash[:person].should == @people.first
      hash[:contact].should be_false
      hash[:request].should be_false
      hash[:aspects].should == user.aspects
    end
    it 'has the correct result for a connected person' do
      hash = @hashes[4]
      hash[:person].should == @people[4]
      hash[:contact].should be_true
      hash[:request].should be_false
      hash[:aspects].should == user.aspects
    end
    it 'has the correct result for a requested person' do
      hash = @hashes[2]
      hash[:person].should == @people[2]
      hash[:contact].should be_false
      hash[:request].should be_true
      hash[:aspects].should == user.aspects
    end
  end
  describe '#index' do
    before do
      @eugene = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
      @korth  = Factory.create(:person, :profile => {:first_name => "Evan", :last_name => "Korth"})
    end

Raphael's avatar
Raphael a validé
    it "assigns hashes" do
      eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
      get :index, :q => "Eu"
Raphael's avatar
Raphael a validé
      people = assigns[:hashes].map{|h| h[:person]}
      people.should include @eugene
      people.should include eugene2
Raphael's avatar
Raphael a validé
    end
    it "assigns people" do
      eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
      get :index, :q => "Eu"
      assigns[:people].should == [@eugene, eugene2]
    it 'shows a contact' do
      connect_users(user, aspect, user2, user2.aspects.create(:name => 'Neuroscience'))
      get :index, :q => user2.person.profile.first_name.to_s
Raphael's avatar
Raphael a validé
      response.should redirect_to user2.person
    it 'shows a non-contact' do
      user2 = make_user
      user2.person.profile.searchable = true
      user2.save
      get :index, :q => user2.person.profile.first_name.to_s
Raphael's avatar
Raphael a validé
      response.should redirect_to user2.person

    it "redirects to person page if there is exactly one match" do
      get :index, :q => "Korth"
      response.should redirect_to @korth
    end

    it "does not redirect if there are no matches" do
      get :index, :q => "Korthsauce"
      response.should_not be_redirect
    end
  describe '#show' do
    it 'should go to the current_user show page' do
      get :show, :id => user.person.id
      response.should be_success
    end

Raphael's avatar
Raphael a validé
    it 'renders with a post' do
      user.post :status_message, :message => 'test more', :to => aspect.id
      get :show, :id => user.person.id
      response.should be_success
    end

    it 'renders with a post' do
      message = user.post :status_message, :message => 'test more', :to => aspect.id
      user.comment 'I mean it', :on => message
      get :show, :id => user.person.id
      response.should be_success
    end

    it "redirects on an invalid id" do
      get :show, :id => 'delicious'
      response.should redirect_to people_path
    end

    it "redirects on a nonexistent person" do
      get :show, :id => user.id
      response.should redirect_to people_path
    end
    
    it "renders the show page of a contact" do
      connect_users(user, aspect, user2, user2.aspects.create(:name => 'Neuroscience'))
      get :show, :id => user2.person.id
      response.should be_success
    end
    
    it "renders the show page of a non-contact" do
      user2 = make_user
      get :show, :id => user2.person.id
      response.should be_success
    end
  describe '#webfinger' do
    it 'enqueues a webfinger job' do
      Resque.should_receive(:enqueue).with(Jobs::SocketWebfinger, user.id, user.diaspora_handle, anything).once
      get :retrieve_remote, :diaspora_handle => user.diaspora_handle
    end
  end

    it "sets the flash" do
      put :update, "id" => user.person.id.to_s, "person" => {
        "profile" => {
          "image_url" => "",
          "last_name" => "Smith",
          "first_name" => "Will"
        }
      }
      flash[:notice].should_not be_empty
    end

    context 'with a profile photo set' do
      before do
        @params = { :profile =>
                   { :image_url => "",
                     :last_name  => user.person.profile.last_name,
                     :first_name => user.person.profile.first_name }}

        user.person.profile.image_url = "http://tom.joindiaspora.com/images/user/tom.jpg"
        user.person.profile.save
      end
      it "doesn't overwrite the profile photo when an empty string is passed in" do
        image_url = user.person.profile.image_url
        put :update, :id => user.person.id.to_s, :person => @params
        user.person.profile.image_url.should == image_url
      end
      it 'updates a profile photo url' do
        fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png'
        photo = user.post(:photo, :user_file => File.open(fixture_name), :to => aspect.id)
        @params[:profile][:image_url] = photo.url(:thumb_medium)
        put :update, :id => user.person.id, :person => @params
        goal_pod_url = (APP_CONFIG[:pod_url][-1,1] == '/' ? APP_CONFIG[:pod_url].chop : APP_CONFIG[:pod_url])
        user.person.reload.profile.image_url.should ==
          "#{goal_pod_url}#{photo.url(:thumb_medium)}"
      end
    it 'does not allow mass assignment' do
      new_user = make_user
      put :update, :id => user.person.id, :person => {
        :owner_id => new_user.id}
      user.person.reload.owner_id.should_not == new_user.id
    end

    it 'does not overwrite the profile diaspora handle' do
      handle_params = {'profile' => {'diaspora_handle' => 'abc@a.com'}}
      put :update, :id => user.person.id, :person => handle_params
      user.person.reload.profile[:diaspora_handle].should_not == 'abc@a.com'
    end