diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-05 21:09:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-05 21:09:06 +0300 |
commit | b042382bbf5a4977c5b5c6b0a9a33f4e8ca8d16d (patch) | |
tree | de31671ab7c6ca8c2a3721cbabd1f2a42b3d0194 /spec/lib/gitlab/sidekiq_middleware | |
parent | eabf8fd774fef6a54903e5141138f47bdafeb331 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/gitlab/sidekiq_middleware')
-rw-r--r-- | spec/lib/gitlab/sidekiq_middleware/admin_mode/client_spec.rb | 94 | ||||
-rw-r--r-- | spec/lib/gitlab/sidekiq_middleware/admin_mode/server_spec.rb | 72 |
2 files changed, 166 insertions, 0 deletions
diff --git a/spec/lib/gitlab/sidekiq_middleware/admin_mode/client_spec.rb b/spec/lib/gitlab/sidekiq_middleware/admin_mode/client_spec.rb new file mode 100644 index 00000000000..f6449bae8c3 --- /dev/null +++ b/spec/lib/gitlab/sidekiq_middleware/admin_mode/client_spec.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::SidekiqMiddleware::AdminMode::Client, :do_not_mock_admin_mode, :request_store do + include AdminModeHelper + + let(:worker) do + Class.new do + def perform; end + end + end + + let(:job) { {} } + let(:queue) { :test } + + it 'yields block' do + expect do |b| + subject.call(worker, job, queue, nil, &b) + end.to yield_control.once + end + + context 'user is a regular user' do + it 'no admin mode field in payload' do + subject.call(worker, job, queue, nil) { nil } + + expect(job).not_to include('admin_mode_user_id') + end + end + + context 'user is an administrator' do + let(:admin) { create(:admin) } + + context 'admin mode disabled' do + it 'no admin mode field in payload' do + subject.call(worker, job, queue, nil) { nil } + + expect(job).not_to include('admin_mode_user_id') + end + end + + context 'admin mode enabled' do + before do + enable_admin_mode!(admin) + end + + context 'when sidekiq required context not set' do + it 'no admin mode field in payload' do + subject.call(worker, job, queue, nil) { nil } + + expect(job).not_to include('admin_mode_user_id') + end + end + + context 'when user stored in current request' do + it 'has admin mode field in payload' do + Gitlab::Auth::CurrentUserMode.with_current_admin(admin) do + subject.call(worker, job, queue, nil) { nil } + + expect(job).to include('admin_mode_user_id' => admin.id) + end + end + end + + context 'when bypassing session' do + it 'has admin mode field in payload' do + Gitlab::Auth::CurrentUserMode.bypass_session!(admin.id) do + subject.call(worker, job, queue, nil) { nil } + + expect(job).to include('admin_mode_user_id' => admin.id) + end + end + end + end + end + + context 'admin mode feature disabled' do + before do + stub_feature_flags(user_mode_in_session: false) + end + + it 'yields block' do + expect do |b| + subject.call(worker, job, queue, nil, &b) + end.to yield_control.once + end + + it 'no admin mode field in payload' do + subject.call(worker, job, queue, nil) { nil } + + expect(job).not_to include('admin_mode_user_id') + end + end +end diff --git a/spec/lib/gitlab/sidekiq_middleware/admin_mode/server_spec.rb b/spec/lib/gitlab/sidekiq_middleware/admin_mode/server_spec.rb new file mode 100644 index 00000000000..60475f0e403 --- /dev/null +++ b/spec/lib/gitlab/sidekiq_middleware/admin_mode/server_spec.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::SidekiqMiddleware::AdminMode::Server, :do_not_mock_admin_mode, :request_store do + include AdminModeHelper + + let(:worker) do + Class.new do + def perform; end + end + end + + let(:job) { {} } + let(:queue) { :test } + + it 'yields block' do + expect do |b| + subject.call(worker, job, queue, &b) + end.to yield_control.once + end + + context 'job has no admin mode field' do + it 'session is not bypassed' do + subject.call(worker, job, queue) do + expect(Gitlab::Auth::CurrentUserMode.bypass_session_admin_id).to be_nil + end + end + end + + context 'job has admin mode field' do + let(:admin) { create(:admin) } + + context 'nil admin mode id' do + let(:job) { { 'admin_mode_user_id' => nil } } + + it 'session is not bypassed' do + subject.call(worker, job, queue) do + expect(Gitlab::Auth::CurrentUserMode.bypass_session_admin_id).to be_nil + end + end + end + + context 'valid admin mode id' do + let(:job) { { 'admin_mode_user_id' => admin.id } } + + it 'session is bypassed' do + subject.call(worker, job, queue) do + expect(Gitlab::Auth::CurrentUserMode.bypass_session_admin_id).to be(admin.id) + end + end + end + end + + context 'admin mode feature disabled' do + before do + stub_feature_flags(user_mode_in_session: false) + end + + it 'yields block' do + expect do |b| + subject.call(worker, job, queue, &b) + end.to yield_control.once + end + + it 'session is not bypassed' do + subject.call(worker, job, queue) do + expect(Gitlab::Auth::CurrentUserMode.bypass_session_admin_id).to be_nil + end + end + end +end |