|
| 1 | +from datetime import date |
1 | 2 | from unittest.mock import Mock |
2 | 3 |
|
3 | 4 | from model_bakery import baker |
@@ -428,3 +429,60 @@ def test_create_log_entry(self): |
428 | 429 | self.assertEqual(str(self.sponsorship), log_entry.object_repr) |
429 | 430 | self.assertEqual(log_entry.action_flag, CHANGE) |
430 | 431 | self.assertEqual(log_entry.change_message, "Notification 'Foo' was sent to contacts: administrative") |
| 432 | + |
| 433 | + |
| 434 | +class AssetCloseToDueDateNotificationToSponsorsTestCase(TestCase): |
| 435 | + def setUp(self): |
| 436 | + self.notification = notifications.AssetCloseToDueDateNotificationToSponsors() |
| 437 | + self.user = baker.make(settings.AUTH_USER_MODEL, email="foo@foo.com") |
| 438 | + self.verified_email = baker.make(EmailAddress, verified=True) |
| 439 | + self.unverified_email = baker.make(EmailAddress, verified=False) |
| 440 | + self.sponsor_contacts = [ |
| 441 | + baker.make( |
| 442 | + "sponsors.SponsorContact", |
| 443 | + email="foo@example.com", |
| 444 | + primary=True, |
| 445 | + sponsor__name="foo", |
| 446 | + ), |
| 447 | + baker.make("sponsors.SponsorContact", email=self.verified_email.email), |
| 448 | + baker.make("sponsors.SponsorContact", email=self.unverified_email.email), |
| 449 | + ] |
| 450 | + self.sponsor = baker.make("sponsors.Sponsor", contacts=self.sponsor_contacts) |
| 451 | + self.sponsorship = baker.make( |
| 452 | + "sponsors.Sponsorship", sponsor=self.sponsor, submited_by=self.user |
| 453 | + ) |
| 454 | + self.subject_template = "sponsors/email/sponsor_expiring_assets_subject.txt" |
| 455 | + self.content_template = "sponsors/email/sponsor_expiring_assets.txt" |
| 456 | + |
| 457 | + def test_send_email_using_correct_templates(self): |
| 458 | + context = {"sponsorship": self.sponsorship} |
| 459 | + expected_subject = render_to_string(self.subject_template, context).strip() |
| 460 | + expected_content = render_to_string(self.content_template, context).strip() |
| 461 | + |
| 462 | + self.notification.notify(sponsorship=self.sponsorship) |
| 463 | + self.assertTrue(mail.outbox) |
| 464 | + |
| 465 | + email = mail.outbox[0] |
| 466 | + self.assertEqual(expected_subject, email.subject) |
| 467 | + self.assertEqual(expected_content, email.body) |
| 468 | + self.assertEqual(settings.SPONSORSHIP_NOTIFICATION_FROM_EMAIL, email.from_email) |
| 469 | + self.assertCountEqual([self.user.email, self.verified_email.email], email.to) |
| 470 | + |
| 471 | + def test_send_email_to_correct_recipients(self): |
| 472 | + context = {"user": self.user, "sponsorship": self.sponsorship} |
| 473 | + expected_contacts = ["foo@foo.com", self.verified_email.email] |
| 474 | + self.assertCountEqual( |
| 475 | + expected_contacts, self.notification.get_recipient_list(context) |
| 476 | + ) |
| 477 | + |
| 478 | + def test_list_required_assets_in_email_context(self): |
| 479 | + cfg = baker.make(RequiredTextAssetConfiguration, internal_name='input') |
| 480 | + benefit = baker.make(SponsorBenefit, sponsorship=self.sponsorship) |
| 481 | + asset = cfg.create_benefit_feature(benefit) |
| 482 | + base_context = {"sponsorship": self.sponsorship, "due_date": date.today()} |
| 483 | + context = self.notification.get_email_context(**base_context) |
| 484 | + self.assertEqual(3, len(context)) |
| 485 | + self.assertEqual(self.sponsorship, context["sponsorship"]) |
| 486 | + self.assertEqual(1, len(context["required_assets"])) |
| 487 | + self.assertEqual(date.today(), context["due_date"]) |
| 488 | + self.assertIn(asset, context["required_assets"]) |
0 commit comments