mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-25 01:03:07 +00:00
* MFA workflow fixes - Fixes https://github.com/inventree/InvenTree/issues/13238 - Verify email on successful password reset - Invalidate password on account creation - Fix MFA workflows - Add regression tests * Update docs * Update OTP secret expectation to use exact match * Attempt to fix flaky test --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
1 parent
d51720e8e1
commit
4210fc95a8
7 files changed
+386
-13
No files matched your search
@@ -966,3 +966,126 @@ class RuleSetPermissionTests(InvenTreeAPITestCase):
|
||||
|
||||
self.patch(group_url, {'name': 'renamed-group'}, expected_code=403)
|
||||
self.patch(ruleset_url, {'can_change': True}, expected_code=403)
|
||||
|
||||
|
||||
class NewUserMfaOnboardingTests(InvenTreeAPITestCase):
|
||||
"""Regression tests for onboarding admin-created users while MFA is enforced.
|
||||
|
||||
Ref: https://github.com/inventree/InvenTree/issues/13238
|
||||
"""
|
||||
|
||||
NEW_PASSWORD = 'Corr3ct-Horse-Battery-Staple!'
|
||||
|
||||
def setUp(self):
|
||||
"""Allow the test user to create other users."""
|
||||
super().setUp()
|
||||
self.user.is_staff = True
|
||||
self.user.save()
|
||||
self.assignRole('admin.add')
|
||||
|
||||
def create_user(self, username='newbie', email='newbie@example.org'):
|
||||
"""Create a new user via the API, as an administrator would."""
|
||||
self.post(
|
||||
reverse('api-user-list'),
|
||||
data={
|
||||
'username': username,
|
||||
'first_name': 'New',
|
||||
'last_name': 'User',
|
||||
'email': email,
|
||||
},
|
||||
expected_code=201,
|
||||
)
|
||||
return User.objects.get(username=username)
|
||||
|
||||
def reset_password(self, user):
|
||||
"""Complete the headless password reset flow using a valid reset key."""
|
||||
from allauth.account.forms import default_token_generator
|
||||
from allauth.account.utils import user_pk_to_url_str
|
||||
|
||||
key = f'{user_pk_to_url_str(user)}-{default_token_generator.make_token(user)}'
|
||||
|
||||
response = self.client.post(
|
||||
reverse('browser:account:reset_password'),
|
||||
{'key': key, 'password': self.NEW_PASSWORD},
|
||||
content_type='application/json',
|
||||
)
|
||||
# Password reset does not log the user in -> 401 'unauthenticated' response
|
||||
self.assertIn(response.status_code, [200, 401], response.content)
|
||||
|
||||
def get_totp(self):
|
||||
"""Request a TOTP secret (first step of MFA registration)."""
|
||||
return self.client.get(reverse('browser:mfa:manage_totp'))
|
||||
|
||||
def test_new_user_can_register_mfa(self):
|
||||
"""A new user must be able to register MFA after resetting their password."""
|
||||
new_user = self.create_user()
|
||||
|
||||
email = EmailAddress.objects.get(user=new_user)
|
||||
self.assertFalse(email.verified)
|
||||
self.assertFalse(new_user.has_usable_password())
|
||||
|
||||
self.client.logout()
|
||||
|
||||
with mock.patch.dict('os.environ', {'INVENTREE_LOGIN_ENFORCE_MFA': 'True'}):
|
||||
self.reset_password(new_user)
|
||||
|
||||
# Completing the reset proves ownership of the mailbox
|
||||
email.refresh_from_db()
|
||||
self.assertTrue(email.verified)
|
||||
|
||||
# The new user can now log in with their new password...
|
||||
response = self.client.post(
|
||||
reverse('browser:account:login'),
|
||||
{'username': 'newbie', 'password': self.NEW_PASSWORD},
|
||||
content_type='application/json',
|
||||
)
|
||||
self.assertEqual(response.status_code, 200, response.content)
|
||||
|
||||
# ...is still blocked from the API until MFA is set up...
|
||||
response = self.client.get(reverse('api-part-list'))
|
||||
self.assertEqual(response.status_code, 401)
|
||||
self.assertEqual(response.json()['id'], 'mfa_register')
|
||||
|
||||
# ...but can obtain a TOTP secret to register MFA
|
||||
response = self.get_totp()
|
||||
self.assertEqual(response.status_code, 404, response.content)
|
||||
self.assertIn('secret', response.json()['meta'])
|
||||
|
||||
def test_unverified_email_blocks_mfa_registration(self):
|
||||
"""Without a verified email, MFA registration is refused (the original bug)."""
|
||||
new_user = self.create_user()
|
||||
new_user.set_password(self.NEW_PASSWORD)
|
||||
new_user.save()
|
||||
|
||||
self.client.logout()
|
||||
self.client.login(username='newbie', password=self.NEW_PASSWORD)
|
||||
|
||||
response = self.get_totp()
|
||||
self.assertEqual(response.status_code, 409, response.content)
|
||||
self.assertEqual(response.json()['errors'][0]['code'], 'unverified_email')
|
||||
|
||||
def test_reset_with_multiple_addresses(self):
|
||||
"""Do not verify any address if the user has more than one."""
|
||||
new_user = self.create_user()
|
||||
EmailAddress.objects.create(
|
||||
user=new_user, email='other@example.org', primary=False, verified=False
|
||||
)
|
||||
|
||||
self.client.logout()
|
||||
self.reset_password(new_user)
|
||||
|
||||
self.assertFalse(
|
||||
EmailAddress.objects.filter(user=new_user, verified=True).exists()
|
||||
)
|
||||
|
||||
def test_reset_with_mismatched_address(self):
|
||||
"""Do not verify an address which does not match the account email."""
|
||||
new_user = self.create_user()
|
||||
EmailAddress.objects.filter(user=new_user).update(email='stale@example.org')
|
||||
|
||||
self.client.logout()
|
||||
self.reset_password(new_user)
|
||||
|
||||
self.assertFalse(
|
||||
EmailAddress.objects.filter(user=new_user, verified=True).exists()
|
||||
)
|
||||
Reference in new issue
Block a user