Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

270 lines
9.7 KiB
Ruby
Raw Permalink Normal View History

2025-05-05 09:29:55 +02:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# 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:
2021-01-13 17:47:45 +01:00
# 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"
2023-05-31 12:15:15 +02:00
RSpec.describe ForumsController do
2026-02-02 17:51:48 +01:00
let(:permissions) { %i[view_messages] }
let(:project_role) { create(:project_role, permissions:, add_public_permissions: false) }
let(:project) { create(:project, enabled_module_names: ["forums"]) }
let(:user) { create(:user, member_with_roles: { project => project_role }) }
2022-01-24 19:22:35 +01:00
let!(:forum) { create(:forum, project:) }
before do
2026-02-02 17:51:48 +01:00
login_as(user)
end
2019-03-12 10:34:45 +01:00
describe "#index" do
2026-02-02 17:51:48 +01:00
let(:other_project) { create(:project, member_with_permissions: { user => permissions }) }
let!(:forum_in_other_project) { create(:forum, project: other_project) }
2019-03-12 10:34:45 +01:00
2026-02-02 17:51:48 +01:00
it "renders the index template with the requested forum" do
get :index, params: { project_id: project.id }
2019-03-12 10:34:45 +01:00
2026-02-02 17:51:48 +01:00
expect(response).to be_successful
expect(response).to render_template("forums/index")
expect(assigns(:forums)).to contain_exactly(forum)
expect(assigns(:project)).to eq(project)
2019-03-12 10:34:45 +01:00
end
2026-02-02 17:51:48 +01:00
context "when user does not have permission to view forums" do
let(:permissions) { [:view_project] }
2026-02-02 17:51:48 +01:00
it "renders 403 forbidden" do
get :index, params: { project_id: project.id }
expect(response).to have_http_status(:forbidden)
end
2019-03-12 10:34:45 +01:00
end
end
describe "#show" do
2026-02-02 17:51:48 +01:00
it "renders the show template with the requested forum" do
get :show, params: { project_id: project.id, id: forum.id }
expect(response).to be_successful
expect(response).to render_template("forums/show")
expect(assigns(:forum)).to eq(forum)
expect(assigns(:project)).to eq(project)
expect(assigns(:message)).to be_a_new(Message)
2019-03-12 10:34:45 +01:00
end
2026-02-02 17:51:48 +01:00
context "when user does not have permission to view messages" do
let(:permissions) { [:view_project] }
2026-02-02 17:51:48 +01:00
it "renders 403 forbidden" do
get :show, params: { project_id: project.id, id: forum.id }
expect(response).to have_http_status(:forbidden)
end
2019-03-12 10:34:45 +01:00
end
2026-02-02 17:51:48 +01:00
describe "with some messages messages" do
let!(:message1) { create(:message, forum:, updated_at: 1.minute.ago) }
let!(:message2) { create(:message, forum:, updated_at: 4.minutes.ago) }
let!(:sticked_message1) do
create(:message, forum_id: forum.id,
subject: "How to",
content: "How to install this cool app",
sticky: true,
updated_at: 2.minutes.ago,
sticked_on: 2.minutes.ago)
end
2026-02-02 17:51:48 +01:00
let!(:sticked_message2) do
create(:message, forum_id: forum.id,
subject: "FAQ",
content: "Frequestly asked question",
sticky: true,
updated_at: 10.minutes.ago,
sticked_on: 10.minutes.ago)
end
2026-02-02 17:51:48 +01:00
it "displays the messages in the correct order, moving stickies to the top" do
get :show, params: { project_id: project.id, id: forum.id }
2026-02-02 17:51:48 +01:00
expect(assigns(:topics)).to eq([
sticked_message2,
sticked_message1,
message1,
message2
])
end
2026-02-02 17:51:48 +01:00
context "when requesting JSON format" do
it "renders the messages in the correct order as JSON" do
# JSON rendering was disfunctional because the template does not exist
2026-02-02 17:51:48 +01:00
expect do
get :show, params: { project_id: project.id, id: forum.id }, format: :json
end.to raise_error(ActionController::UnknownFormat)
end
end
2026-02-02 17:51:48 +01:00
context "when requesting ATOM format" do
it "renders the messages in the correct order as ATOM" do
get :show, params: { project_id: project.id, id: forum.id }, format: :atom
expect(response).to be_successful
expect(response.content_type).to eq("application/atom+xml; charset=utf-8")
expect(assigns(:messages)).to eq([
sticked_message2,
sticked_message1,
message1,
message2
])
end
end
end
end
2013-10-31 08:10:13 +01:00
2026-02-02 17:51:48 +01:00
describe "#create" do
let(:params) { { project_id: project.id, forum: forum_params } }
2019-03-12 10:34:45 +01:00
let(:forum_params) { { name: "my forum", description: "awesome forum" } }
2026-02-02 17:51:48 +01:00
context "when the user is not allowed to manage forums" do
it "renders 403 forbidden" do
post :create, params: params
expect(response).to have_http_status(:forbidden)
2019-03-12 10:34:45 +01:00
end
end
2026-02-02 17:51:48 +01:00
context "when the user is allowed to manage forums" do
let(:permissions) { %i[view_messages manage_forums] }
2013-10-31 08:10:13 +01:00
2026-02-02 17:51:48 +01:00
describe "with valid params" do
it "creates a new forum and redirects to index" do
expect do
post :create, params:
end.to change(Forum, :count).by(1)
2013-10-31 08:10:13 +01:00
2026-02-02 17:51:48 +01:00
expect(response).to redirect_to project_forums_path(project)
expect(flash[:notice]).to eq(I18n.t(:notice_successful_create))
end
2013-10-31 08:10:13 +01:00
end
2026-02-02 17:51:48 +01:00
describe "with invalid params" do
let(:forum_params) { { name: "", description: "awesome forum" } }
2013-10-31 08:10:13 +01:00
2026-02-02 17:51:48 +01:00
it "renders the new template" do
expect do
post :create, params:
end.not_to change(Forum, :count)
2013-10-31 08:10:13 +01:00
2026-02-02 17:51:48 +01:00
expect(response).to render_template("new")
end
end
2013-10-31 08:10:13 +01:00
end
end
2026-02-02 17:51:48 +01:00
describe "#destroy" do
context "when the user is not allowed to manage forums" do
it "renders 403 forbidden" do
delete :destroy, params: { project_id: project.id, id: forum.id }
expect(response).to have_http_status(:forbidden)
end
end
2026-02-02 17:51:48 +01:00
context "when the user is allowed to manage forums" do
let(:permissions) { %i[view_messages manage_forums] }
2026-02-02 17:51:48 +01:00
it "deletes the forum and redirects to index" do
expect do
delete :destroy, params: { project_id: project.id, id: forum.id }
end.to change(Forum, :count).by(-1)
2026-02-02 17:51:48 +01:00
expect(response).to redirect_to project_forums_path(project)
2026-04-22 16:44:50 +02:00
expect(response).to have_http_status(:see_other)
2026-02-02 17:51:48 +01:00
expect(flash[:notice]).to eq(I18n.t(:notice_successful_delete))
end
end
end
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
describe "#move" do
let!(:forum) { create(:forum, project: project, position: 1) }
let!(:forum2) { create(:forum, project: project, position: 2) }
let!(:forum3) { create(:forum, project: project, position: 3) }
context "when the user is not allowed to manage forums" do
it "renders 403 forbidden" do
post :move, params: { project_id: project.id, id: forum3.id, forum: { move_to: "higher" } }
expect(response).to have_http_status(:forbidden)
end
2021-02-11 16:02:18 +01:00
end
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
context "when the user is allowed to manage forums" do
let(:permissions) { %i[view_messages manage_forums] }
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
it "moves the forum and redirects to index" do
post :move, params: { project_id: project.id, id: forum3.id, forum: { move_to: "higher" } }
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
expect(response).to redirect_to project_forums_path(project)
expect(flash[:notice]).to eq(I18n.t(:notice_successful_update))
2026-02-02 17:51:48 +01:00
expect(forum.reload.position).to eq(1)
expect(forum2.reload.position).to eq(3)
expect(forum3.reload.position).to eq(2)
2014-03-17 17:44:17 +02:00
end
end
2026-02-02 17:51:48 +01:00
end
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
describe "#update" do
context "when the user is not allowed to manage forums" do
it "renders 403 forbidden" do
patch :update, params: { project_id: project.id, id: forum.id, forum: { name: "Updated Forum" } }
expect(response).to have_http_status(:forbidden)
2014-03-17 17:44:17 +02:00
end
2026-02-02 17:51:48 +01:00
end
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
context "when the user is allowed to manage forums" do
let(:permissions) { %i[view_messages manage_forums] }
describe "with valid params" do
it "updates the forum and redirects to index" do
patch :update, params: { project_id: project.id, id: forum.id, forum: { name: "Updated Forum" } }
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
expect(response).to redirect_to project_forums_path(project)
expect(flash[:notice]).to eq(I18n.t(:notice_successful_update))
expect(forum.reload.name).to eq("Updated Forum")
2014-03-17 17:44:17 +02:00
end
end
2026-02-02 17:51:48 +01:00
describe "with invalid params" do
it "renders the edit template" do
expect do
patch :update, params: { project_id: project.id, id: forum.id, forum: { name: "" } }
end.not_to change { forum.reload.name }
2014-03-17 17:44:17 +02:00
2026-02-02 17:51:48 +01:00
expect(response).to render_template("edit")
2014-03-17 17:44:17 +02:00
end
end
end
end
end