Skip to content

Commit 1affb5e

Browse files
fix unit tests
1 parent 78e968c commit 1affb5e

File tree

7 files changed

+103
-27
lines changed

7 files changed

+103
-27
lines changed

api/institutions/authentication.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
from osf import features
2222
from osf.exceptions import InstitutionAffiliationStateError
23-
from osf.models import Institution
23+
from osf.models import Institution, NotificationType
2424
from osf.models.institution import SsoFilterCriteriaAction
2525

26-
from website.mails import send_mail, WELCOME_OSF4I, DUPLICATE_ACCOUNTS_OSF4I, ADD_SSO_EMAIL_OSF4I
26+
from website.mails import send_mail, DUPLICATE_ACCOUNTS_OSF4I, ADD_SSO_EMAIL_OSF4I
2727
from website.settings import OSF_SUPPORT_EMAIL, DOMAIN
2828
from website.util.metrics import institution_source_tag
2929

@@ -334,14 +334,13 @@ def authenticate(self, request):
334334
user.save()
335335

336336
# Send confirmation email for all three: created, confirmed and claimed
337-
send_mail(
338-
to_addr=user.username,
339-
mail=WELCOME_OSF4I,
340-
user=user,
341-
domain=DOMAIN,
342-
osf_support_email=OSF_SUPPORT_EMAIL,
343-
storage_flag_is_active=flag_is_active(request, features.STORAGE_I18N),
344-
)
337+
notification_type = NotificationType.objects.filter(name='welcome_osf4i')
338+
if not notification_type.exists():
339+
raise NotificationType.DoesNotExist(
340+
'NotificationType with name welcome_osf4i does not exist.',
341+
)
342+
notification_type = notification_type.first()
343+
notification_type.emit(user=user, message_frequency='instantly', event_context={'domain': DOMAIN, 'osf_support_email': OSF_SUPPORT_EMAIL, 'storage_flag_is_active': flag_is_active(request, features.STORAGE_I18N)})
345344

346345
# Add the email to the user's account if it is identified by the eppn
347346
if email_to_add:

api/users/views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
OSFUser,
100100
Email,
101101
Tag,
102-
NotificationType
102+
NotificationType,
103103
)
104104
from osf.utils.tokens import TokenHandler
105105
from osf.utils.tokens.handlers import sanction_handler
@@ -847,10 +847,10 @@ def get(self, request, *args, **kwargs):
847847
notification_type = NotificationType.objects.filter(name=mail_template)
848848
if not notification_type.exists():
849849
raise NotificationType.DoesNotExist(
850-
f'NotificationType with name {mail_template} does not exist.'
850+
f'NotificationType with name {mail_template} does not exist.',
851851
)
852852
notification_type = notification_type.first()
853-
notification_type.emit(user=user_obj, event_context={'can_change_preferences': False, 'reset_link': reset_link})
853+
notification_type.emit(user=user_obj, message_frequency='instantly', event_context={'can_change_preferences': False, 'reset_link': reset_link})
854854

855855
return Response(status=status.HTTP_200_OK, data={'message': status_message, 'kind': kind, 'institutional': institutional})
856856

@@ -1066,10 +1066,10 @@ def _process_external_identity(self, user, external_identity, service_url):
10661066
notification_type = NotificationType.objects.filter(name='external_confirm_success')
10671067
if not notification_type.exists():
10681068
raise NotificationType.DoesNotExist(
1069-
'NotificationType with name external_confirm_success does not exist.'
1069+
'NotificationType with name external_confirm_success does not exist.',
10701070
)
10711071
notification_type = notification_type.first()
1072-
notification_type.emit(user=user, event_context={'can_change_preferences': False, 'external_id_provider': provider})
1072+
notification_type.emit(user=user, message_frequency='instantly', event_context={'can_change_preferences': False, 'external_id_provider': provider})
10731073

10741074
enqueue_task(update_affiliation_for_orcid_sso_users.s(user._id, provider_id))
10751075

