From dc8873cafc68bc4159880c330b8321fcd8146d70 Mon Sep 17 00:00:00 2001 From: Evarin Date: Mon, 23 Apr 2018 00:01:27 +0200 Subject: [PATCH] Tests for adapter ready --- allauth_ens/adapter.py | 7 ++--- allauth_ens/tests.py | 69 ++++++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/allauth_ens/adapter.py b/allauth_ens/adapter.py index b7a22ab..c08382f 100644 --- a/allauth_ens/adapter.py +++ b/allauth_ens/adapter.py @@ -89,7 +89,7 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter): def pre_social_login(self, request, sociallogin): clipper = sociallogin.account.uid try: - a = SocialAccount.objects.get(provider='old_clipper', + a = SocialAccount.objects.get(provider='clipper_inactive', uid=clipper) # An account with that uid was registered, but potentially # deprecated at the beginning of the year @@ -103,7 +103,6 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter): # But before that, we need to invalidate the email address of # the previous user email = ldap_data.get('email') - print(email, 'deprecate') u_mails = EmailAddress.objects.filter(user=a.user) try: clipper_mail = u_mails.get(email=email) @@ -138,7 +137,6 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter): return "{}@{}".format(clipper, data.get('annee', '00')) def save_user(self, request, sociallogin, form=None): - print("populate user", sociallogin.account.uid) user = sociallogin.user user.set_unusable_password() @@ -154,8 +152,7 @@ class LongTermClipperAccountAdapter(DefaultSocialAccountAdapter): name_parts = (name or '').split(' ') user_field(user, 'first_name', name_parts[0]) user_field(user, 'last_name', ' '.join(name_parts[1:])) - print(user.username, user) - + # Ignore form get_account_adapter().populate_username(request, user) diff --git a/allauth_ens/tests.py b/allauth_ens/tests.py index ee69c3e..d64d229 100644 --- a/allauth_ens/tests.py +++ b/allauth_ens/tests.py @@ -157,13 +157,13 @@ class LongTermClipperTests(CASTestCase): _mock_ldap.reset() def _setup_ldap(self, promo=12): - _mock_ldap.set_return_value('search_s', - ('dc=spi,dc=ens,dc=fr,uid=test'), - ( - ('cn', ('John Smith')), - ('mailRoutingAddress', ('test@clipper.ens.fr')), - ('homeDirectory', ("/users/%d/phy/test/" % promo)) - )) + import ldap + _mock_ldap.directory['dc=spi,dc=ens,dc=fr']={ + 'uid': ['test'], + 'cn': ['John Smith'], + 'mailRoutingAddress' : ['test@clipper.ens.fr'], + 'homeDirectory': ["/users/%d/phy/test/" % promo], + } def test_new_connexion(self): @@ -180,6 +180,15 @@ class LongTermClipperTests(CASTestCase): sa = list(SocialAccount.objects.all())[-1] self.assertEqual(sa.user.id, u.id) + def test_connect_disconnect(self): + self._setup_ldap() + r0 = self.client_cas_login(self.client, provider_id="clipper", username="test") + self.assertIn("_auth_user_id", self.client.session) + self.assertIn('user', r0.context) + + r1 = self.client.logout() + self.assertNotIn("_auth_user_id", self.client.session) + def test_second_connexion(self): self._setup_ldap() @@ -201,34 +210,42 @@ class LongTermClipperTests(CASTestCase): def test_reconnect_after_deprecation(self): self._setup_ldap() - self.client_cas_login(self.client, provider_id="clipper", username="test") - nsa = SocialAccount.objects.count() - nu = User.objects.count() + 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() self.client.logout() deprecate_clippers() - self.client_cas_login(self.client, provider_id="clipper", username="test") - - sa = SocialAccount.objects.all() - self.assertEqual(len(sa), nsa) - u = User.objects.all() - self.assertEqual(len(u), nu) - self.assertEqual(sa[-1].user.id, u[-1].id) + + 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) def test_override_inactive_account(self): self._setup_ldap(12) - self.client_cas_login(self.client, provider_id="clipper", username="test") - nsa = SocialAccount.objects.count() - nu = User.objects.count() + 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() self.client.logout() deprecate_clippers() self._setup_ldap(13) - self.client_cas_login(self.client, provider_id="clipper", username="test") + r = self.client_cas_login(self.client, provider_id="clipper", + username="test") - sa = SocialAccount.objects.all() - self.assertEqual(len(sa), nsa+1) - u = User.objects.all() - self.assertEqual(len(u), nu+1) - self.assertEqual(sa[-1].user.id, u[-1].id) + user1 = r.context['user'] + sa1 = list(SocialAccount.objects.all()) + n_u1 = User.objects.count() + self.assertEqual(len(sa1), n_sa0+1) + self.assertEqual(n_u1, n_u0+1) + self.assertNotEqual(user1.id, user0.id)