Skip to content
Extraits de code Groupes Projets
aspects_controller_spec.rb 14,9 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'
require File.join(Rails.root, "spec", "shared_behaviors", "log_override")
Raphael's avatar
Raphael a validé
describe AspectsController do
    alice.getting_started = false
    alice.save
    sign_in :user, alice
    @alices_aspect_1 = alice.aspects.first
    @alices_aspect_2 = alice.aspects.create(:name => "another aspect")

    @controller.stub(:current_user).and_return(alice)
    request.env["HTTP_REFERER"] = 'http://' + request.host
  describe "custom logging on success" do
    before do
      @action = :index
    end
    it_should_behave_like "it overrides the logs on success"
  end

Raphael's avatar
Raphael a validé
  describe "custom logging on error" do
    class FakeError < RuntimeError;
      attr_accessor :original_exception;
    end
Raphael's avatar
Raphael a validé
    before do
      @action = :index
      @desired_error_message = "I love errors"
      @error = FakeError.new(@desired_error_message)
      @orig_error_message = "I loooooove nested errors!"
      @error.original_exception = NoMethodError.new(@orig_error_message)
      @controller.stub(:index).and_raise(@error)
    end
    it_should_behave_like "it overrides the logs on error"
  end

  describe "custom logging on redirect" do
    before do
      @action = :show
Sarah Mei's avatar
Sarah Mei a validé
      @action_params = {'id' => @alices_aspect_1.id.to_s}
    end
    it_should_behave_like "it overrides the logs on redirect"
  end

  describe "#new" do
    it "renders a remote form if remote is true" do
      get :new, "remote" => "true"
      response.should be_success
      response.body.should =~ /#{Regexp.escape('data-remote="true"')}/
    end
    it "renders a non-remote form if remote is false" do
      get :new, "remote" => "false"
      response.should be_success
      response.body.should_not =~ /#{Regexp.escape('data-remote="true"')}/
    end
    it "renders a non-remote form if remote is missing" do
      get :new
      response.should be_success
      response.body.should_not =~ /#{Regexp.escape('data-remote="true"')}/
    end
  end

  describe "#index" do
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it "generates a jasmine fixture", :fixture => true do
      get :index
      save_fixture(html_for("body"), "aspects_index")
    end
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it "generates a jasmine fixture with a prefill", :fixture => true do
      get :index, :prefill => "reshare things"
      save_fixture(html_for("body"), "aspects_index_prefill")
    end
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it 'generates a jasmine fixture with services', :fixture => true do
      alice.services << Services::Facebook.create(:user_id => alice.id)
      alice.services << Services::Twitter.create(:user_id => alice.id)
      get :index, :prefill => "reshare things"
      save_fixture(html_for("body"), "aspects_index_services")
    end
Raphael Sofaer's avatar
Raphael Sofaer a validé
    it 'generates a jasmine fixture with posts', :fixture => true do
      bob.post(:status_message, :text => "Is anyone out there?", :to => @bob.aspects.first.id)
      message = alice.post(:status_message, :text => "hello "*800, :to => @alices_aspect_2.id)
