|
1 | 1 | # Copyright The IETF Trust 2016-2023, All Rights Reserved
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 |
|
4 |
| - |
5 | 4 | import mock
|
6 | 5 | from pyquery import PyQuery
|
7 | 6 |
|
|
11 | 10 | import debug # pyflakes:ignore
|
12 | 11 |
|
13 | 12 | from ietf.community.models import CommunityList, SearchRule, EmailSubscription
|
| 13 | +from ietf.community.signals import notify_of_event |
14 | 14 | from ietf.community.utils import docs_matching_community_list_rule, community_list_rules_matching_doc
|
15 | 15 | from ietf.community.utils import reset_name_contains_index_for_rule, notify_event_to_subscribers
|
16 | 16 | from ietf.community.tasks import notify_event_to_subscribers_task
|
@@ -431,53 +431,58 @@ def test_subscription_for_group(self):
|
431 | 431 | r = self.client.get(url)
|
432 | 432 | self.assertEqual(r.status_code, 200)
|
433 | 433 |
|
434 |
| - # Mock out the on_commit call so we can tell whether the task was actually queued |
435 |
| - @mock.patch("ietf.submit.views.transaction.on_commit", side_effect=lambda x: x()) |
436 |
| - @mock.patch("ietf.community.models.notify_event_to_subscribers_task") |
437 |
| - def test_notification_signal_receiver(self, mock_notify_task, mock_on_commit): |
438 |
| - """Saving a DocEvent should notify subscribers |
| 434 | + @mock.patch("ietf.community.signals.notify_of_event") |
| 435 | + def test_notification_signal_receiver(self, mock_notify_of_event): |
| 436 | + """Saving a newly created DocEvent should notify subscribers |
439 | 437 |
|
440 |
| - This implicitly tests that notify_events is hooked up to the post_save signal. |
| 438 | + This implicitly tests that notify_of_event_receiver is hooked up to the post_save signal. |
441 | 439 | """
|
442 | 440 | # Arbitrary model that's not a DocEvent
|
443 |
| - person = PersonFactory() |
444 |
| - mock_notify_task.reset_mock() # clear any calls that resulted from the factories |
445 |
| - # be careful overriding SERVER_MODE - we do it here because the method |
446 |
| - # under test does not make this call when in "test" mode |
447 |
| - with override_settings(SERVER_MODE="not-test"): |
448 |
| - person.save() |
449 |
| - self.assertFalse(mock_notify_task.delay.called) |
450 |
| - |
| 441 | + person = PersonFactory.build() # builds but does not save... |
| 442 | + mock_notify_of_event.reset_mock() # clear any calls that resulted from the factories |
| 443 | + person.save() |
| 444 | + self.assertFalse(mock_notify_of_event.called) |
| 445 | + |
451 | 446 | # build a DocEvent that is not yet persisted
|
452 | 447 | doc = DocumentFactory()
|
453 |
| - d = DocEventFactory.build(by=person, doc=doc) |
454 |
| - # mock_notify_task.reset_mock() # clear any calls that resulted from the factories |
| 448 | + event = DocEventFactory.build(by=person, doc=doc) # builds but does not save... |
| 449 | + mock_notify_of_event.reset_mock() # clear any calls that resulted from the factories |
| 450 | + event.save() |
| 451 | + self.assertEqual(mock_notify_of_event.call_count, 1, "notify_task should be run on creation of DocEvent") |
| 452 | + self.assertEqual(mock_notify_of_event.call_args, mock.call(event)) |
| 453 | + |
| 454 | + # save the existing DocEvent and see that no notification is sent |
| 455 | + mock_notify_of_event.reset_mock() |
| 456 | + event.save() |
| 457 | + self.assertFalse(mock_notify_of_event.called, "notify_task should not be run save of on existing DocEvent") |
| 458 | + |
| 459 | + # Mock out the on_commit call so we can tell whether the task was actually queued |
| 460 | + @mock.patch("ietf.submit.views.transaction.on_commit", side_effect=lambda x: x()) |
| 461 | + @mock.patch("ietf.community.signals.notify_event_to_subscribers_task") |
| 462 | + def test_notify_of_event(self, mock_notify_task, mock_on_commit): |
| 463 | + """The community notification task should be called as intended""" |
| 464 | + person = PersonFactory() # builds but does not save... |
| 465 | + doc = DocumentFactory() |
| 466 | + event = DocEventFactory(by=person, doc=doc) |
455 | 467 | # be careful overriding SERVER_MODE - we do it here because the method
|
456 | 468 | # under test does not make this call when in "test" mode
|
457 | 469 | with override_settings(SERVER_MODE="not-test"):
|
458 |
| - d.save() |
459 |
| - self.assertEqual(mock_notify_task.delay.call_count, 1, "notify_task should be run on creation of DocEvent") |
460 |
| - self.assertEqual(mock_notify_task.delay.call_args, mock.call(event_id = d.pk)) |
461 |
| - |
462 |
| - mock_notify_task.reset_mock() |
463 |
| - with override_settings(SERVER_MODE="not-test"): |
464 |
| - d.save() |
465 |
| - self.assertFalse(mock_notify_task.delay.called, "notify_task should not be run save of on existing DocEvent") |
466 |
| - |
| 470 | + notify_of_event(event) |
| 471 | + self.assertTrue(mock_notify_task.delay.called, "notify_task should run for a DocEvent on a draft") |
467 | 472 | mock_notify_task.reset_mock()
|
468 |
| - d = DocEventFactory.build(by=person, doc=doc) |
469 |
| - d.skip_community_list_notification = True |
| 473 | + |
| 474 | + event.skip_community_list_notification = True |
470 | 475 | # be careful overriding SERVER_MODE - we do it here because the method
|
471 | 476 | # under test does not make this call when in "test" mode
|
472 | 477 | with override_settings(SERVER_MODE="not-test"):
|
473 |
| - d.save() |
| 478 | + notify_of_event(event) |
474 | 479 | self.assertFalse(mock_notify_task.delay.called, "notify_task should not run when skip_community_list_notification is set")
|
475 | 480 |
|
476 |
| - d = DocEventFactory.build(by=person, doc=DocumentFactory(type_id="rfc")) |
| 481 | + event = DocEventFactory.build(by=person, doc=DocumentFactory(type_id="rfc")) |
477 | 482 | # be careful overriding SERVER_MODE - we do it here because the method
|
478 | 483 | # under test does not make this call when in "test" mode
|
479 | 484 | with override_settings(SERVER_MODE="not-test"):
|
480 |
| - d.save() |
| 485 | + notify_of_event(event) |
481 | 486 | self.assertFalse(mock_notify_task.delay.called, "notify_task should not run on a document with type 'rfc'")
|
482 | 487 |
|
483 | 488 | @mock.patch("ietf.utils.mail.send_mail_text")
|
|
0 commit comments