ce1e7d1099
- Force python-ldap >= 3 to get proper bytes/string management. - Replace common['name'] with common['first_name'] because the name is already broken by get_names. Actually, allauth breaks down common['name'] in 'first_name + last_name'. - Decrease timeout of LDAP requests to 5s. - Fix get_names in case the LDAP returned a string without any space character. - Fix get_names in case the LDAP doesn't return any cn, which was failing with Py3. - Add tests about populating user data from LDAP.
125 lines
3.8 KiB
Python
125 lines
3.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from allauth_cas.test.testcases import CASTestCase, CASViewTestCase
|
|
|
|
try:
|
|
from unittest import mock
|
|
except ImportError:
|
|
import mock
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class ClipperProviderTests(CASTestCase):
|
|
|
|
def test_auto_signup(self):
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='clipper_uid')
|
|
|
|
u = User.objects.get(username='clipper_uid')
|
|
self.assertEqual(u.email, 'clipper_uid@clipper.ens.fr')
|
|
|
|
|
|
class ClipperViewsTests(CASViewTestCase):
|
|
|
|
def test_login_view(self):
|
|
r = self.client.get('/accounts/clipper/login/')
|
|
expected = (
|
|
"https://cas.eleves.ens.fr/login?service=http%3A%2F%2Ftestserver"
|
|
"%2Faccounts%2Fclipper%2Flogin%2Fcallback%2F"
|
|
)
|
|
self.assertRedirects(
|
|
r, expected,
|
|
fetch_redirect_response=False,
|
|
)
|
|
|
|
def test_callback_view(self):
|
|
# Required to initialize a SocialLogin.
|
|
r = self.client.get('/accounts/clipper/login/')
|
|
|
|
# Tests.
|
|
self.patch_cas_response(valid_ticket='__all__')
|
|
r = self.client.get('/accounts/clipper/login/callback/', {
|
|
'ticket': '123456',
|
|
})
|
|
self.assertLoginSuccess(r)
|
|
|
|
def test_logout_view(self):
|
|
r = self.client.get('/accounts/clipper/logout/')
|
|
expected = (
|
|
"https://cas.eleves.ens.fr/logout?service=http%3A%2F%2Ftestserver"
|
|
"%2F"
|
|
)
|
|
self.assertRedirects(
|
|
r, expected,
|
|
fetch_redirect_response=False,
|
|
)
|
|
|
|
|
|
class ClipperLDAPTests(CASTestCase):
|
|
|
|
def setUp(self):
|
|
self.mock_ldap_conn = mock.Mock()
|
|
self.mock_ldap_conn.search_s = mock.Mock(return_value=[])
|
|
|
|
patch_get_ldap_conn = mock.patch(
|
|
'allauth_ens.providers.clipper.utils.get_ldap_connection',
|
|
return_value=self.mock_ldap_conn,
|
|
)
|
|
patch_get_ldap_conn.start()
|
|
self.addCleanup(patch_get_ldap_conn.stop)
|
|
|
|
def set_returned_fullname(self, fullname):
|
|
try:
|
|
bfullname = bytes(fullname, 'utf-8')
|
|
except TypeError:
|
|
bfullname = bytes(fullname)
|
|
self.mock_ldap_conn.search_s.return_value = [[None, {'cn': bfullname}]]
|
|
|
|
def test_ok(self):
|
|
self.set_returned_fullname('abc def ghi')
|
|
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='theclipper')
|
|
|
|
u = User.objects.get(username='theclipper')
|
|
self.assertEqual(u.first_name, 'abc')
|
|
self.assertEqual(u.last_name, 'def ghi')
|
|
|
|
def test_short_fullname(self):
|
|
self.set_returned_fullname('abc')
|
|
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='theclipper')
|
|
|
|
u = User.objects.get(username='theclipper')
|
|
self.assertEqual(u.first_name, 'abc')
|
|
self.assertEqual(u.last_name, '')
|
|
|
|
def test_bad_uid(self):
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='the_clipper')
|
|
|
|
self.mock_ldap_conn.search_s.assert_not_called()
|
|
u = User.objects.get(username='the_clipper')
|
|
self.assertEqual(u.first_name, '')
|
|
self.assertEqual(u.last_name, '')
|
|
|
|
def test_no_result(self):
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='theclipper')
|
|
|
|
u = User.objects.get(username='theclipper')
|
|
self.assertEqual(u.first_name, '')
|
|
self.assertEqual(u.last_name, '')
|
|
|
|
def test_no_cn(self):
|
|
self.mock_ldap_conn.search_s.return_value = [[None, {}]]
|
|
|
|
self.client_cas_login(
|
|
self.client, provider_id='clipper', username='theclipper')
|
|
|
|
u = User.objects.get(username='theclipper')
|
|
self.assertEqual(u.first_name, '')
|
|
self.assertEqual(u.last_name, '')
|