Newer
Older
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
describe MessagesController, :type => :controller do
@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'} ]
@conversation = Conversation.create!(@conversation_params)
context "with a valid message" do
before do
@message_params = {
:conversation_id => @conversation.id,
:message => { :text => "here is something else" }
}
end
it 'redirects to conversation' do
}.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
@message_params = {
:conversation_id => @conversation.id,
:message => { :text => " " }
}
end
}.not_to change(Message, :count)
expect(flash[:error]).to be_present
end
end
context "on a post from a contact" do
before do
@conversation_params[:author] = bob.person
@conversation = Conversation.create!(@conversation_params)
@message_params = {
:conversation_id => @conversation.id,
:message => { :text => "here is something else" }
}
expect(response.status).to eq(302)
expect(response).to redirect_to(conversations_path(:conversation_id => @conversation))
@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)
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')
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
context 'on a post from a stranger' do
before do
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" }
}
}.not_to change(Message, :count)
expect(flash[:error]).to be_present