2025-05-05 09:29:55 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-02-12 17:18:55 +01:00
|
|
|
#-- copyright
|
|
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2021-02-12 17:18:55 +01: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.
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2021-02-12 17:18:55 +01:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
require "work_package"
|
|
|
|
|
|
2023-05-31 12:15:15 +02:00
|
|
|
RSpec.describe PlaceholderUsersController do
|
2022-01-24 19:22:35 +01:00
|
|
|
shared_let(:placeholder_user) { create(:placeholder_user) }
|
2021-02-12 17:18:55 +01:00
|
|
|
|
|
|
|
|
shared_examples "do not allow non-admins" do
|
|
|
|
|
it "responds with unauthorized status" do
|
|
|
|
|
expect(response).not_to be_successful
|
2024-06-24 17:35:16 +02:00
|
|
|
expect(response).to have_http_status :forbidden
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
shared_examples "renders the show template" do
|
|
|
|
|
it "renders the show template" do
|
|
|
|
|
get :show, params: { id: placeholder_user.id }
|
|
|
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
expect(response).to render_template "placeholder_users/show"
|
|
|
|
|
expect(assigns(:placeholder_user)).to be_present
|
|
|
|
|
expect(assigns(:memberships)).to be_empty
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
shared_examples "authorized flows" do
|
|
|
|
|
describe "GET new" do
|
2021-02-12 17:18:55 +01:00
|
|
|
it "renders the new template" do
|
2021-02-16 14:16:35 +01:00
|
|
|
get :new
|
|
|
|
|
|
2021-02-12 17:18:55 +01:00
|
|
|
expect(response).to be_successful
|
|
|
|
|
expect(response).to render_template "placeholder_users/new"
|
|
|
|
|
expect(assigns(:placeholder_user)).to be_present
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "GET index" do
|
|
|
|
|
it "renders the index template" do
|
2021-02-12 17:18:55 +01:00
|
|
|
get :index
|
|
|
|
|
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
expect(response).to render_template "placeholder_users/index"
|
2021-02-16 14:16:35 +01:00
|
|
|
expect(assigns(:placeholder_users)).to be_present
|
2021-02-12 17:18:55 +01:00
|
|
|
expect(assigns(:groups)).not_to be_present
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "GET show" do
|
2021-02-12 17:18:55 +01:00
|
|
|
it_behaves_like "renders the show template"
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "GET edit" do
|
2021-02-12 17:18:55 +01:00
|
|
|
it "renders the show template" do
|
2021-02-16 14:16:35 +01:00
|
|
|
get :edit, params: { id: placeholder_user.id }
|
2021-02-12 17:18:55 +01:00
|
|
|
expect(response).to be_successful
|
|
|
|
|
expect(response).to render_template "placeholder_users/edit"
|
|
|
|
|
expect(assigns(:placeholder_user)).to eql(placeholder_user)
|
|
|
|
|
expect(assigns(:membership)).to be_present
|
|
|
|
|
expect(assigns(:individual_principal)).to eql(placeholder_user)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "POST create" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "UX Developer"
|
|
|
|
|
}
|
2021-02-12 17:18:55 +01:00
|
|
|
}
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
before do
|
2021-02-12 17:18:55 +01:00
|
|
|
post :create, params:
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
context "without ee" do
|
|
|
|
|
it "returns with an error" do
|
|
|
|
|
expect { post :create, params: }.not_to change { PlaceholderUser.count }
|
2024-08-28 12:43:09 +02:00
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2023-10-23 16:47:28 +02:00
|
|
|
expect(assigns(:placeholder_user).errors.details[:base])
|
2022-10-31 19:38:37 +02:00
|
|
|
.to eq([error: :error_enterprise_only, action: "Placeholder Users"])
|
2021-02-16 21:32:30 +01:00
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
context "with ee", with_ee: %i[placeholder_users] do
|
|
|
|
|
it "is assigned their new values" do
|
|
|
|
|
user_from_db = PlaceholderUser.last
|
|
|
|
|
expect(user_from_db.name).to eq("UX Developer")
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
it "shows a success notice" do
|
|
|
|
|
expect(flash[:notice]).to eql(I18n.t(:notice_successful_create))
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
it "does not send an email" do
|
|
|
|
|
expect(ActionMailer::Base.deliveries.empty?).to be_truthy
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
context "when user chose to directly create the next placeholder user" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "UX Developer"
|
|
|
|
|
},
|
|
|
|
|
continue: true
|
2021-02-12 17:18:55 +01:00
|
|
|
}
|
2021-02-16 21:32:30 +01:00
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
it "redirects to the new page" do
|
|
|
|
|
expect(response).to redirect_to(new_placeholder_user_url)
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
context "when user chose to NOT directly create the next placeholder user" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "UX Developer"
|
|
|
|
|
}
|
2021-02-16 14:16:35 +01:00
|
|
|
}
|
2021-02-16 21:32:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "redirects to the edit page" do
|
|
|
|
|
user_from_db = PlaceholderUser.last
|
|
|
|
|
expect(response).to redirect_to(edit_placeholder_user_url(user_from_db))
|
|
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 21:32:30 +01:00
|
|
|
context "invalid params" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "x" * 300 # Name is too long
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "renders the edit form with a validation error message" do
|
|
|
|
|
expect(assigns(:placeholder_user).errors.messages[:name].first).to include("is too long")
|
|
|
|
|
expect(response).to render_template "placeholder_users/new"
|
|
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "PUT update" do
|
2021-02-12 17:18:55 +01:00
|
|
|
let(:params) do
|
|
|
|
|
{
|
2021-02-16 14:16:35 +01:00
|
|
|
id: placeholder_user.id,
|
2021-02-12 17:18:55 +01:00
|
|
|
placeholder_user: {
|
2021-02-16 14:16:35 +01:00
|
|
|
name: "UX Guru"
|
2021-02-12 17:18:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
before do
|
2021-02-12 17:18:55 +01:00
|
|
|
put :update, params:
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "redirects to the edit page" do
|
|
|
|
|
expect(response).to redirect_to(edit_placeholder_user_url(placeholder_user))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "is assigned their new values" do
|
|
|
|
|
user_from_db = PlaceholderUser.find(placeholder_user.id)
|
|
|
|
|
expect(user_from_db.name).to eq("UX Guru")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "does not send an email" do
|
|
|
|
|
expect(ActionMailer::Base.deliveries.empty?).to be_truthy
|
|
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
|
|
|
|
|
context "invalid params" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
id: placeholder_user.id,
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "x" * 300 # Name is too long
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "renders the edit form with a validation error message" do
|
|
|
|
|
expect(assigns(:placeholder_user).errors.messages[:name].first).to include("is too long")
|
|
|
|
|
expect(response).to render_template "placeholder_users/edit"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-17 10:22:14 +01:00
|
|
|
describe "GET deletion_info" do
|
|
|
|
|
before do
|
|
|
|
|
get :deletion_info, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "renders the deletion info response" do
|
|
|
|
|
expect(response).to be_successful
|
|
|
|
|
expect(response).to render_template "placeholder_users/deletion_info"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "POST destroy" do
|
|
|
|
|
before do
|
|
|
|
|
delete :destroy, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-17 10:22:14 +01:00
|
|
|
it "triggers the deletion" do
|
|
|
|
|
expect(response).to redirect_to action: :index
|
|
|
|
|
expect(flash[:info]).to include I18n.t(:notice_deletion_scheduled)
|
|
|
|
|
|
|
|
|
|
expect(Principals::DeleteJob)
|
|
|
|
|
.to(have_been_enqueued.with(placeholder_user))
|
|
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "as an admin" do
|
2023-03-07 15:04:32 +01:00
|
|
|
current_user { create(:admin) }
|
2021-02-16 21:32:30 +01:00
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
it_behaves_like "authorized flows"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "as a user with global permission" do
|
2023-09-27 17:53:04 +02:00
|
|
|
current_user { create(:user, global_permissions: %i[manage_placeholder_user]) }
|
2021-02-16 14:16:35 +01:00
|
|
|
it_behaves_like "authorized flows"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "as an unauthorized user" do
|
2023-03-07 15:04:32 +01:00
|
|
|
current_user { create(:user) }
|
2021-02-16 14:16:35 +01:00
|
|
|
|
|
|
|
|
describe "GET new" do
|
|
|
|
|
before do
|
|
|
|
|
get :new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "GET index" do
|
|
|
|
|
before do
|
|
|
|
|
get :index
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "GET show" do
|
2026-02-03 12:17:51 +01:00
|
|
|
before do
|
|
|
|
|
get :show, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it { expect(response).to have_http_status :not_found }
|
2021-02-16 14:16:35 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "GET edit" do
|
|
|
|
|
before do
|
|
|
|
|
get :edit, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "POST create" do
|
|
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
placeholder_user: {
|
|
|
|
|
name: "UX Developer"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
post :create, params:
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "PUT update" do
|
2021-02-12 17:18:55 +01:00
|
|
|
let(:params) do
|
|
|
|
|
{
|
|
|
|
|
id: placeholder_user.id,
|
|
|
|
|
placeholder_user: {
|
2021-02-16 14:16:35 +01:00
|
|
|
name: "UX Guru"
|
2021-02-12 17:18:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
before do
|
|
|
|
|
put :update, params:
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
2021-02-16 14:16:35 +01:00
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
|
|
|
|
|
2021-02-17 10:22:14 +01:00
|
|
|
describe "GET deletion_info" do
|
|
|
|
|
before do
|
|
|
|
|
get :deletion_info, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
|
|
|
|
end
|
|
|
|
|
|
2021-02-16 14:16:35 +01:00
|
|
|
describe "POST destroy" do
|
|
|
|
|
before do
|
|
|
|
|
delete :destroy, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "do not allow non-admins"
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|
2021-05-31 16:31:41 +02:00
|
|
|
|
|
|
|
|
context "as a user that may not delete the placeholder" do
|
2023-03-07 15:04:32 +01:00
|
|
|
current_user { create(:user) }
|
2021-05-31 16:31:41 +02:00
|
|
|
|
|
|
|
|
before do
|
|
|
|
|
allow(PlaceholderUsers::DeleteContract)
|
|
|
|
|
.to receive(:deletion_allowed?).and_return false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "GET deletion_info" do
|
|
|
|
|
before do
|
|
|
|
|
get :deletion_info, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "responds with unauthorized status" do
|
|
|
|
|
expect(response).not_to be_successful
|
2024-06-24 17:35:16 +02:00
|
|
|
expect(response).to have_http_status :forbidden
|
2021-05-31 16:31:41 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "POST destroy" do
|
|
|
|
|
before do
|
|
|
|
|
delete :destroy, params: { id: placeholder_user.id }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "responds with unauthorized status" do
|
|
|
|
|
expect(response).not_to be_successful
|
2024-06-24 17:35:16 +02:00
|
|
|
expect(response).to have_http_status :forbidden
|
2021-05-31 16:31:41 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2021-02-12 17:18:55 +01:00
|
|
|
end
|