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

bug/66501 Quote message with attachment fails silently (#19962)

Kabiru Mwenja15dea72aef71
diff --git a/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb b/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb
index 388204afcee..f4a59ff12db 100644
--- a/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb
+++ b/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb
@@ -37,7 +37,10 @@ module WorkPackages
ATTACHMENT_CSS_SELECTOR = "img.op-uc-image"
def perform
- self.params = params.reverse_merge(attachment_ids: collect_attachment_ids_from_notes)
+ ids_from_notes = collect_attachment_ids_from_notes
+ claimable_ids = filter_claimable_attachment_ids(ids_from_notes)
+
+ self.params = params.reverse_merge(attachment_ids: claimable_ids)
super
end
@@ -57,6 +60,20 @@ module WorkPackages
end
end
+ def filter_claimable_attachment_ids(ids)
+ return [] if ids.blank?
+
+ # Only claim attachments that are actually claimable. We must not try to
+ # reassign attachments that are already attached to another container
+ # (e.g., the work package, or another comment), and we must only claim unattached files of
+ # the current user to satisfy validation rules.
+ Attachment
+ .where(container: nil)
+ .or(Attachment.where(container: model))
+ .where(id: ids, author: User.current)
+ .pluck(:id)
+ end
+
def parser
@parser ||= Nokogiri::HTML.fragment(model.notes)
end
diff --git a/spec/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service_spec.rb b/spec/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service_spec.rb
index 3bac1e4e67d..1b3c36e9076 100644
--- a/spec/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service_spec.rb
+++ b/spec/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service_spec.rb
@@ -31,9 +31,12 @@
require "spec_helper"
RSpec.describe WorkPackages::ActivitiesTab::CommentAttachmentsClaims::SetAttributesService do
- let(:user) { build_stubbed(:user) }
+ shared_let(:user) { create(:user) }
+ shared_let(:other_user) { create(:user) }
+ shared_let(:work_package) { create(:work_package, author: user) }
+ shared_let(:journal) { create(:work_package_journal, journable: work_package, version: 2, notes: "") }
- let(:journal) { build_stubbed(:work_package_journal, notes:) }
+ current_user { user }
subject(:set_attributes_service) do
described_class.new(
@@ -44,9 +47,13 @@ RSpec.describe WorkPackages::ActivitiesTab::CommentAttachmentsClaims::SetAttribu
end
describe "#call" do
+ before do
+ journal.update!(notes:)
+ end
+
context "when the journal notes have attachments" do
- let(:attachment1) { build_stubbed(:attachment) }
- let(:attachment2) { build_stubbed(:attachment) }
+ shared_let(:attachment1) { create(:attachment, author: user, container: nil) }
+ shared_let(:attachment2) { create(:attachment, author: user, container: nil) }
let(:notes) do
<<~HTML
@@ -57,18 +64,36 @@ RSpec.describe WorkPackages::ActivitiesTab::CommentAttachmentsClaims::SetAttribu
HTML
end
- before do
- allow(Attachment).to receive(:where)
- .with(id: [attachment1.id.to_s, attachment2.id.to_s])
- .and_return([attachment1, attachment2])
- end
-
it "sets the attachments replacements" do
expect(set_attributes_service).to be_success
expect(set_attributes_service.result.attachments_replacements).to contain_exactly(attachment1, attachment2)
end
end
+ context "when the journal notes reference containered attachments or attachments belonging to other users" do
+ shared_let(:existing_work_package_attachment) { create(:attachment, author: user, container: work_package) }
+ shared_let(:existing_journal_attachment) { create(:attachment, author: user, container: journal) }
+ shared_let(:containered_attachment_other_user) { create(:attachment, author: other_user, container: work_package) }
+ shared_let(:uncontainered_attachment_other_user) { create(:attachment, author: other_user, container: nil) }
+ shared_let(:newly_attached) { create(:attachment, author: user, container: nil) }
+
+ let(:notes) do
+ <<~HTML
+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{existing_work_package_attachment.id}/content">
+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{existing_journal_attachment.id}/content">
+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{containered_attachment_other_user.id}/content">
+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{uncontainered_attachment_other_user.id}/content">
+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{newly_attached.id}/content">
+ HTML
+ end
+
+ it "sets attachment replacements only for attachments belonging to the journal and the user" do
+ expect(set_attributes_service).to be_success
+ expect(set_attributes_service.result.attachments_replacements)
+ .to contain_exactly(existing_journal_attachment, newly_attached)
+ end
+ end
+
context "when the journal notes have no attachments" do
let(:notes) { "Lorem ipsum dolor sit amet" }