@@ -1387,10 +1387,10 @@ def post(self, request, *args, **kwargs):
13871387
notification_type = NotificationType.objects.filter(name='external_confirm_success')
13881388
if not notification_type.exists():
13891389
raise NotificationType.DoesNotExist(
1390-
'NotificationType with name external_confirm_success does not exist.'
1390+
'NotificationType with name external_confirm_success does not exist.',
13911391
)
13921392
notification_type = notification_type.first()
1393-
notification_type.emit(user=user, event_context={'can_change_preferences': False, 'external_id_provider': provider})
1393+
notification_type.emit(user=user, message_frequency='instantly', event_context={'can_change_preferences': False, 'external_id_provider': provider})
13941394

13951395
enqueue_task(update_affiliation_for_orcid_sso_users.s(user._id, provider_id))
13961396

conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from framework.celery_tasks import app as celery_app
1919
from osf.external.spam import tasks as spam_tasks
2020
from website import settings as website_settings
21+
from osf.management.commands.migrate_notifications import update_notification_types
2122

2223

2324
logger = logging.getLogger(__name__)
@@ -374,3 +375,25 @@ def start_mock_send_grid(test_case):
374375
test_case.addCleanup(patcher.stop)
375376
mocked_send.return_value = True
376377
return mocked_send
378+
379+
380+
@pytest.fixture()
381+
def mock_notification_send():
382+
with mock.patch.object(website_settings, 'USE_EMAIL', True):
383+
with mock.patch.object(website_settings, 'USE_CELERY', False):
384+
with mock.patch('osf.models.notification.Notification.send') as mock_emit:
385+
mock_emit.return_value = None # Or True, if needed
386+
yield mock_emit
387+
388+
389+
def start_mock_notification_send(test_case):
390+
patcher = mock.patch('osf.models.notification.Notification.send')
391+
mocked_emit = patcher.start()
392+
test_case.addCleanup(patcher.stop)
393+
mocked_emit.return_value = None
394+
return mocked_emit
395+
396+
397+
@pytest.fixture(autouse=True)
398+
def load_notification_types(db, *args, **kwargs):
399+
update_notification_types(*args, **kwargs)

framework/auth/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from osf.exceptions import ValidationValueError, BlockedEmailError
3434
from osf.models.provider import PreprintProvider
3535
from osf.models.tag import Tag
36-
from osf.models.notification import NotificationType
36+
from osf.models.notification_type import NotificationType
3737
from osf.utils.requests import check_select_for_update
3838
from website.util.metrics import CampaignClaimedTags, CampaignSourceTags
3939
from website.ember_osf_web.decorators import ember_flag_is_active
@@ -279,7 +279,7 @@ def _forgot_password_post(mail_template, reset_route, institutional=False):
279279
f'NotificationType with name {mail_template} does not exist.'
280280
)
281281
notification_type = notification_type.first()
282-
notification_type.emit(user=user_obj, event_context={'can_change_preferences': False, 'reset_link': reset_link})
282+
notification_type.emit(user=user_obj, message_frequency='instantly', event_context={'can_change_preferences': False, 'reset_link': reset_link})
283283

284284
# institutional forgot password page displays the message as main text, not as an alert
285285
if institutional:

