Skip to content
Extraits de code Groupes Projets
aspects_controller_spec.rb 8,58 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
Raphael's avatar
Raphael a validé
    @user  = alice
    @user2 = bob
Raphael's avatar
Raphael a validé
    @aspect0  = @user.aspects.first
    @aspect1  = @user.aspects.create(:name => "another aspect")
Raphael's avatar
Raphael a validé
    @aspect2  = @user2.aspects.first
    @contact = @user.contact_for(@user2.person)
maxwell's avatar
maxwell a validé
    @user.getting_started = false
    @user.save
Jamie Wilkinson's avatar
Jamie Wilkinson a validé
    sign_in :user, @user
    @controller.stub(:current_user).and_return(@user)
    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

  describe "custom logging on redirect" do
    before do
      @action = :show
      @action_params = {'id' => @aspect0.id.to_s}
    end
    it_should_behave_like "it overrides the logs on redirect"
  end

  describe "#index" do
    it "assigns @contacts to all the user's contacts" do
      assigns[:contacts].map{|c| c.id}.should == @user.contacts.map{|c| c.id}

    context 'filtering' do
        @posts = []
        @users = []
        8.times do |n|
Raphael's avatar
Raphael a validé
          user = Factory(:user)
          @users << user
          aspect = user.aspects.create(:name => 'people')
          connect_users(@user, @aspect0, user, aspect)
          post =  @user.post(:status_message, :message => "hello#{n}", :to => eval("@aspect#{(n%2)}.id"))
          @posts << post
        end
      end

      it "returns all posts" do
        get :index
        assigns(:posts).length.should == 8
      end

      it "returns posts filtered by a single aspect" do
        get :index, :a_ids => [@aspect1.id.to_s]
        assigns(:posts).length.should == 4
      end

      it "returns posts from filtered aspects" do
        get :index, :a_ids => [@aspect0.id.to_s, @aspect1.id.to_s]
        assigns(:posts).length.should == 8
      end
    end

Raphael's avatar
Raphael a validé
    context 'performance' do
      before do
        require 'benchmark'
        @posts = []
        @users = []
        8.times do |n|
Raphael's avatar
Raphael a validé
          @users << user
          aspect = user.aspects.create(:name => 'people')
          connect_users(@user, @aspect0, user, aspect)
Raphael's avatar
Raphael a validé
          post =  @user.post(:status_message, :message => "hello#{n}", :to => @aspect1.id)
          @posts << post
          8.times do |n|
            user.comment "yo#{post.message}", :on => post
          end
Raphael's avatar
Raphael a validé
        end
      end

      it 'takes time' do
        Benchmark.realtime{
          get :index
        }.should < 0.8
Raphael's avatar
Raphael a validé
      end
    end
Sarah Mei's avatar
Sarah Mei a validé
  describe "#show" do
    it "succeeds" do
      get :show, 'id' => @aspect0.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
ilya's avatar
ilya a validé
        @user.aspects.count.should == 2
        post :create, "aspect" => {"name" => "new aspect"}
ilya's avatar
ilya a validé
        @user.reload.aspects.count.should == 3
      end
      it "redirects to the aspect page" do
        post :create, "aspect" => {"name" => "new aspect"}
        response.should redirect_to(aspect_path(Aspect.find_by_name("new aspect")))
      end
    end
    context "with invalid params" do
      it "does not create an aspect" do
ilya's avatar
ilya a validé
        @user.aspects.count.should == 2
        post :create, "aspect" => {"name" => ""}
ilya's avatar
ilya a validé
        @user.reload.aspects.count.should == 2
      it "goes back to the page you came from" do
        post :create, "aspect" => {"name" => ""}
  describe "#manage" do
    it "succeeds" do
      get :manage
      response.should be_success
    end
    it "performs reasonably" do
        require 'benchmark'
        8.times do |n|
          aspect = @user.aspects.create(:name => "aspect#{n}")
          8.times do |o|
            person = Factory(:person)
            @user.activate_contact(person, aspect)
          end
        end
        Benchmark.realtime{
          get :manage
        }.should < 2
    end
    it "assigns aspect to manage" do
      get :manage
      assigns(:aspect).should == :manage
    end
    it "assigns remote_requests" do
      get :manage
      assigns(:remote_requests).should be_empty
    end
    it "assigns contacts to only non-pending" do
      @user.contacts.count.should == 1
