profiles_helper_spec.rb 3.28 KB
Newer Older
1 2 3
require 'rails_helper'

describe ProfilesHelper do
4 5 6
  describe '#commit_email_select_options' do
    it 'returns an array with private commit email along with all the verified emails' do
      user = create(:user)
7 8 9 10
      create(:email, user: user)
      confirmed_email1 = create(:email, :confirmed, user: user)
      confirmed_email2 = create(:email, :confirmed, user: user)

11 12 13 14
      private_email = user.private_commit_email

      emails = [
        ["Use a private email - #{private_email}", Gitlab::PrivateCommitEmail::TOKEN],
15 16 17
        user.email,
        confirmed_email1.email,
        confirmed_email2.email
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      ]

      expect(helper.commit_email_select_options(user)).to match_array(emails)
    end
  end

  describe '#selected_commit_email' do
    let(:user) { create(:user) }

    it 'returns main email when commit email attribute is nil' do
      expect(helper.selected_commit_email(user)).to eq(user.email)
    end

    it 'returns DB stored commit_email' do
      user.update(commit_email: Gitlab::PrivateCommitEmail::TOKEN)

      expect(helper.selected_commit_email(user)).to eq(Gitlab::PrivateCommitEmail::TOKEN)
    end
  end

38 39 40 41 42
  describe '#email_provider_label' do
    it "returns nil for users without external email" do
      user = create(:user)
      allow(helper).to receive(:current_user).and_return(user)

43
      expect(helper.attribute_provider_label(:email)).to be_nil
44 45
    end

46 47 48
    it "returns omniauth provider label for users with external attributes" do
      stub_omniauth_setting(sync_profile_from_provider: ['cas3'])
      stub_omniauth_setting(sync_profile_attributes: true)
49
      stub_cas_omniauth_provider
50 51
      cas_user = create(:omniauth_user, provider: 'cas3')
      cas_user.create_user_synced_attributes_metadata(provider: 'cas3', name_synced: true, email_synced: true, location_synced: true)
52 53
      allow(helper).to receive(:current_user).and_return(cas_user)

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      expect(helper.attribute_provider_label(:email)).to eq('CAS')
      expect(helper.attribute_provider_label(:name)).to eq('CAS')
      expect(helper.attribute_provider_label(:location)).to eq('CAS')
    end

    it "returns the correct omniauth provider label for users with some external attributes" do
      stub_omniauth_setting(sync_profile_from_provider: ['cas3'])
      stub_omniauth_setting(sync_profile_attributes: true)
      stub_cas_omniauth_provider
      cas_user = create(:omniauth_user, provider: 'cas3')
      cas_user.create_user_synced_attributes_metadata(provider: 'cas3', name_synced: false, email_synced: true, location_synced: false)
      allow(helper).to receive(:current_user).and_return(cas_user)

      expect(helper.attribute_provider_label(:name)).to be_nil
      expect(helper.attribute_provider_label(:email)).to eq('CAS')
      expect(helper.attribute_provider_label(:location)).to be_nil
70 71 72
    end

    it "returns 'LDAP' for users with external email but no email provider" do
73 74
      ldap_user = create(:omniauth_user)
      ldap_user.create_user_synced_attributes_metadata(email_synced: true)
75 76
      allow(helper).to receive(:current_user).and_return(ldap_user)

77
      expect(helper.attribute_provider_label(:email)).to eq('LDAP')
78 79 80 81 82 83 84 85 86 87 88 89
    end
  end

  def stub_cas_omniauth_provider
    provider = OpenStruct.new(
      'name' => 'cas3',
      'label' => 'CAS'
    )

    stub_omniauth_setting(providers: [provider])
  end
end