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

Douwe Maan's avatar
Douwe Maan committed
3
describe Gitlab::LDAP::Access, lib: true do
4
  let(:access) { Gitlab::LDAP::Access.new user }
Valery Sizov's avatar
Valery Sizov committed
5
  let(:user) { create(:omniauth_user) }
6

7
  describe '#allowed?' do
8
    subject { access.allowed? }
9 10

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

15
      it { is_expected.to be_falsey }
16

17
      it 'should block user in GitLab' do
18 19
        expect(access).to receive(:block_user).with(user, 'does not exist anymore')

20 21
        access.allowed?
      end
22 23 24
    end

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

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

34
        it { is_expected.to be_falsey }
35

36
        it 'blocks user in GitLab' do
37 38
          expect(access).to receive(:block_user).with(user, 'is disabled in Active Directory')

39 40
          access.allowed?
        end
41 42
      end

43
      context 'and has no disabled flag in active diretory' do
44
        before do
45
          allow(Gitlab::LDAP::Person).to receive(:disabled_via_active_directory?).and_return(false)
46
        end
47

48
        it { is_expected.to be_truthy }
49

50 51
        context 'when auto-created users are blocked' do
          before do
52
            user.block
53 54
          end

55
          it 'does not unblock user in GitLab' do
56 57
            expect(access).not_to receive(:unblock_user)

58
            access.allowed?
59

60
            expect(user).to be_blocked
61
            expect(user).not_to be_ldap_blocked # this block is handled by omniauth not by our internal logic
62 63 64
          end
        end

65
        context 'when auto-created users are not blocked' do
66
          before do
67
            user.ldap_block
68 69
          end

70
          it 'unblocks user in GitLab' do
71 72
            expect(access).to receive(:unblock_user).with(user, 'is not disabled anymore')

73 74
            access.allowed?
          end
75
        end
76
      end
77

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

84
        it { is_expected.to be_truthy }
85 86 87 88 89 90 91 92 93

        context 'when 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 'blocks user in GitLab' do
94 95
            expect(access).to receive(:block_user).with(user, 'does not exist anymore')

96 97 98 99 100 101 102 103 104 105
            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
106
            expect(access).to receive(:unblock_user).with(user, 'is available again')
107

108 109 110
            access.allowed?
          end
        end
111
      end
112 113
    end
  end
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  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(
130
        "LDAP account \"123456\" reason, " \
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        "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(
151
        "LDAP account \"123456\" reason, " \
152 153 154 155
        "unblocking Gitlab user \"#{user.name}\" (#{user.email})"
      )
    end
  end
156
end