Flag to keep usernames in install_longterm

This commit is contained in:
Evarin 2018-10-21 23:21:47 +02:00
parent 126f367e76
commit 1a91ca8090
3 changed files with 45 additions and 4 deletions

View file

@ -147,7 +147,7 @@ def deprecate_clippers():
clippers.update(provider='clipper_inactive') 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 Manages the transition from an older django_cas or an allauth_ens
installation without LongTermClipperAccountAdapter installation without LongTermClipperAccountAdapter
@ -184,7 +184,10 @@ def install_longterm_adapter(fake=False, accounts=None):
user = accounts.get(clipper_uid, None) user = accounts.get(clipper_uid, None)
if user is None: if user is None:
continue continue
if not keep_usernames:
user.username = ltc_adapter.get_username(clipper_uid, data) user.username = ltc_adapter.get_username(clipper_uid, data)
if fake: if fake:
cases.append(clipper_uid) cases.append(clipper_uid)
else: else:

View file

@ -26,6 +26,14 @@ class Command(BaseCommand):
default=False, default=False,
help=('Use the existing SocialAccounts rather than all the Users'), 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( parser.add_argument(
'--clipper-field', '--clipper-field',
default=None, default=None,
@ -35,6 +43,7 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
fake = options.get("fake", False) fake = options.get("fake", False)
keep_usernames = options.get("keep_usernames", False)
if options.get('use_socialaccounts', False): if options.get('use_socialaccounts', False):
accounts = {account.uid: account.user for account in accounts = {account.uid: account.user for account in
@ -55,7 +64,7 @@ class Command(BaseCommand):
else: else:
accounts = None accounts = None
logs = install_longterm_adapter(fake, accounts) logs = install_longterm_adapter(fake, accounts, keep_usernames)
self.stdout.write("Social accounts created : %d" self.stdout.write("Social accounts created : %d"
% len(logs["created"])) % len(logs["created"]))

View file

@ -333,7 +333,7 @@ class LongTermClipperTests(CASTestCase):
self.assertEqual(user1.username, "test@12") self.assertEqual(user1.username, "test@12")
self.assertEqual(conn.extra_data['ldap']['entrance_year'], '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) self._setup_ldap(12)
with self.settings( with self.settings(
SOCIALACCOUNT_ADAPTER='allauth.socialaccount.' SOCIALACCOUNT_ADAPTER='allauth.socialaccount.'
@ -362,6 +362,35 @@ class LongTermClipperTests(CASTestCase):
self.assertEqual(user1.username, "test@12") self.assertEqual(user1.username, "test@12")
self.assertEqual(conn.extra_data['ldap']['entrance_year'], '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): def test_longterm_installer_from_allauth_command_socialaccounts(self):
self._setup_ldap(12) self._setup_ldap(12)
with self.settings( with self.settings(