Skip to content
Extraits de code Groupes Projets
comments_controller_spec.rb 4,81 ko
Newer Older
  • Learn to ignore specific revisions
  • Raphael's avatar
    Raphael a validé
    #   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 CommentsController do
    
    Raphael's avatar
    Raphael a validé
      before do
    
        @aspect1 = alice.aspects.first
        @aspect2 = bob.aspects.first
    
        @controller.stub(:current_user).and_return(alice)
        sign_in :user, alice
    
    Raphael's avatar
    Raphael a validé
      end
    
      describe '#create' do
        let(:comment_hash) {
    
          {:text    =>"facebook, is that you?",
    
           :post_id =>"#{@post.id}"}
    
    Raphael's avatar
    Raphael a validé
        context "on my own post" do
          before do
    
            @post = alice.post :status_message, :text => 'GIANTS', :to => @aspect1.id
    
    Raphael's avatar
    Raphael a validé
          end
    
    Raphael's avatar
    Raphael a validé
          it 'responds to format js' do
            post :create, comment_hash.merge(:format => 'js')
            response.code.should == '201'
    
    Raphael's avatar
    Raphael a validé
            response.body.should match comment_hash[:text]
    
    Raphael's avatar
    Raphael a validé
          end
    
          it 'responds to format mobile' do
            post :create, comment_hash.merge(:format => 'mobile')
            response.should be_redirect
          end
    
    Raphael's avatar
    Raphael a validé
        end
    
        context "on a post from a contact" do
    
    Raphael's avatar
    Raphael a validé
          before do
    
            @post = bob.post :status_message, :text => 'GIANTS', :to => @aspect2.id
    
    Raphael's avatar
    Raphael a validé
          end
    
    Raphael's avatar
    Raphael a validé
          it 'comments' do
            post :create, comment_hash
            response.code.should == '201'
          end
    
            comment_hash[:author_id] = new_user.person.id.to_s
    
    Raphael's avatar
    Raphael a validé
            post :create, comment_hash
    
            Comment.find_by_text(comment_hash[:text]).author_id.should == alice.person.id
    
    Raphael's avatar
    Raphael a validé
          end
    
    Raphael's avatar
    Raphael a validé
          it "doesn't overwrite id" do
    
            old_comment = alice.comment("hello", :post => @post)
    
            comment_hash[:id] = old_comment.id
    
    Raphael's avatar
    Raphael a validé
            post :create, comment_hash
            old_comment.reload.text.should == 'hello'
          end
        end
    
    Raphael's avatar
    Raphael a validé
        context 'on a post from a stranger' do
          before do
    
            @post = eve.post :status_message, :text => 'GIANTS', :to => eve.aspects.first.id
    
    Raphael's avatar
    Raphael a validé
          end
    
    Raphael's avatar
    Raphael a validé
          it 'posts no comment' do
    
            alice.should_not_receive(:comment)
    
    Raphael's avatar
    Raphael a validé
            post :create, comment_hash
    
    Raphael's avatar
    Raphael a validé
          end
        end
      end
    
    
      describe '#destroy' do
        context 'your post' do
          before do
            @message = alice.post(:status_message, :text => "hey", :to => @aspect1.id)
    
            @comment = alice.comment("hey", :post => @message)
            @comment2 = bob.comment("hey", :post => @message)
            @comment3 = eve.comment("hey", :post => @message)
    
          it 'lets the user delete his comment' do
            alice.should_receive(:retract).with(@comment)
    
            delete :destroy, :format => "js", :post_id => 1,  :id => @comment.id
    
            response.status.should == 204
          end
    
          it "lets the user destroy other people's comments" do
            alice.should_receive(:retract).with(@comment2)
    
            delete :destroy, :format => "js", :post_id => 1,  :id => @comment2.id
    
            response.status.should == 204
          end
        end
    
        context "another user's post" do
          before do
            @message = bob.post(:status_message, :text => "hey", :to => bob.aspects.first.id)
    
            @comment = alice.comment("hey", :post => @message)
            @comment2 = bob.comment("hey", :post => @message)
            @comment3 = eve.comment("hey", :post => @message)
    
          end
    
          it 'let the user delete his comment' do
            alice.should_receive(:retract).with(@comment)
    
            delete :destroy, :format => "js", :post_id => 1,  :id => @comment.id
    
            response.status.should == 204
          end
    
          it 'does not let the user destroy comments he does not own' do
            alice.should_not_receive(:retract).with(@comment2)
    
            delete :destroy, :format => "js", :post_id => 1,  :id => @comment3.id
    
    MrZYX's avatar
    MrZYX a validé
            response.status.should == 403
    
        it 'renders nothing and 404 on a nonexistent comment' do
    
          delete :destroy, :post_id => 1, :id => 343415
    
          response.status.should == 404
          response.body.strip.should be_empty
        end
    
    
      describe '#index' do
        before do
          @message = bob.post(:status_message, :text => "hey", :to => bob.aspects.first.id)
          @comments = [alice, bob, eve].map{ |u| u.comment("hey", :post => @message) }
        end
    
        it 'works for mobile' do
          get :index, :post_id => @message.id, :format => 'mobile'
          response.should be_success
        end
    
    
        it 'returns all the comments for a post' do
          get :index, :post_id => @message.id, :format => 'js'
          assigns[:comments].should == @comments
        end
        it 'returns a 404 on a nonexistent post' do
          get :index, :post_id => 235236, :format => 'js'
          response.status.should == 404
        end
        it 'returns a 404 on a post that is not visible to the signed in user' do
          message = eve.post(:status_message, :text => "hey", :to => eve.aspects.first.id)
          bob.comment("hey", :post => @message)
          get :index, :post_id => message.id, :format => 'js'
          response.status.should == 404
        end
      end