Commit f1773640 authored by Michael Kozono's avatar Michael Kozono

Refactor spec

parent abe570cd
require 'spec_helper' require 'spec_helper'
describe Gitlab::LDAP::Person do describe Gitlab::LDAP::Person do
using RSpec::Parameterized::TableSyntax
include LdapHelpers include LdapHelpers
let(:entry) { ldap_user_entry('john.doe') } let(:entry) { ldap_user_entry('john.doe') }
...@@ -18,284 +19,129 @@ describe Gitlab::LDAP::Person do ...@@ -18,284 +19,129 @@ describe Gitlab::LDAP::Person do
describe '.normalize_uid_or_dn' do describe '.normalize_uid_or_dn' do
context 'given a DN' do context 'given a DN' do
context 'when there is extraneous (but valid) whitespace' do # Regarding the telephoneNumber test:
it 'removes the extraneous whitespace' do #
given = 'uid =John Smith , ou = People, dc= example,dc =com'
expected = 'uid=John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
context 'for a DN with a single RDN' do
it 'removes the extraneous whitespace' do
given = 'uid = John Smith'
expected = 'uid=John Smith'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end
context 'when there are escaped characters' do
it 'removes extraneous whitespace without changing the escaped characters' do
given = 'uid = Sebasti\\c3\\a1n\\ C.\\20Smith\\ , ou=People (aka. \\22humans\\") ,dc=example, dc=com'
expected = 'uid=Sebasti\\c3\\a1n\\ C.\\20Smith\\ ,ou=People (aka. \\22humans\\"),dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end
context 'with a multivalued RDN' do
it 'removes extraneous whitespace without modifying the multivalued RDN' do
given = 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com'
expected = 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
context 'with a telephoneNumber with a space after the plus sign' do
# I am not sure whether a space after the telephoneNumber plus sign is valid, # I am not sure whether a space after the telephoneNumber plus sign is valid,
# and I am not sure if this is "proper" behavior under these conditions, and # and I am not sure if this is "proper" behavior under these conditions, and
# I am not sure if it matters to us or anyone else, so rather than dig # I am not sure if it matters to us or anyone else, so rather than dig
# through RFCs, I am only documenting the behavior here. # through RFCs, I am only documenting the behavior here.
it 'removes the space after the plus sign in the telephoneNumber' do where(:test_description, :given, :expected) do
given = 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' 'strips extraneous whitespace' | 'uid =John Smith , ou = People, dc= example,dc =com' | 'uid=John Smith,ou=People,dc=example,dc=com'
expected = 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com' 'strips extraneous whitespace for a DN with a single RDN' | 'uid = John Smith' | 'uid=John Smith'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected) 'strips extraneous whitespace without changing escaped characters' | 'uid = Sebasti\\c3\\a1n\\ C.\\20Smith\\ , ou=People (aka. \\22humans\\") ,dc=example, dc=com' | 'uid=Sebasti\\c3\\a1n\\ C.\\20Smith\\ ,ou=People (aka. \\22humans\\"),dc=example,dc=com'
end 'strips extraneous whitespace without modifying the multivalued RDN' | 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com' | 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
end 'strips the space after the plus sign in the telephoneNumber' | 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' | 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
end 'for a null DN (empty string), returns empty string and does not error' | '' | ''
end 'does not strip the escaped leading space in an attribute value (and does not error like Net::LDAP::DN.new does)' | 'uid=\\ John Smith,ou=People,dc=example,dc=com' | 'uid=\\ John Smith,ou=People,dc=example,dc=com'
'does not strip the escaped trailing space in an attribute value' | 'uid=John Smith\\ ,ou=People,dc=example,dc=com' | 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
context 'for a null DN (empty string)' do 'does not strip the escaped leading newline in an attribute value' | 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com' | 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
it 'returns empty string and does not error' do 'does not strip the escaped trailing newline in an attribute value' | 'uid=John Smith\\\n,ou=People,dc=example,dc=com' | 'uid=John Smith\\\n,ou=People,dc=example,dc=com'
given = '' 'does not strip the unescaped leading newline in an attribute value' | 'uid=\nJohn Smith,ou=People,dc=example,dc=com' | 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
expected = '' 'does not strip the unescaped trailing newline in an attribute value' | 'uid=John Smith\n ,ou=People,dc=example,dc=com' | 'uid=John Smith\n,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected) 'does not modify casing' | 'UID=John Smith,ou=People,dc=example,dc=com' | 'UID=John Smith,ou=People,dc=example,dc=com'
end 'does not strip non whitespace' | 'uid=John Smith,ou=People,dc=example,dc=com' | 'uid=John Smith,ou=People,dc=example,dc=com'
end 'for a malformed DN (when an equal sign is escaped), returns the DN completely unmodified' | 'uid= foo\\=bar' | 'uid= foo\\=bar'
context 'when there is an escaped leading space in an attribute value' do
it 'does not remove the escaped leading space (and does not error like Net::LDAP::DN.new does)' do
given = 'uid=\\ John Smith,ou=People,dc=example,dc=com'
expected = 'uid=\\ John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end
context 'when there is an escaped trailing space in an attribute value' do
it 'does not remove the escaped trailing space' do
given = 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
expected = 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end
context 'when there is an escaped leading newline in an attribute value' do
it 'does not remove the escaped leading newline' do
given = 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
expected = 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end end
context 'when there is an escaped trailing newline in an attribute value' do with_them do
it 'does not remove the escaped trailing newline' do it 'normalizes the DN' do
given = 'uid=John Smith\\\n,ou=People,dc=example,dc=com' assert_generic_test(test_description, described_class.normalize_uid_or_dn(given), expected)
expected = 'uid=John Smith\\\n,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end end
context 'when there is an unescaped leading newline in an attribute value' do
it 'does not remove the unescaped leading newline' do
given = 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
expected = 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end end
end end
context 'when there is an unescaped trailing newline in an attribute value' do context 'given a UID' do
it 'does not remove the unescaped trailing newline' do where(:test_description, :given, :expected) do
given = 'uid=John Smith\n ,ou=People,dc=example,dc=com' 'strips extraneous whitespace' | ' John C. Smith ' | 'John C. Smith'
expected = 'uid=John Smith\n,ou=People,dc=example,dc=com' 'strips extraneous whitespace without changing escaped characters' | ' Sebasti\\c3\\a1n\\ C.\\20Smith\\ ' | 'Sebasti\\c3\\a1n\\ C.\\20Smith\\ '
expect(described_class.normalize_uid_or_dn(given)).to eq(expected) 'does not strip the escaped leading space in an attribute value' | ' \\ John Smith ' | '\\ John Smith'
end 'does not strip the escaped trailing space in an attribute value' | ' John Smith\\ ' | 'John Smith\\ '
end 'does not strip the escaped leading newline in an attribute value' | ' \\\nJohn Smith ' | '\\\nJohn Smith'
'does not strip the escaped trailing newline in an attribute value' | ' John Smith\\\n ' | 'John Smith\\\n'
context 'with uppercase characters' do 'does not strip the unescaped leading newline in an attribute value' | ' \nJohn Smith ' | '\nJohn Smith'
# We may need to normalize casing at some point. 'does not strip the unescaped trailing newline in an attribute value' | ' John Smith\n ' | 'John Smith\n'
# I am just making it explicit that we don't at this time. 'does not modify casing' | ' John Smith ' | 'John Smith'
it 'returns the DN with unmodified casing' do 'does not strip non whitespace' | 'John Smith' | 'John Smith'
given = 'UID=John Smith,ou=People,dc=example,dc=com'
expected = 'UID=John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end
end end
context 'with a malformed DN' do with_them do
context 'when an equal sign is escaped' do it 'normalizes the UID' do
it 'returns the DN completely unmodified' do assert_generic_test(test_description, described_class.normalize_uid_or_dn(given), expected)
given = 'uid= foo\\=bar'
expected = 'uid= foo\\=bar'
expect(described_class.normalize_uid_or_dn(given)).to eq(expected)
end end
end end
end end
end end
describe '.normalize_uid' do
context 'given a UID' do context 'given a UID' do
it 'returns the UID (with whitespace stripped)' do where(:test_description, :given, :expected) do
given = ' John C. Smith ' 'strips extraneous whitespace' | ' John C. Smith ' | 'John C. Smith'
expected = 'John C. Smith' 'strips extraneous whitespace without changing escaped characters' | ' Sebasti\\c3\\a1n\\ C.\\20Smith\\ ' | 'Sebasti\\c3\\a1n\\ C.\\20Smith\\ '
expect(described_class.normalize_uid_or_dn(given)).to eq(expected) 'does not strip the escaped leading space in an attribute value' | ' \\ John Smith ' | '\\ John Smith'
end 'does not strip the escaped trailing space in an attribute value' | ' John Smith\\ ' | 'John Smith\\ '
end 'does not strip the escaped leading newline in an attribute value' | ' \\\nJohn Smith ' | '\\\nJohn Smith'
'does not strip the escaped trailing newline in an attribute value' | ' John Smith\\\n ' | 'John Smith\\\n'
'does not strip the unescaped leading newline in an attribute value' | ' \nJohn Smith ' | '\nJohn Smith'
'does not strip the unescaped trailing newline in an attribute value' | ' John Smith\n ' | 'John Smith\n'
'does not modify casing' | ' John Smith ' | 'John Smith'
'does not strip non whitespace' | 'John Smith' | 'John Smith'
end end
describe '.normalize_uid' do with_them do
it 'returns the UID (with whitespace stripped)' do it 'normalizes the UID' do
given = ' John C. Smith ' assert_generic_test(test_description, described_class.normalize_uid(given), expected)
expected = 'John C. Smith'
expect(described_class.normalize_uid(given)).to eq(expected)
end
end end
describe '.normalize_dn' do
context 'when there is extraneous (but valid) whitespace' do
it 'removes the extraneous whitespace' do
given = 'uid =John Smith , ou = People, dc= example,dc =com'
expected = 'uid=John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end end
context 'for a DN with a single RDN' do
it 'removes the extraneous whitespace' do
given = 'uid = John Smith'
expected = 'uid=John Smith'
expect(described_class.normalize_dn(given)).to eq(expected)
end end
end end
context 'when there are escaped characters' do describe '.normalize_dn' do
it 'removes extraneous whitespace without changing the escaped characters' do # Regarding the telephoneNumber test:
given = 'uid = Sebasti\\c3\\a1n\\ C.\\20Smith\\ , ou=People (aka. \\22humans\\") ,dc=example, dc=com' #
expected = 'uid=Sebasti\\c3\\a1n\\ C.\\20Smith\\ ,ou=People (aka. \\22humans\\"),dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'with a multivalued RDN' do
it 'removes extraneous whitespace without modifying the multivalued RDN' do
given = 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com'
expected = 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
context 'with a telephoneNumber with a space after the plus sign' do
# I am not sure whether a space after the telephoneNumber plus sign is valid, # I am not sure whether a space after the telephoneNumber plus sign is valid,
# and I am not sure if this is "proper" behavior under these conditions, and # and I am not sure if this is "proper" behavior under these conditions, and
# I am not sure if it matters to us or anyone else, so rather than dig # I am not sure if it matters to us or anyone else, so rather than dig
# through RFCs, I am only documenting the behavior here. # through RFCs, I am only documenting the behavior here.
it 'removes the space after the plus sign in the telephoneNumber' do where(:test_description, :given, :expected) do
given = 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' 'strips extraneous whitespace' | 'uid =John Smith , ou = People, dc= example,dc =com' | 'uid=John Smith,ou=People,dc=example,dc=com'
expected = 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com' 'strips extraneous whitespace for a DN with a single RDN' | 'uid = John Smith' | 'uid=John Smith'
expect(described_class.normalize_dn(given)).to eq(expected) 'strips extraneous whitespace without changing escaped characters' | 'uid = Sebasti\\c3\\a1n\\ C.\\20Smith\\ , ou=People (aka. \\22humans\\") ,dc=example, dc=com' | 'uid=Sebasti\\c3\\a1n\\ C.\\20Smith\\ ,ou=People (aka. \\22humans\\"),dc=example,dc=com'
end 'strips extraneous whitespace without modifying the multivalued RDN' | 'uid = John Smith + telephoneNumber = +1 555-555-5555 , ou = People,dc=example,dc=com' | 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
end 'strips the space after the plus sign in the telephoneNumber' | 'uid = John Smith + telephoneNumber = + 1 555-555-5555 , ou = People,dc=example,dc=com' | 'uid=John Smith+telephoneNumber=+1 555-555-5555,ou=People,dc=example,dc=com'
end 'for a null DN (empty string), returns empty string and does not error' | '' | ''
'does not strip the escaped leading space in an attribute value (and does not error like Net::LDAP::DN.new does)' | 'uid=\\ John Smith,ou=People,dc=example,dc=com' | 'uid=\\ John Smith,ou=People,dc=example,dc=com'
'does not strip the escaped trailing space in an attribute value' | 'uid=John Smith\\ ,ou=People,dc=example,dc=com' | 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
'does not strip the escaped leading newline in an attribute value' | 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com' | 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
'does not strip the escaped trailing newline in an attribute value' | 'uid=John Smith\\\n,ou=People,dc=example,dc=com' | 'uid=John Smith\\\n,ou=People,dc=example,dc=com'
'does not strip the unescaped leading newline in an attribute value' | 'uid=\nJohn Smith,ou=People,dc=example,dc=com' | 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
'does not strip the unescaped trailing newline in an attribute value' | 'uid=John Smith\n ,ou=People,dc=example,dc=com' | 'uid=John Smith\n,ou=People,dc=example,dc=com'
'does not modify casing' | 'UID=John Smith,ou=People,dc=example,dc=com' | 'UID=John Smith,ou=People,dc=example,dc=com'
'does not strip non whitespace' | 'uid=John Smith,ou=People,dc=example,dc=com' | 'uid=John Smith,ou=People,dc=example,dc=com'
'for a malformed DN (when an equal sign is escaped), returns the DN completely unmodified' | 'uid= foo\\=bar' | 'uid= foo\\=bar'
end end
context 'for a null DN (empty string)' do with_them do
it 'returns empty string and does not error' do it 'normalizes the DN' do
given = '' assert_generic_test(test_description, described_class.normalize_dn(given), expected)
expected = ''
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an escaped leading space in an attribute value' do
it 'does not remove the escaped leading space (and does not error like Net::LDAP::DN.new does)' do
given = 'uid=\\ John Smith,ou=People,dc=example,dc=com'
expected = 'uid=\\ John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an escaped trailing space in an attribute value' do
it 'does not remove the escaped trailing space' do
given = 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
expected = 'uid=John Smith\\ ,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an escaped leading newline in an attribute value' do
it 'does not remove the escaped leading newline' do
given = 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
expected = 'uid=\\\nJohn Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an escaped trailing newline in an attribute value' do
it 'does not remove the escaped trailing newline' do
given = 'uid=John Smith\\\n,ou=People,dc=example,dc=com'
expected = 'uid=John Smith\\\n,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an unescaped leading newline in an attribute value' do
it 'does not remove the unescaped leading newline' do
given = 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
expected = 'uid=\nJohn Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'when there is an unescaped trailing newline in an attribute value' do
it 'does not remove the unescaped trailing newline' do
given = 'uid=John Smith\n ,ou=People,dc=example,dc=com'
expected = 'uid=John Smith\n,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'with uppercase characters' do
# We may need to normalize casing at some point.
# I am just making it explicit that we don't at this time.
it 'returns the DN with unmodified casing' do
given = 'UID=John Smith,ou=People,dc=example,dc=com'
expected = 'UID=John Smith,ou=People,dc=example,dc=com'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end
context 'with a malformed DN' do
context 'when an equal sign is escaped' do
it 'returns the DN completely unmodified' do
given = 'uid= foo\\=bar'
expected = 'uid= foo\\=bar'
expect(described_class.normalize_dn(given)).to eq(expected)
end
end end
end end
end end
describe '.is_dn?' do describe '.is_dn?' do
context 'given a DN' do where(:test_description, :given, :expected) do
context 'with a single RDN' do 'given a DN with a single RDN' | 'uid=John C. Smith' | true
it 'returns true' do 'given a DN with multiple RDNs' | 'uid=John C. Smith,ou=People,dc=example,dc=com' | true
expect(described_class.is_dn?('uid=John Smith')).to be_truthy 'given a UID' | 'John C. Smith' | false
end 'given a DN with a single RDN with excess spaces' | ' uid=John C. Smith ' | true
end 'given a DN with multiple RDNs with excess spaces' | ' uid=John C. Smith,ou=People,dc=example,dc=com ' | true
'given a UID with excess spaces' | ' John C. Smith ' | false
context 'with multiple RDNs' do 'given a DN with an escaped equal sign' | 'uid=John C. Smith,ou=People\\=' | true
it 'returns true' do 'given a DN with an equal sign in escaped hex' | 'uid=John C. Smith,ou=People\\3D' | true
expect(described_class.is_dn?('uid=John Smith,ou=People,dc=example,dc=com')).to be_truthy
end
end
end end
context 'given a UID' do with_them do
it 'returns false' do it 'returns the expected boolean' do
expect(described_class.is_dn?('John Smith')).to be_falsey assert_generic_test(test_description, described_class.is_dn?(given), expected)
end end
end end
end end
...@@ -327,4 +173,9 @@ describe Gitlab::LDAP::Person do ...@@ -327,4 +173,9 @@ describe Gitlab::LDAP::Person do
expect(person.email).to eq([user_principal_name]) expect(person.email).to eq([user_principal_name])
end end
end end
def assert_generic_test(test_description, got, expected)
test_failure_message = "Failed test description: '#{test_description}'\n\n expected: #{expected}\n got: #{got}"
expect(got).to eq(expected), test_failure_message
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment