2018-06-16 17:02:32 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-01-02 17:06:12 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
import django
|
|
|
|
from django.contrib.auth import HASH_SESSION_KEY, get_user_model
|
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.core import mail
|
|
|
|
from django.test import TestCase, override_settings
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
from allauth.socialaccount.models import SocialAccount
|
|
|
|
|
2018-06-16 17:02:32 +02:00
|
|
|
import six
|
2018-04-29 01:28:09 +02:00
|
|
|
from allauth_cas.test.testcases import CASTestCase
|
2018-04-22 20:13:42 +02:00
|
|
|
from fakeldap import MockLDAP
|
2018-04-29 01:28:09 +02:00
|
|
|
from mock import patch
|
2018-04-22 20:13:42 +02:00
|
|
|
|
2018-10-06 22:57:49 +02:00
|
|
|
from allauth_ens.utils import get_ldap_infos
|
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
from .adapter import deprecate_clippers, install_longterm_adapter
|
2018-04-22 20:13:42 +02:00
|
|
|
|
|
|
|
_mock_ldap = MockLDAP()
|
2018-06-03 22:10:34 +02:00
|
|
|
ldap_patcher = patch('allauth_ens.utils.ldap.initialize',
|
2018-04-29 01:28:09 +02:00
|
|
|
lambda x: _mock_ldap)
|
2018-04-22 20:13:42 +02:00
|
|
|
|
2018-01-02 17:06:12 +01:00
|
|
|
if django.VERSION >= (1, 10):
|
|
|
|
from django.urls import reverse
|
|
|
|
else:
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
def prevent_logout_pwd_change(client, user):
|
|
|
|
"""
|
|
|
|
Updating a user's password logs out all sessions for the user.
|
|
|
|
By calling this function this behavior will be prevented.
|
|
|
|
|
|
|
|
See this link, and the source code of `update_session_auth_hash`:
|
|
|
|
https://docs.djangoproject.com/en/dev/topics/auth/default/#session-invalidation-on-password-change
|
|
|
|
"""
|
|
|
|
if hasattr(user, 'get_session_auth_hash'):
|
|
|
|
session = client.session
|
|
|
|
session[HASH_SESSION_KEY] = user.get_session_auth_hash()
|
|
|
|
session.save()
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-01-02 17:06:12 +01:00
|
|
|
class ViewsTests(TestCase):
|
2018-04-23 00:38:37 +02:00
|
|
|
"""
|
2018-01-02 17:06:12 +01:00
|
|
|
Checks (barely) that templates do not contain errors.
|
2018-04-23 00:38:37 +02:00
|
|
|
"""
|
2018-01-02 17:06:12 +01:00
|
|
|
def setUp(self):
|
2018-04-23 00:35:57 +02:00
|
|
|
self.u = User.objects.create_user('user', 'user@mail.net', 'user')
|
|
|
|
|
2018-01-02 17:06:12 +01:00
|
|
|
Site.objects.filter(pk=1).update(domain='testserver')
|
|
|
|
|
|
|
|
def _login(self, client=None):
|
|
|
|
if client is None:
|
|
|
|
client = self.client
|
|
|
|
client.login(username='user', password='user')
|
|
|
|
|
|
|
|
def _get_confirm_email_link(self, email_msg):
|
|
|
|
m = re.search(
|
2018-04-23 00:35:57 +02:00
|
|
|
r'http://testserver(/accounts/confirm-email/.*/)',
|
2018-01-02 17:06:12 +01:00
|
|
|
email_msg.body,
|
|
|
|
)
|
|
|
|
return m.group(1)
|
|
|
|
|
|
|
|
def _get_reset_password_link(self, email_msg):
|
|
|
|
m = re.search(
|
2018-04-23 00:35:57 +02:00
|
|
|
r'http://testserver(/accounts/password/reset/key/.*/)',
|
2018-01-02 17:06:12 +01:00
|
|
|
email_msg.body,
|
|
|
|
)
|
|
|
|
return m.group(1)
|
|
|
|
|
|
|
|
def test_account_signup(self):
|
|
|
|
r = self.client.get(reverse('account_signup'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
ACCOUNT_ADAPTER='tests.adapter.ClosedSignupAccountAdapter',
|
|
|
|
)
|
|
|
|
def test_account_closed_signup(self):
|
|
|
|
r = self.client.get(reverse('account_signup'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_login(self):
|
|
|
|
r = self.client.get(reverse('account_login'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_logout(self):
|
|
|
|
self._login()
|
|
|
|
r = self.client.get(reverse('account_logout'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_change_password(self):
|
|
|
|
self._login()
|
|
|
|
r = self.client.get(reverse('account_change_password'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_set_password(self):
|
|
|
|
self._login()
|
|
|
|
self.u.set_unusable_password()
|
|
|
|
self.u.save()
|
|
|
|
prevent_logout_pwd_change(self.client, self.u)
|
|
|
|
|
|
|
|
r = self.client.get(reverse('account_set_password'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_inactive(self):
|
|
|
|
r = self.client.get(reverse('account_inactive'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_email(self):
|
|
|
|
self._login()
|
|
|
|
r = self.client.get(reverse('account_email'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_email_verification_sent(self):
|
|
|
|
self._login()
|
|
|
|
r = self.client.get(reverse('account_email_verification_sent'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_confirm_email(self):
|
|
|
|
self._login()
|
|
|
|
self.client.post(reverse('account_email'), {
|
|
|
|
'action_add': '',
|
|
|
|
'email': 'test@mail.net',
|
|
|
|
})
|
|
|
|
confirm_url = self._get_confirm_email_link(mail.outbox[0])
|
|
|
|
|
|
|
|
r = self.client.get(confirm_url)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_reset_password(self):
|
|
|
|
r = self.client.get(reverse('account_reset_password'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_reset_password_done(self):
|
|
|
|
r = self.client.get(reverse('account_reset_password_done'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_reset_password_from_key(self):
|
|
|
|
self.client.post(reverse('account_reset_password'), {
|
|
|
|
'email': 'user@mail.net',
|
|
|
|
})
|
|
|
|
reset_url = self._get_reset_password_link(mail.outbox[0])
|
|
|
|
|
|
|
|
r = self.client.get(reset_url, follow=True)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
def test_account_reset_password_from_key_done(self):
|
|
|
|
r = self.client.get(reverse('account_reset_password_from_key_done'))
|
|
|
|
self.assertEqual(r.status_code, 200)
|
2018-04-22 20:13:42 +02:00
|
|
|
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
@override_settings(
|
|
|
|
SOCIALACCOUNT_ADAPTER='allauth_ens.adapter.LongTermClipperAccountAdapter'
|
|
|
|
)
|
|
|
|
class LongTermClipperTests(CASTestCase):
|
2018-04-22 20:13:42 +02:00
|
|
|
def setUp(self):
|
|
|
|
ldap_patcher.start()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2018-04-29 01:28:09 +02:00
|
|
|
ldap_patcher.stop()
|
2018-04-22 20:13:42 +02:00
|
|
|
_mock_ldap.reset()
|
|
|
|
|
2018-04-23 01:13:03 +02:00
|
|
|
def _setup_ldap(self, promo=12, username="test"):
|
2018-06-16 17:02:32 +02:00
|
|
|
try:
|
|
|
|
buid = six.binary_type(username, 'utf-8')
|
2018-09-29 23:58:32 +02:00
|
|
|
home = six.binary_type('/users/%d/phy/test/' % promo, 'utf-8')
|
2018-06-16 17:02:32 +02:00
|
|
|
except TypeError:
|
|
|
|
buid = six.binary_type(username)
|
2018-09-29 23:58:32 +02:00
|
|
|
home = six.binary_type('/users/%d/phy/test/' % promo)
|
2018-04-29 01:28:09 +02:00
|
|
|
_mock_ldap.directory['dc=spi,dc=ens,dc=fr'] = {
|
2018-06-16 17:02:32 +02:00
|
|
|
'uid': [buid],
|
2018-04-29 01:28:09 +02:00
|
|
|
'cn': [b'John Smith'],
|
|
|
|
'mailRoutingAddress': [b'test@clipper.ens.fr'],
|
2018-09-29 23:58:32 +02:00
|
|
|
'homeDirectory': [home],
|
2018-04-23 00:01:27 +02:00
|
|
|
}
|
2018-04-28 16:33:08 +02:00
|
|
|
|
|
|
|
def _count_ldap_queries(self):
|
|
|
|
queries = _mock_ldap.ldap_methods_called()
|
2018-09-29 23:32:50 +02:00
|
|
|
count = len([op for op in queries if op != 'set_option'])
|
2018-04-28 16:33:08 +02:00
|
|
|
return count
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
def test_new_connexion(self):
|
|
|
|
self._setup_ldap()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-22 20:13:42 +02:00
|
|
|
u = r.context['user']
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
self.assertEqual(u.username, "test@12")
|
|
|
|
self.assertEqual(u.first_name, "John")
|
|
|
|
self.assertEqual(u.last_name, "Smith")
|
|
|
|
self.assertEqual(u.email, "test@clipper.ens.fr")
|
2018-04-28 16:33:08 +02:00
|
|
|
self.assertEqual(self._count_ldap_queries(), 1)
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
sa = list(SocialAccount.objects.all())[-1]
|
|
|
|
self.assertEqual(sa.user.id, u.id)
|
2018-06-16 17:02:32 +02:00
|
|
|
self.assertEqual(sa.extra_data['ldap']['entrance_year'], '12')
|
2018-04-22 20:13:42 +02:00
|
|
|
|
2018-04-23 00:01:27 +02:00
|
|
|
def test_connect_disconnect(self):
|
|
|
|
self._setup_ldap()
|
2018-04-29 01:28:09 +02:00
|
|
|
r0 = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-23 00:01:27 +02:00
|
|
|
self.assertIn("_auth_user_id", self.client.session)
|
|
|
|
self.assertIn('user', r0.context)
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
self.client.logout()
|
2018-04-23 00:01:27 +02:00
|
|
|
self.assertNotIn("_auth_user_id", self.client.session)
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
def test_second_connexion(self):
|
|
|
|
self._setup_ldap()
|
|
|
|
|
2018-04-29 01:28:09 +02:00
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-22 20:13:42 +02:00
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
nu = User.objects.count()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-22 20:13:42 +02:00
|
|
|
self.assertEqual(User.objects.count(), nu)
|
2018-04-28 16:33:08 +02:00
|
|
|
self.assertEqual(self._count_ldap_queries(), 1)
|
2018-04-22 20:13:42 +02:00
|
|
|
|
|
|
|
def test_deprecation(self):
|
|
|
|
self._setup_ldap()
|
2018-04-29 01:28:09 +02:00
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-22 20:13:42 +02:00
|
|
|
deprecate_clippers()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
sa = SocialAccount.objects.all()[0]
|
|
|
|
self.assertEqual(sa.provider, "clipper_inactive")
|
|
|
|
|
|
|
|
def test_reconnect_after_deprecation(self):
|
|
|
|
self._setup_ldap()
|
2018-04-23 00:01:27 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
|
|
|
user0 = r.context['user']
|
|
|
|
n_sa0 = SocialAccount.objects.count()
|
|
|
|
n_u0 = User.objects.count()
|
2018-04-22 20:13:42 +02:00
|
|
|
self.client.logout()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
deprecate_clippers()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-23 00:01:27 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
|
|
|
user1 = r.context['user']
|
|
|
|
sa1 = list(SocialAccount.objects.all())
|
|
|
|
n_u1 = User.objects.count()
|
|
|
|
self.assertEqual(len(sa1), n_sa0)
|
|
|
|
self.assertEqual(n_u1, n_u0)
|
|
|
|
self.assertEqual(user1.id, user0.id)
|
2018-04-28 16:33:08 +02:00
|
|
|
self.assertEqual(self._count_ldap_queries(), 2)
|
2018-04-22 20:13:42 +02:00
|
|
|
|
|
|
|
def test_override_inactive_account(self):
|
|
|
|
self._setup_ldap(12)
|
2018-04-23 00:01:27 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
|
|
|
user0 = r.context['user']
|
|
|
|
n_sa0 = SocialAccount.objects.count()
|
|
|
|
n_u0 = User.objects.count()
|
2018-04-22 20:13:42 +02:00
|
|
|
self.client.logout()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-22 20:13:42 +02:00
|
|
|
deprecate_clippers()
|
|
|
|
|
|
|
|
self._setup_ldap(13)
|
2018-04-23 00:01:27 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-23 00:01:27 +02:00
|
|
|
user1 = r.context['user']
|
|
|
|
sa1 = list(SocialAccount.objects.all())
|
|
|
|
n_u1 = User.objects.count()
|
2018-04-29 01:28:09 +02:00
|
|
|
self.assertEqual(len(sa1), n_sa0 + 1)
|
|
|
|
self.assertEqual(n_u1, n_u0 + 1)
|
2018-04-23 00:01:27 +02:00
|
|
|
self.assertNotEqual(user1.id, user0.id)
|
2018-04-23 01:13:03 +02:00
|
|
|
|
|
|
|
def test_multiple_deprecation(self):
|
|
|
|
self._setup_ldap(12)
|
2018-09-29 23:32:50 +02:00
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-23 01:13:03 +02:00
|
|
|
self.client.logout()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
self._setup_ldap(15, "truc")
|
2018-09-29 23:32:50 +02:00
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="truc")
|
2018-04-23 01:13:03 +02:00
|
|
|
self.client.logout()
|
|
|
|
sa0 = SocialAccount.objects.count()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-23 01:13:03 +02:00
|
|
|
deprecate_clippers()
|
|
|
|
|
|
|
|
self._setup_ldap(13)
|
2018-04-29 01:28:09 +02:00
|
|
|
self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username="test")
|
2018-04-23 01:13:03 +02:00
|
|
|
self.client.logout()
|
|
|
|
|
|
|
|
sa1 = SocialAccount.objects.count()
|
|
|
|
|
|
|
|
deprecate_clippers()
|
|
|
|
sa2 = SocialAccount.objects.count()
|
|
|
|
|
|
|
|
# Older "test" inactive SocialAccount gets erased by new one
|
|
|
|
# while "truc" remains
|
|
|
|
self.assertEqual(sa0, sa2)
|
2018-04-29 01:28:09 +02:00
|
|
|
self.assertEqual(sa1, sa0 + 1)
|
2018-04-24 00:19:42 +02:00
|
|
|
|
|
|
|
def test_longterm_installer_from_allauth(self):
|
|
|
|
self._setup_ldap(12)
|
2018-09-29 23:32:50 +02:00
|
|
|
with self.settings(
|
|
|
|
SOCIALACCOUNT_ADAPTER='allauth.socialaccount.'
|
|
|
|
'adapter.DefaultSocialAccountAdapter'):
|
2018-04-24 00:19:42 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username='test')
|
|
|
|
user0 = r.context["user"]
|
|
|
|
nsa0 = SocialAccount.objects.count()
|
|
|
|
self.assertEqual(user0.username, "test")
|
|
|
|
self.client.logout()
|
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
outputs = install_longterm_adapter()
|
2018-04-24 00:19:42 +02:00
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
self.assertEqual(outputs["updated"], [("test", "test@12")])
|
2018-04-24 00:19:42 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username='test')
|
|
|
|
user1 = r.context["user"]
|
|
|
|
nsa1 = SocialAccount.objects.count()
|
2018-06-16 17:02:32 +02:00
|
|
|
conn = user1.socialaccount_set.get(provider='clipper')
|
2018-04-24 00:19:42 +02:00
|
|
|
self.assertEqual(user1.id, user0.id)
|
|
|
|
self.assertEqual(nsa1, nsa0)
|
|
|
|
self.assertEqual(user1.username, "test@12")
|
2018-06-16 17:02:32 +02:00
|
|
|
self.assertEqual(conn.extra_data['ldap']['entrance_year'], '12')
|
2018-04-24 00:19:42 +02:00
|
|
|
|
|
|
|
def test_longterm_installer_from_djangocas(self):
|
2018-09-29 23:32:50 +02:00
|
|
|
with self.settings(
|
|
|
|
SOCIALACCOUNT_ADAPTER='allauth.socialaccount.'
|
|
|
|
'adapter.DefaultSocialAccountAdapter'):
|
2018-04-29 01:28:09 +02:00
|
|
|
user0 = User.objects.create_user('test', 'test@clipper.ens.fr',
|
|
|
|
'test')
|
2018-04-24 00:19:42 +02:00
|
|
|
nsa0 = SocialAccount.objects.count()
|
2018-04-29 01:28:09 +02:00
|
|
|
|
2018-04-24 00:19:42 +02:00
|
|
|
self._setup_ldap(12)
|
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
outputs = install_longterm_adapter()
|
2018-04-24 00:19:42 +02:00
|
|
|
|
2018-09-29 23:32:50 +02:00
|
|
|
self.assertEqual(outputs["created"], [("test", "test@12")])
|
2018-04-24 00:19:42 +02:00
|
|
|
r = self.client_cas_login(self.client, provider_id="clipper",
|
|
|
|
username='test')
|
|
|
|
user1 = r.context["user"]
|
|
|
|
nsa1 = SocialAccount.objects.count()
|
2018-06-16 17:02:32 +02:00
|
|
|
conn = user1.socialaccount_set.get(provider='clipper')
|
2018-04-24 00:19:42 +02:00
|
|
|
self.assertEqual(user1.id, user0.id)
|
2018-04-29 01:28:09 +02:00
|
|
|
self.assertEqual(nsa1, nsa0 + 1)
|
2018-04-24 00:19:42 +02:00
|
|
|
self.assertEqual(user1.username, "test@12")
|
2018-06-16 17:02:32 +02:00
|
|
|
self.assertEqual(conn.extra_data['ldap']['entrance_year'], '12')
|
2018-04-29 01:28:09 +02:00
|
|
|
|
|
|
|
def test_disconnect_ldap(self):
|
|
|
|
nu0 = User.objects.count()
|
|
|
|
nsa0 = SocialAccount.objects.count()
|
|
|
|
|
|
|
|
ldap_patcher.stop()
|
2018-06-03 22:10:34 +02:00
|
|
|
with self.settings(CLIPPER_LDAP_SERVER=''):
|
2018-04-29 01:28:09 +02:00
|
|
|
self.assertRaises(ValueError, self.client_cas_login,
|
|
|
|
self.client, provider_id="clipper",
|
|
|
|
username="test")
|
|
|
|
|
|
|
|
nu1 = User.objects.count()
|
|
|
|
nsa1 = SocialAccount.objects.count()
|
|
|
|
self.assertEqual(nu0, nu1)
|
|
|
|
self.assertEqual(nsa0, nsa1)
|
|
|
|
ldap_patcher.start()
|
2018-10-06 11:57:36 +02:00
|
|
|
|
|
|
|
def test_invalid_uid(self):
|
|
|
|
self._setup_ldap(12, "test")
|
|
|
|
uids = [" test", "test ", "\\test", "test)"]
|
|
|
|
for uid in uids:
|
|
|
|
with self.assertRaises(ValueError) as cm:
|
|
|
|
get_ldap_infos(uid)
|
2018-10-06 17:18:40 +02:00
|
|
|
self.assertIn(uid, str(cm.exception))
|