Skip to content

Commit aa1b0e3

Browse files
committed
improve notification mocking
1 parent eae2207 commit aa1b0e3

102 files changed

Lines changed: 737 additions & 664 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

addons/boa/tests/test_tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def test_handle_boa_error(self):
6868
output_file_name=self.output_file_name,
6969
job_id=self.job_id
7070
)
71-
assert len(notifications) == 1
72-
assert notifications[0]['type'] == NotificationType.Type.ADDONS_BOA_JOB_FAILURE
71+
assert len(notifications['emits']) == 1
72+
assert notifications['emits'][0]['type'] == NotificationType.Type.ADDONS_BOA_JOB_FAILURE
7373
mock_sentry_log_message.assert_called_with(self.error_message, skip_session=True)
7474
mock_logger_error.assert_called_with(self.error_message)
7575
assert return_value == BoaErrorCode.UNKNOWN

admin_tests/users/test_views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,12 @@ def test_get_password_reset_link(self):
624624

625625
def test_get_unclaimed_node_links(self):
626626
project = ProjectFactory()
627-
unregistered_contributor = project.add_unregistered_contributor(fullname='Brother Nero', email='matt@hardyboyz.biz', auth=Auth(project.creator))
627+
unregistered_contributor = project.add_unregistered_contributor(
628+
fullname='Brother Nero',
629+
email='matt@hardyboyz.biz',
630+
auth=Auth(project.creator),
631+
notification_type=False
632+
)
628633
project.save()
629634

630635
request = RequestFactory().get('/fake_path')

