user_spec.rb 25.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
require 'spec_helper'

3
describe Gitlab::Auth::OAuth::User do
4 5
  include LdapHelpers

6
  let(:oauth_user) { described_class.new(auth_hash) }
7 8
  let(:gl_user) { oauth_user.gl_user }
  let(:uid) { 'my-uid' }
9
  let(:dn) { 'uid=user1,ou=people,dc=example' }
10
  let(:provider) { 'my-provider' }
11
  let(:auth_hash) { OmniAuth::AuthHash.new(uid: uid, provider: provider, info: info_hash) }
12 13
  let(:info_hash) do
    {
14
      nickname: '-john+gitlab-ETC%.git@gmail.com',
D
Dmitriy Zaporozhets 已提交
15
      name: 'John',
16 17 18 19 20
      email: 'john@mail.com',
      address: {
        locality: 'locality',
        country: 'country'
      }
21
    }
D
Dmitriy Zaporozhets 已提交
22
  end
23
  let(:ldap_user) { Gitlab::Auth::LDAP::Person.new(Net::LDAP::Entry.new, 'ldapmain') }
D
Dmitriy Zaporozhets 已提交
24

25
  describe '#persisted?' do
26
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }
27 28

    it "finds an existing user based on uid and provider (facebook)" do
29
      expect( oauth_user.persisted? ).to be_truthy
30 31
    end

32
    it 'returns false if user is not found in database' do
R
Robert Speicher 已提交
33
      allow(auth_hash).to receive(:uid).and_return('non-existing')
34
      expect( oauth_user.persisted? ).to be_falsey
35 36 37
    end
  end

38 39 40
  def stub_omniauth_config(messages)
    allow(Gitlab.config.omniauth).to receive_messages(messages)
  end
R
Robert Speicher 已提交
41

42
  describe '#save' do
J
Jan-Willem van der Meer 已提交
43
    let(:provider) { 'twitter' }
D
Dmitriy Zaporozhets 已提交
44

45 46 47 48 49 50 51 52 53 54 55 56
    describe 'when account exists on server' do
      it 'does not mark the user as external' do
        create(:omniauth_user, extern_uid: 'my-uid', provider: provider)
        stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])

        oauth_user.save

        expect(gl_user).to be_valid
        expect(gl_user.external).to be_falsey
      end
    end

57
    describe 'signup' do
58 59 60 61 62 63
      context 'when signup is disabled' do
        before do
          stub_application_setting signup_enabled: false
        end

        it 'creates the user' do
64
          stub_omniauth_config(allow_single_sign_on: [provider])
65 66 67 68 69 70 71

          oauth_user.save

          expect(gl_user).to be_persisted
        end
      end

72 73 74 75 76 77
      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
78
          stub_omniauth_config(allow_single_sign_on: [provider])
79 80 81 82 83 84 85 86

          oauth_user.save

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

87
      it 'marks user as having password_automatically_set' do
88
        stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])
89 90 91 92 93 94 95

        oauth_user.save

        expect(gl_user).to be_persisted
        expect(gl_user).to be_password_automatically_set
      end

96 97
      shared_examples 'to verify compliance with allow_single_sign_on' do
        context 'provider is marked as external' do
98
          it 'marks user as external' do
99
            stub_omniauth_config(allow_single_sign_on: [provider], external_providers: [provider])
100 101 102 103 104 105 106
            oauth_user.save
            expect(gl_user).to be_valid
            expect(gl_user.external).to be_truthy
          end
        end

        context 'provider was external, now has been removed' do
107
          it 'does not mark external user as internal' do
108 109
            create(:omniauth_user, extern_uid: 'my-uid', provider: provider, external: true)
            stub_omniauth_config(allow_single_sign_on: [provider], external_providers: ['facebook'])
110 111
            oauth_user.save
            expect(gl_user).to be_valid
112 113 114 115 116 117
            expect(gl_user.external).to be_truthy
          end
        end

        context 'provider is not external' do
          context 'when adding a new OAuth identity' do
118
            it 'does not promote an external user to internal' do
119 120 121 122 123 124 125
              user = create(:user, email: 'john@mail.com', external: true)
              user.identities.create(provider: provider, extern_uid: uid)

              oauth_user.save
              expect(gl_user).to be_valid
              expect(gl_user.external).to be_truthy
            end
126 127 128 129
          end
        end

        context 'with new allow_single_sign_on enabled syntax' do
130
          before do
131
            stub_omniauth_config(allow_single_sign_on: [provider])
132
          end
133

