summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ldap/access_spec.rb
blob: a628d0c01574fb3ad8371f361d46809e6565111c (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
require 'spec_helper'

describe Gitlab::LDAP::Access, lib: true do
  let(:access) { Gitlab::LDAP::Access.new user }
  let(:user) { create(:omniauth_user) }

  describe :allowed? do
    subject { access.allowed? }

    context 'when the user cannot be found' do
      before do
        allow(Gitlab::LDAP::Person).to receive(:find_by_dn).and_return(nil)
      end

      it { is_expected.to be_falsey }
      
      it 'should block user in GitLab' do
        access.allowed?
        expect(user).to be_blocked
      end
    end

    context 'when the user is found' do
      before do
        allow(Gitlab::LDAP::Person).
          to receive(:find_by_dn).and_return(:ldap_user)
      end

      context 'and the user is disabled via active directory' do
        before do
          allow(Gitlab::LDAP::Person).
            to receive(:disabled_via_active_directory?).and_return(true)
        end

        it { is_expected.to be_falsey }

        it "should block user in GitLab" do
          access.allowed?
          expect(user).to be_blocked
        end
      end

      context 'and has no disabled flag in active diretory' do
        before do
          user.block

          allow(Gitlab::LDAP::Person).
            to receive(:disabled_via_active_directory?).and_return(false)
        end

        it { is_expected.to be_truthy }

        context 'when auto-created users are blocked' do

          before do
            allow_any_instance_of(Gitlab::LDAP::Config).
              to receive(:block_auto_created_users).and_return(true)
          end

          it "does not unblock user in GitLab" do
            access.allowed?
            expect(user).to be_blocked
          end
        end

        context "when auto-created users are not blocked" do

          before do
            allow_any_instance_of(Gitlab::LDAP::Config).
              to receive(:block_auto_created_users).and_return(false)
          end

          it "should unblock user in GitLab" do
            access.allowed?
            expect(user).not_to be_blocked
          end
        end
      end

      context 'without ActiveDirectory enabled' do
        before do
          allow(Gitlab::LDAP::Config).to receive(:enabled?).and_return(true)
          allow_any_instance_of(Gitlab::LDAP::Config).
            to receive(:active_directory).and_return(false)
        end

        it { is_expected.to be_truthy }
      end
    end
  end
end