Skip to content

Commit 3125f32

Browse files
fix unit test
1 parent ae2f742 commit 3125f32

File tree

8 files changed

+51
-57
lines changed

8 files changed

+51
-57
lines changed

api_tests/draft_registrations/views/test_draft_registration_contributor_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_add_unregistered_contributor_signal_if_default(self, app, user, url_pro
300300
)
301301
assert res.status_code == 201
302302
assert len(notifications['emits']) == 1
303-
assert notifications['emits'][0]['type'] == NotificationType.Type.USER_INVITE_DRAFT_REGISTRATION
303+
assert notifications['emits'][0]['type'] == NotificationType.Type.user_invite_draft_registration
304304

305305
# Overrides TestNodeContributorCreateEmail
306306
def test_add_unregistered_contributor_without_email_no_email(self, app, user, url_project_contribs):

osf/models/mixins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,7 @@ def add_contributors(
15211521
auth=auth,
15221522
log=False,
15231523
save=False,
1524-
notification_type=notification_type,
1524+
notification_type=notification_type
15251525
)
15261526
if log and contributors:
15271527
params = self.log_params
@@ -1640,9 +1640,9 @@ def add_contributor_registered_or_not(self,
16401640
contributor = self.add_contributor(
16411641
contributor=contributor,
16421642
auth=auth,
1643-
notification_type=notification_type,
16441643
visible=bibliographic,
16451644
permissions=permissions,
1645+
notification_type=notification_type,
16461646
save=True
16471647
)
16481648
else:
@@ -1670,8 +1670,8 @@ def add_contributor_registered_or_not(self,
16701670
self.add_contributor(
16711671
contributor=contributor,
16721672
auth=auth,
1673-
notification_type=notification_type,
16741673
visible=bibliographic,
1674+
notification_type=notification_type,
16751675
permissions=permissions,
16761676
save=True
16771677
)

osf_tests/test_draft_registration.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def test_add_contributors(self, draft_registration, auth):
348348
{'user': user1, 'permissions': ADMIN, 'visible': True},
349349
{'user': user2, 'permissions': WRITE, 'visible': False}
350350
],
351-
auth=auth
351+
auth=auth,
352+
notification_type=None
352353
)
353354
last_log = draft_registration.logs.all().order_by('-created')[0]
354355
assert (
@@ -510,7 +511,8 @@ def test_remove_contributors(self, draft_registration, auth):
510511
{'user': user1, 'permissions': WRITE, 'visible': True},
511512
{'user': user2, 'permissions': WRITE, 'visible': True}
512513
],
513-
auth=auth
514+
auth=auth,
515+
notification_type=None
514516
)
515517
assert user1 in draft_registration.contributors
516518
assert user2 in draft_registration.contributors

osf_tests/test_node.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ def test_add_contributors(self, node, auth):
916916
{'user': user1, 'permissions': ADMIN, 'visible': True},
917917
{'user': user2, 'permissions': WRITE, 'visible': False}
918918
],
919-
auth=auth
919+
auth=auth,
920+
notification_type=None
920921
)
921922
last_log = node.logs.all().order_by('-date')[0]
922923
assert (
@@ -1114,7 +1115,8 @@ def test_remove_contributors(self, node, auth):
11141115
{'user': user1, 'permissions': permissions.WRITE, 'visible': True},
11151116
{'user': user2, 'permissions': permissions.WRITE, 'visible': True}
11161117
],
1117-
auth=auth
1118+
auth=auth,
1119+
notification_type=None
11181120
)
11191121
assert user1 in node.contributors
11201122
assert user2 in node.contributors
@@ -1232,7 +1234,7 @@ class TestNodeAddContributorRegisteredOrNot:
12321234
def test_add_contributor_user_id(self, user, node):
12331235
registered_user = UserFactory()
12341236
with capture_notifications():
1235-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), user_id=registered_user._id)
1237+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), user_id=registered_user._id, notification_type=None)
12361238
contributor = contributor_obj.user
12371239
assert contributor in node.contributors
12381240
assert contributor.is_registered is True
@@ -1241,7 +1243,7 @@ def test_add_contributor_registered_or_not_unreg_user_without_unclaimed_records(
12411243
unregistered_user = UnregUserFactory()
12421244
unregistered_user.save()
12431245
with capture_notifications():
1244-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), email=unregistered_user.email, full_name=unregistered_user.fullname)
1246+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), email=unregistered_user.email, full_name=unregistered_user.fullname, notification_type=None)
12451247

12461248
contributor = contributor_obj.user
12471249
assert contributor in node.contributors
@@ -1260,22 +1262,22 @@ def test_add_contributor_invalid_user_id(self, user, node):
12601262

12611263
def test_add_contributor_fullname_email(self, user, node):
12621264
with capture_notifications():
1263-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', email='[email protected]')
1265+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', email='[email protected]', notification_type=None)
12641266
contributor = contributor_obj.user
12651267
assert contributor in node.contributors
12661268
assert contributor.is_registered is False
12671269

12681270
def test_add_contributor_fullname(self, user, node):
1269-
with capture_notifications():
1270-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe')
1271+
with capture_notifications(expect_none=True):
1272+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', notification_type=None)
12711273
contributor = contributor_obj.user
12721274
assert contributor in node.contributors
12731275
assert contributor.is_registered is False
12741276

12751277
def test_add_contributor_fullname_email_already_exists(self, user, node):
12761278
registered_user = UserFactory()
12771279
with capture_notifications():
1278-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='F Mercury', email=registered_user.username)
1280+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='F Mercury', email=registered_user.username, notification_type=None)
12791281
contributor = contributor_obj.user
12801282
assert contributor in node.contributors
12811283
assert contributor.is_registered is True
@@ -1284,7 +1286,7 @@ def test_add_contributor_fullname_email_exists_as_secondary(self, user, node):
12841286
registered_user = UserFactory()
12851287
secondary_email = '[email protected]'
12861288
Email.objects.create(address=secondary_email, user=registered_user)
1287-
with capture_notifications():
1289+
with capture_notifications(expect_none=True):
12881290
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name='F Mercury', email=secondary_email)
12891291
contributor = contributor_obj.user
12901292
assert contributor == registered_user
@@ -1295,7 +1297,7 @@ def test_add_contributor_unregistered(self, user, node):
12951297
unregistered_user = UnregUserFactory()
12961298
unregistered_user.save()
12971299
with capture_notifications():
1298-
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name=unregistered_user.fullname, email=unregistered_user.email)
1300+
contributor_obj = node.add_contributor_registered_or_not(auth=Auth(user), full_name=unregistered_user.fullname, email=unregistered_user.email, notification_type=None)
12991301
contributor = contributor_obj.user
13001302
assert contributor == unregistered_user
13011303
assert contributor in node.contributors
@@ -1345,8 +1347,7 @@ def test_add_contributors_sends_contributor_added_signal(self, node, auth):
13451347
'permissions': permissions.WRITE
13461348
}]
13471349
with capture_signals() as mock_signals:
1348-
with capture_notifications():
1349-
node.add_contributors(contributors=contributors, auth=auth)
1350+
node.add_contributors(contributors=contributors, auth=auth)
13501351
node.save()
13511352
assert node.is_contributor(user)
13521353
assert mock_signals.signals_sent() == {contributor_added}
@@ -2612,7 +2613,8 @@ def test_contributor_set_visibility_validation(self, node, user, auth):
26122613
[
26132614
{'user': reg_user1, 'permissions': ADMIN, 'visible': True},
26142615
{'user': reg_user2, 'permissions': ADMIN, 'visible': False},
2615-
]
2616+
],
2617+
notification_type=None
26162618
)
26172619
with pytest.raises(ValueError) as e:
26182620
node.set_visible(user=reg_user1, visible=False, auth=None)
@@ -3364,7 +3366,8 @@ def test_move_contributor(self, user, node, auth):
33643366
{'user': user1, 'permissions': WRITE, 'visible': True},
33653367
{'user': user2, 'permissions': WRITE, 'visible': True}
33663368
],
3367-
auth=auth
3369+
auth=auth,
3370+
notification_type=None
33683371
)
33693372

33703373
user_contrib_id = node.contributor_set.get(user=user).id

tests/test_adding_contributor_views.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def test_add_contributor_with_unreg_contribs_and_reg_contribs(self):
147147
'node_ids': []
148148
}
149149
url = self.project.api_url_for('project_contributors_post')
150-
with capture_notifications():
151-
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
150+
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
152151
self.project.reload()
153152
assert len(self.project.contributors) == n_contributors_pre + len(payload['users'])
154153