Dan Hansen's avatar
Dan Hansen a validé
      5.times { bob.comment("what", :post => message) }
      get :index
      save_fixture(html_for("body"), "aspects_index_with_posts")
    end
    it 'generates a jasmine fixture with a followed tag', :fixture => true do
      @tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent")
      TagFollowing.create!(:tag => @tag, :user => alice)
      get :index
      save_fixture(html_for("body"), "aspects_index_with_one_followed_tag")
    end

    it "generates a jasmine fixture with a post containing a video", :fixture => true do
      stub_request(:get, "http://gdata.youtube.com/feeds/api/videos/UYrkQL1bX4A?v=2").
          with(:headers => {'Accept'=>'*/*'}).
          to_return(:status => 200, :body => "<title>LazyTown song - Cooking By The Book</title>", :headers => {})
      alice.post(:status_message, :text => "http://www.youtube.com/watch?v=UYrkQL1bX4A", :to => @alices_aspect_2.id)
      get :index
      save_fixture(html_for("body"), "aspects_index_with_video_post")
    end

    it "generates a jasmine fixture with a post that has been liked", :fixture => true do
      message = alice.post(:status_message, :text => "hello "*800, :to => @alices_aspect_2.id)
      alice.build_like(:positive => true, :target => message).save
      bob.build_like(:positive => true, :target => message).save

      get :index
      save_fixture(html_for("body"), "aspects_index_with_a_post_with_likes")
    end

    context "mobile" do
      it "renders a share button when you don't pass aspect IDs" do
        get :index, :format => :mobile
        response.body.should =~ /#{Regexp.escape('id="status_message_submit"')}/
      end
      
      it "renders a share button when you pass aspect IDs" do
        get :index, :a_ids => [@alices_aspect_1], :format => :mobile
        response.body.should =~ /#{Regexp.escape('id="status_message_submit"')}/
      end
    end

    context 'with getting_started = true' do
      before do
        alice.getting_started = true
        alice.save
      it 'does not redirect mobile users to getting_started' do
        get :index, :format => :mobile
        response.should_not be_redirect
      end
      it 'does not redirect ajax to getting_started' do
        get :index, :format => :js
        response.should_not be_redirect
      end
    end
Raphael Sofaer's avatar
Raphael Sofaer a validé

MrZYX's avatar
MrZYX a validé
    context 'with no aspects' do
      before do
        alice.aspects.each { |aspect| aspect.destroy }
        alice.reload
      end
Raphael Sofaer's avatar
Raphael Sofaer a validé

MrZYX's avatar
MrZYX a validé
      it 'redirects to the new aspect page' do
        get :index
        response.should redirect_to new_aspect_path
      end
    end

    context 'with posts in multiple aspects' do
        @posts = []
Sarah Mei's avatar
Sarah Mei a validé
        2.times do |n|
Raphael's avatar
Raphael a validé
          user = Factory(:user)
          aspect = user.aspects.create(:name => 'people')
          connect_users(alice, @alices_aspect_1, user, aspect)
Sarah Mei's avatar
Sarah Mei a validé
          target_aspect = n.even? ? @alices_aspect_1 : @alices_aspect_2
          post = alice.post(:status_message, :text=> "hello#{n}", :to => target_aspect)
Sarah Mei's avatar
Sarah Mei a validé
          post.created_at = Time.now - (2 - n).seconds
          @posts << post
        end
        alice.build_comment(:text => 'lalala', :post => @posts.first).save
zhitomirskiyi's avatar
zhitomirskiyi a validé
      describe "post visibilities" do
        before do
          aspect_to_post = bob.aspects.where(:name => "generic").first
          @status = bob.post(:status_message, :text=> "hello", :to => aspect_to_post)
zhitomirskiyi's avatar
zhitomirskiyi a validé
          @vis = @status.post_visibilities.first
        end

        it "pulls back non hidden posts" do
          get :index
          assigns[:posts].include?(@status).should be_true
zhitomirskiyi's avatar
zhitomirskiyi a validé
        end
        it "does not pull back hidden posts" do
          @vis.update_attributes(:hidden => true)
zhitomirskiyi's avatar
zhitomirskiyi a validé
          get :index
          assigns[:posts].include?(@status).should be_false
zhitomirskiyi's avatar
zhitomirskiyi a validé
        end
      end

      describe 'infinite scroll' do
        it 'renders with the infinite scroll param' do
          get :index, :only_posts => true
          assigns[:posts].include?(@posts.first).should be_true
      describe "ordering" do
        it "orders posts by updated_at by default" do
          get :index
        end

        it "orders posts by created_at on request" do
          get :index, :sort_order => 'created_at'
        it "remembers your sort order and lets you override the memory" do
          get :index, :sort_order => "created_at"
          get :index
          get :index, :sort_order => "updated_at"

        it "doesn't allow SQL injection" do
          get :index, :sort_order => "\"; DROP TABLE users;"
          get :index, :sort_order => "created_at"