134 135
          it "creates a user from Omniauth" do
            oauth_user.save
136

137 138 139
            expect(gl_user).to be_valid
            identity = gl_user.identities.first
            expect(identity.extern_uid).to eql uid
140
            expect(identity.provider).to eql provider
141 142 143
          end
        end

144
        context "with old allow_single_sign_on enabled syntax" do
145 146 147
          before do
            stub_omniauth_config(allow_single_sign_on: true)
          end
148 149 150 151 152 153 154

          it "creates a user from Omniauth" do
            oauth_user.save

            expect(gl_user).to be_valid
            identity = gl_user.identities.first
            expect(identity.extern_uid).to eql uid
155
            expect(identity.provider).to eql provider
156 157 158
          end
        end

159
        context 'with new allow_single_sign_on disabled syntax' do
160 161 162 163
          before do
            stub_omniauth_config(allow_single_sign_on: [])
          end

164
          it 'throws an error' do
165
            expect { oauth_user.save }.to raise_error StandardError
166
          end
167
        end
168

169
        context 'with old allow_single_sign_on disabled (Default)' do
170 171 172 173
          before do
            stub_omniauth_config(allow_single_sign_on: false)
          end

174
          it 'throws an error' do
175
            expect { oauth_user.save }.to raise_error StandardError
176 177
          end
        end
178
      end
179

180
      context "with auto_link_ldap_user disabled (default)" do
181 182 183 184
        before do
          stub_omniauth_config(auto_link_ldap_user: false)
        end

185 186 187 188
        include_examples "to verify compliance with allow_single_sign_on"
      end

      context "with auto_link_ldap_user enabled" do
189 190 191
        before do
          stub_omniauth_config(auto_link_ldap_user: true)
        end
R
Robert Speicher 已提交
192

193
        context "and no LDAP provider defined" do
194 195 196
          before do
            stub_ldap_config(providers: [])
          end
R
Robert Speicher 已提交
197

198 199
          include_examples "to verify compliance with allow_single_sign_on"
        end
R
Robert Speicher 已提交
200

201
        context "and at least one LDAP provider is defined" do
202 203 204
          before do
            stub_ldap_config(providers: %w(ldapmain))
          end
205 206 207

          context "and a corresponding LDAP person" do
            before do
R
Robert Speicher 已提交
208 209
              allow(ldap_user).to receive(:uid) { uid }
              allow(ldap_user).to receive(:username) { uid }
210
              allow(ldap_user).to receive(:email) { ['johndoe@example.com', 'john2@example.com'] }
211
              allow(ldap_user).to receive(:dn) { dn }
212
            end
R
Robert Speicher 已提交
213

214
            context "and no account for the LDAP user" do
215
              before do
216
                allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user)
217

218
                oauth_user.save
219
              end
R
Robert Speicher 已提交
220

221
              it "creates a user with dual LDAP and omniauth identities" do
222 223 224
                expect(gl_user).to be_valid
                expect(gl_user.username).to eql uid
                expect(gl_user.email).to eql 'johndoe@example.com'
R
Rémy Coutable 已提交
225
                expect(gl_user.identities.length).to be 2
226 227
                identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
                expect(identities_as_hash).to match_array(
228
                  [
229
                    { provider: 'ldapmain', extern_uid: dn },
230 231 232
                    { provider: 'twitter', extern_uid: uid }
                  ]
                )
233
              end
234 235 236 237 238 239 240 241 242 243 244 245

              it "has email set as synced" do
                expect(gl_user.user_synced_attributes_metadata.email_synced).to be_truthy
              end

              it "has email set as read-only" do
                expect(gl_user.read_only_attribute?(:email)).to be_truthy
              end

              it "has synced attributes provider set to ldapmain" do
                expect(gl_user.user_synced_attributes_metadata.provider).to eql 'ldapmain'
              end
246
            end
R
Robert Speicher 已提交
247

248
            context "and LDAP user has an account already" do
249
              let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: dn, provider: 'ldapmain', username: 'john') }
250
              it "adds the omniauth identity to the LDAP account" do
251
                allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user)
252

253
                oauth_user.save
R
Robert Speicher 已提交
254

255 256 257
                expect(gl_user).to be_valid
                expect(gl_user.username).to eql 'john'
                expect(gl_user.email).to eql 'john@example.com'
R
Rémy Coutable 已提交
258
                expect(gl_user.identities.length).to be 2
