diff options
author | Roger Meier <r.meier@siemens.com> | 2019-06-03 06:15:53 +0200 |
---|---|---|
committer | Roger Meier <r.meier@siemens.com> | 2019-06-13 08:43:14 +0200 |
commit | 35d928c4a9fe7c8545f2ad1866a45ff28c1ef5d3 (patch) | |
tree | 1fbc5564681bb15335d6fc19a9231c377170ab10 /spec | |
parent | ff22dfbf7a4f539b676101b51e5ad892d56da920 (diff) | |
download | gitlab-ce-35d928c4a9fe7c8545f2ad1866a45ff28c1ef5d3.tar.gz |
refactor: apply "require 2FA" to all subgroup and ancestor group members, when changing
Diffstat (limited to 'spec')
-rw-r--r-- | spec/models/group_spec.rb | 117 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 25 |
2 files changed, 93 insertions, 49 deletions
diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index e1c5479851c..074aec3eb07 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -603,73 +603,96 @@ describe Group do describe '#update_two_factor_requirement' do let(:user) { create(:user) } - before do - group.add_user(user, GroupMember::OWNER) - end - - it 'is called when require_two_factor_authentication is changed' do - expect_any_instance_of(User).to receive(:update_two_factor_requirement) + context 'group membership' do + before do + group.add_user(user, GroupMember::OWNER) + end - group.update!(require_two_factor_authentication: true) - end + it 'is called when require_two_factor_authentication is changed' do + expect_any_instance_of(User).to receive(:update_two_factor_requirement) - it 'is called when two_factor_grace_period is changed' do - expect_any_instance_of(User).to receive(:update_two_factor_requirement) + group.update!(require_two_factor_authentication: true) + end - group.update!(two_factor_grace_period: 23) - end + it 'is called when two_factor_grace_period is changed' do + expect_any_instance_of(User).to receive(:update_two_factor_requirement) - it 'is not called when other attributes are changed' do - expect_any_instance_of(User).not_to receive(:update_two_factor_requirement) + group.update!(two_factor_grace_period: 23) + end - group.update!(description: 'foobar') - end + it 'is not called when other attributes are changed' do + expect_any_instance_of(User).not_to receive(:update_two_factor_requirement) - def expects_other_user_to_require_two_factors(expected_calls_mysql_db = 1) - calls = 0 - allow_any_instance_of(User).to receive(:update_two_factor_requirement) do - calls += 1 + group.update!(description: 'foobar') end - group.update!(require_two_factor_authentication: true, two_factor_grace_period: 23) + it 'calls #update_two_factor_requirement on each group member' do + other_user = create(:user) + group.add_user(other_user, GroupMember::OWNER) + + calls = 0 + allow_any_instance_of(User).to receive(:update_two_factor_requirement) do + calls += 1 + end + + group.update!(require_two_factor_authentication: true, two_factor_grace_period: 23) - if Group.supports_nested_objects? expect(calls).to eq 2 - else - expect(calls).to eq expected_calls_mysql_db end end - it 'calls #update_two_factor_requirement on each group member' do - other_user = create(:user) - group.add_user(other_user, GroupMember::OWNER) + context 'sub groups and projects', :nested_groups do + it 'enables two_factor_requirement for group member' do + group.add_user(user, GroupMember::OWNER) - expects_other_user_to_require_two_factors(2) - end + group.update!(require_two_factor_authentication: true) - it 'calls #update_two_factor_requirement on each subgroup member' do - subgroup = create(:group, :nested, parent: group) - subgroup_user = create(:user) - subgroup.add_user(subgroup_user, GroupMember::OWNER) + expect(user.reload.require_two_factor_authentication_from_group).to be_truthy + end - expects_other_user_to_require_two_factors - end + context 'expanded group members', :nested_groups do + let(:indirect_user) { create(:user) } - it 'calls #update_two_factor_requirement on each child project member' do - project = create(:project, group: group) - project_user = create(:user) - project.add_developer(project_user) + it 'enables two_factor_requirement for subgroup member' do + subgroup = create(:group, :nested, parent: group) + subgroup.add_user(indirect_user, GroupMember::OWNER) - expects_other_user_to_require_two_factors(2) - end + group.update!(require_two_factor_authentication: true) + + expect(indirect_user.reload.require_two_factor_authentication_from_group).to be_truthy + end + + it 'enables two_factor_requirement for ancestor group member' do + ancestor_group = create(:group) + ancestor_group.add_user(indirect_user, GroupMember::OWNER) + group.update!(parent: ancestor_group) + + group.update!(require_two_factor_authentication: true) + + expect(indirect_user.reload.require_two_factor_authentication_from_group).to be_truthy + end + end - it 'calls #update_two_factor_requirement on each subgroups child project member' do - subgroup = create(:group, :nested, parent: group) - project = create(:project, group: subgroup) - project_user = create(:user) - project.add_developer(project_user) + context 'project members' do + it 'does not enable two_factor_requirement for child project member' do + project = create(:project, group: group) + project.add_maintainer(user) - expects_other_user_to_require_two_factors + group.update!(require_two_factor_authentication: true) + + expect(user.reload.require_two_factor_authentication_from_group).to be_falsey + end + + it 'does not enable two_factor_requirement for subgroup child project member', :nested_groups do + subgroup = create(:group, :nested, parent: group) + project = create(:project, group: subgroup) + project.add_maintainer(user) + + group.update!(require_two_factor_authentication: true) + + expect(user.reload.require_two_factor_authentication_from_group).to be_falsey + end + end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d1338e34bb8..c95bbb0b3f5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -2655,9 +2655,9 @@ describe User do end end - context 'with 2FA requirement on nested parent group', :nested_groups do + context 'with 2FA requirement from expanded groups', :nested_groups do let!(:group1) { create :group, require_two_factor_authentication: true } - let!(:group1a) { create :group, require_two_factor_authentication: false, parent: group1 } + let!(:group1a) { create :group, parent: group1 } before do group1a.add_user(user, GroupMember::OWNER) @@ -2685,6 +2685,27 @@ describe User do end end + context "with 2FA requirement from shared project's group" do + let!(:group1) { create :group, require_two_factor_authentication: true } + let!(:group2) { create :group } + let(:shared_project) { create(:project, namespace: group1) } + + before do + shared_project.project_group_links.create!( + group: group2, + group_access: ProjectGroupLink.default_access + ) + + group2.add_user(user, GroupMember::OWNER) + end + + it 'does not require 2FA' do + user.update_two_factor_requirement + + expect(user.require_two_factor_authentication_from_group).to be false + end + end + context 'without 2FA requirement on groups' do let(:group) { create :group } |