2025-02-11 12:27:32 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2013-09-13 08:59:44 +02:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2013-09-13 08:59:44 +02:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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
|
2013-09-16 17:59:31 +02:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2013-09-13 08:59:44 +02:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
|
2023-05-31 12:15:15 +02:00
|
|
|
RSpec.describe WorkPackages::AutoCompletesController do
|
2022-01-24 19:22:35 +01:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
let(:project) { create(:project) }
|
2018-07-06 11:11:02 +02:00
|
|
|
let(:role) do
|
2023-10-05 15:28:31 +02:00
|
|
|
create(:project_role,
|
2022-02-02 21:48:06 +01:00
|
|
|
permissions: [:view_work_packages])
|
2018-07-06 11:11:02 +02:00
|
|
|
end
|
|
|
|
|
let(:member) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:member,
|
2022-02-02 21:48:06 +01:00
|
|
|
project:,
|
|
|
|
|
principal: user,
|
|
|
|
|
roles: [role])
|
2018-07-06 11:11:02 +02:00
|
|
|
end
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:work_package1) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Can't print recipes",
|
|
|
|
|
project:)
|
2014-09-30 18:05:54 +02:00
|
|
|
end
|
|
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:work_package2) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Error when updating a recipe",
|
|
|
|
|
project:)
|
2014-09-30 18:05:54 +02:00
|
|
|
end
|
|
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:work_package3) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Lorem ipsum",
|
|
|
|
|
project:)
|
2014-09-30 18:05:54 +02:00
|
|
|
end
|
2013-09-13 08:59:44 +02:00
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
member
|
|
|
|
|
|
2014-03-29 14:50:29 +01:00
|
|
|
allow(User).to receive(:current).and_return user
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
work_package1
|
|
|
|
|
work_package2
|
|
|
|
|
work_package3
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
shared_examples_for "successful response" do
|
2013-09-13 08:59:44 +02:00
|
|
|
subject { response }
|
|
|
|
|
|
2018-11-23 15:06:32 +01:00
|
|
|
it { is_expected.to be_successful }
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
shared_examples_for "contains expected values" do
|
2013-09-13 08:59:44 +02:00
|
|
|
subject { assigns(:work_packages) }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to include(*expected_values) }
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2015-02-03 22:16:25 +01:00
|
|
|
describe "#work_packages" do
|
2014-09-30 18:08:47 +02:00
|
|
|
describe "search is case insensitive" do
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:expected_values) { [work_package1, work_package2] }
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2015-06-27 18:16:05 +02:00
|
|
|
before do
|
2014-11-03 23:43:33 +01:00
|
|
|
get :index,
|
2016-10-17 10:21:42 +02:00
|
|
|
params: {
|
|
|
|
|
project_id: project.id,
|
|
|
|
|
q: "ReCiPe"
|
2018-07-06 11:11:02 +02:00
|
|
|
},
|
|
|
|
|
format: :json
|
2015-06-27 18:16:05 +02:00
|
|
|
end
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "successful response"
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "contains expected values"
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
describe "returns work package for given id" do
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:expected_values) { work_package1 }
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2015-06-27 18:16:05 +02:00
|
|
|
before do
|
2014-11-03 23:43:33 +01:00
|
|
|
get :index,
|
2016-10-17 10:21:42 +02:00
|
|
|
params: {
|
|
|
|
|
project_id: project.id,
|
2025-02-11 12:27:32 -03:00
|
|
|
q: work_package1.id
|
2018-07-06 11:11:02 +02:00
|
|
|
},
|
|
|
|
|
format: :json
|
2015-06-27 18:16:05 +02:00
|
|
|
end
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "successful response"
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "contains expected values"
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
describe "returns work package for given ids" do
|
2014-04-10 15:03:22 +02:00
|
|
|
# this relies on all expected work packages to have ids that contain the given string
|
2025-02-11 12:27:32 -03:00
|
|
|
# we do not want to have work_package3 so we take its id + 1 to create a string
|
|
|
|
|
# we are sure to not be part of work_package3's id.
|
2014-04-10 15:03:22 +02:00
|
|
|
let(:ids) do
|
|
|
|
|
taken_ids = WorkPackage.pluck(:id).map(&:to_s)
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
id = work_package3.id + 1
|
2014-04-10 15:03:22 +02:00
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
while taken_ids.include?(id.to_s) || work_package3.id.to_s.include?(id.to_s)
|
2014-09-30 18:08:47 +02:00
|
|
|
id = id + 1
|
2014-04-10 15:03:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
id.to_s
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
let!(:expected_values) do
|
2025-02-11 12:27:32 -03:00
|
|
|
expected = [work_package1, work_package2]
|
2014-04-10 15:03:22 +02:00
|
|
|
|
|
|
|
|
WorkPackage.pluck(:id)
|
|
|
|
|
|
|
|
|
|
expected_return = []
|
|
|
|
|
expected.each do |wp|
|
|
|
|
|
new_id = wp.id.to_s + ids
|
2015-06-29 13:47:40 +02:00
|
|
|
WorkPackage.where(id: wp.id).update_all(id: new_id)
|
2014-04-10 15:03:22 +02:00
|
|
|
expected_return << WorkPackage.find(new_id)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expected_return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
get :index,
|
2016-10-17 10:21:42 +02:00
|
|
|
params: {
|
|
|
|
|
project_id: project.id,
|
|
|
|
|
q: ids
|
2018-07-06 11:11:02 +02:00
|
|
|
},
|
|
|
|
|
format: :json
|
2014-04-10 15:03:22 +02:00
|
|
|
end
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-11-03 23:43:33 +01:00
|
|
|
it_behaves_like "successful response"
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "contains expected values"
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
context "when unique" do
|
2013-09-13 08:59:44 +02:00
|
|
|
let(:assigned) { assigns(:work_packages) }
|
|
|
|
|
|
|
|
|
|
subject { assigned.size }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to eq(assigned.uniq.size) }
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
describe "returns work package for given id, escaping html" do
|
2014-02-24 18:23:33 +01:00
|
|
|
render_views
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:work_package3) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "<script>alert('danger!');</script>",
|
|
|
|
|
project:)
|
2018-07-06 11:11:02 +02:00
|
|
|
end
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:expected_values) { work_package3 }
|
2014-02-24 18:23:33 +01:00
|
|
|
|
2015-06-27 18:16:05 +02:00
|
|
|
before do
|
2014-11-03 23:43:33 +01:00
|
|
|
get :index,
|
2016-10-17 10:21:42 +02:00
|
|
|
params: {
|
|
|
|
|
project_id: project.id,
|
2025-02-11 12:27:32 -03:00
|
|
|
q: work_package3.id
|
2016-10-17 10:21:42 +02:00
|
|
|
},
|
2014-11-03 23:43:33 +01:00
|
|
|
format: :json
|
2015-06-27 18:16:05 +02:00
|
|
|
end
|
2014-02-24 18:23:33 +01:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it_behaves_like "successful response"
|
|
|
|
|
it_behaves_like "contains expected values"
|
2014-02-24 18:23:33 +01:00
|
|
|
|
2014-09-30 18:08:47 +02:00
|
|
|
it "escapes html" do
|
2014-03-29 14:50:29 +01:00
|
|
|
expect(response.body).not_to include "<script>"
|
2014-02-24 18:23:33 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-05-08 12:20:47 +03:00
|
|
|
describe "displayId JSON contract" do
|
|
|
|
|
# The CKEditor mention plugin reads `displayId` (camelCase, stringified)
|
|
|
|
|
# off each entry to insert `#PROJ-7` or `#1234` into the editor and to
|
|
|
|
|
# build the mention's link URL. The contract must hold in both modes
|
|
|
|
|
# so the frontend doesn't have to branch on identifier shape.
|
|
|
|
|
render_views
|
|
|
|
|
|
|
|
|
|
let(:expected_values) { work_package1 }
|
|
|
|
|
let(:json) { response.parsed_body }
|
|
|
|
|
let(:entry) { json.find { |e| e["id"] == work_package1.id } }
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
get :index,
|
|
|
|
|
params: { project_id: project.id, q: work_package1.id },
|
|
|
|
|
format: :json
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "in classic mode",
|
|
|
|
|
with_settings: { work_packages_identifier: "classic" } do
|
|
|
|
|
it "exposes displayId as the stringified numeric id" do
|
|
|
|
|
expect(entry).to include("displayId" => work_package1.id.to_s)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "in semantic mode",
|
|
|
|
|
with_settings: { work_packages_identifier: "semantic" } do
|
|
|
|
|
let(:project) { create(:project, identifier: "MENTPROJ") }
|
|
|
|
|
|
|
|
|
|
before { work_package1.allocate_and_register_semantic_id }
|
|
|
|
|
|
|
|
|
|
it "exposes displayId as the semantic identifier string" do
|
|
|
|
|
expect(entry["displayId"]).to start_with("MENTPROJ-")
|
|
|
|
|
expect(entry["displayId"]).to be_a(String)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-06-22 15:13:32 +02:00
|
|
|
describe "in different projects" do
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:project2) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:project,
|
2022-02-02 21:48:06 +01:00
|
|
|
parent: project)
|
2018-07-06 11:11:02 +02:00
|
|
|
end
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:expected_values) { work_package3 }
|
|
|
|
|
let(:member2) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:member,
|
2025-02-11 12:27:32 -03:00
|
|
|
project: project2,
|
2022-02-02 21:48:06 +01:00
|
|
|
principal: user,
|
|
|
|
|
roles: [role])
|
2018-07-06 11:11:02 +02:00
|
|
|
end
|
2025-02-11 12:27:32 -03:00
|
|
|
let(:work_package3) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Foo Bar Baz",
|
2025-02-11 12:27:32 -03:00
|
|
|
project: project2)
|
2014-09-30 18:05:54 +02:00
|
|
|
end
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2014-02-24 18:23:33 +01:00
|
|
|
before do
|
2025-02-11 12:27:32 -03:00
|
|
|
member2
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2025-02-11 12:27:32 -03:00
|
|
|
work_package3
|
2013-09-13 08:59:44 +02:00
|
|
|
|
2018-07-06 11:11:02 +02:00
|
|
|
get :index,
|
|
|
|
|
params: {
|
|
|
|
|
project_id: project.id,
|
2025-02-11 12:27:32 -03:00
|
|
|
q: work_package3.id
|
2018-07-06 11:11:02 +02:00
|
|
|
},
|
|
|
|
|
format: :json
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
|
2018-07-06 11:11:02 +02:00
|
|
|
it_behaves_like "successful response"
|
2015-06-22 15:13:32 +02:00
|
|
|
|
2018-07-06 11:11:02 +02:00
|
|
|
it_behaves_like "contains expected values"
|
2013-09-13 08:59:44 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|