259 260
                identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
                expect(identities_as_hash).to match_array(
261
                  [
262
                    { provider: 'ldapmain', extern_uid: dn },
263 264 265
                    { provider: 'twitter', extern_uid: uid }
                  ]
                )
266
              end
267
            end
268 269

            context 'when an LDAP person is not found by uid' do
270
              it 'tries to find an LDAP person by email and adds the omniauth identity to the user' do
271
                allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(nil)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_email).and_return(ldap_user)

                oauth_user.save

                identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
                expect(identities_as_hash).to match_array(result_identities(dn, uid))
              end

              context 'when also not found by email' do
                it 'tries to find an LDAP person by DN and adds the omniauth identity to the user' do
                  allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(nil)
                  allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_email).and_return(nil)
                  allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_dn).and_return(ldap_user)

                  oauth_user.save

                  identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
                  expect(identities_as_hash).to match_array(result_identities(dn, uid))
                end
              end
            end
293

294 295 296 297 298 299 300 301 302 303 304 305 306
            def result_identities(dn, uid)
              [
                { provider: 'ldapmain', extern_uid: dn },
                { provider: 'twitter', extern_uid: uid }
              ]
            end

            context 'when there is an LDAP connection error' do
              before do
                raise_ldap_connection_error
              end

              it 'does not save the identity' do
307 308 309
                oauth_user.save

                identities_as_hash = gl_user.identities.map { |id| { provider: id.provider, extern_uid: id.extern_uid } }
310
                expect(identities_as_hash).to match_array([{ provider: 'twitter', extern_uid: uid }])
311 312
              end
            end
313
          end
R
Robert Speicher 已提交
314

315 316 317 318 319 320 321 322 323 324
          context 'and a corresponding LDAP person with a non-default username' do
            before do
              allow(ldap_user).to receive(:uid) { uid }
              allow(ldap_user).to receive(:username) { 'johndoe@example.com' }
              allow(ldap_user).to receive(:email) { %w(johndoe@example.com john2@example.com) }
              allow(ldap_user).to receive(:dn) { dn }
            end

            context 'and no account for the LDAP user' do
              it 'creates a user favoring the LDAP username and strips email domain' do
325
                allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user)
326 327 328 329 330 331 332 333 334

                oauth_user.save

                expect(gl_user).to be_valid
                expect(gl_user.username).to eql 'johndoe'
              end
            end
          end

335
          context "and no corresponding LDAP person" do
336
            before do
337
              allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(nil)
338
            end
R
Robert Speicher 已提交
339

340 341
            include_examples "to verify compliance with allow_single_sign_on"
          end
342
        end
343
      end
344
    end
345

346 347
    describe 'blocking' do
      let(:provider) { 'twitter' }
348 349 350 351

      before do
        stub_omniauth_config(allow_single_sign_on: ['twitter'])
      end
352

353
      context 'signup with omniauth only' do
354
        context 'dont block on create' do
355 356 357
          before do
            stub_omniauth_config(block_auto_created_users: false)
          end
358 359 360

          it do
            oauth_user.save
361 362
            expect(gl_user).to be_valid
            expect(gl_user).not_to be_blocked
363 364 365 366
          end
        end

        context 'block on create' do
367 368 369
          before do
            stub_omniauth_config(block_auto_created_users: true)
          end
370 371 372

          it do
            oauth_user.save
373 374
            expect(gl_user).to be_valid
            expect(gl_user).to be_blocked
375 376 377 378
          end
        end
      end

379 380
      context 'signup with linked omniauth and LDAP account' do
        before do
R
Robert Speicher 已提交
381 382 383
          stub_omniauth_config(auto_link_ldap_user: true)
          allow(ldap_user).to receive(:uid) { uid }
          allow(ldap_user).to receive(:username) { uid }
384
          allow(ldap_user).to receive(:email) { ['johndoe@example.com', 'john2@example.com'] }
385
          allow(ldap_user).to receive(:dn) { dn }
386
          allow(Gitlab::Auth::LDAP::Person).to receive(:find_by_uid).and_return(ldap_user)
387 388 389 390
        end

        context "and no account for the LDAP user" do
          context 'dont block on create (LDAP)' do
391
            before do
392
              allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: false)
393
            end
394 395 396 397 398 399 400 401 402

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

          context 'block on create (LDAP)' do
403
            before do
404
              allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: true)
405
            end
406 407 408 409 410 411 412 413 414 415

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

        context 'and LDAP user has an account already' do
416
          let!(:existing_user) { create(:omniauth_user, email: 'john@example.com', extern_uid: dn, provider: 'ldapmain', username: 'john') }
