Welcome to mirror list, hosted at ThFree Co, Russian Federation.

client_spec.rb « admin_mode « sidekiq_middleware « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d5d5f28eab3b5e1cb98a455af1a04d386a8c47a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::SidekiqMiddleware::AdminMode::Client, :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 setting disabled' do
    before do
      stub_application_setting(admin_mode: 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