OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/services/base_services/behaves_like_update_service.rb

136 lines
4.3 KiB

[35507] Allow global permission to add and edit users (#8937) * Add global permission for add_user * Rename fieldset for global roles to "Global" * Add permission to admin actions * Add index action to add_user permission * Redirect to first admin item if only one * Hide status action for non admins * Break down user form into partials for easier rendering * Disable some user form tabs for non-admins * Make users API and services conformant with endpoints * Fix references to DeleteService#deletion_allowed? * Authorize add_user on show as well * Only show invite user toolbar item with permission * Fix Delete Service spec * Fix the way user prefs are handled in service * Ensure session_id is treated as string This causes a cast error otherwise as it passes rack session locally * Fix service call on onboarding controller * Fix service call on users controller * Add delete spec for global user * Hide login attribute again when adding a new user * Render auth source correctly in simple form * Fix creating invited users through service The invitation requires the mail attribute to be present. Previously, there was a manual error added to the mail. As the errors are now determined by the contract + model, we now end up with all missing properties as errors. * Properly constraint attributes for non-admins * Add specs for global user * Start working on how to update password from UsersController that code is a mess... * Change permitted_params spec to include non-admin params * Fix create user service spec * Remove mail_notification param from users controller It's not part of the contract/params passed to user * Remove todos * Extend docs * Correct the way backlogs patches into the user settings * Remove superfluous UpdateUserService * Rewrite duplicated update service examples into common shared example * Remove duplicate password writable check * Base Users::DeleteContract on base delete contract * Move checks for active users into the UserAllowedService * Restore password writable check as it is not an attribute * Fix menus for global user * Allow global users to add custom fields * Allow global user add permission to reinvite user * Fix changed var name in update service spec * Ensure also invited or registered users can be authroized This ensure that e.g., invited users can also be set as watchers * fix typo Co-authored-by: ulferts <jens.ulferts@googlemail.com>
4 years ago
#-- encoding: UTF-8
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2021 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 docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
shared_examples 'BaseServices update service' do
let(:service_class) { described_class }
let(:namespace) { service_class.to_s.deconstantize }
let(:model_class) { namespace.singularize.constantize }
let(:contract_class) { "#{namespace}::UpdateContract".constantize }
let(:factory) { namespace.singularize.underscore }
let(:set_attributes_class) { "#{namespace}::SetAttributesService".constantize }
let(:user) { FactoryBot.build_stubbed(:user) }
let(:contract_class) do
double('contract_class', '<=': true)
end
let(:instance) do
described_class.new(user: user,
model: model_instance,
contract_class: contract_class)
end
let(:call_attributes) { { name: 'Some name', identifier: 'Some identifier' } }
let(:set_attributes_success) do
true
end
let(:set_attributes_errors) do
double('set_attributes_errors')
end
let(:set_attributes_result) do
ServiceResult.new result: model_instance,
success: set_attributes_success,
errors: set_attributes_errors
end
let!(:model_instance) { FactoryBot.build_stubbed(factory) }
let!(:set_attributes_service) do
service = double('set_attributes_service_instance')
allow(set_attributes_class)
.to receive(:new)
.with(user: user,
model: model_instance,
contract_class: contract_class,
contract_options: {})
.and_return(service)
allow(service)
.to receive(:call)
.and_return(set_attributes_result)
end
before do
allow(model_instance).to receive(:save).and_return(true)
end
subject { instance.call(call_attributes) }
describe '#user' do
it 'exposes a user which is available as a getter' do
expect(instance.user).to eql user
end
end
describe '#contract' do
it 'uses the UpdateContract contract' do
expect(instance.contract_class).to eql contract_class
end
end
describe "#call" do
context 'when the model instance is valid' do
it 'is a successful call', :aggregate_failures do
expect(subject.success?).to be_truthy
expect(subject).to eql set_attributes_result
expect(subject.result).to eql model_instance
end
end
context 'if the SetAttributeService is unsuccessful' do
let(:set_attributes_success) { false }
it 'is unsuccessful', :aggregate_failures do
expect(model_instance).not_to receive(:save)
expect(subject.success?).to be_falsey
expect(subject).to eql set_attributes_result
expect(model_instance).to_not receive(:save)
expect(subject.errors).to eql set_attributes_errors
end
end
context 'when the model instance is invalid' do
before do
expect(model_instance)
.to(receive(:save))
.and_return(false)
end
it 'is unsuccessful and returns the errors', :aggregate_failures do
expect(subject.success?).to be_falsey
expect(subject.errors).to eql model_instance.errors
end
end
end
end