test_digests.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import uuid
  2. from unittest import mock
  3. from django.core import mail
  4. from django.core.mail.message import EmailMultiAlternatives
  5. import sentry
  6. from sentry.digests.backends.redis import RedisBackend
  7. from sentry.digests.notifications import event_to_record
  8. from sentry.models.projectownership import ProjectOwnership
  9. from sentry.models.rule import Rule
  10. from sentry.tasks.digests import deliver_digest
  11. from sentry.testutils.cases import TestCase
  12. from sentry.testutils.helpers.datetime import before_now, iso_format
  13. from sentry.testutils.skips import requires_snuba
  14. pytestmark = [requires_snuba]
  15. class DeliverDigestTest(TestCase):
  16. def run_test(self, key: str) -> None:
  17. """Simple integration test to make sure that digests are firing as expected."""
  18. with mock.patch.object(sentry, "digests") as digests:
  19. backend = RedisBackend()
  20. digests.backend.digest = backend.digest
  21. rule = Rule.objects.create(project=self.project, label="Test Rule", data={})
  22. ProjectOwnership.objects.create(project_id=self.project.id, fallthrough=True)
  23. event = self.store_event(
  24. data={"timestamp": iso_format(before_now(days=1)), "fingerprint": ["group-1"]},
  25. project_id=self.project.id,
  26. )
  27. event_2 = self.store_event(
  28. data={"timestamp": iso_format(before_now(days=1)), "fingerprint": ["group-2"]},
  29. project_id=self.project.id,
  30. )
  31. notification_uuid = str(uuid.uuid4())
  32. backend.add(
  33. key,
  34. event_to_record(event, [rule], notification_uuid),
  35. increment_delay=0,
  36. maximum_delay=0,
  37. )
  38. backend.add(
  39. key,
  40. event_to_record(event_2, [rule], notification_uuid),
  41. increment_delay=0,
  42. maximum_delay=0,
  43. )
  44. with self.tasks():
  45. deliver_digest(key)
  46. def test_old_key(self):
  47. self.run_test(f"mail:p:{self.project.id}")
  48. assert len(mail.outbox) == 0
  49. def test_new_key(self):
  50. self.run_test(f"mail:p:{self.project.id}:IssueOwners:")
  51. assert len(mail.outbox) == 0
  52. def test_fallthrough_choice_key(self):
  53. self.run_test(f"mail:p:{self.project.id}:IssueOwners::AllMembers")
  54. assert "2 new alerts since" in mail.outbox[0].subject
  55. message = mail.outbox[0]
  56. assert isinstance(message, EmailMultiAlternatives)
  57. assert isinstance(message.alternatives[0][0], str)
  58. assert "notification_uuid" in message.alternatives[0][0]
  59. def test_member_key(self):
  60. self.run_test(f"mail:p:{self.project.id}:Member:{self.user.id}")
  61. assert "2 new alerts since" in mail.outbox[0].subject
  62. message = mail.outbox[0]
  63. assert isinstance(message, EmailMultiAlternatives)
  64. assert isinstance(message.alternatives[0][0], str)
  65. assert "notification_uuid" in message.alternatives[0][0]
  66. def test_no_records(self):
  67. # This shouldn't error if no records are present
  68. deliver_digest(f"mail:p:{self.project.id}:IssueOwners:")