2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-09-13 22:21:37 +00:00

improve error messages (#10207)

* fix remove mfa task

* add test for command

* clean up after schema test

* add assert to esure authenticators are really present/removed

* simplify handler

* improve error message

* make more readable
This commit is contained in:
Matthias Mair
2025-08-21 01:08:26 +02:00
committed by GitHub
parent 9dae8724d8
commit e3a1c5fa20
2 changed files with 24 additions and 7 deletions

View File

@@ -28,7 +28,16 @@ class Command(BaseCommand):
if len(mfa_user) == 0: if len(mfa_user) == 0:
logger.warning('No user with this mail associated') logger.warning('No user with this mail associated')
elif len(mfa_user) > 1: elif len(mfa_user) > 1:
logger.error('More than one user found with this mail') emails_list = ', '.join(
sorted(
{b.email for a in mfa_user for b in a.emailaddress_set.all()}
| {a.email for a in mfa_user}
)
)
usernames_list = ', '.join(sorted({a.username for a in mfa_user}))
logger.error(
f"Multiple users found with the provided email; Usernames: '{usernames_list}', Emails: '{emails_list}'"
)
else: else:
# and clean out all MFA methods # and clean out all MFA methods
auths = mfa_user[0].authenticator_set.all() auths = mfa_user[0].authenticator_set.all()

View File

@@ -34,17 +34,25 @@ class CommandTestCase(TestCase):
self.assertIn('No user with this mail associated', str(cm[1])) self.assertIn('No user with this mail associated', str(cm[1]))
# correct removal # correct removal
my_admin = User.objects.create_user(username='admin', email='admin@example.org') my_admin1 = User.objects.create_user(
my_admin.authenticator_set.create(type='TOTP', data={}) username='admin', email='admin@example.org'
self.assertEqual(my_admin.authenticator_set.all().count(), 1) )
my_admin1.authenticator_set.create(type='TOTP', data={})
self.assertEqual(my_admin1.authenticator_set.all().count(), 1)
output = call_command('remove_mfa', 'admin@example.org', verbosity=0) output = call_command('remove_mfa', 'admin@example.org', verbosity=0)
self.assertEqual(output, 'done') self.assertEqual(output, 'done')
self.assertEqual(my_admin.authenticator_set.all().count(), 0) self.assertEqual(my_admin1.authenticator_set.all().count(), 0)
# two users with same email # two users with same email
User.objects.create_user(username='admin2', email='admin@example.org') my_admin2 = User.objects.create_user(
username='admin2', email='admin@example.org'
)
my_admin2.emailaddress_set.create(email='456')
my_admin2.emailaddress_set.create(email='123')
with self.assertLogs('inventree') as cm: with self.assertLogs('inventree') as cm:
self.assertFalse( self.assertFalse(
call_command('remove_mfa', 'admin@example.org', verbosity=0) call_command('remove_mfa', 'admin@example.org', verbosity=0)
) )
self.assertIn('More than one user found with this mail', str(cm[1])) self.assertIn('Multiple users found with the provided email', str(cm[1]))
self.assertIn('admin, admin2', str(cm[1]))
self.assertIn('123, 456, admin@example.org', str(cm[1]))