Skip to content

Commit e3a1c5f

Browse files
authored
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
1 parent 9dae872 commit e3a1c5f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/backend/InvenTree/InvenTree/management/commands/remove_mfa.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,16 @@ def handle(self, *args, mail, **kwargs):
2828
if len(mfa_user) == 0:
2929
logger.warning('No user with this mail associated')
3030
elif len(mfa_user) > 1:
31-
logger.error('More than one user found with this mail')
31+
emails_list = ', '.join(
32+
sorted(
33+
{b.email for a in mfa_user for b in a.emailaddress_set.all()}
34+
| {a.email for a in mfa_user}
35+
)
36+
)
37+
usernames_list = ', '.join(sorted({a.username for a in mfa_user}))
38+
logger.error(
39+
f"Multiple users found with the provided email; Usernames: '{usernames_list}', Emails: '{emails_list}'"
40+
)
3241
else:
3342
# and clean out all MFA methods
3443
auths = mfa_user[0].authenticator_set.all()

src/backend/InvenTree/InvenTree/test_commands.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,25 @@ def test_remove_mfa(self):
3434
self.assertIn('No user with this mail associated', str(cm[1]))
3535

3636
# correct removal
37-
my_admin = User.objects.create_user(username='admin', email='[email protected]')
38-
my_admin.authenticator_set.create(type='TOTP', data={})
39-
self.assertEqual(my_admin.authenticator_set.all().count(), 1)
37+
my_admin1 = User.objects.create_user(
38+
username='admin', email='[email protected]'
39+
)
40+
my_admin1.authenticator_set.create(type='TOTP', data={})
41+
self.assertEqual(my_admin1.authenticator_set.all().count(), 1)
4042
output = call_command('remove_mfa', '[email protected]', verbosity=0)
4143
self.assertEqual(output, 'done')
42-
self.assertEqual(my_admin.authenticator_set.all().count(), 0)
44+
self.assertEqual(my_admin1.authenticator_set.all().count(), 0)
4345

4446
# two users with same email
45-
User.objects.create_user(username='admin2', email='[email protected]')
47+
my_admin2 = User.objects.create_user(
48+
username='admin2', email='[email protected]'
49+
)
50+
my_admin2.emailaddress_set.create(email='456')
51+
my_admin2.emailaddress_set.create(email='123')
4652
with self.assertLogs('inventree') as cm:
4753
self.assertFalse(
4854
call_command('remove_mfa', '[email protected]', verbosity=0)
4955
)
50-
self.assertIn('More than one user found with this mail', str(cm[1]))
56+
self.assertIn('Multiple users found with the provided email', str(cm[1]))
57+
self.assertIn('admin, admin2', str(cm[1]))
58+
self.assertIn('123, 456, [email protected]', str(cm[1]))

0 commit comments

Comments
 (0)