Skip to content
Extraits de code Groupes Projets
comments_controller_spec.rb 5,23 ko
Newer Older
danielgrippi's avatar
danielgrippi a validé
#   Copyright (c) 2010-2011, Diaspora Inc.  This file is
Raphael's avatar
Raphael a validé
#   licensed under the Affero General Public License version 3 or later.  See
#   the COPYRIGHT file.

require 'spec_helper'

describe CommentsController, :type => :controller do
Raphael's avatar
Raphael a validé
  before do
    allow(@controller).to receive(:current_user).and_return(alice)
cmrd Senya's avatar
cmrd Senya a validé
    sign_in alice, scope: :user
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é
    }
Raphael's avatar
Raphael a validé
    context "on my own post" do
      before do
        aspect_to_post = alice.aspects.where(:name => "generic").first
        @post = alice.post :status_message, :text => 'GIANTS', :to => aspect_to_post
Raphael's avatar
Raphael a validé
      end
      it 'responds to format json' do
        post :create, comment_hash.merge(:format => 'json')
        expect(response.code).to eq('201')
        expect(response.body).to match comment_hash[:text]
Raphael's avatar
Raphael a validé
      end
      it 'responds to format mobile' do
        post :create, comment_hash.merge(:format => 'mobile')
        expect(response).to be_success
Raphael's avatar
Raphael a validé
    end
    context "on a post from a contact" do
Raphael's avatar
Raphael a validé
      before do
        aspect_to_post = bob.aspects.where(:name => "generic").first
        @post = bob.post :status_message, :text => 'GIANTS', :to => aspect_to_post
Raphael's avatar
Raphael a validé
      end
Raphael's avatar
Raphael a validé
      it 'comments' do
        post :create, comment_hash
        expect(response.code).to eq('201')
Raphael's avatar
Raphael a validé
      end
      it "doesn't overwrite author_id" do
Jonne Haß's avatar
Jonne Haß a validé
        new_user = FactoryGirl.create(:user)
        comment_hash[:author_id] = new_user.person.id.to_s
Raphael's avatar
Raphael a validé
        post :create, comment_hash
        expect(Comment.find_by_text(comment_hash[:text]).author_id).to eq(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!(@post, "hello")
        comment_hash[:id] = old_comment.id
Raphael's avatar
Raphael a validé
        post :create, comment_hash
        expect(old_comment.reload.text).to eq('hello')
Raphael's avatar
Raphael a validé
      end
    end
    it 'posts no comment on a post from a stranger' do
      aspect_to_post = eve.aspects.where(:name => "generic").first
      @post = eve.post :status_message, :text => 'GIANTS', :to => aspect_to_post
      expect(alice).not_to receive(:comment)
      post :create, comment_hash
      expect(response.code).to eq("404")
      expect(response.body).to eq(I18n.t("javascripts.failed_to_comment"))
Raphael's avatar
Raphael a validé
    end
  end

  describe '#destroy' do
    before do
      aspect_to_post = bob.aspects.where(:name => "generic").first
      @message = bob.post(:status_message, :text => "hey", :to => aspect_to_post)
    end

    context 'your post' do
      before do
        allow(@controller).to receive(:current_user).and_return(bob)
cmrd Senya's avatar
cmrd Senya a validé
        sign_in bob, scope: :user
      it 'lets the user delete his comment' do
        comment = bob.comment!(@message, "hey")
        expect(bob).to receive(:retract).with(comment)
        delete :destroy, :format => "js", :post_id => 1, :id => comment.id
        expect(response.status).to eq(204)
      end

      it "lets the user destroy other people's comments" do
        comment = alice.comment!(@message, "hey")
        expect(bob).to receive(:retract).with(comment)
        delete :destroy, :format => "js", :post_id => 1, :id => comment.id
        expect(response.status).to eq(204)
      end
    end

    context "another user's post" do
      it 'let the user delete his comment' do
        comment = alice.comment!(@message, "hey")
        expect(alice).to receive(:retract).with(comment)
        delete :destroy, :format => "js", :post_id => 1,  :id => comment.id
        expect(response.status).to eq(204)
      end

      it 'does not let the user destroy comments he does not own' do
        comment1 = bob.comment!(@message, "hey")
        comment2 = eve.comment!(@message, "hey")
        expect(alice).not_to receive(:retract).with(comment1)
        delete :destroy, :format => "js", :post_id => 1,  :id => comment2.id
        expect(response.status).to eq(403)
    it 'renders nothing and 404 on a nonexistent comment' do
      delete :destroy, :post_id => 1, :id => 343415
      expect(response.status).to eq(404)
      expect(response.body.strip).to be_empty
      aspect_to_post = bob.aspects.where(:name => "generic").first
      @message = bob.post(:status_message, :text => "hey", :to => aspect_to_post.id)
    it 'works for mobile' do
      get :index, :post_id => @message.id, :format => 'mobile'
      expect(response).to be_success
    it 'returns all the comments for a post' do
      comments = [alice, bob, eve].map{ |u| u.comment!(@message, "hey") }
      get :index, :post_id => @message.id, :format => :json
Benjamin Neff's avatar
Benjamin Neff a validé
      expect(JSON.parse(response.body).map {|comment| comment["id"] }).to match_array(comments.map(&:id))
    it 'returns a 404 on a nonexistent post' do
      get :index, :post_id => 235236, :format => :json
      expect(response.status).to eq(404)
    it 'returns a 404 on a post that is not visible to the signed in user' do
      aspect_to_post = eve.aspects.where(:name => "generic").first
      message = eve.post(:status_message, :text => "hey", :to => aspect_to_post.id)
      bob.comment!(@message, "hey")
      get :index, :post_id => message.id, :format => :json
      expect(response.status).to eq(404)