BigW Consortium Gitlab

identity_spec.rb 2.25 KB
Newer Older
1 2
require 'spec_helper'

3
describe Identity do
4 5 6 7 8 9 10 11 12 13 14 15 16 17
  describe 'relations' do
    it { is_expected.to belong_to(:user) }
  end

  describe 'fields' do
    it { is_expected.to respond_to(:provider) }
    it { is_expected.to respond_to(:extern_uid) }
  end

  describe '#is_ldap?' do
    let(:ldap_identity) { create(:identity, provider: 'ldapmain') }
    let(:other_identity) { create(:identity, provider: 'twitter') }

    it 'returns true if it is a ldap identity' do
Gabriel Mazetto committed
18
      expect(ldap_identity.ldap?).to be_truthy
19 20 21
    end

    it 'returns false if it is not a ldap identity' do
Gabriel Mazetto committed
22
      expect(other_identity.ldap?).to be_falsey
23 24
    end
  end
25 26 27 28 29 30 31 32 33 34 35

  describe '.with_extern_uid' do
    context 'LDAP identity' do
      let!(:ldap_identity) { create(:identity, provider: 'ldapmain', extern_uid: 'uid=john smith,ou=people,dc=example,dc=com') }

      it 'finds the identity when the DN is formatted differently' do
        identity = described_class.with_extern_uid('ldapmain', 'uid=John Smith, ou=People, dc=example, dc=com').first

        expect(identity).to eq(ldap_identity)
      end
    end
36 37 38 39 40 41 42 43 44 45

    context 'any other provider' do
      let!(:test_entity) { create(:identity, provider: 'test_provider', extern_uid: 'test_uid') }

      it 'the extern_uid lookup is case insensitive' do
        identity = described_class.with_extern_uid('test_provider', 'TEST_UID').first

        expect(identity).to eq(test_entity)
      end
    end
46
  end
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

  context 'callbacks' do
    context 'before_save' do
      describe 'normalizes extern uid' do
        let!(:ldap_identity) { create(:identity, provider: 'ldapmain', extern_uid: 'uid=john smith,ou=people,dc=example,dc=com') }

        it 'if extern_uid changes' do
          expect(ldap_identity).not_to receive(:ensure_normalized_extern_uid)
          ldap_identity.save
        end

        it 'if current_uid is nil' do
          expect(ldap_identity).to receive(:ensure_normalized_extern_uid)

          ldap_identity.update(extern_uid: nil)

          expect(ldap_identity.extern_uid).to be_nil
        end

        it 'if extern_uid changed and not nil' do
          ldap_identity.update(extern_uid: 'uid=john1,ou=PEOPLE,dc=example,dc=com')

          expect(ldap_identity.extern_uid).to eq 'uid=john1,ou=people,dc=example,dc=com'
        end
      end
    end
  end
74
end