-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add additional email management and verification (#874)
- Loading branch information
Showing
28 changed files
with
1,538 additions
and
174 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
# Projectroles dependency | ||
from projectroles.app_settings import AppSettingAPI | ||
from projectroles.tests.test_models import SODARUserAdditionalEmailMixin | ||
|
||
from adminalerts.models import AdminAlert | ||
from adminalerts.tests.test_models import AdminAlertMixin | ||
|
@@ -26,9 +27,13 @@ | |
ALERT_DESC_UPDATED = 'Updated description' | ||
ALERT_DESC_MARKDOWN = '## Description' | ||
EMAIL_DESC_LEGEND = 'Additional details' | ||
ADD_EMAIL = '[email protected]' | ||
ADD_EMAIL2 = '[email protected]' | ||
|
||
|
||
class AdminalertsViewTestBase(AdminAlertMixin, TestCase): | ||
class AdminalertsViewTestBase( | ||
AdminAlertMixin, SODARUserAdditionalEmailMixin, TestCase | ||
): | ||
"""Base class for adminalerts view testing""" | ||
|
||
def _make_alert(self): | ||
|
@@ -199,16 +204,10 @@ def test_post_multiple_users(self): | |
self.assertIn(EMAIL_DESC_LEGEND, mail.outbox[0].body) | ||
self.assertIn(ALERT_DESC, mail.outbox[0].body) | ||
|
||
def test_post_alt_email_regular_user(self): | ||
"""Test POST with alt emails on regular user""" | ||
alt_email = '[email protected]' | ||
alt_email2 = '[email protected]' | ||
app_settings.set( | ||
'projectroles', | ||
'user_email_additional', | ||
';'.join([alt_email, alt_email2]), | ||
user=self.user_regular, | ||
) | ||
def test_post_add_email_regular_user(self): | ||
"""Test POST with additional emails on regular user""" | ||
self.make_email(self.user_regular, ADD_EMAIL) | ||
self.make_email(self.user_regular, ADD_EMAIL2) | ||
self.assertEqual(len(mail.outbox), 0) | ||
data = self._get_post_data() | ||
with self.login(self.superuser): | ||
|
@@ -220,28 +219,41 @@ def test_post_alt_email_regular_user(self): | |
[ | ||
settings.EMAIL_SENDER, | ||
self.user_regular.email, | ||
alt_email, | ||
alt_email2, | ||
ADD_EMAIL, | ||
ADD_EMAIL2, | ||
], | ||
) | ||
|
||
def test_post_alt_email_superuser(self): | ||
"""Test POST with alt emails on superuser""" | ||
alt_email = '[email protected]' | ||
alt_email2 = '[email protected]' | ||
app_settings.set( | ||
'projectroles', | ||
'user_email_additional', | ||
';'.join([alt_email, alt_email2]), | ||
user=self.superuser, | ||
def test_post_add_email_regular_user_unverified(self): | ||
"""Test POST with additional and unverified emails on regular user""" | ||
self.make_email(self.user_regular, ADD_EMAIL) | ||
self.make_email(self.user_regular, ADD_EMAIL2, verified=False) | ||
self.assertEqual(len(mail.outbox), 0) | ||
data = self._get_post_data() | ||
with self.login(self.superuser): | ||
response = self.client.post(self.url, data) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(len(mail.outbox), 1) | ||
self.assertEqual( | ||
mail.outbox[0].recipients(), | ||
[ | ||
settings.EMAIL_SENDER, | ||
self.user_regular.email, | ||
ADD_EMAIL, | ||
], | ||
) | ||
|
||
def test_post_add_email_superuser(self): | ||
"""Test POST with additional emails on superuser""" | ||
self.make_email(self.superuser, ADD_EMAIL) | ||
self.make_email(self.superuser, ADD_EMAIL2) | ||
self.assertEqual(len(mail.outbox), 0) | ||
data = self._get_post_data() | ||
with self.login(self.superuser): | ||
response = self.client.post(self.url, data) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(len(mail.outbox), 1) | ||
# Superuser alt emails should not be included | ||
# Superuser additional emails should not be included | ||
self.assertEqual( | ||
mail.outbox[0].recipients(), | ||
[settings.EMAIL_SENDER, self.user_regular.email], | ||
|
This file contains 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 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 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 |
---|---|---|
|
@@ -98,18 +98,6 @@ | |
'user_modifiable': True, | ||
'global': True, | ||
}, | ||
'user_email_additional': { | ||
'scope': APP_SETTING_SCOPE_USER, | ||
'type': 'STRING', | ||
'default': '', | ||
'placeholder': '[email protected];[email protected]', | ||
'label': 'Additional email', | ||
'description': 'Also send user emails to these addresses. Separate ' | ||
'multiple emails with semicolon.', | ||
'user_modifiable': True, | ||
'global': True, | ||
'project_types': [PROJECT_TYPE_PROJECT], | ||
}, | ||
'project_star': { | ||
'scope': APP_SETTING_SCOPE_PROJECT_USER, | ||
'type': 'BOOLEAN', | ||
|
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Generated by Django 4.2.11 on 2024-04-25 11:48 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import uuid | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
("projectroles", "0028_populate_finder_role"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="SODARUserAdditionalEmail", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name="ID", | ||
), | ||
), | ||
("email", models.EmailField(help_text="Email address", max_length=254)), | ||
( | ||
"verified", | ||
models.BooleanField( | ||
default=False, help_text="Email verification status" | ||
), | ||
), | ||
( | ||
"secret", | ||
models.CharField( | ||
help_text="Secret token for email verification", | ||
max_length=255, | ||
unique=True, | ||
), | ||
), | ||
( | ||
"date_created", | ||
models.DateTimeField( | ||
auto_now_add=True, help_text="DateTime of creation" | ||
), | ||
), | ||
( | ||
"date_modified", | ||
models.DateTimeField( | ||
auto_now=True, help_text="DateTime of last modification" | ||
), | ||
), | ||
( | ||
"sodar_uuid", | ||
models.UUIDField( | ||
default=uuid.uuid4, | ||
help_text="SODARUserAdditionalEmail SODAR UUID", | ||
unique=True, | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
help_text="User for whom the email is assigned", | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="additional_emails", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
options={ | ||
"ordering": ["user__username", "email"], | ||
"unique_together": {("user", "email")}, | ||
}, | ||
), | ||
] |
48 changes: 48 additions & 0 deletions
48
projectroles/migrations/0030_populate_sodaruseradditionalemail.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Generated by Django 4.2.11 on 2024-04-25 13:14 | ||
|
||
import random | ||
import string | ||
|
||
from django.db import migrations | ||
|
||
|
||
def populate_model(apps, schema_editor): | ||
""" | ||
Move existing additional email app settings into the | ||
SODARUserAdditionalEmail model as verified emais. Delete the settings | ||
objects. | ||
""" | ||
AppSetting = apps.get_model('projectroles', 'AppSetting') | ||
SODARUserAdditionalEmail = apps.get_model( | ||
'projectroles', 'SODARUserAdditionalEmail' | ||
) | ||
for a in AppSetting.objects.filter( | ||
app_plugin=None, name='user_email_additional' | ||
): | ||
for v in a.value.split(';'): | ||
secret = ''.join( | ||
random.SystemRandom().choice( | ||
string.ascii_lowercase + string.digits | ||
) | ||
for _ in range(32) | ||
) | ||
try: | ||
SODARUserAdditionalEmail.objects.create( | ||
user=a.user, email=v, verified=True, secret=secret | ||
) | ||
except Exception: | ||
pass | ||
a.delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("projectroles", "0029_sodaruseradditionalemail"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
populate_model, reverse_code=migrations.RunPython.noop | ||
) | ||
] |
Oops, something went wrong.