Skip to content
Extraits de code Groupes Projets
reshares_controller_spec.rb 2,95 ko
Newer Older
require 'spec_helper'

describe ResharesController, :type => :controller do
  describe '#create' do
    let(:post_request!) {
      post :create, :format => :json, :root_guid => @post_guid
    }

    before do
      @post = FactoryGirl.create(:status_message, :public => true)
      @post_guid = @post.guid
    it 'requires authentication' do
      expect(response).not_to be_success
    end

    context 'with an authenticated user' do
      before do
cmrd Senya's avatar
cmrd Senya a validé
        sign_in(bob, scope: :user)
        allow(@controller).to receive(:current_user).and_return(bob)
      end

      it 'succeeds' do
        expect(response).to be_success
      end

      it 'creates a reshare' do
        expect{
        }.to change(Reshare, :count).by(1)
      it 'calls dispatch' do
        expect(bob).to receive(:dispatch_post)

      context 'resharing a reshared post' do
        before do
          FactoryGirl.create(:reshare, :root => @post, :author => bob.person)
        end

        it 'doesn\'t allow the user to reshare the post again' do
          post_request!
          expect(response.code).to eq('422')
          expect(response.body).to eq(I18n.t("javascripts.failed_to_reshare"))

      context 'resharing another user\'s reshare' do
        before do
          @root = @post
          @post = FactoryGirl.create(:reshare, :root => @root, :author => alice.person)
        end

        it 'reshares the absolute root' do
          post_request!
          expect(@post.reshares.count).to eq(0)
          expect(@root.reshares.count).to eq(2)
        end
      end

  describe "#index" do
    context "with a private post" do
      before do
        @alices_aspect = alice.aspects.where(name: "generic").first
        @post = alice.post(:status_message, text: "hey", to: @alices_aspect.id)
      end

      it "returns a 404 for a post not visible to the user" do
        sign_in(eve, scope: :user)
        expect {
          get :index, post_id: @post.id, format: :json
        }.to raise_error(ActiveRecord::RecordNotFound)
      end

      it "returns an empty array for a post visible to the user" do
        sign_in(bob, scope: :user)
        get :index, post_id: @post.id, format: :json
        expect(JSON.parse(response.body)).to eq([])
      end
    end

    context "with a public post" do
      before do
        sign_in(alice, scope: :user)
        @post = alice.post(:status_message, text: "hey", public: true)
      end

      it "returns an array of reshares for a post" do
        bob.reshare!(@post)
        get :index, post_id: @post.id, format: :json
        expect(JSON.parse(response.body).map {|h| h["id"] }).to eq(@post.reshares.map(&:id))
      end

      it "returns an empty array for a post with no reshares" do
        get :index, post_id: @post.id, format: :json
        expect(JSON.parse(response.body)).to eq([])
      end
    end
  end