Skip to content
Extraits de code Groupes Projets
Valider 2d54d9a9 rédigé par Gonzalo Rodriguez's avatar Gonzalo Rodriguez
Parcourir les fichiers

Validate presence of message text on conversations. Closes #1329.

parent 8231ce22
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -31,15 +31,17 @@ class ConversationsController < ApplicationController ...@@ -31,15 +31,17 @@ class ConversationsController < ApplicationController
message_text = params[:conversation].delete(:text) message_text = params[:conversation].delete(:text)
params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }] params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }]
if @conversation = Conversation.create(params[:conversation]) @conversation = Conversation.new(params[:conversation])
if @conversation.save
Postzord::Dispatch.new(current_user, @conversation).post Postzord::Dispatch.new(current_user, @conversation).post
flash[:notice] = I18n.t('conversations.create.sent') flash[:notice] = I18n.t('conversations.create.sent')
if params[:profile] else
redirect_to person_path(params[:profile]) flash[:error] = I18n.t('conversations.create.fail')
else end
redirect_to conversations_path(:conversation_id => @conversation.id) if params[:profile]
end redirect_to person_path(params[:profile])
else
redirect_to conversations_path(:conversation_id => @conversation.id)
end end
end end
......
...@@ -19,11 +19,10 @@ class MessagesController < ApplicationController ...@@ -19,11 +19,10 @@ class MessagesController < ApplicationController
if message.save if message.save
Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success message=#{message.id} chars=#{params[:message][:text].length}") Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success message=#{message.id} chars=#{params[:message][:text].length}")
Postzord::Dispatch.new(current_user, message).post Postzord::Dispatch.new(current_user, message).post
redirect_to conversations_path(:conversation_id => cnv.id)
else else
render :nothing => true, :status => 422 flash[:error] = I18n.t('conversations.new_message.fail')
end end
redirect_to conversations_path(:conversation_id => cnv.id)
else else
render :nothing => true, :status => 422 render :nothing => true, :status => 422
end end
......
...@@ -14,6 +14,8 @@ class Message < ActiveRecord::Base ...@@ -14,6 +14,8 @@ class Message < ActiveRecord::Base
belongs_to :author, :class_name => 'Person' belongs_to :author, :class_name => 'Person'
belongs_to :conversation, :touch => true belongs_to :conversation, :touch => true
validates_presence_of :text
after_create do after_create do
#sign comment as commenter #sign comment as commenter
self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner
......
...@@ -257,6 +257,9 @@ en: ...@@ -257,6 +257,9 @@ en:
other: "%{count} new messages" other: "%{count} new messages"
create: create:
sent: "Message sent" sent: "Message sent"
fail: "Invalid message"
new_message:
fail: "Invalid message"
destroy: destroy:
success: "Conversation successfully removed" success: "Conversation successfully removed"
......
...@@ -16,3 +16,7 @@ Feature: private messages ...@@ -16,3 +16,7 @@ Feature: private messages
And I should see "Greetings" within "#conversation_show" And I should see "Greetings" within "#conversation_show"
And "Alice Awesome" should be part of active conversation And "Alice Awesome" should be part of active conversation
And I should see "hello, alice!" within ".stream" And I should see "hello, alice!" within ".stream"
Scenario: send an empty message
Given I send a message with subject "Greetings" and text " " to "Alice Awesome"
Then I should not see "Greetings" within "#conversation_inbox"
...@@ -54,47 +54,75 @@ describe ConversationsController do ...@@ -54,47 +54,75 @@ describe ConversationsController do
end end
describe '#create' do describe '#create' do
before do context 'with a valid conversation' do
@hash = { before do
:conversation => { @hash = {
:subject => "secret stuff", :conversation => {
:text => 'text'}, :subject => "secret stuff",
:contact_ids => [alice.contacts.first.id] :text => 'text debug'
} },
end :contact_ids => [alice.contacts.first.id]
}
it 'creates a conversation' do end
lambda {
it 'creates a conversation' do
lambda {
post :create, @hash
}.should change(Conversation, :count).by(1)
end
it 'creates a message' do
lambda {
post :create, @hash
}.should change(Message, :count).by(1)
end
it 'sets the author to the current_user' do
@hash[:author] = Factory.create(:user)
post :create, @hash post :create, @hash
}.should change(Conversation, :count).by(1) Message.first.author.should == alice.person
end Conversation.first.author.should == alice.person
end
it 'creates a message' do
lambda { it 'dispatches the conversation' do
cnv = Conversation.create(
{
:author => alice.person,
:participant_ids => [alice.contacts.first.person.id, alice.person.id],
:subject => 'not spam',
:messages_attributes => [ {:author => alice.person, :text => 'cool stuff'} ]
}
)
p = Postzord::Dispatch.new(alice, cnv)
Postzord::Dispatch.stub!(:new).and_return(p)
p.should_receive(:post)
post :create, @hash post :create, @hash
}.should change(Message, :count).by(1) end
end
it 'sets the author to the current_user' do
@hash[:author] = Factory.create(:user)
post :create, @hash
Message.first.author.should == alice.person
Conversation.first.author.should == alice.person
end end
it 'dispatches the conversation' do context 'with empty text' do
cnv = Conversation.create( before do
{ @hash = {
:author => alice.person, :conversation => {
:participant_ids => [alice.contacts.first.person.id, alice.person.id], :subject => 'secret stuff',
:subject => 'not spam', :text => ' '
:messages_attributes => [ {:author => alice.person, :text => 'cool stuff'} ] },
:contact_ids => [alice.contacts.first.id]
} }
) end
p = Postzord::Dispatch.new(alice, cnv)
Postzord::Dispatch.stub!(:new).and_return(p) it 'does not create a conversation' do
p.should_receive(:post) lambda {
post :create, @hash post :create, @hash
}.should_not change(Conversation, :count).by(1)
end
it 'does not create a message' do
lambda {
post :create, @hash
}.should_not change(Message, :count).by(1)
end
end end
end end
......
...@@ -28,15 +28,34 @@ describe MessagesController do ...@@ -28,15 +28,34 @@ describe MessagesController do
context "on my own post" do context "on my own post" do
before do before do
@cnv = Conversation.create(@create_hash) @cnv = Conversation.create(@create_hash)
@message_hash = {:conversation_id => @cnv.id, :message => {:text => "here is something else"}}
end end
it 'redirects to conversation' do context "with a valid message" do
lambda{ before do
post :create, @message_hash @message_hash = {:conversation_id => @cnv.id, :message => {:text => "here is something else"}}
}.should change(Message, :count).by(1) end
response.code.should == '302'
response.should redirect_to(conversations_path(:conversation_id => @cnv)) it 'redirects to conversation' do
lambda{
post :create, @message_hash
}.should change(Message, :count).by(1)
response.code.should == '302'
response.should redirect_to(conversations_path(:conversation_id => @cnv))
end
end
context "with an empty message" do
before do
@message_hash = {:conversation_id => @cnv.id, :message => {:text => " "}}
end
it 'redirects to conversation' do
lambda{
post :create, @message_hash
}.should_not change(Message, :count).by(1)
response.code.should == '302'
response.should redirect_to(conversations_path(:conversation_id => @cnv))
end
end end
end end
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter