-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Send email notifications on failure
- Loading branch information
1 parent
60ddec2
commit b8b9acb
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import datetime as dt | ||
from typing import Tuple | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
from asgiref.sync import sync_to_async | ||
from freezegun import freeze_time | ||
|
||
from posthog.api.authentication import password_reset_token_generator | ||
from posthog.api.email_verification import email_verification_token_generator | ||
from posthog.batch_exports.models import BatchExport, BatchExportDestination, BatchExportRun | ||
from posthog.models import Organization, Team, User | ||
from posthog.models.instance_setting import set_instance_setting | ||
from posthog.models.organization import OrganizationInvite, OrganizationMembership | ||
from posthog.models.plugin import Plugin, PluginConfig | ||
from posthog.tasks.email import ( | ||
send_async_migration_complete_email, | ||
send_async_migration_errored_email, | ||
send_batch_export_run_failure, | ||
send_canary_email, | ||
send_email_verification, | ||
send_fatal_plugin_error, | ||
|
@@ -144,6 +149,62 @@ def test_send_fatal_plugin_error_with_settings(self, MockEmailMessage: MagicMock | |
# should be sent to both | ||
assert len(mocked_email_messages[1].to) == 2 | ||
|
||
@pytest.mark.asyncio | ||
async def test_send_batch_export_run_failure(self, MockEmailMessage: MagicMock) -> None: | ||
mocked_email_messages = mock_email_messages(MockEmailMessage) | ||
_, user = await sync_to_async(create_org_team_and_user)("2022-01-02 00:00:00", "[email protected]") | ||
batch_export_destination = await sync_to_async(BatchExportDestination.objects.create)( | ||
type=BatchExportDestination.Destination.S3, config={"bucket_name": "my_production_s3_bucket"} | ||
) | ||
batch_export = await sync_to_async(BatchExport.objects.create)( | ||
team=user.team, name="A batch export", destination=batch_export_destination | ||
) | ||
now = dt.datetime.now() | ||
batch_export_run = await sync_to_async(BatchExportRun.objects.create)( | ||
batch_export=batch_export, | ||
status=BatchExportRun.Status.FAILED, | ||
data_interval_start=now - dt.timedelta(hours=1), | ||
data_interval_end=now, | ||
) | ||
|
||
await send_batch_export_run_failure(batch_export_run.id) | ||
|
||
assert len(mocked_email_messages) == 1 | ||
assert mocked_email_messages[0].send.call_count == 1 | ||
assert mocked_email_messages[0].html_body | ||
|
||
@pytest.mark.asyncio | ||
async def test_send_batch_export_run_failure_with_settings(self, MockEmailMessage: MagicMock) -> None: | ||
mocked_email_messages = mock_email_messages(MockEmailMessage) | ||
batch_export_destination = await sync_to_async(BatchExportDestination.objects.create)( | ||
type=BatchExportDestination.Destination.S3, config={"bucket_name": "my_production_s3_bucket"} | ||
) | ||
batch_export = await sync_to_async(BatchExport.objects.create)( | ||
team=self.user.team, name="A batch export", destination=batch_export_destination | ||
) | ||
now = dt.datetime.now() | ||
batch_export_run = await sync_to_async(BatchExportRun.objects.create)( | ||
batch_export=batch_export, | ||
status=BatchExportRun.Status.FAILED, | ||
data_interval_start=now - dt.timedelta(hours=1), | ||
data_interval_end=now, | ||
) | ||
|
||
await sync_to_async(self._create_user)("[email protected]") | ||
self.user.partial_notification_settings = {"batch_export_run_failure": False} | ||
await sync_to_async(self.user.save)() | ||
|
||
await send_batch_export_run_failure(batch_export_run.id) | ||
# Should only be sent to user2 | ||
assert mocked_email_messages[0].to == [{"recipient": "[email protected]", "raw_email": "[email protected]"}] | ||
|
||
self.user.partial_notification_settings = {"batch_export_run_failure": True} | ||
await sync_to_async(self.user.save)() | ||
|
||
await send_batch_export_run_failure(batch_export_run.id) | ||
# should be sent to both | ||
assert len(mocked_email_messages[1].to) == 2 | ||
|
||
def test_send_canary_email(self, MockEmailMessage: MagicMock) -> None: | ||
mocked_email_messages = mock_email_messages(MockEmailMessage) | ||
send_canary_email("[email protected]") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters