From 4a119c7d13bf7ea7399680d59fc6a97a8046f2a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 6 Oct 2018 11:57:36 +0200 Subject: [PATCH 1/2] Show ill-formed clipper uids in error messages --- allauth_ens/tests.py | 10 ++++++++++ allauth_ens/utils.py | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/allauth_ens/tests.py b/allauth_ens/tests.py index b902d81..9d0e366 100644 --- a/allauth_ens/tests.py +++ b/allauth_ens/tests.py @@ -9,6 +9,7 @@ from django.core import mail from django.test import TestCase, override_settings from allauth.socialaccount.models import SocialAccount +from allauth_ens.utils import get_ldap_infos import six from allauth_cas.test.testcases import CASTestCase @@ -367,3 +368,12 @@ class LongTermClipperTests(CASTestCase): self.assertEqual(nu0, nu1) self.assertEqual(nsa0, nsa1) ldap_patcher.start() + + 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) + print(cm.exception) + self.assertIn(uid, cm.exception.message) diff --git a/allauth_ens/utils.py b/allauth_ens/utils.py index afd29a9..02d4c10 100644 --- a/allauth_ens/utils.py +++ b/allauth_ens/utils.py @@ -69,7 +69,11 @@ def extract_infos_from_ldap(infos): def get_ldap_infos(clipper_uid): - assert clipper_uid.isalnum() + if not clipper_uid.isalnum(): + raise ValueError( + 'Invalid uid "{}": contains non-alphanumeric characters' + .format(clipper_uid) + ) data = {} try: ldap_connection = init_ldap() From d22d1ea567db6100426ff741705a5afc227f66b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 6 Oct 2018 16:15:31 +0200 Subject: [PATCH 2/2] remove leftover debug print --- allauth_ens/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/allauth_ens/tests.py b/allauth_ens/tests.py index 9d0e366..f8bf5c0 100644 --- a/allauth_ens/tests.py +++ b/allauth_ens/tests.py @@ -375,5 +375,4 @@ class LongTermClipperTests(CASTestCase): for uid in uids: with self.assertRaises(ValueError) as cm: get_ldap_infos(uid) - print(cm.exception) self.assertIn(uid, cm.exception.message)