notifications.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,57 @@ notification_types:
1414
object_content_type_model_name: osfuser
1515
template: 'website/templates/emails/new_pending_submissions.html.mako'
1616
notification_freq_default: instantly
17+
- name: password_reset
18+
__docs__: ...
19+
object_content_type_model_name: osfuser
20+
template: 'website/templates/emails/password_reset.html.mako'
21+
notification_freq_default: instantly
22+
- name: forgot_password
23+
__docs__: ...
24+
object_content_type_model_name: osfuser
25+
template: 'website/templates/emails/forgot_password.html.mako'
26+
notification_freq_default: instantly
27+
- name: welcome_osf4i
28+
__docs__: ...
29+
object_content_type_model_name: osfuser
30+
template: 'website/templates/emails/welcome_osf4i.html.mako'
31+
notification_freq_default: instantly
32+
- name: invite_preprints_osf
33+
__docs__: ...
34+
object_content_type_model_name: osfuser
35+
template: 'website/templates/emails/invite_preprints_osf.html.mako'
36+
notification_freq_default: instantly
37+
- name: invite_preprints
38+
__docs__: ...
39+
object_content_type_model_name: osfuser
40+
template: 'website/templates/emails/invite_preprints.html.mako'
41+
notification_freq_default: instantly
42+
- name: invite_draft_registration
43+
__docs__: ...
44+
object_content_type_model_name: osfuser
45+
template: 'website/templates/emails/invite_draft_registration.html.mako'
46+
notification_freq_default: instantly
47+
- name: invite_default
48+
__docs__: ...
49+
object_content_type_model_name: osfuser
50+
template: 'website/templates/emails/invite_default.html.mako'
51+
notification_freq_default: instantly
52+
- name: pending_invite
53+
__docs__: ...
54+
object_content_type_model_name: osfuser
55+
template: 'website/templates/emails/pending_invite.html.mako'
56+
notification_freq_default: instantly
57+
- name: forward_invite
58+
__docs__: ...
59+
object_content_type_model_name: osfuser
60+
template: 'website/templates/emails/forward_invite.html.mako'
61+
notification_freq_default: instantly
62+
- name: external_confirm_success
63+
__docs__: ...
64+
object_content_type_model_name: osfuser
65+
template: 'website/templates/emails/external_confirm_success.html.mako'
66+
notification_freq_default: instantly
67+
1768
#### PROVIDER
1869
- name: new_pending_submissions
1970
__docs__: ...

osf/models/notification.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Notification(models.Model):
1717
created = models.DateTimeField(auto_now_add=True)
1818

1919
def send(self, protocol_type='email', recipient=None):
20+
if not settings.USE_EMAIL:
21+
return
2022
if not protocol_type == 'email':
2123
raise NotImplementedError(f'Protocol type {protocol_type}. Email notifications are only implemented.')
2224

osf/models/user.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@
5757
from osf.utils.requests import check_select_for_update
5858
from osf.utils.permissions import API_CONTRIBUTOR_PERMISSIONS, MANAGER, MEMBER, ADMIN
5959
from website import settings as website_settings
60-
from website import filters, mails
60+
from website import filters
6161
from website.project import new_bookmark_collection
6262
from website.util.metrics import OsfSourceTags, unregistered_created_source_tag
6363
from importlib import import_module
6464
from osf.utils.requests import get_headers_from_request
65+
from osf.models.notification_type import NotificationType
6566

6667
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
6768

@@ -1071,13 +1072,13 @@ def set_password(self, raw_password, notify=True):
10711072
raise ChangePasswordError(['Password cannot be the same as your email address'])
10721073
super().set_password(raw_password)
10731074
if had_existing_password and notify:
1074-
mails.send_mail(
1075-
to_addr=self.username,
1076-
mail=mails.PASSWORD_RESET,
1077-
user=self,
1078-
can_change_preferences=False,
1079-
osf_contact_email=website_settings.OSF_CONTACT_EMAIL
1080-
)
1075+
notification_type = NotificationType.objects.filter(name='password_reset')
1076+
if not notification_type.exists():
1077+
raise NotificationType.DoesNotExist(
1078+
'NotificationType with name password_reset does not exist.',
1079+
)
1080+
notification_type = notification_type.first()
1081+
notification_type.emit(user=self, message_frequency='instantly', event_context={'can_change_preferences': False, 'osf_contact_email': website_settings.OSF_CONTACT_EMAIL})
10811082
remove_sessions_for_user(self)
10821083

10831084
@classmethod

0 commit comments

Comments
 (0)