diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-16 03:08:32 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-16 03:08:32 +0300 |
commit | b0f27742e78a4aa4208c271536b6b9d84c53b49e (patch) | |
tree | be4664298b0a70efd233e23751aab518ad3e1f4e /spec/lib/gitlab/database_importers | |
parent | 600ca978129e3ccab758f57d60786cefe66ad775 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/database_importers')
-rw-r--r-- | spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb | 164 | ||||
-rw-r--r-- | spec/lib/gitlab/database_importers/self_monitoring/project/create_service_spec.rb | 50 |
2 files changed, 173 insertions, 41 deletions
diff --git a/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb b/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb new file mode 100644 index 00000000000..97f4a7eec75 --- /dev/null +++ b/spec/lib/gitlab/database_importers/instance_administrators/create_group_spec.rb @@ -0,0 +1,164 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::DatabaseImporters::InstanceAdministrators::CreateGroup do + describe '#execute' do + let(:result) { subject.execute } + + context 'without application_settings' do + it 'returns error' do + expect(subject).to receive(:log_error).and_call_original + expect(result).to eq( + status: :error, + message: 'No application_settings found', + last_step: :validate_application_settings + ) + + expect(Group.count).to eq(0) + end + end + + context 'without admin users' do + let(:application_setting) { Gitlab::CurrentSettings.current_application_settings } + + before do + allow(ApplicationSetting).to receive(:current_without_cache) { application_setting } + end + + it 'returns error' do + expect(subject).to receive(:log_error).and_call_original + expect(result).to eq( + status: :error, + message: 'No active admin user found', + last_step: :validate_admins + ) + + expect(Group.count).to eq(0) + end + end + + context 'with application settings and admin users' do + let(:group) { result[:group] } + let(:application_setting) { Gitlab::CurrentSettings.current_application_settings } + + let!(:user) { create(:user, :admin) } + + before do + allow(ApplicationSetting).to receive(:current_without_cache) { application_setting } + end + + it 'returns correct keys' do + expect(result.keys).to contain_exactly( + :status, :group + ) + end + + it "tracks successful install" do + expect(::Gitlab::Tracking).to receive(:event).with( + 'instance_administrators_group', 'group_created' + ) + + result + end + + it 'creates group' do + expect(result[:status]).to eq(:success) + expect(group).to be_persisted + expect(group.name).to eq('GitLab Instance Administrators') + expect(group.path).to start_with('gitlab-instance-administrators') + expect(group.path.split('-').last.length).to eq(8) + expect(group.visibility_level).to eq(described_class::VISIBILITY_LEVEL) + end + + it 'adds all admins as maintainers' do + admin1 = create(:user, :admin) + admin2 = create(:user, :admin) + create(:user) + + expect(result[:status]).to eq(:success) + expect(group.members.collect(&:user)).to contain_exactly(user, admin1, admin2) + expect(group.members.collect(&:access_level)).to contain_exactly( + Gitlab::Access::OWNER, + Gitlab::Access::MAINTAINER, + Gitlab::Access::MAINTAINER + ) + end + + it 'saves the group id' do + expect(result[:status]).to eq(:success) + expect(application_setting.instance_administrators_group_id).to eq(group.id) + end + + it 'returns error when saving group ID fails' do + allow(application_setting).to receive(:save) { false } + + expect(result).to eq( + status: :error, + message: 'Could not save group ID', + last_step: :save_group_id + ) + end + + context 'when group already exists' do + let(:existing_group) { create(:group) } + + before do + admin1 = create(:user, :admin) + admin2 = create(:user, :admin) + + existing_group.add_owner(user) + existing_group.add_users([admin1, admin2], Gitlab::Access::MAINTAINER) + + application_setting.instance_administrators_group_id = existing_group.id + end + + it 'returns success' do + expect(result).to eq( + status: :success, + group: existing_group + ) + + expect(Group.count).to eq(1) + end + end + + context 'when group cannot be created' do + let(:group) { build(:group) } + + before do + group.errors.add(:base, "Test error") + + expect_next_instance_of(::Groups::CreateService) do |group_create_service| + expect(group_create_service).to receive(:execute) + .and_return(group) + end + end + + it 'returns error' do + expect(subject).to receive(:log_error).and_call_original + expect(result).to eq( + status: :error, + message: 'Could not create group', + last_step: :create_group + ) + end + end + + context 'when user cannot be added to group' do + before do + subject.instance_variable_set(:@instance_admins, [user, build(:user, :admin)]) + end + + it 'returns error' do + expect(subject).to receive(:log_error).and_call_original + expect(result).to eq( + status: :error, + message: 'Could not add admins as members', + last_step: :add_group_members + ) + end + end + end + end +end diff --git a/spec/lib/gitlab/database_importers/self_monitoring/project/create_service_spec.rb b/spec/lib/gitlab/database_importers/self_monitoring/project/create_service_spec.rb index 082485f5ddd..10efdd44f20 100644 --- a/spec/lib/gitlab/database_importers/self_monitoring/project/create_service_spec.rb +++ b/spec/lib/gitlab/database_importers/self_monitoring/project/create_service_spec.rb @@ -39,11 +39,10 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do end it 'returns error' do - expect(subject).to receive(:log_error).and_call_original expect(result).to eq( status: :error, message: 'No active admin user found', - last_step: :validate_admins + last_step: :create_group ) expect(Project.count).to eq(0) @@ -78,8 +77,8 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do it_behaves_like 'has prometheus service', 'http://localhost:9090' it "tracks successful install" do - expect(::Gitlab::Tracking).to receive(:event) - expect(::Gitlab::Tracking).to receive(:event).with("self_monitoring", "project_created") + expect(::Gitlab::Tracking).to receive(:event).twice + expect(::Gitlab::Tracking).to receive(:event).with('self_monitoring', 'project_created') result end @@ -87,10 +86,6 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do it 'creates group' do expect(result[:status]).to eq(:success) expect(group).to be_persisted - expect(group.name).to eq('GitLab Instance Administrators') - expect(group.path).to start_with('gitlab-instance-administrators') - expect(group.path.split('-').last.length).to eq(8) - expect(group.visibility_level).to eq(described_class::VISIBILITY_LEVEL) end it 'creates project with internal visibility' do @@ -120,19 +115,9 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do expect(File).to exist("doc/#{path}.md") end - it 'adds all admins as maintainers' do - admin1 = create(:user, :admin) - admin2 = create(:user, :admin) - create(:user) - + it 'creates project with group as owner' do expect(result[:status]).to eq(:success) expect(project.owner).to eq(group) - expect(group.members.collect(&:user)).to contain_exactly(user, admin1, admin2) - expect(group.members.collect(&:access_level)).to contain_exactly( - Gitlab::Access::OWNER, - Gitlab::Access::MAINTAINER, - Gitlab::Access::MAINTAINER - ) end it 'saves the project id' do @@ -141,7 +126,10 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do end it 'returns error when saving project ID fails' do - allow(application_setting).to receive(:save) { false } + allow(application_setting).to receive(:update).and_call_original + allow(application_setting).to receive(:update) + .with(instance_administration_project_id: anything) + .and_return(false) expect(result).to eq( status: :error, @@ -155,12 +143,7 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do let(:existing_project) { create(:project, namespace: existing_group) } before do - admin1 = create(:user, :admin) - admin2 = create(:user, :admin) - - existing_group.add_owner(user) - existing_group.add_users([admin1, admin2], Gitlab::Access::MAINTAINER) - + application_setting.instance_administrators_group_id = existing_group.id application_setting.instance_administration_project_id = existing_project.id end @@ -272,21 +255,6 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do end end - context 'when user cannot be added to project' do - before do - subject.instance_variable_set(:@instance_admins, [user, build(:user, :admin)]) - end - - it 'returns error' do - expect(subject).to receive(:log_error).and_call_original - expect(result).to eq( - status: :error, - message: 'Could not add admins as members', - last_step: :add_group_members - ) - end - end - context 'when prometheus manual configuration cannot be saved' do let(:prometheus_settings) do { |