access_spec.rb 5.2 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Auth::LDAP::Access do
4 5
  include LdapHelpers

6
  let(:access) { described_class.new user }
V
Valery Sizov 已提交
7
  let(:user) { create(:omniauth_user) }
8

9 10
  describe '.allowed?' do
    it 'updates the users `last_credential_check_at' do
11
      allow(access).to receive(:update_user)
12 13 14 15 16 17 18 19
      expect(access).to receive(:allowed?) { true }
      expect(described_class).to receive(:open).and_yield(access)

      expect { described_class.allowed?(user) }
        .to change { user.last_credential_check_at }
    end
  end

20 21 22 23 24 25 26 27
  describe '#find_ldap_user' do
    it 'finds a user by dn first' do
      expect(Gitlab::Auth::LDAP::Person).to receive(:find_by_dn).and_return(:ldap_user)

      access.find_ldap_user
    end
  end

28
  describe '#allowed?' do
29
    subject { access.allowed? }
30 31

    context 'when the user cannot be found' do
32
      before do
33
        allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_dn).and_return(nil)
34
        allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_email).and_return(nil)
35
      end
36

37
      it { is_expected.to be_falsey }
38

V
Valery Sizov 已提交
39
      it 'blocks user in GitLab' do
40 41
        expect(access).to receive(:block_user).with(user, 'does not exist anymore')

42 43
        access.allowed?
      end
44 45 46
    end

    context 'when the user is found' do
47 48
      let(:ldap_user) { Gitlab::Auth::LDAP::Person.new(Net::LDAP::Entry.new, 'ldapmain') }

49
      before do
50
        allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_dn).and_return(ldap_user)
51
      end
52

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

58
        it { is_expected.to be_falsey }
59

60
        it 'blocks user in GitLab' do
61 62
          expect(access).to receive(:block_user).with(user, 'is disabled in Active Directory')

63 64
          access.allowed?
        end
65 66
      end

67
      context 'and has no disabled flag in active directory' do
68
        before do
69
          allow(Gitlab::Auth::LDAP::Person).to receive(:disabled_via_active_directory?).and_return(false)
70
        end
71

72
        it { is_expected.to be_truthy }
73

74 75
        context 'when auto-created users are blocked' do
          before do
76
            user.block
77 78
          end

79
          it 'does not unblock user in GitLab' do
80 81
            expect(access).not_to receive(:unblock_user)

82
            access.allowed?
83

84
            expect(user).to be_blocked
85
            expect(user).not_to be_ldap_blocked # this block is handled by omniauth not by our internal logic
86 87 88
          end
        end

89
        context 'when auto-created users are not blocked' do
90
          before do
91
            user.ldap_block
92 93
          end

94
          it 'unblocks user in GitLab' do
95 96
            expect(access).to receive(:unblock_user).with(user, 'is not disabled anymore')

97 98
            access.allowed?
          end
99
        end
100
      end
101

102 103
      context 'without ActiveDirectory enabled' do
        before do
104 105
          allow(Gitlab::Auth::LDAP::Config).to receive(:enabled?).and_return(true)
          allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive(:active_directory).and_return(false)
106
        end
107

108
        it { is_expected.to be_truthy }
109 110 111

        context 'when user cannot be found' do
          before do
112
            allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_dn).and_return(nil)
113
            allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_email).and_return(nil)
114 115 116 117 118
          end

          it { is_expected.to be_falsey }

          it 'blocks user in GitLab' do
119 120
            expect(access).to receive(:block_user).with(user, 'does not exist anymore')

121 122 123 124 125 126 127 128 129 130
            access.allowed?
          end
        end

        context 'when user was previously ldap_blocked' do
          before do
            user.ldap_block
          end

          it 'unblocks the user if it exists' do
131
            expect(access).to receive(:unblock_user).with(user, 'is available again')
132

133 134 135
            access.allowed?
          end
        end
136
      end
137
    end
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    context 'when the connection fails' do
      before do
        raise_ldap_connection_error
      end

      it 'does not block the user' do
        access.allowed?

        expect(user.ldap_blocked?).to be_falsey
      end

      it 'denies access' do
        expect(access.allowed?).to be_falsey
      end
    end
154
  end
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  describe '#block_user' do
    before do
      user.activate
      allow(Gitlab::AppLogger).to receive(:info)

      access.block_user user, 'reason'
    end

    it 'blocks the user' do
      expect(user).to be_blocked
      expect(user).to be_ldap_blocked
    end

    it 'logs the reason' do
      expect(Gitlab::AppLogger).to have_received(:info).with(
171
        "LDAP account \"123456\" reason, " \
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        "blocking Gitlab user \"#{user.name}\" (#{user.email})"
      )
    end
  end

  describe '#unblock_user' do
    before do
      user.ldap_block
      allow(Gitlab::AppLogger).to receive(:info)

      access.unblock_user user, 'reason'
    end

    it 'activates the user' do
      expect(user).not_to be_blocked
      expect(user).not_to be_ldap_blocked
    end

    it 'logs the reason' do
      Gitlab::AppLogger.info(
192
        "LDAP account \"123456\" reason, " \
193 194 195 196
        "unblocking Gitlab user \"#{user.name}\" (#{user.email})"
      )
    end
  end
197
end