Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix topic attachment upload

ulferts70a29885d570
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
index c9d224c239d..4d4d92a9f74 100644
--- a/app/controllers/messages_controller.rb
+++ b/app/controllers/messages_controller.rb
@@ -56,7 +56,7 @@ class MessagesController < ApplicationController
.page(page)
.per_page(per_page_param)
- @reply = Message.new(subject: "RE: #{@message.subject}", parent: @topic)
+ @reply = Message.new(subject: "RE: #{@message.subject}", parent: @topic, forum: @topic.forum)
render action: 'show', layout: !request.xhr?
end
@@ -88,11 +88,11 @@ class MessagesController < ApplicationController
def reply
@topic = @message.root
- call = create_message(@forum)
+ call = create_reply(@forum, @topic)
@reply = call.result
if call.success?
- @topic.children << @reply
+ #@topic.children << @reply
call_hook(:controller_messages_reply_after_save, params: params, message: @reply)
end
@@ -162,8 +162,8 @@ class MessagesController < ApplicationController
.merge(attachment_params))
end
- def create_message(forum)
- params = permitted_params.message(forum.project)
+ def create_message(forum, message_params = permitted_params.message(forum.project))
+ params = message_params
.merge(forum: forum)
.merge(attachment_params)
@@ -172,6 +172,10 @@ class MessagesController < ApplicationController
.call(params)
end
+ def create_reply(forum, parent)
+ create_message(forum, permitted_params.reply.merge(parent: parent))
+ end
+
def attachment_params
attachment_params = permitted_params.attachments.to_h
diff --git a/app/models/message.rb b/app/models/message.rb
index d38bd37b430..132786a483d 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -97,10 +97,13 @@ class Message < ActiveRecord::Base
end
end
+ # TODO: move into create contract
def update_last_reply_in_parent
if parent
- parent.reload.update_attribute(:last_reply_id, id)
+ parent.reload
+ parent.update_attribute(:last_reply_id, id)
end
+
forum.reset_counters!
end
diff --git a/app/views/messages/_form.html.erb b/app/views/messages/_form.html.erb
index 7fce6015e82..297ac39fb00 100644
--- a/app/views/messages/_form.html.erb
+++ b/app/views/messages/_form.html.erb
@@ -28,11 +28,7 @@ See docs/COPYRIGHT.rdoc for more details.
++#%>
<%= error_messages_for 'message' %>
<% replying ||= false %>
-<% representer_object = if replying
- f.object.parent
- else
- f.object
- end %>
+<% representer_object = f.object %>
<% resource = message_attachment_representer(representer_object) %>
<div class="form--field -required">
diff --git a/spec/controllers/messages_controller_spec.rb b/spec/controllers/messages_controller_spec.rb
index 93822dac057..785e35fba67 100644
--- a/spec/controllers/messages_controller_spec.rb
+++ b/spec/controllers/messages_controller_spec.rb
@@ -45,9 +45,6 @@ describe MessagesController, type: :controller do
let(:filename) { 'testfile.txt' }
let(:file) { File.open(Rails.root.join('spec/fixtures/files', filename)) }
- let(:uploaded_file) do
- fixture_file_upload "files/#{filename}", filename
- end
before { allow(User).to receive(:current).and_return user }
@@ -71,6 +68,7 @@ describe MessagesController, type: :controller do
describe '#create' do
context 'attachments' do
+ let(:attachment) { FactoryBot.create(:attachment, container: nil, author: user) }
# see ticket #2464 on OpenProject.org
context 'new attachment on new messages' do
before do
@@ -81,8 +79,7 @@ describe MessagesController, type: :controller do
post 'create', params: { forum_id: forum.id,
message: { subject: 'Test created message',
content: 'Messsage body' },
- attachments: { '1' => { 'file' => uploaded_file,
- 'description' => '' } } }
+ attachments: { '0' => { 'id' => attachment.id } } }
end
describe '#journal' do
@@ -111,65 +108,42 @@ describe MessagesController, type: :controller do
it 'allows for changing the board' do
expect(message.reload.forum).to eq(other_forum)
end
- end
-
- describe '#attachment' do
- let!(:message) { FactoryBot.create(:message) }
- let(:attachment_id) { "attachments_#{message.attachments.first.id}" }
- let(:params) do
- { id: message.id,
- attachments: { '1' => { 'file' => uploaded_file,
- 'description' => '' } } }
- end
- describe '#add' do
- before do
- allow_any_instance_of(Message).to receive(:editable_by?).and_return(true)
-
- allow_any_instance_of(Attachment).to receive(:filename).and_return(filename)
+ context 'attachment upload' do
+ let!(:message) { FactoryBot.create(:message) }
+ let(:attachment_id) { "attachments_#{message.attachments.first.id}" }
+ # Attachment is already uploaded
+ let(:attachment) { FactoryBot.create(:attachment, container: nil, author: user) }
+ let(:params) do
+ { id: message.id,
+ attachments: { '0' => { 'id' => attachment.id } } }
end
- context 'invalid attachment' do
- let(:max_filesize) { Setting.attachment_max_size.to_i.kilobytes }
-
+ describe 'add' do
before do
- allow_any_instance_of(Attachment).to receive(:filesize).and_return(max_filesize + 1)
-
- put :update, params: params
- end
-
- describe '#view' do
- subject { response }
-
- it { is_expected.to render_template('messages/edit') }
- end
-
- describe '#error' do
- subject { assigns(:message).errors.messages }
+ allow_any_instance_of(Message).to receive(:editable_by?).and_return(true)
- it { is_expected.to have_key(:attachments) }
-
- it { subject[:attachments] =~ /too long/ }
+ allow_any_instance_of(Attachment).to receive(:filename).and_return(filename)
end
- end
- context 'journal' do
- before do
- put :update, params: params
+ context 'journal' do
+ before do
+ put :update, params: params
- message.reload
- end
+ message.reload
+ end
- describe '#key' do
- subject { message.journals.last.details }
+ describe '#key' do
+ subject { message.journals.last.details }
- it { is_expected.to have_key attachment_id }
- end
+ it { is_expected.to have_key attachment_id }
+ end
- describe '#value' do
- subject { message.journals.last.details[attachment_id].last }
+ describe '#value' do
+ subject { message.journals.last.details[attachment_id].last }
- it { is_expected.to eq(filename) }
+ it { is_expected.to eq(filename) }
+ end
end
end
end
diff --git a/spec/models/permitted_params_spec.rb b/spec/models/permitted_params_spec.rb
index dad9802e48a..bb60ddf1607 100644
--- a/spec/models/permitted_params_spec.rb
+++ b/spec/models/permitted_params_spec.rb
@@ -240,6 +240,7 @@ describe PermittedParams, type: :model do
context 'with instance passed' do
let(:instance) { double('message', project: double('project')) }
+ let(:project) { double('project') }
let(:allowed_params) do
{ 'subject' => 'value',
'content' => 'value',
@@ -253,10 +254,10 @@ describe PermittedParams, type: :model do
end
before do
- allow(user).to receive(:allowed_to?).with(:edit_messages, instance.project).and_return(true)
+ allow(user).to receive(:allowed_to?).with(:edit_messages, project).and_return(true)
end
- subject { PermittedParams.new(hash, user).message(instance).to_h }
+ subject { PermittedParams.new(hash, user).message(project).to_h }
it do
expect(subject).to eq(allowed_params)