BigW Consortium Gitlab

user_spec.rb 2.84 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::LDAP::User do
4 5
  let(:ldap_user) { Gitlab::LDAP::User.new(auth_hash) }
  let(:gl_user) { ldap_user.gl_user }
6
  let(:info) do
7
    {
8
      name: 'John',
9
      email: 'john@example.com',
10
      nickname: 'john'
11 12 13
    }
  end
  let(:auth_hash) do
14
    double(uid: 'my-uid', provider: 'ldapmain', info: double(info))
15
  end
16

17 18
  describe :changed? do
    it "marks existing ldap user as changed" do
19
      create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain')
20
      expect(ldap_user.changed?).to be_truthy
21 22 23
    end

    it "marks existing non-ldap user if the email matches as changed" do
24
      create(:user, email: 'john@example.com')
25
      expect(ldap_user.changed?).to be_truthy
26 27 28
    end

    it "dont marks existing ldap user as changed" do
29
      create(:omniauth_user, email: 'john@example.com', extern_uid: 'my-uid', provider: 'ldapmain')
30
      expect(ldap_user.changed?).to be_falsey
31 32 33
    end
  end

34 35
  describe :find_or_create do
    it "finds the user if already existing" do
36
      create(:omniauth_user, extern_uid: 'my-uid', provider: 'ldapmain')
37

38
      expect{ ldap_user.save }.not_to change{ User.count }
39 40
    end

41
    it "connects to existing non-ldap user if the email matches" do
Valery Sizov committed
42
      existing_user = create(:omniauth_user, email: 'john@example.com', provider: "twitter")
43
      expect{ ldap_user.save }.not_to change{ User.count }
44 45

      existing_user.reload
46 47
      expect(existing_user.ldap_identity.extern_uid).to eql 'my-uid'
      expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
48 49
    end

50
    it "creates a new user if not found" do
51 52 53 54 55
      expect{ ldap_user.save }.to change{ User.count }.by(1)
    end
  end

  describe 'blocking' do
56 57 58 59 60
    def configure_block(value)
      allow_any_instance_of(Gitlab::LDAP::Config).
        to receive(:block_auto_created_users).and_return(value)
    end

61 62
    context 'signup' do
      context 'dont block on create' do
63
        before { configure_block(false) }
64 65 66 67 68 69 70 71 72

        it do
          ldap_user.save
          expect(gl_user).to be_valid
          expect(gl_user).not_to be_blocked
        end
      end

      context 'block on create' do
73
        before { configure_block(true) }
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

        it do
          ldap_user.save
          expect(gl_user).to be_valid
          expect(gl_user).to be_blocked
        end
      end
    end

    context 'sign-in' do
      before do
        ldap_user.save
        ldap_user.gl_user.activate
      end

      context 'dont block on create' do
90
        before { configure_block(false) }
91 92 93 94 95 96 97 98 99

        it do
          ldap_user.save
          expect(gl_user).to be_valid
          expect(gl_user).not_to be_blocked
        end
      end

      context 'block on create' do
100
        before { configure_block(true) }
101 102 103 104 105 106 107

        it do
          ldap_user.save
          expect(gl_user).to be_valid
          expect(gl_user).not_to be_blocked
        end
      end
108 109 110
    end
  end
end