Skip to content
Extraits de code Groupes Projets
aspects_controller_spec.rb 5,63 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'
Raphael's avatar
Raphael a validé
describe AspectsController do
Raphael's avatar
Raphael a validé
    @user  = make_user
    @user2 = make_user

    @aspect   = @user.aspects.create(:name => "lame-os")
    @aspect1  = @user.aspects.create(:name => "another aspect")
    @aspect2  = @user2.aspects.create(:name => "party people")

    connect_users(@user, @aspect, @user2, @aspect2)
    @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
    request.env["HTTP_REFERER"] = 'http://' + request.host
  describe "#index" do
    it "assigns @contacts to all the user's contacts" do
      Factory.create :person
Raphael's avatar
Raphael a validé
      begin
Raphael's avatar
Raphael a validé
      rescue Exception => e
        raise e.original_exception
      end
      assigns[:contacts].should == @user.contacts
Raphael's avatar
Raphael a validé
    context 'performance' do
      before do
        require 'benchmark'
        @posts = []
        @users = []
        8.times do |n|
          user = make_user
          @users << user
          aspect = user.aspects.create(:name => 'people')
          connect_users(@user, @aspect, user, aspect)
          post =  @user.post(:status_message, :message => "hello#{n}", :to => @aspect1.id)
          @posts << post
          user.comment "yo#{post.message}", :on => post
        end
      end

      it 'takes time' do
        Benchmark.realtime{
          get :index
Raphael's avatar
Raphael a validé
        }.should < 3
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' => @aspect.id.to_s
      response.should be_success
    end
    it "assigns aspect, aspect_contacts, and posts" do
      get :show, 'id' => @aspect.id.to_s
      assigns(:aspect).should == @aspect
      assigns(:aspect_contacts).should == @aspect.contacts
      assigns(:posts).should == []
    end
    it "paginates" do
      16.times { |i| @user2.post(:status_message, :to => @aspect2.id, :message => "hi #{i}") }

      get :show, 'id' => @aspect.id.to_s
      assigns(:posts).count.should == 15

      get :show, 'id' => @aspect.id.to_s, 'page' => '2'
      assigns(:posts).count.should == 1
    end
  end

  describe "#create" do
    describe "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
    describe "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 "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
    context "when the user has pending requests" do
      before do
        requestor        = make_user
        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
    end
  end

  describe "#move_contact" do
    let(:opts) { {:person_id => "person_id", :from => "from_aspect_id", :to => {:to => "to_aspect_id"}} }
    it 'calls the move_contact_method' do
      pending "need to figure out what is the deal with remote requests"
      @controller.stub!(:current_user).and_return(@user)
      @user.should_receive(:move_contact).with(:person_id => "person_id", :from => "from_aspect_id", :to => "to_aspect_id")
      post :move_contact, opts
  describe "#update" do
    before do
      @aspect = @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 => @aspect.id, "aspect" => params)
      Aspect.find(@aspect.id).user_id.should == @user.id
    end
  end
ilya's avatar
ilya a validé

  describe "#add_to_aspect" do
    it 'adds the users to the aspect' do
      @aspect1.reload
      @aspect1.contacts.include?(@contact).should be_false
      post 'add_to_aspect', {:person_id => @user2.person.id, :aspect_id => @aspect1.id}
ilya's avatar
ilya a validé
      @aspect1.reload
      @aspect1.contacts.include?(@contact).should be_true
ilya's avatar
ilya a validé
    end
  describe "#remove_from_aspect" do
    it 'removes contacts from an aspect' do
      pending 'this needs to test with another aspect present'

      @aspect.contacts.include?(@contact).should be true
      post 'remove_from_aspect', {:person_id => @user2.person.id, :aspect_id => @aspect1.id}
      @aspect1.contacts.include?(@contact).should be false
ilya's avatar
ilya a validé
  end