Skip to content

Commit aedf5a3

Browse files
fix unit tests
1 parent 25ce6c2 commit aedf5a3

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

api_tests/notifications/test_notification_digest.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ def add_notification_subscription(user, notification_type, frequency, subscribed
1818
Create a NotificationSubscription for a user.
1919
If the notification type corresponds to a subscribed_object, set subscribed_object to get the provider.
2020
"""
21-
from osf.models import NotificationSubscription
21+
from osf.models import NotificationSubscription, AbstractProvider
2222
kwargs = {
2323
'user': user,
2424
'notification_type': NotificationType.objects.get(name=notification_type),
2525
'message_frequency': frequency,
2626
}
2727
if subscribed_object is not None:
2828
kwargs['object_id'] = subscribed_object.id
29-
kwargs['content_type'] = ContentType.objects.get_for_model(subscribed_object)
29+
if isinstance(subscribed_object, AbstractProvider):
30+
kwargs['content_type'] = ContentType.objects.get_for_model(subscribed_object, for_concrete_model=False) if subscribed_object else None
31+
else:
32+
kwargs['content_type'] = ContentType.objects.get_for_model(subscribed_object) if subscribed_object else None
3033
if subscription is not None:
3134
kwargs['object_id'] = subscription.id
3235
kwargs['content_type'] = ContentType.objects.get_for_model(subscription)
@@ -113,16 +116,17 @@ def test_send_user_email_task_no_notifications(self):
113116
def test_send_moderator_email_task_registration_provider_admin(self):
114117
user = AuthUserFactory(fullname='Admin User')
115118
reg_provider = RegistrationProviderFactory(_id='abc123')
116-
reg = RegistrationFactory(provider=reg_provider)
117-
admin_group = reg_provider.get_group('admin')
118-
admin_group.user_set.add(user)
119+
reg_provider_content_type = ContentType.objects.get_for_model(reg_provider)
120+
RegistrationFactory(provider=reg_provider)
121+
moderator_group = reg_provider.get_group('moderator')
122+
moderator_group.user_set.add(user)
119123
notification_type = NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS)
120124
notification = Notification.objects.create(
121125
subscription=add_notification_subscription(
122126
user,
123127
notification_type,
124128
'daily',
125-
subscribed_object=reg
129+
subscribed_object=reg_provider
126130
),
127131
event_context={
128132
'profile_image_url': 'http://example.com/profile.png',
@@ -137,7 +141,7 @@ def test_send_moderator_email_task_registration_provider_admin(self):
137141
)
138142
notification_ids = [notification.id]
139143
with capture_notifications() as notifications:
140-
send_moderator_email_task.apply(args=(user._id, notification_ids)).get()
144+
send_moderator_email_task.apply(args=(user._id, notification_ids, reg_provider_content_type.id, reg_provider.id)).get()
141145
assert len(notifications['emits']) == 1
142146
assert notifications['emits'][0]['type'] == NotificationType.Type.DIGEST_REVIEWS_MODERATORS
143147
assert notifications['emits'][0]['kwargs']['user'] == user
@@ -150,30 +154,30 @@ def test_send_moderator_email_task_registration_provider_admin(self):
150154
def test_send_moderator_email_task_no_notifications(self):
151155
user = AuthUserFactory(fullname='Admin User')
152156
provider = RegistrationProviderFactory()
153-
reg = RegistrationFactory(provider=provider)
157+
reg_provider_content_type = ContentType.objects.get_for_model(provider)
158+
RegistrationFactory(provider=provider)
154159

155160
notification_ids = []
156161
notification_type = NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS)
157162
add_notification_subscription(
158163
user,
159164
notification_type,
160165
'daily',
161-
subscribed_object=reg
166+
subscribed_object=provider
162167
)
163-
164-
send_moderator_email_task.apply(args=(user._id, notification_ids)).get()
168+
send_moderator_email_task.apply(args=(user._id, notification_ids, reg_provider_content_type.id, provider.id)).get()
165169
email_task = EmailTask.objects.filter(user_id=user.id).first()
166170
assert email_task.status == 'SUCCESS'
167171

168172
def test_send_moderator_email_task_user_not_found(self):
169-
send_moderator_email_task.apply(args=('nouser', [])).get()
173+
send_moderator_email_task.apply(args=('nouser', [], 1, 1)).get()
170174
email_task = EmailTask.objects.filter()
171175
assert email_task.exists()
172176
assert email_task.first().status == 'NO_USER_FOUND'
173177

174178
def test_get_users_emails(self):
175179
user = AuthUserFactory()
176-
notification_type = NotificationType.objects.get(name=NotificationType.Type.USER_DIGEST)
180+
notification_type = NotificationType.objects.get(name=NotificationType.Type.USER_FILE_UPDATED)
177181
notification1 = Notification.objects.create(
178182
subscription=add_notification_subscription(user, notification_type, 'daily'),
179183
sent=None,
@@ -249,10 +253,12 @@ def test_send_users_digest_email_end_to_end(self):
249253
def test_send_moderators_digest_email_end_to_end(self):
250254
user = AuthUserFactory()
251255
provider = RegistrationProviderFactory()
252-
reg = RegistrationFactory(provider=provider)
256+
RegistrationFactory(provider=provider)
257+
moderator_group = provider.get_group('moderator')
258+
moderator_group.user_set.add(user)
253259
notification_type = NotificationType.objects.get(name=NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS)
254260
Notification.objects.create(
255-
subscription=add_notification_subscription(user, notification_type, 'daily', subscribed_object=reg),
261+
subscription=add_notification_subscription(user, notification_type, 'daily', subscribed_object=provider),
256262
sent=None,
257263
event_context={
258264
'submitter_fullname': 'submitter_fullname',

notifications.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,3 +884,10 @@ notification_types:
884884
object_content_type_model_name: abstractnode
885885
template: 'website/templates/file_updated.html.mako'
886886
tests: ['tests/test_events.py']
887+
888+
- name: node_withdrawal_request_rejected
889+
subject: 'Your withdrawal request has been declined'
890+
__docs__: ...
891+
object_content_type_model_name: abstractnode
892+
template: 'website/templates/withdrawal_request_declined.html.mako'
893+
tests: []

osf_tests/test_registration_moderation_notifications.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ def test_submit_notifications(self, registration, moderator, admin, contrib, pro
140140
assert notification['emits'][1]['type'] == NotificationType.Type.PROVIDER_REVIEWS_SUBMISSION_CONFIRMATION
141141
assert notification['emits'][1]['kwargs']['user'] == contrib
142142
assert notification['emits'][2]['type'] == NotificationType.Type.PROVIDER_NEW_PENDING_SUBMISSIONS
143-
144-
assert NotificationSubscription.objects.count() == 5
143+
assert NotificationSubscription.objects.count() == 6
145144
digest = NotificationSubscription.objects.last()
146145
assert digest.user == moderator
147146

0 commit comments

Comments
 (0)