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 deletion of messages and news

Oliver Günther169cf7fab325
diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
index a3aa5a1e57a..c016d914e06 100644
--- a/app/controllers/messages_controller.rb
+++ b/app/controllers/messages_controller.rb
@@ -137,7 +137,7 @@ class MessagesController < ApplicationController
{ action: "show", id: @message.parent, r: @message }
end
- redirect_to redirect_target
+ redirect_to redirect_target, status: :see_other
end
def quote
diff --git a/app/controllers/news/comments_controller.rb b/app/controllers/news/comments_controller.rb
index 4ba15b7df5f..ca5cf324793 100644
--- a/app/controllers/news/comments_controller.rb
+++ b/app/controllers/news/comments_controller.rb
@@ -41,11 +41,11 @@ class News::CommentsController < ApplicationController
flash[:notice] = I18n.t(:label_comment_added)
end
- redirect_to news_path(@news)
+ redirect_to news_path(@news), status: :see_other
end
def destroy
@comment.destroy
- redirect_to news_path(@news)
+ redirect_to news_path(@news), status: :see_other
end
end
diff --git a/spec/requests/messages/destroy_spec.rb b/spec/requests/messages/destroy_spec.rb
new file mode 100644
index 00000000000..1a6653683da
--- /dev/null
+++ b/spec/requests/messages/destroy_spec.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe "Messages destroy redirect",
+ :skip_csrf,
+ type: :rails_request do
+ shared_let(:project) { create(:project, enabled_module_names: %i[forums]) }
+ shared_let(:forum) { create(:forum, project:) }
+ shared_let(:message) { create(:message, forum:) }
+
+ context "when an admin deletes a message" do
+ current_user { create(:admin) }
+
+ let(:request) { delete "/topics/#{message.id}" }
+
+ subject do
+ request
+ response
+ end
+
+ it "responds with 303 See Other and redirects to the forum" do
+ expect(subject).to have_http_status(:see_other)
+ expect(response).to redirect_to(project_forum_path(project, forum))
+
+ expect { Message.find(message.id) }.to raise_error(ActiveRecord::RecordNotFound)
+ expect { Forum.find(forum.id) }.not_to raise_error
+ end
+ end
+
+ context "when deleting a reply message" do
+ shared_let(:topic) { create(:message, forum:) }
+ shared_let(:reply) { create(:message, forum:, parent: topic) }
+
+ current_user { create(:admin) }
+
+ let(:request) { delete "/topics/#{reply.id}" }
+
+ subject do
+ request
+ response
+ end
+
+ it "responds with 303 See Other and redirects to the topic" do
+ expect(subject).to have_http_status(:see_other)
+ expect(response).to redirect_to(topic_path(topic, r: reply))
+
+ expect { Message.find(reply.id) }.to raise_error(ActiveRecord::RecordNotFound)
+ expect { Message.find(topic.id) }.not_to raise_error
+ end
+ end
+end
diff --git a/spec/requests/news/comments_destroy_spec.rb b/spec/requests/news/comments_destroy_spec.rb
new file mode 100644
index 00000000000..5036007c028
--- /dev/null
+++ b/spec/requests/news/comments_destroy_spec.rb
@@ -0,0 +1,58 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe "News comments destroy redirect",
+ :skip_csrf,
+ type: :rails_request do
+ shared_let(:project) { create(:project, enabled_module_names: %i[news]) }
+ shared_let(:news) { create(:news, project:) }
+ shared_let(:comment) { create(:comment, commented: news) }
+
+ context "when an admin deletes a news comment" do
+ current_user { create(:admin) }
+
+ let(:request) { delete "/comments/#{comment.id}" }
+
+ subject do
+ request
+ response
+ end
+
+ it "responds with 303 See Other and redirects to the news page" do
+ expect(subject).to have_http_status(:see_other)
+ expect(response).to redirect_to(news_path(news))
+
+ expect { Comment.find(comment.id) }.to raise_error(ActiveRecord::RecordNotFound)
+ expect { News.find(news.id) }.not_to raise_error
+ end
+ end
+end