417 418

          context 'dont block on create (LDAP)' do
419
            before do
420
              allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: false)
421
            end
422 423 424 425 426 427 428 429 430

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

          context 'block on create (LDAP)' do
431
            before do
432
              allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: true)
433
            end
434 435 436 437 438 439 440 441 442 443

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

444 445 446 447 448 449 450
      context 'sign-in' do
        before do
          oauth_user.save
          oauth_user.gl_user.activate
        end

        context 'dont block on create' do
451 452 453
          before do
            stub_omniauth_config(block_auto_created_users: false)
          end
454 455 456

          it do
            oauth_user.save
457 458
            expect(gl_user).to be_valid
            expect(gl_user).not_to be_blocked
459 460 461 462
          end
        end

        context 'block on create' do
463 464 465
          before do
            stub_omniauth_config(block_auto_created_users: true)
          end
466 467 468

          it do
            oauth_user.save
469 470
            expect(gl_user).to be_valid
            expect(gl_user).not_to be_blocked
471 472
          end
        end
473 474

        context 'dont block on create (LDAP)' do
475
          before do
476
            allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: false)
477
          end
478 479 480 481 482 483 484 485 486

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

        context 'block on create (LDAP)' do
487
          before do
488
            allow_any_instance_of(Gitlab::Auth::LDAP::Config).to receive_messages(block_auto_created_users: true)
489
          end
490 491 492 493 494 495 496

          it do
            oauth_user.save
            expect(gl_user).to be_valid
            expect(gl_user).not_to be_blocked
          end
        end
497 498
      end
    end
D
Dmitriy Zaporozhets 已提交
499
  end
500

501
  describe 'ensure backwards compatibility with with sync email from provider option' do
502 503 504 505
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    before do
      stub_omniauth_config(sync_email_from_provider: 'my-provider')
506
      stub_omniauth_config(sync_profile_from_provider: ['my-provider'])
507 508 509 510 511 512 513
    end

    context "when provider sets an email" do
      it "updates the user email" do
        expect(gl_user.email).to eq(info_hash[:email])
      end

514 515 516 517 518 519
      it "has email set as synced" do
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be_truthy
      end

      it "has email set as read-only" do
        expect(gl_user.read_only_attribute?(:email)).to be_truthy
520 521
      end

522
      it "has synced attributes provider set to my-provider" do
523
        expect(gl_user.user_synced_attributes_metadata.provider).to eql 'my-provider'
524 525 526 527 528 529 530 531 532 533 534 535
      end
    end

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

      it "does not update the user email" do
        expect(gl_user.email).not_to eq(info_hash[:email])
      end

536
      it "has email set as not synced" do
537
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be_falsey
538
      end
539 540 541 542

      it "does not have email set as read-only" do
        expect(gl_user.read_only_attribute?(:email)).to be_falsey
      end
543 544
    end
  end
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

  describe 'generating username' do
    context 'when no collision with existing user' do
      it 'generates the username with no counter' do
        expect(gl_user.username).to eq('johngitlab-ETC')
      end
    end

    context 'when collision with existing user' do
      it 'generates the username with a counter' do
        oauth_user.save
        oauth_user2 = described_class.new(OmniAuth::AuthHash.new(uid: 'my-uid2', provider: provider, info: { nickname: 'johngitlab-ETC@othermail.com', email: 'john@othermail.com' }))

        expect(oauth_user2.gl_user.username).to eq('johngitlab-ETC1')
      end
    end

    context 'when username is a reserved word' do
      let(:info_hash) do
        {
          nickname: 'admin@othermail.com',
          email: 'admin@othermail.com'
        }
      end
569

570 571 572 573 574
      it 'generates the username with a counter' do
        expect(gl_user.username).to eq('admin1')
      end
    end
  end
575 576 577 578 579 580 581 582 583 584 585 586 587 588

  describe 'updating email with sync profile' do
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    before do
      stub_omniauth_config(sync_profile_from_provider: ['my-provider'])
      stub_omniauth_config(sync_profile_attributes: true)
    end

    context "when provider sets an email" do
      it "updates the user email" do
        expect(gl_user.email).to eq(info_hash[:email])
      end

589
      it "has email set as synced" do
590 591 592
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
      end

593 594 595 596 597
      it "has email set as read-only" do
        expect(gl_user.read_only_attribute?(:email)).to be_truthy
      end

      it "has synced attributes provider set to my-provider" do
