Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed unicode error and empty country by default #1351

Merged
merged 1 commit into from
Apr 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ install:
- make install
- pip install coveralls
- pip install flake8
- pip install django-auth-ldap==1.1.3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍



# command to run tests, e.g. python setup.py test
Expand Down
10 changes: 8 additions & 2 deletions src/ralph/account/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ def manager_country_attribute_populate(sender, profile, ldap_user, **kwargs):
if profile_map['manager'] in ldap_user.attrs:
manager_ref = ldap_user.attrs[profile_map['manager']][0]
# CN=John Smith,OU=TOR,OU=Corp-Users,DC=mydomain,DC=internal
manager_ref = manager_ref.decode('utf-8')
# prevent UnicodeEncodeError
if not isinstance(manager_ref, unicode):
manager_ref = manager_ref.decode('utf-8')
cn = manager_ref.split(',')[0][3:]
profile.manager = cn
# raw value from LDAP is in profile.country for this reason we assign
# some correct value
profile.country = None
if 'country' in profile_map:
if profile_map['country'] in ldap_user.attrs:
country = ldap_user.attrs[profile_map['country']][0]
if len(country) == 2:
profile.country = getattr(Country, country.lower())
# assign None if `country` doesn't exist in Country
profile.country = getattr(Country, country.lower(), None)

class MappedGroupOfNamesType(ActiveDirectoryGroupType):

Expand Down
45 changes: 45 additions & 0 deletions src/ralph/account/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.conf import settings
from ralph.account.models import Region
from ralph.account.tests import utils
from ralph.account.ldap import manager_country_attribute_populate


try:
Expand Down Expand Up @@ -71,3 +72,47 @@ def test_short_surname_stays_unmodified(self):
def test_truncate_works_when_no_sn(self):
ldap_dict = {}
_truncate_surname(ldap_dict)


@unittest.skipIf(NO_LDAP_MODULE, "'ldap' module is not installed")
class LdapPopulateTest(TestCase):

def _get_mocked_ldap_user(self, attrs):
class MockedLdapUser(object):
@property
def attrs(self):
return attrs
return MockedLdapUser()

def test_manager_country_attribute_populate_country(self):
ldap_attr = 'c'
override_settings = {'country': ldap_attr}
user = utils.UserFactory()
user_profile = user.get_profile()
faked_ldap_user = self._get_mocked_ldap_user({
ldap_attr: ['XXX']
})
with self.settings(AUTH_LDAP_PROFILE_ATTR_MAP=override_settings):
manager_country_attribute_populate(
None, user_profile, faked_ldap_user
)
self.assertEqual(user_profile.country, None)

def test_manager_country_attribute_populate_unicode_in_manager(self):
ldap_attr = 'manager'
override_settings = {'manager': ldap_attr}
manager_names = [unicode('Żółcień'), str('John Smith')]
user = utils.UserFactory()
user_profile = user.get_profile()

for manager_name in manager_names:
faked_ldap_user = self._get_mocked_ldap_user({
ldap_attr: ['CN={},OU=XXX,DC=group'.format(manager_name)]
})

with self.settings(AUTH_LDAP_PROFILE_ATTR_MAP=override_settings):
manager_country_attribute_populate(
None, user_profile, faked_ldap_user
)
self.assertEqual(user_profile.manager, manager_name)
self.assertTrue(isinstance(user_profile.manager, unicode))