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/attachment journalizing (#9292)

ulferts08ce0a5436d1
diff --git a/app/services/attachments/create_service.rb b/app/services/attachments/create_service.rb
index 998166c2cb9..806d8f1d273 100644
--- a/app/services/attachments/create_service.rb
+++ b/app/services/attachments/create_service.rb
@@ -27,6 +27,8 @@
#++
class Attachments::CreateService
+ include Attachments::TouchContainer
+
attr_reader :container, :author
def initialize(container, author:)
@@ -53,29 +55,23 @@ class Attachments::CreateService
def create_journalized(uploaded_file, description)
OpenProject::Mutex.with_advisory_lock_transaction(container) do
- attachment = create_attachment(uploaded_file, description)
- # Get the latest attachments to ensure having them all for journalization.
- # We just created an attachment and a different worker might have added attachments
- # in the meantime, e.g when bulk uploading.
- container.attachments.reload
-
- add_journal
- save_container
+ create_attachment(uploaded_file, description).tap do
+ # Get the latest attachments to ensure having them all for journalization.
+ # We just created an attachment and a different worker might have added attachments
+ # in the meantime, e.g when bulk uploading.
+ container.attachments.reload
- attachment
+ touch(container)
+ end
end
end
def create_unjournalized(uploaded_file, description)
create_attachment(uploaded_file, description).tap do
- save_container
+ touch(container)
end
end
- def add_journal
- container.add_journal author
- end
-
def create_attachment(uploaded_file, description)
attachment = Attachment.new(file: uploaded_file,
container: container,
@@ -89,12 +85,4 @@ class Attachments::CreateService
def build_attachment(uploaded_file, description)
container.attachments.build(file: uploaded_file, description: description, author: author)
end
-
- def save_container
- # We allow invalid containers to be saved as
- # adding the attachments does not change the validity of the container
- # but without that leeway, the user needs to fix the container before
- # the attachment can be added.
- container.save!(validate: false)
- end
end
diff --git a/app/services/attachments/delete_service.rb b/app/services/attachments/delete_service.rb
index f6d2c7e4ef4..042e2c45f5e 100644
--- a/app/services/attachments/delete_service.rb
+++ b/app/services/attachments/delete_service.rb
@@ -27,6 +27,8 @@
#++
class Attachments::DeleteService < ::BaseServices::Delete
+ include Attachments::TouchContainer
+
def call(params = nil)
in_context(model.container || model) do
perform(params)
@@ -36,10 +38,8 @@ class Attachments::DeleteService < ::BaseServices::Delete
private
def destroy(attachment)
- if attachment.container
- attachment.container.attachments.delete(attachment)
- else
- super
+ super.tap do
+ touch(attachment.container) if attachment.container
end
end
end
diff --git a/app/services/attachments/touch_container.rb b/app/services/attachments/touch_container.rb
new file mode 100644
index 00000000000..5e8f129f3e2
--- /dev/null
+++ b/app/services/attachments/touch_container.rb
@@ -0,0 +1,50 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Attachments
+ module TouchContainer
+ extend ActiveSupport::Concern
+
+ included do
+ private
+
+ def touch(container)
+ # We allow invalid containers to be saved as
+ # adding the attachments does not change the validity of the container
+ # but without that leeway, the user needs to fix the container before
+ # the attachment can be added.
+ # However we want the container to be updated when uploading an attachment. This is important,
+ # e.g. for invalidating caches and also for journalizing
+ container.update_column(:updated_at, Time.current)
+ container.save_journals if container.respond_to?(:save_journals)
+ end
+ end
+ end
+end
diff --git a/spec/services/attachments/create_service_integration_spec.rb b/spec/services/attachments/create_service_integration_spec.rb
new file mode 100644
index 00000000000..135f23d3a7f
--- /dev/null
+++ b/spec/services/attachments/create_service_integration_spec.rb
@@ -0,0 +1,173 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+
+require 'spec_helper'
+
+describe Attachments::CreateService, 'integration' do
+ let(:user) { FactoryBot.create(:user) }
+ let(:description) { 'a fancy description' }
+
+ subject { described_class.new(container, author: user) }
+
+ describe '#call' do
+ def call_tested_method
+ subject.call uploaded_file: FileHelpers.mock_uploaded_file(name: 'foobar.txt'),
+ description: description
+ end
+
+ context 'when journalized' do
+ let(:container) { FactoryBot.create(:work_package) }
+
+ shared_examples 'successful creation' do
+ it 'saves the attachment' do
+ attachment = Attachment.first
+ expect(attachment.filename).to eq 'foobar.txt'
+ expect(attachment.description).to eq description
+ end
+
+ it 'adds the attachment to the container' do
+ container.reload
+ expect(container.attachments).to include Attachment.first
+ end
+
+ it 'adds a journal entry on the container' do
+ expect(container.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
+ end
+
+ it 'updates the timestamp on the container' do
+ expect(container.reload.updated_at)
+ .not_to eql timestamp_before
+ end
+ end
+
+ context 'with a valid container' do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ call_tested_method
+ end
+
+ it_behaves_like 'successful creation'
+ end
+
+ context "with an invalid container" do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ # have an invalid work package
+ container.update_column(:subject, '')
+
+ call_tested_method
+ end
+
+ it_behaves_like 'successful creation'
+ end
+
+ context 'with an invalid attachment', with_settings: { attachment_max_size: 0 } do
+ it 'raises the exception' do
+ expect { call_tested_method }
+ .to raise_exception ActiveRecord::RecordInvalid
+ end
+
+ it 'does not create the attachment' do
+ begin
+ call_tested_method
+ rescue ActiveRecord::RecordInvalid
+ # expected
+ end
+
+ expect(Attachment.count)
+ .to eq 0
+ end
+ end
+ end
+
+ context 'when not journalized' do
+ let(:container) { FactoryBot.create(:message) }
+
+ shared_examples 'successful creation' do
+ it 'saves the attachment' do
+ attachment = Attachment.first
+ expect(attachment.filename).to eq 'foobar.txt'
+ expect(attachment.description).to eq description
+ end
+
+ it 'adds the attachment to the container' do
+ container.reload
+ expect(container.attachments).to include Attachment.first
+ end
+
+ it 'adds a journal entry on the container' do
+ expect(container.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
+ end
+
+ it 'updates the timestamp on the container' do
+ expect(container.reload.updated_at)
+ .not_to eql timestamp_before
+ end
+ end
+
+ context 'with a valid container' do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ call_tested_method
+ end
+
+ it_behaves_like 'successful creation'
+ end
+
+ context "with an invalid container" do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ container.update_column(:subject, '')
+
+ call_tested_method
+ end
+
+ it_behaves_like 'successful creation'
+ end
+ end
+
+ context "when uncontainered" do
+ let(:container) { nil }
+
+ before do
+ call_tested_method
+ end
+
+ it 'saves the attachment' do
+ attachment = Attachment.first
+ expect(attachment.filename).to eq 'foobar.txt'
+ expect(attachment.description).to eq description
+ end
+ end
+ end
+end
diff --git a/spec/services/attachments/create_service_spec.rb b/spec/services/attachments/create_service_spec.rb
deleted file mode 100644
index bebda3ca5b4..00000000000
--- a/spec/services/attachments/create_service_spec.rb
+++ /dev/null
@@ -1,114 +0,0 @@
-#-- encoding: UTF-8
-
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2020 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See docs/COPYRIGHT.rdoc for more details.
-
-require 'spec_helper'
-
-describe Attachments::CreateService do
- let(:user) { FactoryBot.create(:user) }
- let(:work_package) { FactoryBot.create(:work_package) }
- let(:container) { work_package }
- let(:description) { 'a fancy description' }
-
- subject { described_class.new(container, author: user) }
-
- describe '#call' do
- def call_tested_method
- subject.call uploaded_file: FileHelpers.mock_uploaded_file(name: 'foobar.txt'),
- description: description
- end
-
- shared_examples 'successful creation' do
- it 'saves the attachment' do
- attachment = Attachment.first
- expect(attachment.filename).to eq 'foobar.txt'
- expect(attachment.description).to eq description
- end
-
- it 'adds the attachment to the WP' do
- container.reload
- expect(container.attachments).to include Attachment.first
- end
-
- it 'adds a journal entry on the WP' do
- expect(container.journals.count).to eq 2 # 1 for WP creation + 1 for the attachment
- end
- end
-
- context 'happy path' do
- before do
- call_tested_method
- end
-
- it_behaves_like 'successful creation'
- end
-
- context "invalid container" do
- before do
- # have an invalid work package
- work_package.subject = ''
-
- call_tested_method
- end
-
- it_behaves_like 'successful creation'
- end
-
- context "uncontainered" do
- let(:container) { nil }
-
- before do
- call_tested_method
- end
-
- it 'saves the attachment' do
- attachment = Attachment.first
- expect(attachment.filename).to eq 'foobar.txt'
- expect(attachment.description).to eq description
- end
- end
-
- context 'invalid attachment', with_settings: { attachment_max_size: 0 } do
- it 'raises the exception' do
- expect { call_tested_method }
- .to raise_exception ActiveRecord::RecordInvalid
- end
-
- it 'does not create the attachment' do
- begin
- call_tested_method
- rescue ActiveRecord::RecordInvalid
- # expected
- end
-
- expect(Attachment.count)
- .to eq 0
- end
- end
- end
-end
diff --git a/spec/services/attachments/delete_service_integration_spec.rb b/spec/services/attachments/delete_service_integration_spec.rb
new file mode 100644
index 00000000000..394e6ca9637
--- /dev/null
+++ b/spec/services/attachments/delete_service_integration_spec.rb
@@ -0,0 +1,185 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+
+require 'spec_helper'
+
+describe Attachments::DeleteService, 'integration' do
+ subject(:call) { described_class.new(model: attachment, user: user).call }
+
+ let(:user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:project) { FactoryBot.create(:project) }
+ let(:attachment) { FactoryBot.create(:attachment, container: container, author: author) }
+ let(:author) { user }
+
+ describe '#call' do
+ context 'when container is journalized' do
+ let(:container) { FactoryBot.create(:work_package, project: project) }
+ let(:permissions) { %i[edit_work_packages] }
+
+ shared_examples 'successful deletion' do
+ it 'is successful' do
+ expect(call)
+ .to be_success
+ end
+
+ it 'removes the attachment' do
+ expect(Attachment.where(id: attachment.id))
+ .not_to exist
+ end
+
+ it 'adds a journal entry to the container' do
+ expect(container.journals.reload.count).to eq 3 # 1 for WP creation + 1 for adding the attachment + 1 for deletion
+ end
+
+ it 'updates the timestamp on the container' do
+ expect(container.reload.updated_at)
+ .not_to eql timestamp_before
+ end
+ end
+
+ context 'with a valid container' do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ # Force to have a journal for the attachment
+ attachment
+ container.add_journal(user)
+ container.save!
+
+ call
+ end
+
+ it_behaves_like 'successful deletion'
+ end
+
+ context "with an invalid container" do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ # Force to have a journal for the attachment
+ attachment
+ container.add_journal(user, 'Some notes')
+ container.save!
+
+ # have an invalid work package
+ container.update_column(:subject, '')
+
+ call
+ end
+
+ it_behaves_like 'successful deletion'
+ end
+ end
+
+ context 'when not journalized' do
+ let(:container) { FactoryBot.create(:message, forum: forum) }
+ let(:forum) { FactoryBot.create(:forum, project: project)}
+ let(:permissions) { %i[delete_messages edit_messages] }
+
+ shared_examples 'successful deletion' do
+ it 'is successful' do
+ expect(call)
+ .to be_success
+ end
+
+ it 'removes the attachment' do
+ expect(Attachment.where(id: attachment.id))
+ .not_to exist
+ end
+
+ it 'updates the timestamp on the container' do
+ expect(container.reload.updated_at)
+ .not_to eql timestamp_before
+ end
+ end
+
+ context 'with a valid container' do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ call
+ end
+
+ it_behaves_like 'successful deletion'
+ end
+
+ context "with an invalid container" do
+ let!(:timestamp_before) { container.updated_at }
+
+ before do
+ # have an invalid container
+ container.update_column(:subject, '')
+
+ call
+ end
+
+ it_behaves_like 'successful deletion'
+ end
+ end
+
+ context "when uncontainered" do
+ let(:container) { nil }
+ let(:permissions) { [] }
+
+ before do
+ call
+ end
+
+ context 'when the user is the attachment author' do
+ it 'is successful' do
+ expect(call)
+ .to be_success
+ end
+
+ it 'removes the attachment' do
+ expect(Attachment.where(id: attachment.id))
+ .not_to exist
+ end
+ end
+
+ context 'with the user not being the attachment author' do
+ let(:author) { FactoryBot.create(:user) }
+
+ it 'fails' do
+ expect(call)
+ .to be_failure
+ end
+
+ it 'keeps the attachment' do
+ expect(Attachment.find(attachment.id))
+ .to eql attachment
+ end
+ end
+ end
+ end
+end