def test_fetch_user_details_lists_expanded(self): func = ldap_lookup.fetch_user_details fake_user = [ ('[email protected],o=com,dc=mozilla', {'cn': ['Peter Bengtsson'], 'givenName': ['Pet\xc3\xa3r', 'Two'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': [], 'uid': ['pbengtsson'] }) ] key = '[email protected]' _wrapper = ldap_lookup.account_wrap_search_filter ldap.initialize = Mock(return_value=MockLDAP({ _wrapper(key): fake_user, } )) # search by uid result = func('*****@*****.**') assert result eq_(result['givenName'], [u'Pet\xe3r', u'Two']) ok_(isinstance(result['givenName'][0], unicode)) ok_(isinstance(result['givenName'][1], unicode)) eq_(result['sn'], u'')
def test_login_failure(self): ldap.initialize = Mock(return_value=MockLDAP({ '([email protected])': 'anything', }, credentials={ 'mail=mortal,o=com,dc=mozilla': 'secret', })) url = reverse('users.login') mortal = User.objects.create( username='******', first_name='Mortal', last_name='Joe', email='*****@*****.**', ) mortal.set_password('secret') mortal.save() response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 200) ok_('errorlist' in response.content) response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 200) ok_('errorlist' in response.content)
def test_fetch_user_details(self): func = ldap_lookup.fetch_user_details fake_user = [ ('[email protected],o=com,dc=mozilla', {'cn': ['Peter Bengtsson'], 'givenName': ['Pet\xc3\xa3r'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['pbengtsson'] }) ] _key = '[email protected]' _key = ldap_lookup.account_wrap_search_filter(_key) ldap.initialize = Mock(return_value=MockLDAP({ _key: fake_user, } )) details = func('xxx') ok_(not details) details = func('*****@*****.**') assert details eq_(details['givenName'], u'Pet\xe3r') different_fake_user = [ ('mail=mortal,o=com,dc=mozilla', {'cn': ['Peter Bengtsson'], 'givenName': ['Different'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['pbengtsson'] }) ] ldap.initialize = Mock(return_value=MockLDAP({ _key: different_fake_user, } )) details = func('*****@*****.**') eq_(details['givenName'], u'Pet\xe3r') details = func('*****@*****.**', force_refresh=True) eq_(details['givenName'], u'Different')
def test_search_users_canonical_search(self): func = ldap_lookup.search_users fake_user = [ ('[email protected],o=com,dc=mozilla', {'cn': ['Peter Bengtsson'], 'givenName': ['Pet\xc3\xa3r'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['pbengtsson'] }) ] key = '[email protected]' _wrapper = ldap_lookup.account_wrap_search_filter ldap.initialize = Mock(return_value=MockLDAP({ _wrapper(key): fake_user, } )) # search by uid result = func('Peter bengtsson', 1) ok_(not result) ldap.initialize = Mock(return_value=MockLDAP({ _wrapper(key): fake_user, _wrapper('cn=*PETER BENGTSSON*'): fake_user, } )) result = func('PETER BENGTSSON', 1) ok_(result) eq_(result[0]['cn'], 'Peter Bengtsson') ldap.initialize = Mock(return_value=MockLDAP({ _wrapper(key): fake_user, _wrapper('(|(mail=PETER BENGT*)(givenName=PETER BENGT*)(sn=PETER ' 'BENGT*)(cn=PETER BENGT*))'): fake_user, } )) result = func('PETER BENGT', 1, autocomplete=True) ok_(result) eq_(result[0]['cn'], 'Peter Bengtsson')
def test_login_with_local_django_user(self): ldap.initialize = Mock(return_value=MockLDAP({ '([email protected])': 'anything', }, credentials={ 'mail=mortal,o=com,dc=mozilla': 'secret', })) url = reverse('users.login') response = self.client.get(url) eq_(response.status_code, 200) mortal = User.objects.create( username='******', first_name='Mortal', last_name='Joe' ) mortal.set_password('secret') mortal.save() response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 200) ok_('errorlist' in response.content) response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 302) path = urlparse(response['location']).path eq_(path, settings.LOGIN_REDIRECT_URL) response = self.client.get('/') eq_(response.status_code, 200) ok_('Mortal' in response.content) url = reverse('users.logout') response = self.client.get(url) eq_(response.status_code, 302) path = urlparse(response['location']).path eq_(path, settings.LOGOUT_REDIRECT_URL) response = self.client.get('/') path = urlparse(response['location']).path eq_(path, settings.LOGIN_URL) eq_(response.status_code, 302) response = self.client.get(settings.LOGIN_URL) eq_(response.status_code, 200) ok_('Mortal' not in response.content)
def test_users(self): results = [( '[email protected],o=com,dc=mozilla', { 'cn': ['Peter Bengtsson'], 'givenName': ['Pet\xc3\xa3r'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['pbengtsson'] })] ldap.initialize = Mock(return_value=MockLDAP({ '(&(objectClass=inetOrgPerson)(mail=*)(|(mail=peter*)(givenName=peter*)(sn=peter*)))': results })) url = reverse('autocomplete.users') response = self.client.get(url, {'term': ' i '}) eq_(response.status_code, 403) mortal = User.objects.create(username='******', first_name='Mortal', last_name='Joe') mortal.set_password('secret') mortal.save() assert self.client.login(username='******', password='******') response = self.client.get(url, {'term': ' i '}) eq_(response.status_code, 200) ok_(response['content-type'].startswith('application/json')) response = self.client.get(url, {'term': 'peter'}) eq_(response.status_code, 200) ok_(response['content-type'].startswith('application/json')) struct = json.loads(response.content) ok_(isinstance(struct, list)) first_item = struct[0] label = '%s %s <%s>' % (u'Pet\xe3r', u'Bengtss\xa2n', '*****@*****.**') value = label eq_(first_item, { 'id': 'pbengtsson', 'label': label, 'value': value, })
def test_search_users_invalid_email(self): func = ldap_lookup.search_users fake_user = [ ('[email protected],o=com,dc=mozilla', {'cn': ['Peter Bengtsson'], 'givenName': ['Pet\xc3\xa3r'], # utf-8 encoded 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['pbengtsson'] }) ] key = '[email protected]' _wrapper = ldap_lookup.account_wrap_search_filter ldap.initialize = Mock(return_value=MockLDAP({ _wrapper(key): fake_user, } )) result = func('mortal@[email protected]', 10) ok_(not result)
def test_login_with_ldap_user(self): fake_user = [ ('mail=mortal,o=com,dc=mozilla', {'cn': ['Mortal Bengtsson'], 'givenName': ['Mortal'], 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['mortal'], }) ] fake_user_plus = [ ('mail=mortal,o=com,dc=mozilla', {'cn': ['Mortal Bengtsson'], 'givenName': ['Mortal'], 'mail': ['*****@*****.**'], 'sn': ['Bengtss\xc2\xa2n'], 'uid': ['mortal'], 'manager': ['[email protected],dc=foo'], 'physicalDeliveryOfficeName': ['London:::GB'], }) ] ldap.initialize = Mock(return_value=MockLDAP({ '[email protected],o=com,dc=mozilla': fake_user, '([email protected])': fake_user_plus, }, credentials={ '[email protected],o=com,dc=mozilla': 'secret', })) url = reverse('users.login') response = self.client.get(url) eq_(response.status_code, 200) response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 200) ok_('errorlist' in response.content) response = self.client.post(url, {'username': '******', 'password': '******'}) eq_(response.status_code, 302) path = urlparse(response['location']).path eq_(path, settings.LOGIN_REDIRECT_URL) response = self.client.get('/') eq_(response.status_code, 200) ok_('Mortal' in response.content) user, = User.objects.all() eq_(user.email, '*****@*****.**') eq_(user.username, 'mortal') eq_(user.first_name, u'Mortal') eq_(user.last_name, u'Bengtss\xa2n') profile = user.get_profile() eq_(profile.manager, '*****@*****.**') eq_(profile.office, u'London:::GB') eq_(profile.country, u'GB') eq_(profile.city, u'London') url = reverse('users.logout') response = self.client.get(url) eq_(response.status_code, 302) path = urlparse(response['location']).path eq_(path, settings.LOGOUT_REDIRECT_URL) response = self.client.get('/') path = urlparse(response['location']).path eq_(path, settings.LOGIN_URL) eq_(response.status_code, 302) response = self.client.get(settings.LOGIN_URL) eq_(response.status_code, 200) ok_('Mortal' not in response.content)