BigW Consortium Gitlab

access_spec.rb 2.24 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::LDAP::Access do
4
  let(:access) { Gitlab::LDAP::Access.new user }
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 18
    end

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

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

30
        it { is_expected.to be_falsey }
31 32 33

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

38
      context 'and has no disabled flag in active diretory' do
39 40
        before do
          user.block
41 42 43

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

46
        it { is_expected.to be_truthy }
47

48 49 50
        context 'when auto-created users are blocked' do

          before do
51 52
            allow_any_instance_of(Gitlab::LDAP::Config).
              to receive(:block_auto_created_users).and_return(true)
53 54 55 56
          end

          it "does not unblock user in GitLab" do
            access.allowed?
57
            expect(user).to be_blocked
58 59 60 61 62 63
          end
        end

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

          before do
64 65
            allow_any_instance_of(Gitlab::LDAP::Config).
              to receive(:block_auto_created_users).and_return(false)
66 67 68 69
          end

          it "should unblock user in GitLab" do
            access.allowed?
70
            expect(user).not_to be_blocked
71
          end
72
        end
73
      end
74

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

82
        it { is_expected.to be_truthy }
83
      end
84 85
    end
  end
86
end