Sarah Mei's avatar
Sarah Mei a validé
      it "returns all posts by default" do
      it "posts include reshares" do
        reshare = alice.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects)
        get :index
        assigns[:posts].map { |x| x.id }.should include(reshare.id)
      it "can filter to a single aspect" do
Sarah Mei's avatar
Sarah Mei a validé
        get :index, :a_ids => [@alices_aspect_2.id.to_s]
      it "can filter to multiple aspects" do
Sarah Mei's avatar
Sarah Mei a validé
        get :index, :a_ids => [@alices_aspect_1.id.to_s, @alices_aspect_2.id.to_s]

    describe 'performance', :performance => true do
Raphael's avatar
Raphael a validé
      before do
        require 'benchmark'
        8.times do |n|
Raphael's avatar
Raphael a validé
          aspect = user.aspects.create(:name => 'people')
          connect_users(alice, @alices_aspect_1, user, aspect)
          post = alice.post(:status_message, :text => "hello#{n}", :to => @alices_aspect_2.id)
          8.times do |n|
            user.comment "yo#{post.text}", :post => post
Raphael's avatar
Raphael a validé
        end
      end
      it 'takes time' do
Raphael's avatar
Raphael a validé
          get :index
maxwell's avatar
maxwell a validé
        }.should < 1.5
Raphael's avatar
Raphael a validé
      end
    end
Sarah Mei's avatar
Sarah Mei a validé
  describe "#show" do
    it "succeeds" do
Sarah Mei's avatar
Sarah Mei a validé
      get :show, 'id' => @alices_aspect_1.id.to_s
danielvincent's avatar
danielvincent a validé
      response.should be_redirect
Sarah Mei's avatar
Sarah Mei a validé
    end
    it 'redirects on an invalid id' do
      get :show, 'id' => 4341029835
      response.should be_redirect
    end
  describe "#create" do
    context "with valid params" do
      it "creates an aspect" do
        alice.aspects.count.should == 2
        post :create, "aspect" => {"name" => "new aspect"}
        alice.reload.aspects.count.should == 3
      it "redirects to the aspect's contact page" do
        post :create, "aspect" => {"name" => "new aspect"}
        response.should redirect_to(contacts_path(:a_id => Aspect.find_by_name("new aspect").id))
danielgrippi's avatar
danielgrippi a validé

      context "with person_id param" do
        it "creates a contact if one does not already exist" do
          lambda {
            post :create, :format => 'js', :aspect => {:name => "new", :person_id => eve.person.id}
danielgrippi's avatar
danielgrippi a validé
            alice.contacts.count
          }.by(1)
        end

        it "adds a new contact to the new aspect" do
          post :create, :format => 'js', :aspect => {:name => "new", :person_id => eve.person.id}
          alice.aspects.find_by_name("new").contacts.count.should == 1
        end

        it "adds an existing contact to the new aspect" do
          post :create, :format => 'js', :aspect => {:name => "new", :person_id => bob.person.id}
          alice.aspects.find_by_name("new").contacts.count.should == 1
        end
      end
    context "with invalid params" do
      it "does not create an aspect" do
        alice.aspects.count.should == 2
        post :create, "aspect" => {"name" => ""}
        alice.reload.aspects.count.should == 2
      it "goes back to the page you came from" do
        post :create, "aspect" => {"name" => ""}
  describe "#update" do
    before do
      @alices_aspect_1 = alice.aspects.create(:name => "Bruisers")
    it "doesn't overwrite random attributes" do
      new_user = Factory.create :user
      params = {"name" => "Bruisers"}
      params[:user_id] = new_user.id
Sarah Mei's avatar
Sarah Mei a validé
      put('update', :id => @alices_aspect_1.id, "aspect" => params)
      Aspect.find(@alices_aspect_1.id).user_id.should == alice.id
Raphael's avatar
Raphael a validé
  describe '#edit' do
      eve.profile.first_name = nil
      eve.profile.save
      eve.save
