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

3
describe Gitlab::Auth::LDAP::User do
4
  let(:ldap_user) { described_class.new(auth_hash) }
5
  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
Michael Kozono's avatar
Michael Kozono committed
14
    OmniAuth::AuthHash.new(uid: 'uid=John Smith,ou=People,dc=example,dc=com', provider: 'ldapmain', info: info)
15
  end
16
  let(:ldap_user_upper_case) { described_class.new(auth_hash_upper_case) }
17 18 19 20 21 22 23 24
  let(:info_upper_case) do
    {
      name: 'John',
      email: 'John@Example.com', # Email address has upper case chars
      nickname: 'john'
    }
  end
  let(:auth_hash_upper_case) do
Michael Kozono's avatar
Michael Kozono committed
25
    OmniAuth::AuthHash.new(uid: 'uid=John Smith,ou=People,dc=example,dc=com', provider: 'ldapmain', info: info_upper_case)
26
  end
27

28
  describe '#should_save?' do
29
    it "marks existing ldap user as changed" do
Michael Kozono's avatar
Michael Kozono committed
30
      create(:omniauth_user, extern_uid: 'uid=John Smith,ou=People,dc=example,dc=com', provider: 'ldapmain')
31
      expect(ldap_user.should_save?).to be_truthy
32 33 34
    end

    it "marks existing non-ldap user if the email matches as changed" do
35
      create(:user, email: 'john@example.com')
36
      expect(ldap_user.should_save?).to be_truthy
37 38
    end

39
    it "does not mark existing ldap user as changed" do
Michael Kozono's avatar
Michael Kozono committed
40
      create(:omniauth_user, email: 'john@example.com', extern_uid: 'uid=john smith,ou=people,dc=example,dc=com', provider: 'ldapmain')
41
      expect(ldap_user.should_save?).to be_falsey
42 43 44
    end
  end

45
  describe '.find_by_uid_and_provider' do
46 47
    let(:dn) { 'CN=John Åström, CN=Users, DC=Example, DC=com' }

48 49 50 51 52 53
    it 'retrieves the correct user' do
      special_info = {
        name: 'John Åström',
        email: 'john@example.com',
        nickname: 'jastrom'
      }
54
      special_hash = OmniAuth::AuthHash.new(uid: dn, provider: 'ldapmain', info: special_info)
55 56 57
      special_chars_user = described_class.new(special_hash)
      user = special_chars_user.save

58
      expect(described_class.find_by_uid_and_provider(dn, 'ldapmain')).to eq user
59 60 61
    end
  end

62
  describe 'find or create' do
63
    it "finds the user if already existing" do
64
      create(:omniauth_user, extern_uid: 'uid=john smith,ou=people,dc=example,dc=com', provider: 'ldapmain')
65

66
      expect { ldap_user.save }.not_to change { User.count }
67 68
    end

69
    it "connects to existing non-ldap user if the email matches" do
Valery Sizov's avatar
Valery Sizov committed
70
      existing_user = create(:omniauth_user, email: 'john@example.com', provider: "twitter")
71
      expect { ldap_user.save }.not_to change { User.count }
72 73

      existing_user.reload
Michael Kozono's avatar
Michael Kozono committed
74
      expect(existing_user.ldap_identity.extern_uid).to eql 'uid=john smith,ou=people,dc=example,dc=com'
75
      expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
76 77
    end

78 79
    it 'connects to existing ldap user if the extern_uid changes' do
      existing_user = create(:omniauth_user, email: 'john@example.com', extern_uid: 'old-uid', provider: 'ldapmain')
80
      expect { ldap_user.save }.not_to change { User.count }
81 82

      existing_user.reload
Michael Kozono's avatar
Michael Kozono committed
83
      expect(existing_user.ldap_identity.extern_uid).to eql 'uid=john smith,ou=people,dc=example,dc=com'
84
      expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