@@ -187,10 +186,7 @@ def test_add_contributors_post_only_sends_one_email_to_unreg_user(self, mock_sen
187186
# send request
188187
url = self.project.api_url_for('project_contributors_post')
189188
assert self.project.can_edit(user=self.creator)
190-
with capture_notifications() as notifications:
191-
self.app.post(url, json=payload, auth=self.creator.auth)
192-
assert len(notifications['emits']) == 1
193-
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
189+
self.app.post(url, json=payload, auth=self.creator.auth)
194190

195191
def test_add_contributors_post_only_sends_one_email_to_registered_user(self):
196192
# Project has components
@@ -214,10 +210,7 @@ def test_add_contributors_post_only_sends_one_email_to_registered_user(self):
214210
# send request
215211
url = self.project.api_url_for('project_contributors_post')
216212
assert self.project.can_edit(user=self.creator)
217-
with capture_notifications() as notifications:
218-
self.app.post(url, json=payload, auth=self.creator.auth)
219-
assert len(notifications['emits']) == 1
220-
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
213+
self.app.post(url, json=payload, auth=self.creator.auth)
221214

222215
def test_add_contributors_post_sends_email_if_user_not_contributor_on_parent_node(self):
223216
# Project has a component with a sub-component
@@ -241,12 +234,7 @@ def test_add_contributors_post_sends_email_if_user_not_contributor_on_parent_nod
241234
# send request
242235
url = self.project.api_url_for('project_contributors_post')
243236
assert self.project.can_edit(user=self.creator)
244-
with capture_notifications() as notifications:
245-
self.app.post(url, json=payload, auth=self.creator.auth)
246-
247-
# send_mail is called for both the project and the sub-component
248-
assert len(notifications['emits']) == 1
249-
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
237+
self.app.post(url, json=payload, auth=self.creator.auth)
250238

251239
@mock.patch('website.project.views.contributor.send_claim_email')
252240
def test_email_sent_when_unreg_user_is_added(self, send_mail):
@@ -264,10 +252,7 @@ def test_email_sent_when_unreg_user_is_added(self, send_mail):
264252
'node_ids': []
265253
}
266254
url = self.project.api_url_for('project_contributors_post')
267-
with capture_notifications() as notifications:
268-
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
269-
assert len(notifications['emits']) == 1
270-
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
255+
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
271256

272257
def test_email_sent_when_reg_user_is_added(self):
273258
contributor = UserFactory()
@@ -278,7 +263,7 @@ def test_email_sent_when_reg_user_is_added(self):
278263
}]
279264
project = ProjectFactory(creator=self.auth.user)
280265
with capture_notifications() as notifications:
281-
project.add_contributors(contributors, auth=self.auth)
266+
project.add_contributors(contributors, auth=self.auth, notification_type=None)
282267
project.save()
283268
assert len(notifications['emits']) == 1
284269
assert notifications['emits'][0]['type'] == NotificationType.Type.NODE_CONTRIBUTOR_ADDED_DEFAULT
@@ -410,8 +395,7 @@ def test_add_multiple_contributors_only_adds_one_log(self):
410395
'node_ids': []
411396
}
412397
url = self.project.api_url_for('project_contributors_post')
413-
with capture_notifications():
414-
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
398+
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
415399
self.project.reload()
416400
assert self.project.logs.count() == n_logs_pre + 1
417401

@@ -436,8 +420,7 @@ def test_add_contribs_to_multiple_nodes(self):
436420
'node_ids': [self.project._primary_key, child._primary_key]
437421
}
438422
url = f'/api/v1/project/{self.project._id}/contributors/'
439-
with capture_notifications():
440-
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
423+
self.app.post(url, json=payload, follow_redirects=True, auth=self.creator.auth)
441424
child.reload()
442425
assert child.contributors.count() == n_contributors_pre + len(payload['users'])
443426

