-
-
Notifications
You must be signed in to change notification settings - Fork 458
Change EmailDevice default to unconfirmed until verified #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c06dbf
Change EmailDevice default to unconfirmed until verified
MAkcanca e7896f6
Remove whitespace
MAkcanca 71cd92c
Merge branch 'jazzband:master' into master
MAkcanca f50e7c5
Adds tests for default_device behavior and email confirmation
MAkcanca 364e81a
Ruff cleanup
MAkcanca File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,3 +189,100 @@ def test_device_user_without_email(self): | |
self.user.email = "" | ||
self.user.save() | ||
self.test_device_without_email() | ||
|
||
|
||
def test_default_device_returns_only_confirmed(self): | ||
"""Test that default_device(user) returns only confirmed devices.""" | ||
# Create a confirmed default device | ||
confirmed_device = self.user.emaildevice_set.create( | ||
name='default', | ||
email='[email protected]', | ||
confirmed=True | ||
) | ||
self.user.emaildevice_set.create( | ||
name='default', | ||
email='[email protected]', | ||
confirmed=False | ||
) | ||
|
||
# Verify default_device only returns the confirmed device | ||
device = default_device(self.user) | ||
self.assertIsNotNone(device, "A confirmed default device should be returned") | ||
self.assertEqual(device.pk, confirmed_device.pk, | ||
"The returned device should be the confirmed one") | ||
self.assertTrue(device.confirmed, | ||
"The returned device should be confirmed") | ||
|
||
def test_default_device_includes_unconfirmed_when_flag_false(self): | ||
"""Test that default_device(user, confirmed=False) returns unconfirmed devices.""" | ||
# Create an unconfirmed default device only. | ||
unconfirmed_device = self.user.emaildevice_set.create( | ||
name="default", | ||
email="[email protected]", | ||
confirmed=False | ||
) | ||
|
||
# When using the default (confirmed=True), no device should be returned because | ||
# the only default device is unconfirmed. | ||
self.assertIsNone(default_device(self.user), | ||
"No device should be returned when only unconfirmed devices exist") | ||
|
||
# When passing confirmed=False, it should return the unconfirmed device. | ||
device = default_device(self.user, confirmed=False) | ||
self.assertIsNotNone(device) | ||
self.assertFalse(device.confirmed) | ||
self.assertEqual(device.pk, unconfirmed_device.pk) | ||
|
||
@override_settings(OTP_EMAIL_THROTTLE_FACTOR=0) | ||
def test_confirmed_email(self): | ||
# Setup | ||
self.client.post(reverse('two_factor:setup'), | ||
data={'setup_view-current_step': 'welcome'}) | ||
self.assertIsNone( | ||
default_device(self.user, confirmed=False), | ||
"User should not have a default device before setup" | ||
) | ||
# user has email, so we skip the email form. | ||
method_response = self.client.post( | ||
reverse('two_factor:setup'), | ||
data={ | ||
'setup_view-current_step': 'method', | ||
'method-method': 'email' | ||
} | ||
) | ||
self.assertEqual(method_response.status_code, 200, "Method selection should succeed") | ||
|
||
# Now we look at the device, it should be unconfirmed. | ||
device = default_device(self.user, confirmed=False) | ||
self.assertIsNotNone(device) | ||
self.assertIsInstance(device, EmailDevice) | ||
self.assertFalse(device.confirmed) | ||
self.assertEqual(len(mail.outbox), 1) | ||
msg = mail.outbox.pop(0) | ||
token = re.findall(r'[0-9]{6}', msg.body)[0] | ||
|
||
# Confirm the email | ||
response = self.client.post(reverse('two_factor:setup'), | ||
data={'setup_view-current_step': 'validation', | ||
'validation-token': token}) | ||
self.assertRedirects(response, reverse('two_factor:setup_complete')) | ||
# Now the user has a confirmed default 2FA device that is an EmailDevice. | ||
device.refresh_from_db() | ||
self.assertTrue(device.confirmed, "Device should be confirmed after validation") | ||
|
||
@override_settings(OTP_EMAIL_THROTTLE_FACTOR=0) | ||
def test_unconfirmed_email(self): | ||
# Setup | ||
self.client.post(reverse('two_factor:setup'), | ||
data={'setup_view-current_step': 'welcome'}) | ||
# right now, the user does not have a default 2FA device. | ||
self.assertEqual(default_device(self.user), None) | ||
# user has email, so we skip the email form. | ||
self.client.post(reverse('two_factor:setup'), | ||
data={'setup_view-current_step': 'method', | ||
'method-method': 'email'}) | ||
# Now we look at the device, it should be unconfirmed. | ||
device = default_device(self.user, confirmed=False) | ||
self.assertIsNotNone(device) | ||
self.assertIsInstance(device, EmailDevice) | ||
self.assertFalse(device.confirmed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ | |
USER_DEFAULT_DEVICE_ATTR_NAME = "_default_device" | ||
|
||
|
||
def default_device(user): | ||
def default_device(user, confirmed=True): | ||
if not user or user.is_anonymous: | ||
return | ||
if hasattr(user, USER_DEFAULT_DEVICE_ATTR_NAME): | ||
return getattr(user, USER_DEFAULT_DEVICE_ATTR_NAME) | ||
for device in devices_for_user(user): | ||
for device in devices_for_user(user, confirmed=confirmed): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per django-otp docstring for this function, it already defaults to True |
||
if device.name == 'default': | ||
setattr(user, USER_DEFAULT_DEVICE_ATTR_NAME, device) | ||
return device | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.