598 599 600 601 602 603 604 605 606 607 608
        expect(gl_user.user_synced_attributes_metadata.provider).to eql 'my-provider'
      end
    end

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

      it "does not update the user email" do
        expect(gl_user.email).not_to eq(info_hash[:email])
609 610 611 612 613 614 615 616
      end

      it "has email set as not synced" do
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be_falsey
      end

      it "does not have email set as read-only" do
        expect(gl_user.read_only_attribute?(:email)).to be_falsey
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
      end
    end
  end

  describe 'updating name' do
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    before do
      stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
      stub_omniauth_setting(sync_profile_attributes: true)
    end

    context "when provider sets a name" do
      it "updates the user name" do
        expect(gl_user.name).to eq(info_hash[:name])
      end
    end

    context "when provider doesn't set a name" do
      before do
        info_hash.delete(:name)
      end

      it "does not update the user name" do
        expect(gl_user.name).not_to eq(info_hash[:name])
        expect(gl_user.user_synced_attributes_metadata.name_synced).to be(false)
      end
    end
  end

  describe 'updating location' do
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    before do
      stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
      stub_omniauth_setting(sync_profile_attributes: true)
    end

    context "when provider sets a location" do
      it "updates the user location" do
        expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
        expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
      end
    end

    context "when provider doesn't set a location" do
      before do
        info_hash[:address].delete(:country)
        info_hash[:address].delete(:locality)
      end

      it "does not update the user location" do
        expect(gl_user.location).to be_nil
        expect(gl_user.user_synced_attributes_metadata.location_synced).to be(false)
      end
    end
  end

  describe 'updating user info' do
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    context "update all info" do
      before do
        stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
        stub_omniauth_setting(sync_profile_attributes: true)
      end

      it "updates the user email" do
        expect(gl_user.email).to eq(info_hash[:email])
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
      end

      it "updates the user name" do
        expect(gl_user.name).to eq(info_hash[:name])
        expect(gl_user.user_synced_attributes_metadata.name_synced).to be(true)
      end

      it "updates the user location" do
        expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
        expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
      end

      it "sets my-provider as the attributes provider" do
        expect(gl_user.user_synced_attributes_metadata.provider).to eql('my-provider')
      end
    end

    context "update only requested info" do
      before do
        stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
        stub_omniauth_setting(sync_profile_attributes: %w(name location))
      end

      it "updates the user name" do
        expect(gl_user.name).to eq(info_hash[:name])
        expect(gl_user.user_synced_attributes_metadata.name_synced).to be(true)
      end

      it "updates the user location" do
        expect(gl_user.location).to eq(info_hash[:address][:locality] + ', ' + info_hash[:address][:country])
        expect(gl_user.user_synced_attributes_metadata.location_synced).to be(true)
      end

      it "does not update the user email" do
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be(false)
      end
    end

    context "update default_scope" do
      before do
        stub_omniauth_setting(sync_profile_from_provider: ['my-provider'])
      end

      it "updates the user email" do
        expect(gl_user.email).to eq(info_hash[:email])
        expect(gl_user.user_synced_attributes_metadata.email_synced).to be(true)
      end
    end

    context "update no info when profile sync is nil" do
      it "does not have sync_attribute" do
        expect(gl_user.user_synced_attributes_metadata).to be(nil)
      end

      it "does not update the user email" do
        expect(gl_user.email).not_to eq(info_hash[:email])
      end

      it "does not update the user name" do
        expect(gl_user.name).not_to eq(info_hash[:name])
      end

      it "does not update the user location" do
        expect(gl_user.location).not_to eq(info_hash[:address][:country])
      end
752 753 754 755

      it 'does not create associated user synced attributes metadata' do
        expect(gl_user.user_synced_attributes_metadata).to be_nil
      end
756 757
    end
  end
758 759 760 761 762 763 764 765 766

  describe '.find_by_uid_and_provider' do
    let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') }

    it 'normalizes extern_uid' do
      allow(oauth_user.auth_hash).to receive(:uid).and_return('MY-UID')
      expect(oauth_user.find_user).to eql gl_user
    end
  end
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781

  describe '#find_ldap_person' do
    context 'when LDAP connection fails' do
      before do
        raise_ldap_connection_error
      end

      it 'returns nil' do
        adapter = Gitlab::Auth::LDAP::Adapter.new('ldapmain')
        hash = OmniAuth::AuthHash.new(uid: 'whatever', provider: 'ldapmain')

        expect(oauth_user.send(:find_ldap_person, hash, adapter)).to be_nil
      end
    end
  end
D
Dmitriy Zaporozhets 已提交
782
end