tests/test_preprints.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ def test_add_contributors(self, preprint, auth):
337337
{'user': user2, 'permissions': WRITE, 'visible': False}
338338
],
339339
auth=auth,
340+
notification_type=None
340341
)
341342
last_log = preprint.logs.all().order_by('-created')[0]
342343
assert (
@@ -472,7 +473,8 @@ def test_remove_contributors(self, preprint, auth):
472473
{'user': user1, 'permissions': WRITE, 'visible': True},
473474
{'user': user2, 'permissions': WRITE, 'visible': True}
474475
],
475-
auth=auth
476+
auth=auth,
477+
notification_type=None
476478
)
477479
assert user1 in preprint.contributors
478480
assert user2 in preprint.contributors
@@ -571,7 +573,7 @@ class TestPreprintAddContributorRegisteredOrNot:
571573
def test_add_contributor_user_id(self, user, preprint):
572574
registered_user = UserFactory()
573575
with capture_notifications():
574-
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), user_id=registered_user._id)
576+
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), user_id=registered_user._id, notification_type=None)
575577
contributor = contributor_obj.user
576578
assert contributor in preprint.contributors
577579
assert contributor.is_registered is True
@@ -588,22 +590,22 @@ def test_add_contributor_invalid_user_id(self, user, preprint):
588590

589591
def test_add_contributor_fullname_email(self, user, preprint):
590592
with capture_notifications():
591-
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', email='[email protected]')
593+
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', email='[email protected]', notification_type=None)
592594
contributor = contributor_obj.user
593595
assert contributor in preprint.contributors
594596
assert contributor.is_registered is False
595597

596598
def test_add_contributor_fullname(self, user, preprint):
597-
with capture_notifications():
598-
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe')
599+
with capture_notifications(expect_none=True):
600+
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='Jane Doe', notification_type=None)
599601
contributor = contributor_obj.user
600602
assert contributor in preprint.contributors
601603
assert contributor.is_registered is False
602604

603605
def test_add_contributor_fullname_email_already_exists(self, user, preprint):
604606
registered_user = UserFactory()
605-
with capture_notifications():
606-
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='F Mercury', email=registered_user.username)
607+
with capture_notifications(expect_none=True):
608+
contributor_obj = preprint.add_contributor_registered_or_not(auth=Auth(user), full_name='F Mercury', email=registered_user.username, notification_type=None)
607609
contributor = contributor_obj.user
608610
assert contributor in preprint.contributors
609611
assert contributor.is_registered is True
@@ -1075,7 +1077,8 @@ def test_contributor_set_visibility_validation(self, preprint, user, auth):
10751077
[
10761078
{'user': reg_user1, 'permissions': ADMIN, 'visible': True},
10771079
{'user': reg_user2, 'permissions': ADMIN, 'visible': False},
1078-
]
1080+
],
1081+
notification_type=None,
10791082
)
10801083
with pytest.raises(ValueError) as e:
10811084
preprint.set_visible(user=reg_user1, visible=False, auth=None)
@@ -1282,7 +1285,8 @@ def test_move_contributor(self, user, preprint, auth):
12821285
{'user': user1, 'permissions': WRITE, 'visible': True},
12831286
{'user': user2, 'permissions': WRITE, 'visible': True}
12841287
],
1285-
auth=auth
1288+
auth=auth,
1289+
notification_type=None
12861290
)
12871291

12881292
user_contrib_id = preprint.preprintcontributor_set.get(user=user).id

tests/test_project_contibutor_views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ def test_contributor_manage_reorder(self):
325325
[
326326
{'user': reg_user1, 'permissions': permissions.ADMIN, 'visible': True},
327327
{'user': reg_user2, 'permissions': permissions.ADMIN, 'visible': False},
328-
]
328+
],
329+
notification_type=None,
329330
)
330331
# Add a non-registered user
331332
unregistered_user = project.add_unregistered_contributor(
@@ -550,7 +551,8 @@ def test_get_contributors_abbrev(self):
550551
'permissions': permissions.ADMIN,
551552
'visible': True
552553
},
553-
]
554+
],
555+
notification_type=None,
554556
)
555557

556558
# add an unregistered contributor

website/project/views/contributor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def send_claim_email(
560560
destination_address=email,
561561
event_context={
562562
'user_fullname': referrer.id,
563-
'referrer_fullname': referrer.fullname,
563+
'referrer_name': referrer.fullname,
564564
'fullname': unclaimed_record['name'],
565565
'node_url': node.url,
566566
'logo': logo,

0 commit comments

Comments
 (0)