BigW Consortium Gitlab

auth_hash_spec.rb 3.35 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan committed
3
describe Gitlab::OAuth::AuthHash, lib: true do
4
  let(:auth_hash) do
5
    Gitlab::OAuth::AuthHash.new(
6
      OmniAuth::AuthHash.new(
7 8
        provider: provider_ascii,
        uid: uid_ascii,
9 10
        info: info_hash
      )
11
    )
12
  end
13

14
  let(:uid_raw) do
15
    "CN=Onur K\xC3\xBC\xC3\xA7\xC3\xBCk,OU=Test,DC=example,DC=net"
16
  end
17
  let(:email_raw) { "onur.k\xC3\xBC\xC3\xA7\xC3\xBCk_ABC-123@example.net" }
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  let(:nickname_raw) { "ok\xC3\xBC\xC3\xA7\xC3\xBCk" }
  let(:first_name_raw) { 'Onur' }
  let(:last_name_raw) { "K\xC3\xBC\xC3\xA7\xC3\xBCk" }
  let(:name_raw) { "Onur K\xC3\xBC\xC3\xA7\xC3\xBCk" }

  let(:provider_ascii) { 'ldap'.force_encoding(Encoding::ASCII_8BIT) }
  let(:uid_ascii) { uid_raw.force_encoding(Encoding::ASCII_8BIT) }
  let(:email_ascii) { email_raw.force_encoding(Encoding::ASCII_8BIT) }
  let(:nickname_ascii) { nickname_raw.force_encoding(Encoding::ASCII_8BIT) }
  let(:first_name_ascii) { first_name_raw.force_encoding(Encoding::ASCII_8BIT) }
  let(:last_name_ascii) { last_name_raw.force_encoding(Encoding::ASCII_8BIT) }
  let(:name_ascii) { name_raw.force_encoding(Encoding::ASCII_8BIT) }

  let(:provider_utf8) { provider_ascii.force_encoding(Encoding::UTF_8) }
  let(:uid_utf8) { uid_ascii.force_encoding(Encoding::UTF_8) }
  let(:email_utf8) { email_ascii.force_encoding(Encoding::UTF_8) }
  let(:nickname_utf8) { nickname_ascii.force_encoding(Encoding::UTF_8) }
  let(:name_utf8) { name_ascii.force_encoding(Encoding::UTF_8) }

37
  let(:info_hash) do
38
    {
39
      email:      email_ascii,
40
      first_name: first_name_ascii,
41 42 43 44
      last_name:  last_name_ascii,
      name:       name_ascii,
      nickname:   nickname_ascii,
      uid:        uid_ascii
45
    }
46
  end
47

48 49 50 51 52 53
  context 'defaults' do
    it { expect(auth_hash.provider).to eql provider_utf8 }
    it { expect(auth_hash.uid).to eql uid_utf8 }
    it { expect(auth_hash.email).to eql email_utf8 }
    it { expect(auth_hash.username).to eql nickname_utf8 }
    it { expect(auth_hash.name).to eql name_utf8 }
54
    it { expect(auth_hash.password).not_to be_empty }
55 56
  end

57
  context 'email not provided' do
58
    before { info_hash.delete(:email) }
59 60

    it 'generates a temp email' do
61 62 63 64
      expect( auth_hash.email).to start_with('temp-email-for-oauth')
    end
  end

65
  context 'username not provided' do
66 67
    before { info_hash.delete(:nickname) }

68
    it 'takes the first part of the email as username' do
69
      expect(auth_hash.username).to eql 'onur.kucuk_ABC-123'
70 71 72
    end
  end

73
  context 'name not provided' do
74 75
    before { info_hash.delete(:name) }

76 77 78 79 80 81 82
    it 'concats first and lastname as the name' do
      expect(auth_hash.name).to eql name_utf8
    end
  end

  context 'auth_hash constructed with ASCII-8BIT encoding' do
    it 'forces utf8 encoding on uid' do
83
      expect(auth_hash.uid.encoding).to eql Encoding::UTF_8
84 85 86
    end

    it 'forces utf8 encoding on provider' do
87
      expect(auth_hash.provider.encoding).to eql Encoding::UTF_8
88 89 90
    end

    it 'forces utf8 encoding on name' do
91
      expect(auth_hash.name.encoding).to eql Encoding::UTF_8
92 93 94
    end

    it 'forces utf8 encoding on username' do
95
      expect(auth_hash.username.encoding).to eql Encoding::UTF_8
96 97 98
    end

    it 'forces utf8 encoding on email' do
99
      expect(auth_hash.email.encoding).to eql Encoding::UTF_8
100 101 102
    end

    it 'forces utf8 encoding on password' do
103
      expect(auth_hash.password.encoding).to eql Encoding::UTF_8
104 105
    end
  end
106
end