zhitomirskiyi's avatar
zhitomirskiyi a validé

      @zed = Factory(:user_with_aspect, :username => "zed")
zhitomirskiyi's avatar
zhitomirskiyi a validé
      @zed.profile.first_name = "zed"
      @zed.profile.save
      @zed.save
      @katz = Factory(:user_with_aspect, :username => "katz")
zhitomirskiyi's avatar
zhitomirskiyi a validé
      @katz.profile.first_name = "katz"
      @katz.profile.save
      @katz.save

      connect_users(alice, @alices_aspect_2, eve, eve.aspects.first)
      connect_users(alice, @alices_aspect_2, @zed, @zed.aspects.first)
      connect_users(alice, @alices_aspect_1, @katz, @katz.aspects.first)
Raphael's avatar
Raphael a validé
    it 'renders' do
Sarah Mei's avatar
Sarah Mei a validé
      get :edit, :id => @alices_aspect_1.id
Raphael's avatar
Raphael a validé
      response.should be_success
    end
zhitomirskiyi's avatar
zhitomirskiyi a validé
    it 'assigns the contacts in alphabetical order with people in aspects first' do
      get :edit, :id => @alices_aspect_2.id
      assigns[:contacts].map(&:id).should == [alice.contact_for(eve.person), alice.contact_for(@zed.person), alice.contact_for(bob.person), alice.contact_for(@katz.person)].map(&:id)

    it 'assigns all the contacts if noone is there' do
      alices_aspect_3 = alice.aspects.create(:name => "aspect 3")

      get :edit, :id => alices_aspect_3.id
      assigns[:contacts].map(&:id).should == [alice.contact_for(bob.person), alice.contact_for(eve.person), alice.contact_for(@katz.person), alice.contact_for(@zed.person)].map(&:id)

    it 'eager loads the aspect memberships for all the contacts' do
      get :edit, :id => @alices_aspect_2.id
      assigns[:contacts].each do |c|
        c.aspect_memberships.loaded?.should be_true
      end
Raphael's avatar
Raphael a validé
  end
  describe "#toggle_contact_visibility" do
    it 'sets contacts visible' do
Sarah Mei's avatar
Sarah Mei a validé
      @alices_aspect_1.contacts_visible = false
      @alices_aspect_1.save
Sarah Mei's avatar
Sarah Mei a validé
      get :toggle_contact_visibility, :format => 'js', :aspect_id => @alices_aspect_1.id
      @alices_aspect_1.reload.contacts_visible.should be_true
    end

    it 'sets contacts hidden' do
Sarah Mei's avatar
Sarah Mei a validé
      @alices_aspect_1.contacts_visible = true
      @alices_aspect_1.save
Sarah Mei's avatar
Sarah Mei a validé
      get :toggle_contact_visibility, :format => 'js', :aspect_id => @alices_aspect_1.id
      @alices_aspect_1.reload.contacts_visible.should be_false

  context 'helper methods' do
    before do
      @tag = ActsAsTaggableOn::Tag.create!(:name => "partytimeexcellent")
      TagFollowing.create!(:tag => @tag, :user => alice)
      alice.should_receive(:followed_tags).once.and_return([42])
      it 'queries current_users tag if there are tag_followings' do
        @controller.tags.should == [42]
      it 'does not query twice' do
        @controller.tags.should == [42]
        @controller.tags.should == [42]

  describe "mobile site" do
    before do
      ap = alice.person
      posts = []
      posts << alice.post(:reshare, :root_guid => Factory(:status_message, :public => true).guid, :to => 'all')
      posts << alice.post(:status_message, :text => 'foo', :to => alice.aspects)
      photo = Factory(:activity_streams_photo, :public => true, :author => ap)
      posts << photo
      posts.each do |p|
        alice.build_like(:positive => true, :target => p).save
      end
      alice.add_to_streams(photo, alice.aspects)
      sign_in alice
    end

    it 'should not 500' do
      get :index, :format => :mobile
      response.should be_success

    end
  end