85 86 87 88 89
      expect(existing_user.id).to eql ldap_user.gl_user.id
    end

    it 'connects to existing ldap user if the extern_uid changes and email address has upper case characters' do
      existing_user = create(:omniauth_user, email: 'john@example.com', extern_uid: 'old-uid', provider: 'ldapmain')
90
      expect { ldap_user_upper_case.save }.not_to change { User.count }
91 92

      existing_user.reload
Michael Kozono's avatar
Michael Kozono committed
93
      expect(existing_user.ldap_identity.extern_uid).to eql 'uid=john smith,ou=people,dc=example,dc=com'
94
      expect(existing_user.ldap_identity.provider).to eql 'ldapmain'
95 96 97 98 99
      expect(existing_user.id).to eql ldap_user.gl_user.id
    end

    it 'maintains an identity per provider' do
      existing_user = create(:omniauth_user, email: 'john@example.com', provider: 'twitter')
100
      expect(existing_user.identities.count).to be(1)
101 102

      ldap_user.save
103
      expect(ldap_user.gl_user.identities.count).to be(2)
104 105 106 107 108 109

      # Expect that find_by provider only returns a single instance of an identity and not an Enumerable
      expect(ldap_user.gl_user.identities.find_by(provider: 'twitter')).to be_instance_of Identity
      expect(ldap_user.gl_user.identities.find_by(provider: auth_hash.provider)).to be_instance_of Identity
    end

110
    it "creates a new user if not found" do
111
      expect { ldap_user.save }.to change { User.count }.by(1)
112
    end
113 114 115 116 117 118 119 120 121 122 123 124

    context 'when signup is disabled' do
      before do
        stub_application_setting signup_enabled: false
      end

      it 'creates the user' do
        ldap_user.save

        expect(gl_user).to be_persisted
      end
    end
125 126 127 128 129 130 131 132 133 134 135 136 137

    context 'when user confirmation email is enabled' do
      before do
        stub_application_setting send_user_confirmation_email: true
      end

      it 'creates and confirms the user anyway' do
        ldap_user.save

        expect(gl_user).to be_persisted
        expect(gl_user).to be_confirmed
      end
    end
138 139
  end

140 141 142 143 144 145
  describe 'updating email' do
    context "when LDAP sets an email" do
      it "has a real email" do
        expect(ldap_user.gl_user.email).to eq(info[:email])
      end

146
      it "has email set as synced" do
147
        expect(ldap_user.gl_user.user_synced_attributes_metadata.email_synced).to be_truthy
148 149
      end

150 151 152 153 154
      it "has email set as read-only" do
        expect(ldap_user.gl_user.read_only_attribute?(:email)).to be_truthy
      end

      it "has synced attributes provider set to ldapmain" do
155
        expect(ldap_user.gl_user.user_synced_attributes_metadata.provider).to eql 'ldapmain'
156 157 158 159 160 161 162 163 164
      end
    end

    context "when LDAP doesn't set an email" do
      before do
        info.delete(:email)
      end

      it "has a temp email" do
165
        expect(ldap_user.gl_user.temp_oauth_email?).to be_truthy
166 167
      end

168
      it "has email set as not synced" do
169
        expect(ldap_user.gl_user.user_synced_attributes_metadata.email_synced).to be_falsey
170
      end
171 172 173 174

      it "does not have email set as read-only" do
        expect(ldap_user.gl_user.read_only_attribute?(:email)).to be_falsey
      end
175 176 177
    end
  end

178
  describe 'blocking' do
179
    def configure_block(value)
180
      allow_any_instance_of(Gitlab::Auth::LDAP::Config)
181
          .to receive(:block_auto_created_users).and_return(value)
182 183
    end

184 185
    context 'signup' do
      context 'dont block on create' do
186 187 188
        before do
          configure_block(false)
        end
189 190 191 192 193 194 195 196 197

        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
198 199 200
        before do
          configure_block(true)
        end
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

        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
217 218 219
        before do
          configure_block(false)
        end
220 221 222 223 224 225 226 227 228

        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
229 230 231
        before do
          configure_block(true)
        end
232 233 234 235 236 237 238

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