diff options
Diffstat (limited to 'spec/controllers/admin')
6 files changed, 112 insertions, 350 deletions
diff --git a/spec/controllers/admin/application_settings_controller_spec.rb b/spec/controllers/admin/application_settings_controller_spec.rb index a18ebe9c9a0..4a92911f914 100644 --- a/spec/controllers/admin/application_settings_controller_spec.rb +++ b/spec/controllers/admin/application_settings_controller_spec.rb @@ -66,6 +66,26 @@ RSpec.describe Admin::ApplicationSettingsController, :do_not_mock_admin_mode_set sign_in(admin) end + context 'when there are recent ServicePing reports' do + it 'attempts to use prerecorded data' do + create(:raw_usage_data) + + expect(Gitlab::Usage::ServicePingReport).not_to receive(:for) + + get :usage_data, format: :json + end + end + + context 'when there are NO recent ServicePing reports' do + it 'calculates data on the fly' do + allow(Gitlab::Usage::ServicePingReport).to receive(:for).and_call_original + + get :usage_data, format: :json + + expect(Gitlab::Usage::ServicePingReport).to have_received(:for) + end + end + it 'returns HTML data' do get :usage_data, format: :html @@ -331,6 +351,17 @@ RSpec.describe Admin::ApplicationSettingsController, :do_not_mock_admin_mode_set end end end + + context 'pipeline creation rate limiting' do + let(:application_settings) { ApplicationSetting.current } + + it 'updates pipeline_limit_per_project_user_sha setting' do + put :update, params: { application_setting: { pipeline_limit_per_project_user_sha: 25 } } + + expect(response).to redirect_to(general_admin_application_settings_path) + expect(application_settings.reload.pipeline_limit_per_project_user_sha).to eq(25) + end + end end describe 'PUT #reset_registration_token' do @@ -368,4 +399,37 @@ RSpec.describe Admin::ApplicationSettingsController, :do_not_mock_admin_mode_set expect(response).to redirect_to("https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf") end end + + describe 'GET #service_usage_data' do + before do + stub_usage_data_connections + stub_database_flavor_check + sign_in(admin) + end + + it 'assigns truthy value if there are recent ServicePing reports in database' do + create(:raw_usage_data) + + get :service_usage_data, format: :html + + expect(assigns(:service_ping_data_present)).to be_truthy + expect(response).to have_gitlab_http_status(:ok) + end + + it 'assigns truthy value if there are recent ServicePing reports in cache', :use_clean_rails_memory_store_caching do + Rails.cache.write('usage_data', true) + + get :service_usage_data, format: :html + + expect(assigns(:service_ping_data_present)).to be_truthy + expect(response).to have_gitlab_http_status(:ok) + end + + it 'assigns falsey value if there are NO recent ServicePing reports' do + get :service_usage_data, format: :html + + expect(assigns(:service_ping_data_present)).to be_falsey + expect(response).to have_gitlab_http_status(:ok) + end + end end diff --git a/spec/controllers/admin/clusters_controller_spec.rb b/spec/controllers/admin/clusters_controller_spec.rb index fed9d2e8588..ca2b50b529c 100644 --- a/spec/controllers/admin/clusters_controller_spec.rb +++ b/spec/controllers/admin/clusters_controller_spec.rb @@ -102,87 +102,6 @@ RSpec.describe Admin::ClustersController do end end - describe 'GET #new' do - let(:user) { admin } - - def go(provider: 'gcp') - get :new, params: { provider: provider } - end - - include_examples ':certificate_based_clusters feature flag controller responses' do - let(:subject) { go } - end - - describe 'functionality for new cluster' do - context 'when omniauth has been configured' do - let(:key) { 'secret-key' } - let(:session_key_for_redirect_uri) do - GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(key) - end - - context 'when selected provider is gke and no valid gcp token exists' do - it 'redirects to gcp authorize_url' do - go - - expect(response).to redirect_to(assigns(:authorize_url)) - end - end - end - - context 'when omniauth has not configured' do - before do - stub_omniauth_setting(providers: []) - end - - it 'does not have authorize_url' do - go - - expect(assigns(:authorize_url)).to be_nil - end - end - - context 'when access token is valid' do - before do - stub_google_api_validate_token - end - - it 'has new object' do - go - - expect(assigns(:gcp_cluster)).to be_an_instance_of(Clusters::ClusterPresenter) - end - end - - context 'when access token is expired' do - before do - stub_google_api_expired_token - end - - it { expect(@valid_gcp_token).to be_falsey } - end - - context 'when access token is not stored in session' do - it { expect(@valid_gcp_token).to be_falsey } - end - end - - describe 'functionality for existing cluster' do - it 'has new object' do - go - - expect(assigns(:user_cluster)).to be_an_instance_of(Clusters::ClusterPresenter) - end - end - - include_examples 'GET new cluster shared examples' - - describe 'security' do - it { expect { go }.to be_allowed_for(:admin) } - it { expect { go }.to be_denied_for(:user) } - it { expect { go }.to be_denied_for(:external) } - end - end - it_behaves_like 'GET #metrics_dashboard for dashboard', 'Cluster health' do let(:cluster) { create(:cluster, :instance, :provided_by_gcp) } @@ -216,164 +135,6 @@ RSpec.describe Admin::ClustersController do end end - describe 'POST #create_gcp' do - let(:legacy_abac_param) { 'true' } - let(:params) do - { - cluster: { - name: 'new-cluster', - provider_gcp_attributes: { - gcp_project_id: 'gcp-project-12345', - legacy_abac: legacy_abac_param - } - } - } - end - - def post_create_gcp - post :create_gcp, params: params - end - - include_examples ':certificate_based_clusters feature flag controller responses' do - let(:subject) { post_create_gcp } - end - - describe 'functionality' do - context 'when access token is valid' do - before do - stub_google_api_validate_token - end - - it 'creates a new cluster' do - expect(ClusterProvisionWorker).to receive(:perform_async) - expect { post_create_gcp }.to change { Clusters::Cluster.count } - .and change { Clusters::Providers::Gcp.count } - - cluster = Clusters::Cluster.instance_type.first - - expect(response).to redirect_to(admin_cluster_path(cluster)) - expect(cluster).to be_gcp - expect(cluster).to be_kubernetes - expect(cluster.provider_gcp).to be_legacy_abac - end - - context 'when legacy_abac param is false' do - let(:legacy_abac_param) { 'false' } - - it 'creates a new cluster with legacy_abac_disabled' do - expect(ClusterProvisionWorker).to receive(:perform_async) - expect { post_create_gcp }.to change { Clusters::Cluster.count } - .and change { Clusters::Providers::Gcp.count } - expect(Clusters::Cluster.instance_type.first.provider_gcp).not_to be_legacy_abac - end - end - end - - context 'when access token is expired' do - before do - stub_google_api_expired_token - end - - it { expect(@valid_gcp_token).to be_falsey } - end - - context 'when access token is not stored in session' do - it { expect(@valid_gcp_token).to be_falsey } - end - end - - describe 'security' do - before do - allow_next_instance_of(described_class) do |instance| - allow(instance).to receive(:token_in_session).and_return('token') - allow(instance).to receive(:expires_at_in_session).and_return(1.hour.since.to_i.to_s) - end - allow_next_instance_of(GoogleApi::CloudPlatform::Client) do |instance| - allow(instance).to receive(:projects_zones_clusters_create) do - double( - 'instance', - self_link: 'projects/gcp-project-12345/zones/us-central1-a/operations/ope-123', - status: 'RUNNING' - ) - end - end - - allow(WaitForClusterCreationWorker).to receive(:perform_in).and_return(nil) - end - - it { expect { post_create_gcp }.to be_allowed_for(:admin) } - it { expect { post_create_gcp }.to be_denied_for(:user) } - it { expect { post_create_gcp }.to be_denied_for(:external) } - end - end - - describe 'POST #create_aws' do - let(:params) do - { - cluster: { - name: 'new-cluster', - provider_aws_attributes: { - key_name: 'key', - role_arn: 'arn:role', - region: 'region', - vpc_id: 'vpc', - instance_type: 'instance type', - num_nodes: 3, - security_group_id: 'security group', - subnet_ids: %w(subnet1 subnet2) - } - } - } - end - - def post_create_aws - post :create_aws, params: params - end - - include_examples ':certificate_based_clusters feature flag controller responses' do - let(:subject) { post_create_aws } - end - - it 'creates a new cluster' do - expect(ClusterProvisionWorker).to receive(:perform_async) - expect { post_create_aws }.to change { Clusters::Cluster.count } - .and change { Clusters::Providers::Aws.count } - - cluster = Clusters::Cluster.instance_type.first - - expect(response).to have_gitlab_http_status(:created) - expect(response.location).to eq(admin_cluster_path(cluster)) - expect(cluster).to be_aws - expect(cluster).to be_kubernetes - end - - context 'params are invalid' do - let(:params) do - { - cluster: { name: '' } - } - end - - it 'does not create a cluster' do - expect { post_create_aws }.not_to change { Clusters::Cluster.count } - - expect(response).to have_gitlab_http_status(:unprocessable_entity) - expect(response.media_type).to eq('application/json') - expect(response.body).to include('is invalid') - end - end - - describe 'security' do - before do - allow(WaitForClusterCreationWorker).to receive(:perform_in) - end - - it { expect { post_create_aws }.to be_allowed_for(:admin) } - it { expect { post_create_aws }.to be_denied_for(:user) } - it { expect { post_create_aws }.to be_denied_for(:external) } - end - end - describe 'POST #create_user' do let(:params) do { diff --git a/spec/controllers/admin/groups_controller_spec.rb b/spec/controllers/admin/groups_controller_spec.rb index d9b7e00fd75..fb843ac6a7a 100644 --- a/spec/controllers/admin/groups_controller_spec.rb +++ b/spec/controllers/admin/groups_controller_spec.rb @@ -51,7 +51,7 @@ RSpec.describe Admin::GroupsController do it 'adds user to members', :aggregate_failures, :snowplow do put :members_update, params: { id: group, - user_ids: group_user.id, + user_id: group_user.id, access_level: Gitlab::Access::GUEST } @@ -70,7 +70,7 @@ RSpec.describe Admin::GroupsController do it 'can add unlimited members', :aggregate_failures do put :members_update, params: { id: group, - user_ids: 1.upto(1000).to_a.join(','), + user_id: 1.upto(1000).to_a.join(','), access_level: Gitlab::Access::GUEST } @@ -81,7 +81,7 @@ RSpec.describe Admin::GroupsController do it 'adds no user to members', :aggregate_failures do put :members_update, params: { id: group, - user_ids: '', + user_id: '', access_level: Gitlab::Access::GUEST } diff --git a/spec/controllers/admin/requests_profiles_controller_spec.rb b/spec/controllers/admin/requests_profiles_controller_spec.rb deleted file mode 100644 index 7ee46b5b28a..00000000000 --- a/spec/controllers/admin/requests_profiles_controller_spec.rb +++ /dev/null @@ -1,72 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Admin::RequestsProfilesController do - let_it_be(:admin) { create(:admin) } - - before do - sign_in(admin) - end - - describe '#show' do - let(:tmpdir) { Dir.mktmpdir('profiler-test') } - let(:test_file) { File.join(tmpdir, basename) } - - subject do - get :show, params: { name: basename } - end - - before do - stub_const('Gitlab::RequestProfiler::PROFILES_DIR', tmpdir) - File.write(test_file, sample_data) - end - - after do - FileUtils.rm_rf(tmpdir) - end - - context 'when loading HTML profile' do - let(:basename) { "profile_#{Time.current.to_i}_execution.html" } - - let(:sample_data) do - '<html> <body> <h1>Heading</h1> <p>paragraph.</p> </body> </html>' - end - - it 'renders the data' do - subject - - expect(response).to have_gitlab_http_status(:ok) - expect(response.body).to eq(sample_data) - end - end - - context 'when loading TXT profile' do - let(:basename) { "profile_#{Time.current.to_i}_memory.txt" } - - let(:sample_data) do - <<~TXT - Total allocated: 112096396 bytes (1080431 objects) - Total retained: 10312598 bytes (53567 objects) - TXT - end - - it 'renders the data' do - subject - - expect(response).to have_gitlab_http_status(:ok) - expect(response.body).to eq(sample_data) - end - end - - context 'when loading PDF profile' do - let(:basename) { "profile_#{Time.current.to_i}_anything.pdf" } - - let(:sample_data) { 'mocked pdf content' } - - it 'fails to render the data' do - expect { subject }.to raise_error(ActionController::UrlGenerationError, /No route matches.*unmatched constraints:/) - end - end - end -end diff --git a/spec/controllers/admin/runners_controller_spec.rb b/spec/controllers/admin/runners_controller_spec.rb index 8f70cb32d3e..fea59969400 100644 --- a/spec/controllers/admin/runners_controller_spec.rb +++ b/spec/controllers/admin/runners_controller_spec.rb @@ -26,27 +26,12 @@ RSpec.describe Admin::RunnersController do describe '#show' do render_views - let_it_be(:project) { create(:project) } - - before_all do - create(:ci_build, runner: runner, project: project) - end - it 'shows a runner show page' do get :show, params: { id: runner.id } expect(response).to have_gitlab_http_status(:ok) expect(response).to render_template(:show) end - - it 'when runner_read_only_admin_view is off, redirects to the runner edit page' do - stub_feature_flags(runner_read_only_admin_view: false) - - get :show, params: { id: runner.id } - - expect(response).to have_gitlab_http_status(:redirect) - expect(response).to redirect_to edit_admin_runner_path(runner) - end end describe '#edit' do @@ -55,11 +40,6 @@ RSpec.describe Admin::RunnersController do let_it_be(:project) { create(:project) } let_it_be(:project_two) { create(:project) } - before_all do - create(:ci_build, runner: runner, project: project) - create(:ci_build, runner: runner, project: project_two) - end - it 'shows a runner edit page' do get :edit, params: { id: runner.id } @@ -77,9 +57,6 @@ RSpec.describe Admin::RunnersController do control_count = ActiveRecord::QueryRecorder.new { get :edit, params: { id: runner.id } }.count - new_project = create(:project) - create(:ci_build, runner: runner, project: new_project) - # There is one additional query looking up subject.group in ProjectPolicy for the # needs_new_sso_session permission expect { get :edit, params: { id: runner.id } }.not_to exceed_query_limit(control_count + 1) @@ -89,17 +66,42 @@ RSpec.describe Admin::RunnersController do end describe '#update' do - it 'updates the runner and ticks the queue' do - new_desc = runner.description.swapcase + let(:new_desc) { runner.description.swapcase } + let(:runner_params) { { id: runner.id, runner: { description: new_desc } } } - expect do - post :update, params: { id: runner.id, runner: { description: new_desc } } - end.to change { runner.ensure_runner_queue_value } + subject(:request) { post :update, params: runner_params } - runner.reload + context 'with update succeeding' do + before do + expect_next_instance_of(Ci::Runners::UpdateRunnerService, runner) do |service| + expect(service).to receive(:update).with(anything).and_call_original + end + end - expect(response).to have_gitlab_http_status(:found) - expect(runner.description).to eq(new_desc) + it 'updates the runner and ticks the queue' do + expect { request }.to change { runner.ensure_runner_queue_value } + + runner.reload + + expect(response).to have_gitlab_http_status(:found) + expect(runner.description).to eq(new_desc) + end + end + + context 'with update failing' do + before do + expect_next_instance_of(Ci::Runners::UpdateRunnerService, runner) do |service| + expect(service).to receive(:update).with(anything).and_return(false) + end + end + + it 'does not update runner or tick the queue' do + expect { request }.not_to change { runner.ensure_runner_queue_value } + expect { request }.not_to change { runner.reload.description } + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to render_template(:show) + end end end diff --git a/spec/controllers/admin/topics_controller_spec.rb b/spec/controllers/admin/topics_controller_spec.rb index ea510f916da..67943525687 100644 --- a/spec/controllers/admin/topics_controller_spec.rb +++ b/spec/controllers/admin/topics_controller_spec.rb @@ -77,24 +77,31 @@ RSpec.describe Admin::TopicsController do describe 'POST #create' do it 'creates topic' do expect do - post :create, params: { projects_topic: { name: 'test' } } + post :create, params: { projects_topic: { name: 'test', title: 'Test' } } end.to change { Projects::Topic.count }.by(1) end - it 'shows error message for invalid topic' do - post :create, params: { projects_topic: { name: nil } } + it 'shows error message for invalid topic name' do + post :create, params: { projects_topic: { name: nil, title: 'Test' } } errors = assigns[:topic].errors expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.blank'))) end - it 'shows error message if topic not unique (case insensitive)' do - post :create, params: { projects_topic: { name: topic.name.upcase } } + it 'shows error message if topic name not unique (case insensitive)' do + post :create, params: { projects_topic: { name: topic.name.upcase, title: topic.title } } errors = assigns[:topic].errors expect(errors).to contain_exactly(errors.full_message(:name, I18n.t('errors.messages.taken'))) end + it 'shows error message for invalid topic title' do + post :create, params: { projects_topic: { name: 'test', title: nil } } + + errors = assigns[:topic].errors + expect(errors).to contain_exactly(errors.full_message(:title, I18n.t('errors.messages.blank'))) + end + context 'as a normal user' do before do sign_in(user) |