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

require 'spec_helper'

describe MessagesController, :type => :controller do
zhitomirskiyi's avatar
zhitomirskiyi a validé
  before do
Gonzalo's avatar
Gonzalo a validé
    sign_in :user, alice
zhitomirskiyi's avatar
zhitomirskiyi a validé
  end

  describe '#create' do
    before do
Gonzalo's avatar
Gonzalo a validé
      @conversation_params = {
        :author              => alice.person,
        :participant_ids     => [alice.contacts.first.person.id, alice.person.id],
        :subject             => 'cool stuff',
        :messages_attributes => [ {:author => alice.person, :text => 'stuff'} ]
zhitomirskiyi's avatar
zhitomirskiyi a validé
    end
zhitomirskiyi's avatar
zhitomirskiyi a validé
    context "on my own post" do
      before do
Gonzalo's avatar
Gonzalo a validé
        @conversation = Conversation.create!(@conversation_params)
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
      context "with a valid message" do
        before do
Gonzalo's avatar
Gonzalo a validé
          @message_params = {
            :conversation_id => @conversation.id,
            :message         => { :text => "here is something else" }
          }
        end

        it 'redirects to conversation' do
          expect {
Gonzalo's avatar
Gonzalo a validé
            post :create, @message_params
          }.to change(Message, :count).by(1)
          expect(response.status).to eq(302)
          expect(response).to redirect_to(conversations_path(:conversation_id => @conversation))

        it "dispatches the message" do
          expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, instance_of(Message))
          post :create, @message_params
        end
      end

      context "with an empty message" do
        before do
Gonzalo's avatar
Gonzalo a validé
          @message_params = {
            :conversation_id => @conversation.id,
            :message         => { :text => " " }
          }
Gonzalo's avatar
Gonzalo a validé
        it 'does not create the message' do
          expect {
Gonzalo's avatar
Gonzalo a validé
            post :create, @message_params
          }.not_to change(Message, :count)
          expect(flash[:error]).to be_present
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
    end

    context "on a post from a contact" do
      before do
Gonzalo's avatar
Gonzalo a validé
        @conversation_params[:author] = bob.person
        @conversation = Conversation.create!(@conversation_params)
        @message_params = {
          :conversation_id => @conversation.id,
          :message         => { :text => "here is something else" }
        }
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
zhitomirskiyi's avatar
zhitomirskiyi a validé
      it 'comments' do
Gonzalo's avatar
Gonzalo a validé
        post :create, @message_params
        expect(response.status).to eq(302)
        expect(response).to redirect_to(conversations_path(:conversation_id => @conversation))
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
zhitomirskiyi's avatar
zhitomirskiyi a validé
      it "doesn't overwrite author_id" do
Jonne Haß's avatar
Jonne Haß a validé
        new_user = FactoryGirl.create(:user)
Gonzalo's avatar
Gonzalo a validé
        @message_params[:author_id] = new_user.person.id.to_s

        post :create, @message_params
        created_message = Message.find_by_text(@message_params[:message][:text])
        expect(created_message.author).to eq(alice.person)
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
zhitomirskiyi's avatar
zhitomirskiyi a validé
      it "doesn't overwrite id" do
Gonzalo's avatar
Gonzalo a validé
        old_message = Message.create!(
          :text            => "hello",
          :author_id       => alice.person.id,
          :conversation_id => @conversation.id
        )
        @message_params[:id] = old_message.id

        post :create, @message_params
        expect(old_message.reload.text).to eq('hello')
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end

      it "dispatches the message" do
        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, instance_of(Message))
        post :create, @message_params
      end

      it "dispatches the message twice if the conversation author is local and it has remote users" do
        @conversation_params[:participant_ids] = [bob.person.id, alice.person.id, remote_raphael.id]
        conversation = Conversation.create!(@conversation_params)
        @message_params[:conversation_id] = conversation.id

        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(alice, instance_of(Message))
        expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch).with(
          bob, instance_of(Message), subscriber_ids: [remote_raphael.id]
        )

        post :create, @message_params
      end
zhitomirskiyi's avatar
zhitomirskiyi a validé
    end
zhitomirskiyi's avatar
zhitomirskiyi a validé
    context 'on a post from a stranger' do
      before do
Gonzalo's avatar
Gonzalo a validé
        conversation = Conversation.create!(
          :author          => eve.person,
          :participant_ids => [eve.person.id, bob.person.id]
        )
        @message_params = {
          :conversation_id => conversation.id,
          :message         => { :text => "here is something else" }
        }
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
Gonzalo's avatar
Gonzalo a validé
      it 'does not create the message' do
        expect {
Gonzalo's avatar
Gonzalo a validé
          post :create, @message_params
        }.not_to change(Message, :count)
        expect(flash[:error]).to be_present
zhitomirskiyi's avatar
zhitomirskiyi a validé
      end
    end
  end
Gonzalo's avatar
Gonzalo a validé
end