diff --git a/app/contracts/messages/update_contract.rb b/app/contracts/messages/update_contract.rb index d8668405b38..101ac13a810 100644 --- a/app/contracts/messages/update_contract.rb +++ b/app/contracts/messages/update_contract.rb @@ -31,5 +31,20 @@ # TODO: This is but a stub module Messages class UpdateContract < BaseContract + validate :moving_message_to_another_forum + + private + + def moving_message_to_another_forum + return if !model.forum_id_changed? + return if model.forum_id_was.nil? + + old_forum = Forum.find_by(id: model.forum_id_was) + return if old_forum.nil? + + return if old_forum.project_id == model.forum.project_id + + errors.add(:forum_id, :cannot_move_message_to_forum_of_different_project) + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index a39ea0e44c8..ab6c149476a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1740,6 +1740,8 @@ en: not_unique: "is already in use. Please select another name." meeting: error_conflict: "Unable to save because the meeting was updated by someone else in the meantime. Please reload the page." + message: + cannot_move_message_to_forum_of_different_project: "A message cannot be moved to a forum of a different project." notifications: at_least_one_channel: "At least one channel for sending notifications needs to be specified." attributes: diff --git a/spec/contracts/messages/update_contract_spec.rb b/spec/contracts/messages/update_contract_spec.rb index 32ea07b9d78..e437ff847c6 100644 --- a/spec/contracts/messages/update_contract_spec.rb +++ b/spec/contracts/messages/update_contract_spec.rb @@ -53,4 +53,37 @@ RSpec.describe Messages::UpdateContract do end end end + + context "when moving a message to another forum" do + let(:project) { create(:project) } + let(:forum) { create(:forum, project: project) } + let(:other_forum) { create(:forum, project: project) } + let(:message) { create(:message, forum: forum) } + let(:forum_in_other_project) { create(:forum) } + + let(:current_user) { create(:user, member_with_permissions: { project => [:edit_messages] }) } + + subject(:contract) { described_class.new(message, current_user) } + + context "when moving the message to another forum in the same project" do + before do + message.forum = other_forum + end + + it "is valid" do + expect(contract).to be_valid + end + end + + context "when moving the message to a forum in another project" do + before do + message.forum = forum_in_other_project + end + + it "is invalid" do + expect(contract).not_to be_valid + expect(contract.errors[:forum_id]).to include("A message cannot be moved to a forum of a different project.") + end + end + end end