2019-04-11 08:17:24 -04:00
# frozen_string_literal: true
2016-03-08 19:01:33 -05:00
require 'spec_helper'
2017-07-10 10:24:02 -04:00
describe Groups :: UpdateService do
2016-12-20 11:52:27 -05:00
let! ( :user ) { create ( :user ) }
let! ( :private_group ) { create ( :group , :private ) }
let! ( :internal_group ) { create ( :group , :internal ) }
let! ( :public_group ) { create ( :group , :public ) }
2016-03-08 19:01:33 -05:00
2016-03-20 18:09:33 -04:00
describe " # execute " do
2016-03-08 19:01:33 -05:00
context " project visibility_level validation " do
context " public group with public projects " do
2016-12-20 11:52:27 -05:00
let! ( :service ) { described_class . new ( public_group , user , visibility_level : Gitlab :: VisibilityLevel :: INTERNAL ) }
2016-03-08 19:01:33 -05:00
before do
2018-08-02 10:16:58 -04:00
public_group . add_user ( user , Gitlab :: Access :: OWNER )
2017-08-02 15:55:11 -04:00
create ( :project , :public , group : public_group )
2018-08-02 10:16:58 -04:00
expect ( TodosDestroyer :: GroupPrivateWorker ) . not_to receive ( :perform_in )
2016-03-08 19:01:33 -05:00
end
2016-03-20 18:26:58 -04:00
it " does not change permission level " do
service . execute
expect ( public_group . errors . count ) . to eq ( 1 )
2018-08-02 10:16:58 -04:00
expect ( TodosDestroyer :: GroupPrivateWorker ) . not_to receive ( :perform_in )
2016-03-08 19:01:33 -05:00
end
2018-10-18 04:43:49 -04:00
it " returns false if save failed " do
allow ( public_group ) . to receive ( :save ) . and_return ( false )
expect ( service . execute ) . to be_falsey
end
2016-03-08 19:01:33 -05:00
end
2016-03-18 08:28:16 -04:00
context " internal group with internal project " do
2016-12-20 11:52:27 -05:00
let! ( :service ) { described_class . new ( internal_group , user , visibility_level : Gitlab :: VisibilityLevel :: PRIVATE ) }
2016-03-20 18:09:33 -04:00
before do
2018-08-02 10:16:58 -04:00
internal_group . add_user ( user , Gitlab :: Access :: OWNER )
2017-08-02 15:55:11 -04:00
create ( :project , :internal , group : internal_group )
2018-08-02 10:16:58 -04:00
expect ( TodosDestroyer :: GroupPrivateWorker ) . not_to receive ( :perform_in )
2016-03-20 18:09:33 -04:00
end
2016-03-20 18:26:58 -04:00
it " does not change permission level " do
service . execute
expect ( internal_group . errors . count ) . to eq ( 1 )
2016-03-08 19:01:33 -05:00
end
2016-03-20 18:09:33 -04:00
end
2018-08-02 10:16:58 -04:00
context " internal group with private project " do
let! ( :service ) { described_class . new ( internal_group , user , visibility_level : Gitlab :: VisibilityLevel :: PRIVATE ) }
before do
internal_group . add_user ( user , Gitlab :: Access :: OWNER )
create ( :project , :private , group : internal_group )
expect ( TodosDestroyer :: GroupPrivateWorker ) . to receive ( :perform_in )
2018-12-11 13:15:10 -05:00
. with ( Todo :: WAIT_FOR_DELETE , internal_group . id )
2018-08-02 10:16:58 -04:00
end
it " changes permission level to private " do
service . execute
expect ( internal_group . visibility_level )
. to eq ( Gitlab :: VisibilityLevel :: PRIVATE )
end
end
2016-03-08 19:01:33 -05:00
end
2017-03-28 07:09:44 -04:00
context " with parent_id user doesn't have permissions for " do
let ( :service ) { described_class . new ( public_group , user , parent_id : private_group . id ) }
before do
service . execute
end
it 'does not update parent_id' do
updated_group = public_group . reload
expect ( updated_group . parent_id ) . to be_nil
end
end
2016-03-08 19:01:33 -05:00
end
context " unauthorized visibility_level validation " do
2016-12-20 11:52:27 -05:00
let! ( :service ) { described_class . new ( internal_group , user , visibility_level : 99 ) }
2016-03-20 18:09:33 -04:00
before do
2018-07-11 10:36:08 -04:00
internal_group . add_user ( user , Gitlab :: Access :: MAINTAINER )
2016-03-20 18:09:33 -04:00
end
2016-03-08 19:01:33 -05:00
it " does not change permission level " do
2016-03-20 18:26:58 -04:00
service . execute
2016-03-08 19:01:33 -05:00
expect ( internal_group . errors . count ) . to eq ( 1 )
end
end
2016-12-20 11:52:27 -05:00
context 'rename group' do
2017-02-06 23:08:51 -05:00
let! ( :service ) { described_class . new ( internal_group , user , path : SecureRandom . hex ) }
2016-12-20 11:52:27 -05:00
before do
2018-07-11 10:36:08 -04:00
internal_group . add_user ( user , Gitlab :: Access :: MAINTAINER )
2017-08-02 15:55:11 -04:00
create ( :project , :internal , group : internal_group )
2016-12-20 11:52:27 -05:00
end
it 'returns true' do
expect ( service . execute ) . to eq ( true )
end
context 'error moving group' do
before do
allow ( internal_group ) . to receive ( :move_dir ) . and_raise ( Gitlab :: UpdatePathError )
end
it 'does not raise an error' do
expect { service . execute } . not_to raise_error
end
it 'returns false' do
expect ( service . execute ) . to eq ( false )
end
it 'has the right error' do
service . execute
expect ( internal_group . errors . full_messages . first ) . to eq ( 'Gitlab::UpdatePathError' )
end
2016-12-21 07:29:27 -05:00
it " hasn't changed the path " do
expect { service . execute } . not_to change { internal_group . reload . path }
end
2016-12-20 11:52:27 -05:00
end
end
2017-09-01 21:00:46 -04:00
2017-09-01 21:22:55 -04:00
context 'for a subgroup' , :nested_groups do
2017-09-01 21:00:46 -04:00
let ( :subgroup ) { create ( :group , :private , parent : private_group ) }
context 'when the parent group share_with_group_lock is enabled' do
before do
private_group . update_column ( :share_with_group_lock , true )
end
context 'for the parent group owner' do
it 'allows disabling share_with_group_lock' do
private_group . add_owner ( user )
result = described_class . new ( subgroup , user , share_with_group_lock : false ) . execute
expect ( result ) . to be_truthy
expect ( subgroup . reload . share_with_group_lock ) . to be_falsey
end
end
context 'for a subgroup owner (who does not own the parent)' do
it 'does not allow disabling share_with_group_lock' do
subgroup_owner = create ( :user )
subgroup . add_owner ( subgroup_owner )
result = described_class . new ( subgroup , subgroup_owner , share_with_group_lock : false ) . execute
expect ( result ) . to be_falsey
2017-09-06 14:31:45 -04:00
expect ( subgroup . errors . full_messages . first ) . to match ( / cannot be disabled when the parent group "Share with group lock" is enabled, except by the owner of the parent group / )
2017-09-01 21:00:46 -04:00
expect ( subgroup . reload . share_with_group_lock ) . to be_truthy
end
end
end
end
2016-03-08 19:01:33 -05:00
end