summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/api_authentication/builder_spec.rb
blob: e241aa77805abbb9ec861f3d94bb3d019d80210d (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::APIAuthentication::Builder do
  describe '#build' do
    shared_examples 'builds the correct result' do |token_type:, sent_through:, builds:|
      context "with #{token_type.size} token type(s) and #{sent_through.size} sent through(s)" do
        it 'works when passed together' do
          strategies = described_class.new.build { |allow| allow.token_types(*token_type).sent_through(*sent_through) }

          expect(strategies).to eq(builds)
        end

        it 'works when token types are passed separately' do
          strategies = described_class.new.build { |allow| token_type.each { |t| allow.token_types(t).sent_through(*sent_through) } }

          expect(strategies).to eq(builds)
        end

        it 'works when sent throughs are passed separately' do
          strategies = described_class.new.build { |allow| sent_through.each { |s| allow.token_types(*token_type).sent_through(s) } }

          expect(strategies).to eq(builds)
        end

        it 'works when token types and sent throughs are passed separately' do
          strategies = described_class.new.build { |allow| token_type.each { |t| sent_through.each { |s| allow.token_types(t).sent_through(s) } } }

          expect(strategies).to eq(builds)
        end
      end
    end

    it_behaves_like 'builds the correct result',
      token_type: [:pat],
      sent_through: [:basic],
      builds: { basic: [:pat] }

    it_behaves_like 'builds the correct result',
      token_type: [:pat],
      sent_through: [:basic, :oauth],
      builds: { basic: [:pat], oauth: [:pat] }

    it_behaves_like 'builds the correct result',
      token_type: [:pat, :job],
      sent_through: [:basic],
      builds: { basic: [:pat, :job] }

    it_behaves_like 'builds the correct result',
      token_type: [:pat, :job],
      sent_through: [:basic, :oauth],
      builds: { basic: [:pat, :job], oauth: [:pat, :job] }

    context 'with a complex auth strategy' do
      it 'builds the correct result' do
        strategies = described_class.new.build do |allow|
          allow.token_types(:pat, :job, :deploy).sent_through(:http_basic, :oauth)
          allow.token_types(:pat).sent_through(:http_private, :query_private)
          allow.token_types(:oauth2).sent_through(:http_bearer, :query_access)
        end

        expect(strategies).to eq({
          http_basic: [:pat, :job, :deploy],
          oauth: [:pat, :job, :deploy],

          http_private: [:pat],
          query_private: [:pat],

          http_bearer: [:oauth2],
          query_access: [:oauth2]
        })
      end
    end
  end
end