Raphael's avatar
Raphael a validé
      @user.send_contact_request_to(Factory(:user).person, @aspect0)
      @user.contacts.count.should == 2

      get :manage
      contacts = assigns(:contacts)
      contacts.count.should == 1
      contacts.first.should == @contact
    end
    context "when the user has pending requests" do
      before do
        requestor_aspect = requestor.aspects.create(:name => "Meh")
        requestor.send_contact_request_to(@user.person, requestor_aspect)

        requestor.reload
        requestor_aspect.reload
        @user.reload
      end
      it "succeeds" do
        get :manage
        response.should be_success
      end
      it "assigns aspect to manage" do
        get :manage
        assigns(:aspect).should == :manage
      end
      it "assigns remote_requests" do
        get :manage
        assigns(:remote_requests).count.should == 1
      end
      it "generates a jasmine fixture" do
        get :manage
        save_fixture(html_for("body"), "aspects_manage")
      end
  describe "#move_contact" do
    before do
      @person = Factory.create(:person)
      @opts = {
        :person_id => @person.id,
        :from => @aspect0.id,
        :to =>
    it 'calls the move_contact_method' do
      @controller.stub!(:current_user).and_return(@user)
      @user.should_receive(:move_contact)
      post :move_contact, @opts
  describe "#update" do
    before do
      @aspect0 = @user.aspects.create(:name => "Bruisers")
    end
    it "doesn't overwrite random attributes" do
      new_user         = Factory.create :user
      params           = {"name" => "Bruisers"}
      params[:user_id] = new_user.id
      put('update', :id => @aspect0.id, "aspect" => params)
      Aspect.find(@aspect0.id).user_id.should == @user.id
ilya's avatar
ilya a validé

  describe "#add_to_aspect" do
        @user3.send_contact_request_to(@user.person, @user3.aspects.create(:name => "Walruses"))
      end
      it 'deletes the request' do
        post 'add_to_aspect',
          :format => 'js',
          :person_id => @user3.person.id,
          :aspect_id => @aspect1.id
Raphael's avatar
Raphael a validé
        Request.where(:sender_id => @user3.person.id, :recipient_id => @user.person.id).first.should be_nil
      end
      it 'does not leave the contact pending' do
        post 'add_to_aspect',
          :format => 'js',
          :person_id => @user3.person.id,
          :aspect_id => @aspect1.id
        @user.contact_for(@user3.person).should_not be_pending
      end
    end
Raphael's avatar
Raphael a validé
    context 'with a non-contact' do
      before do
        @person = Factory(:person)
      end
      it 'calls send_contact_request_to' do
        @user.should_receive(:send_contact_request_to).with(@person, @aspect1)
        post 'add_to_aspect',
          :format => 'js',
          :person_id => @person.id,
          :aspect_id => @aspect1.id
      end
      it 'does not call add_contact_to_aspect' do
        @user.should_not_receive(:add_contact_to_aspect)
        post 'add_to_aspect',
          :format => 'js',
          :person_id => @person.id,
          :aspect_id => @aspect1.id
Raphael's avatar
Raphael a validé
      end
    end
ilya's avatar
ilya a validé
    it 'adds the users to the aspect' do
      @user.should_receive(:add_contact_to_aspect)
      post 'add_to_aspect',
        :format => 'js',
        :person_id => @user2.person.id,
        :aspect_id => @aspect1.id
Raphael's avatar
Raphael a validé
      response.should be_success
ilya's avatar
ilya a validé
    end
Raphael's avatar
Raphael a validé
  describe '#edit' do
    it 'renders' do
      get :edit, :id => @aspect0.id
      response.should be_success
    end
  end

  describe "#remove_from_aspect" do
    it 'removes contacts from an aspect' do
      @user.add_contact_to_aspect(@contact, @aspect1)
      post 'remove_from_aspect',
        :format => 'js',
        :person_id => @user2.person.id,
        :aspect_id => @aspect0.id
Raphael's avatar
Raphael a validé
      response.should be_success
      @aspect0.reload
      @aspect0.contacts.include?(@contact).should be false
ilya's avatar
ilya a validé
  end