From 1a91ca80904c4d896d97b656389051ceafdbc9d3 Mon Sep 17 00:00:00 2001 From: Evarin Date: Sun, 21 Oct 2018 23:21:47 +0200 Subject: [PATCH] Flag to keep usernames in install_longterm --- allauth_ens/adapter.py | 7 +++-- .../management/commands/install_longterm.py | 11 ++++++- allauth_ens/tests.py | 31 ++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/allauth_ens/adapter.py b/allauth_ens/adapter.py index c68a5bf..3c02f2a 100644 --- a/allauth_ens/adapter.py +++ b/allauth_ens/adapter.py @@ -147,7 +147,7 @@ def deprecate_clippers(): clippers.update(provider='clipper_inactive') -def install_longterm_adapter(fake=False, accounts=None): +def install_longterm_adapter(fake=False, accounts=None, keep_usernames=False): """ Manages the transition from an older django_cas or an allauth_ens installation without LongTermClipperAccountAdapter @@ -184,7 +184,10 @@ def install_longterm_adapter(fake=False, accounts=None): user = accounts.get(clipper_uid, None) if user is None: continue - user.username = ltc_adapter.get_username(clipper_uid, data) + + if not keep_usernames: + user.username = ltc_adapter.get_username(clipper_uid, data) + if fake: cases.append(clipper_uid) else: diff --git a/allauth_ens/management/commands/install_longterm.py b/allauth_ens/management/commands/install_longterm.py index fdff5f1..a839555 100644 --- a/allauth_ens/management/commands/install_longterm.py +++ b/allauth_ens/management/commands/install_longterm.py @@ -26,6 +26,14 @@ class Command(BaseCommand): default=False, help=('Use the existing SocialAccounts rather than all the Users'), ) + parser.add_argument( + '--keep-usernames', + action='store_true', + default=False, + help=('Do not apply the username template (e.g. clipper@promo) to' + 'the existing account, only populate the SocialAccounts with' + 'ldap informations'), + ) parser.add_argument( '--clipper-field', default=None, @@ -35,6 +43,7 @@ class Command(BaseCommand): def handle(self, *args, **options): fake = options.get("fake", False) + keep_usernames = options.get("keep_usernames", False) if options.get('use_socialaccounts', False): accounts = {account.uid: account.user for account in @@ -55,7 +64,7 @@ class Command(BaseCommand): else: accounts = None - logs = install_longterm_adapter(fake, accounts) + logs = install_longterm_adapter(fake, accounts, keep_usernames) self.stdout.write("Social accounts created : %d" % len(logs["created"])) diff --git a/allauth_ens/tests.py b/allauth_ens/tests.py index 1824b60..61bb3a4 100644 --- a/allauth_ens/tests.py +++ b/allauth_ens/tests.py @@ -333,7 +333,7 @@ class LongTermClipperTests(CASTestCase): self.assertEqual(user1.username, "test@12") self.assertEqual(conn.extra_data['ldap']['entrance_year'], '12') - def test_longterm_installer_from_allauth_command_username(self): + def test_longterm_installer_from_allauth_command_using_username(self): self._setup_ldap(12) with self.settings( SOCIALACCOUNT_ADAPTER='allauth.socialaccount.' @@ -362,6 +362,35 @@ class LongTermClipperTests(CASTestCase): self.assertEqual(user1.username, "test@12") self.assertEqual(conn.extra_data['ldap']['entrance_year'], '12') + def test_longterm_installer_from_allauth_command_keeping_username(self): + self._setup_ldap(12) + with self.settings( + SOCIALACCOUNT_ADAPTER='allauth.socialaccount.' + 'adapter.DefaultSocialAccountAdapter'): + 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() + + with captured_stdout() as stdout: + command = InstallLongterm() + command.handle(keep_usernames=True) + + output = stdout.getvalue() + self.assertIn('test -> test', output) + + r = self.client_cas_login(self.client, provider_id="clipper", + username='test') + user1 = r.context["user"] + nsa1 = SocialAccount.objects.count() + conn = user1.socialaccount_set.get(provider='clipper') + self.assertEqual(user1.id, user0.id) + self.assertEqual(nsa1, nsa0) + self.assertEqual(user1.username, "test") + self.assertEqual(conn.extra_data['ldap']['entrance_year'], '12') + def test_longterm_installer_from_allauth_command_socialaccounts(self): self._setup_ldap(12) with self.settings(