From 76960690493db51cbbf3d24943f2fc6bb09e5ce4 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 11 Nov 2022 18:07:37 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- .../concerns/preferred_language_switcher_spec.rb | 51 ++++++++++++++++++++++ spec/controllers/registrations_controller_spec.rb | 28 ++++++++++++ spec/controllers/sessions_controller_spec.rb | 2 + spec/lib/gitlab/gitaly_client_spec.rb | 44 +++++++++++++++++++ spec/models/clusters/platforms/kubernetes_spec.rb | 49 +++++++++++++++++++++ spec/support/helpers/kubernetes_helpers.rb | 23 ++++++++++ .../preferred_language_switcher_shared_examples.rb | 22 ++++++++++ spec/workers/repository_import_worker_spec.rb | 32 +++++--------- 8 files changed, 229 insertions(+), 22 deletions(-) create mode 100644 spec/controllers/concerns/preferred_language_switcher_spec.rb create mode 100644 spec/support/shared_examples/controllers/preferred_language_switcher_shared_examples.rb (limited to 'spec') diff --git a/spec/controllers/concerns/preferred_language_switcher_spec.rb b/spec/controllers/concerns/preferred_language_switcher_spec.rb new file mode 100644 index 00000000000..40d6ac10c37 --- /dev/null +++ b/spec/controllers/concerns/preferred_language_switcher_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe PreferredLanguageSwitcher, type: :controller do + controller(ActionController::Base) do + include PreferredLanguageSwitcher # rubocop:disable RSpec/DescribedClass + + before_action :init_preferred_language, only: :new + + def new + render html: 'new page' + end + end + + context 'when first visit' do + before do + get :new + end + + it 'sets preferred_language to default' do + expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language + end + end + + context 'when preferred language in cookies has been modified' do + let(:user_preferred_language) { nil } + + before do + cookies[:preferred_language] = user_preferred_language + + get :new + end + + context 'with a valid value' do + let(:user_preferred_language) { 'zh_CN' } + + it 'keeps preferred language unchanged' do + expect(cookies[:preferred_language]).to eq user_preferred_language + end + end + + context 'with an invalid value' do + let(:user_preferred_language) { 'xxx' } + + it 'sets preferred_language to default' do + expect(cookies[:preferred_language]).to eq Gitlab::CurrentSettings.default_preferred_language + end + end + end +end diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 119a7be25ef..8775f68a5de 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -19,6 +19,8 @@ RSpec.describe RegistrationsController do expect(response).to have_gitlab_http_status(:ok) expect(assigns(:resource)).to be_a(User) end + + it_behaves_like "switches to user preferred language", 'Sign up' end describe '#create' do @@ -524,6 +526,32 @@ RSpec.describe RegistrationsController do ) end end + + context 'with preferred language' do + let(:user_preferred_language) { nil } + + before do + cookies['preferred_language'] = user_preferred_language + + post :create, params: { new_user: base_user_params } + end + + subject { User.last.preferred_language } + + context 'with default behavior' do + it 'sets preferred language to default' do + is_expected.to eq(Gitlab::CurrentSettings.default_preferred_language) + end + end + + context 'when user sets preferred language' do + let(:user_preferred_language) { 'zh_CN' } + + it 'sets name from first and last name' do + is_expected.to eq(user_preferred_language) + end + end + end end describe '#destroy' do diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 80cf060bc45..69282f951f9 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -69,6 +69,8 @@ RSpec.describe SessionsController do expect(controller.stored_location_for(:redirect)).to eq(search_path) end + + it_behaves_like "switches to user preferred language", 'Sign in' end describe '#create' do diff --git a/spec/lib/gitlab/gitaly_client_spec.rb b/spec/lib/gitlab/gitaly_client_spec.rb index a3b2fb2d9d0..3d33bf93c23 100644 --- a/spec/lib/gitlab/gitaly_client_spec.rb +++ b/spec/lib/gitlab/gitaly_client_spec.rb @@ -311,6 +311,50 @@ RSpec.describe Gitlab::GitalyClient do it_behaves_like 'gitaly feature flags in metadata' end + context 'logging information in metadata' do + let(:user) { create(:user) } + + context 'user is added to application context' do + it 'injects username and user_id into gRPC metadata' do + metadata = {} + ::Gitlab::ApplicationContext.with_context(user: user) do + metadata = described_class.request_kwargs('default', timeout: 1)[:metadata] + end + + expect(metadata['username']).to eql(user.username) + expect(metadata['user_id']).to eql(user.id.to_s) + end + end + + context 'user is not added to application context' do + it 'does not inject username and user_id into gRPC metadata' do + metadata = described_class.request_kwargs('default', timeout: 1)[:metadata] + + expect(metadata).not_to have_key('username') + expect(metadata).not_to have_key('user_id') + end + end + + context 'remote_ip is added to application context' do + it 'injects remote_ip into gRPC metadata' do + metadata = {} + ::Gitlab::ApplicationContext.with_context(remote_ip: '1.2.3.4') do + metadata = described_class.request_kwargs('default', timeout: 1)[:metadata] + end + + expect(metadata['remote_ip']).to eql('1.2.3.4') + end + end + + context 'remote_ip is not added to application context' do + it 'does not inject remote_ip into gRPC metadata' do + metadata = described_class.request_kwargs('default', timeout: 1)[:metadata] + + expect(metadata).not_to have_key('remote_ip') + end + end + end + context 'gitlab_git_env' do let(:policy) { 'gitaly-route-repository-accessor-policy' } diff --git a/spec/models/clusters/platforms/kubernetes_spec.rb b/spec/models/clusters/platforms/kubernetes_spec.rb index 99dbb654b7f..b280275c2e5 100644 --- a/spec/models/clusters/platforms/kubernetes_spec.rb +++ b/spec/models/clusters/platforms/kubernetes_spec.rb @@ -433,6 +433,55 @@ RSpec.describe Clusters::Platforms::Kubernetes do let(:cluster) { create(:cluster, :project, platform_kubernetes: service) } include_examples 'successful deployment request' + + context 'when reading ingress raises NoMethodError' do + before do + allow_next_instance_of(Gitlab::Kubernetes::KubeClient) do |kube_client| + allow(kube_client).to receive(:get_pods).with(namespace: namespace).and_return([]) + allow(kube_client).to receive(:get_deployments).with(namespace: namespace).and_return([]) + allow(kube_client).to receive(:get_ingresses).with(namespace: namespace).and_raise(NoMethodError) + end + end + + context 'when version request succeeds' do + before do + stub_server_min_version(min_server_version) + end + + context 'when server min version is < 23' do + let(:min_server_version) { "18" } + + it 'does not raise error', :unlimited_max_formatted_output_length do + expect { subject }.not_to raise_error + end + + it 'returns empty array for the K8s component keys' do + expect(subject).to include({ pods: [], deployments: [], ingresses: [] }) + end + end + + context 'when server min version is >= 23' do + let(:min_server_version) { "23" } + + it 'does raise error' do + expect { subject }.to raise_error(NoMethodError) + end + end + end + + context 'when the version request fails' do + before do + stub_server_min_version_failed_request + end + + it "tracks error and returns empty arrays" do + expect(Gitlab::ErrorTracking) + .to receive(:track_exception).with(kind_of(Clusters::Platforms::Kubernetes::FailedVersionCheckError)) + + expect(subject).to include({ pods: [], deployments: [], ingresses: [] }) + end + end + end end context 'on a group level cluster' do diff --git a/spec/support/helpers/kubernetes_helpers.rb b/spec/support/helpers/kubernetes_helpers.rb index fb695ce8e61..72524453f34 100644 --- a/spec/support/helpers/kubernetes_helpers.rb +++ b/spec/support/helpers/kubernetes_helpers.rb @@ -142,6 +142,29 @@ module KubernetesHelpers WebMock.stub_request(method, ingresses_url).to_return(response) end + def stub_server_min_version_failed_request + WebMock.stub_request(:get, service.api_url + '/version').to_return( + status: [500, "Internal Server Error"], + body: {}.to_json) + end + + def stub_server_min_version(min_version) + response = kube_response({ + "major": "1", # not used, just added here to be a bit more realistic purposes + "minor": min_version.to_s + }) + + WebMock.stub_request( :get, service.api_url + '/version') + .with( + headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'Authorization' => 'Bearer aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + 'User-Agent' => 'Ruby' + }) + .to_return(response) + end + def stub_kubeclient_knative_services(options = {}) namespace_path = options[:namespace].present? ? "namespaces/#{options[:namespace]}/" : "" diff --git a/spec/support/shared_examples/controllers/preferred_language_switcher_shared_examples.rb b/spec/support/shared_examples/controllers/preferred_language_switcher_shared_examples.rb new file mode 100644 index 00000000000..74456e62eb8 --- /dev/null +++ b/spec/support/shared_examples/controllers/preferred_language_switcher_shared_examples.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +RSpec.shared_examples 'switches to user preferred language' do |msg_id_example| + context 'with preferred_language in cookies' do + render_views + let(:user_preferred_language) { 'zh_CN' } + + subject { get :new } + + before do + cookies['preferred_language'] = user_preferred_language + end + + it 'renders new template with cookies preferred language' do + expect(subject).to render_template(:new) + expect(response).to have_gitlab_http_status(:ok) + + expected_text = Gitlab::I18n.with_locale(user_preferred_language) { _(msg_id_example) } + expect(response.body).to include(expected_text) + end + end +end diff --git a/spec/workers/repository_import_worker_spec.rb b/spec/workers/repository_import_worker_spec.rb index 82d975cb85a..1dc77fbf83f 100644 --- a/spec/workers/repository_import_worker_spec.rb +++ b/spec/workers/repository_import_worker_spec.rb @@ -46,36 +46,24 @@ RSpec.describe RepositoryImportWorker do end context 'when the import has failed' do - it 'hide the credentials that were used in the import URL' do - error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found } + it 'updates the error on Import/Export & hides credentials from import URL' do + import_url = 'https://user:pass@test.com/root/repoC.git/' + error = "#{import_url} not found" import_state.update!(jid: '123') - expect_next_instance_of(Projects::ImportService) do |instance| - expect(instance).to receive(:execute).and_return({ status: :error, message: error }) - end - - expect do - subject.perform(project.id) - end.to raise_error(RuntimeError, error) - expect(import_state.reload.jid).not_to be_nil - expect(import_state.status).to eq('failed') - end - - it 'updates the error on Import/Export' do - error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found } - project.update!(import_type: 'gitlab_project') - import_state.update!(jid: '123') + expect_next_instance_of(Projects::ImportService) do |instance| - expect(instance).to receive(:execute).and_return({ status: :error, message: error }) + expect(instance).to receive(:track_start_import).and_raise(StandardError, error) end - expect do - subject.perform(project.id) - end.to raise_error(RuntimeError, error) + expect { subject.perform(project.id) }.not_to raise_error - expect(import_state.reload.last_error).not_to be_nil + import_state.reload + expect(import_state.jid).to eq('123') expect(import_state.status).to eq('failed') + expect(import_state.last_error).to include("[FILTERED] not found") + expect(import_state.last_error).not_to include(import_url) end end -- cgit v1.2.1