api_tests/crossref/views/test_crossref_email_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ def test_error_response_sends_message_does_not_set_doi(self, app, url, preprint,
162162
context_data = self.make_mailgun_payload(crossref_response=error_xml)
163163
with capture_notifications() as notifications:
164164
app.post(url, context_data)
165-
assert len(notifications) == 1
166-
assert notifications[0]['type'] == NotificationType.Type.PROVIDER_MODERATOR_ADDED
165+
assert len(notifications['emits']) == 1
166+
assert notifications['emits'][0]['type'] == NotificationType.Type.PROVIDER_MODERATOR_ADDED
167167
assert not preprint.get_identifier_value('doi')
168168

169169
def test_success_response_sets_doi(self, app, url, preprint, success_xml):

api_tests/draft_registrations/views/test_draft_registration_contributor_list.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ def test_permissions_work_with_many_users(
183183
perm = random.choice(list(users.keys()))
184184
user = AuthUserFactory()
185185

186-
project_private.add_contributor(user, permissions=perm)
186+
project_private.add_contributor(
187+
user,
188+
permissions=perm,
189+
notification_type=False
190+
)
187191
users[perm].append(user._id)
188192

189193
res = app.get(url_private, auth=user.auth)
@@ -242,8 +246,8 @@ def test_add_contributor_sends_email(self, app, user, user_two, url_project_cont
242246
auth=user.auth
243247
)
244248
assert res.status_code == 201
245-
assert len(notifications) == 1
246-
assert notifications[0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
249+
assert len(notifications['emits']) == 1
250+
assert notifications['emits'][0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
247251

248252
# Overrides TestNodeContributorCreateEmail
249253
def test_add_contributor_signal_if_default(
@@ -285,8 +289,8 @@ def test_add_unregistered_contributor_sends_email(self, app, user, url_project_c
285289
auth=user.auth
286290
)
287291
assert res.status_code == 201
288-
assert len(notifications) == 1
289-
assert notifications[0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
292+
assert len(notifications['emits']) == 1
293+
assert notifications['emits'][0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
290294

291295
# Overrides TestNodeContributorCreateEmail
292296
def test_add_unregistered_contributor_signal_if_default(self, app, user, url_project_contribs):
@@ -304,8 +308,8 @@ def test_add_unregistered_contributor_signal_if_default(self, app, user, url_pro
304308
}, auth=user.auth
305309
)
306310
assert res.status_code == 201
307-
assert len(notifications) == 1
308-
assert notifications[0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
311+
assert len(notifications['emits']) == 1
312+
assert notifications['emits'][0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
309313

310314
# Overrides TestNodeContributorCreateEmail
311315
def test_add_unregistered_contributor_without_email_no_email(self, app, user, url_project_contribs):
@@ -374,11 +378,17 @@ def project_private(
374378
project_private.add_contributor(
375379
user_two,
376380
permissions=permissions.READ,
377-
visible=True, save=True)
381+
visible=True,
382+
save=True,
383+
notification_type=False
384+
)
378385
project_private.add_contributor(
379386
user_three,
380387
permissions=permissions.READ,
381-
visible=True, save=True)
388+
visible=True,
389+
save=True,
390+
notification_type=False
391+
)
382392
return project_private
383393

384394
@pytest.fixture()
@@ -484,11 +494,17 @@ def project_private(
484494
project_private.add_contributor(
485495
user_two,
486496
permissions=permissions.READ,
487-
visible=True, save=True)
497+
visible=True,
498+
save=True,
499+
notification_type=False
500+
)
488501
project_private.add_contributor(
489502
user_three,
490503
permissions=permissions.READ,
491-
visible=True, save=True)
504+
visible=True,
505+
save=True,
506+
notification_type=False
507+
)
492508
return project_private
493509

494510

api_tests/draft_registrations/views/test_draft_registration_list.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ def test_create_no_project_draft_emails_initiator(self, app, user, url_draft_reg
442442
payload,
443443
auth=user.auth
444444
)
445-
assert len(notifications) == 1
446-
assert notifications[0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
447-
assert notifications[0]['kwargs']['user'] == user
445+
assert len(notifications['emits']) == 1
446+
assert notifications['emits'][0]['type'] == NotificationType.Type.DRAFT_REGISTRATION_CONTRIBUTOR_ADDED_DEFAULT
447+
assert notifications['emits'][0]['kwargs']['user'] == user
448448

449449
def test_create_draft_with_provider(
450450
self, app, user, url_draft_registrations, non_default_provider, payload_with_non_default_provider

api_tests/draft_registrations/views/test_draft_registration_relationship_institutions.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import pytest
22

33
from api.base.settings.defaults import API_BASE
4+
from osf.models import NotificationType
45

56
from osf_tests.factories import DraftRegistrationFactory, AuthUserFactory, InstitutionFactory
67
from osf.utils import permissions
8+
from tests.utils import capture_notifications
79

810

911
@pytest.mark.django_db
@@ -139,18 +141,21 @@ def test_delete_user_is_read_write(
139141
# Overrides TestNodeRelationshipInstitutions
140142
def test_read_write_contributor_can_add_affiliated_institution(
141143
self, app, write_contrib, write_contrib_institution, node, node_institutions_url):
142-
payload = {
143-
'data': [{
144-
'type': 'institutions',
145-
'id': write_contrib_institution._id
146-
}]
147-
}
148-
res = app.post_json_api(
149-
node_institutions_url,
150-
payload,
151-
auth=write_contrib.auth,
152-
expect_errors=True
153-
)
144+
with capture_notifications() as notifications:
145+
res = app.post_json_api(
146+
node_institutions_url,
147+
{
148+
'data': [{
149+
'type': 'institutions',
150+
'id': write_contrib_institution._id
151+
}]
152+
},
153+
auth=write_contrib.auth,
154+
expect_errors=True
155+
)
156+
assert len(notifications['emits']) == 1
157+
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
158+
154159
node.reload()
155160
assert res.status_code == 201
156161

@@ -235,11 +240,14 @@ def test_user_with_institution_and_permissions(
235240
user.add_or_update_affiliated_institution(institution_three)
236241
assert institution_three not in node.affiliated_institutions.all()
237242

238-
res = app.post_json_api(
239-
node_institutions_url,
240-
create_payload(institution_three._id),
241-
auth=user.auth
242-
)
243+
with capture_notifications() as notifications:
244+
res = app.post_json_api(
245+
node_institutions_url,
246+
create_payload(institution_three._id),
247+
auth=user.auth
248+
)
249+
assert len(notifications['emits']) == 1
250+
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
243251

244252
assert res.status_code == 201
245253
data = res.json['data']
@@ -276,11 +284,13 @@ def test_remove_institutions_with_affiliated_user(
276284
node.affiliated_institutions.add(institution_one)
277285
assert institution_one in node.affiliated_institutions.all()
278286

279-
res = app.put_json_api(
280-
node_institutions_url,
281-
{'data': []},
282-
auth=user.auth
283-
)
287+
with capture_notifications() as notifications:
288+
res = app.put_json_api(
289+
node_institutions_url,
290+
{'data': []},
291+
auth=user.auth
292+
)
293+
assert not notifications
284294

285295
assert res.status_code == 200
286296
node.reload()

api_tests/institutions/views/test_institution_auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ def test_user_unclaimed(self, app, institution, url_auth_institution):
323323
user = project.add_unregistered_contributor(
324324
fullname=fullname,
325325
email=username,
326-
auth=Auth(project.creator)
326+
auth=Auth(project.creator),
327+
notification_type=False
327328
)
328329
user.save()
329330
# Unclaimed user is given an unusable password when being added as a contributor

api_tests/institutions/views/test_institution_relationship_nodes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ def test_email_sent_on_affiliation_addition(
398398
)
399399

400400
assert res.status_code == 201
401-
assert len(notifications) == 1
402-
assert notifications[0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
401+
assert len(notifications['emits']) == 1
402+
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
403403

404404
def test_email_sent_on_affiliation_removal(self, app, admin, institution, node_public, url_institution_nodes):
405405
current_institution = InstitutionFactory()
@@ -421,8 +421,8 @@ def test_email_sent_on_affiliation_removal(self, app, admin, institution, node_p
421421
# Assert response is successful
422422
assert res.status_code == 204
423423

424-
assert len(notifications) == 2
425-
assert notifications[0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
426-
assert notifications[0]['kwargs']['user'] == node_public.creator
427-
assert notifications[1]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
424+
assert len(notifications['emits']) == 2
425+
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
426+
assert notifications['emits'][0]['kwargs']['user'] == node_public.creator
427+
assert notifications['emits'][1]['type'] == NotificationType.Type.NODE_AFFILIATION_CHANGED
428428
assert notifications[1]['kwargs']['user'] == admin

api_tests/logs/views/test_log_params.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def test_unregistered_contributor_added_has_contributor_info_in_params(
3737
project.add_unregistered_contributor(
3838
'Robert Jackson',
3939
'robert@gmail.com',
40-
auth=Auth(user_one)
40+
auth=Auth(user_one),
41+
notification_type=False
4142
)
4243
relevant_log = project.logs.latest()
4344
url = f'/{API_BASE}logs/{relevant_log._id}/'

api_tests/mailhog/test_mailhog.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest import mock
2+
13
import requests
24
import pytest
35
from waffle.testutils import override_switch
@@ -21,7 +23,9 @@
2123

2224
@pytest.mark.django_db
2325
class TestMailHog:
26+
passthrough_notifications = True
2427

28+
@mock.patch('website.settings.DEV_MODE', True)
2529
def test_mailhog_received_mail(self):
2630
with override_switch(features.ENABLE_MAILHOG, active=True):
2731
mailhog_v1 = f'{settings.MAILHOG_API_HOST}/api/v1/messages'
@@ -31,6 +35,7 @@ def test_mailhog_received_mail(self):
3135
NotificationType.objects.get(
3236
name=NotificationType.Type.USER_REGISTRATION_BULK_UPLOAD_FAILURE_ALL
3337
).emit(
38+
message_frequency='instantly',
3439
destination_address='to_addr@mail.com',
3540
event_context={
3641
'fullname': '<NAME>',
@@ -51,6 +56,7 @@ def test_mailhog_received_mail(self):
5156

5257
@pytest.mark.django_db
5358
class TestAuthMailhog(OsfTestCase):
59+
passthrough_notifications = True
5460

5561
def setUp(self):
5662
super().setUp()

0 commit comments

Comments
 (0)