2018-05-21 18:56:45 +02:00
|
|
|
# These are tests for Zulip's database migrations. System documented at:
|
|
|
|
# https://zulip.readthedocs.io/en/latest/subsystems/schema-migrations.html
|
|
|
|
#
|
|
|
|
# You can also read
|
|
|
|
# https://www.caktusgroup.com/blog/2016/02/02/writing-unit-tests-django-migrations/
|
|
|
|
# to get a tutorial on the framework that inspired this feature.
|
2023-07-18 12:18:56 +02:00
|
|
|
from datetime import datetime, timezone
|
2023-08-30 21:38:59 +02:00
|
|
|
from unittest import skip
|
2022-03-02 02:19:34 +01:00
|
|
|
|
2023-07-18 12:18:56 +02:00
|
|
|
import orjson
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
2018-05-21 18:56:45 +02:00
|
|
|
|
2018-04-09 18:37:07 +02:00
|
|
|
from zerver.lib.test_classes import MigrationsTestCase
|
2019-02-13 10:22:16 +01:00
|
|
|
from zerver.lib.test_helpers import use_db_models
|
2018-04-09 18:37:07 +02:00
|
|
|
|
2019-02-13 10:22:16 +01:00
|
|
|
# Important note: These tests are very expensive, and details of
|
|
|
|
# Django's database transaction model mean it does not super work to
|
|
|
|
# have a lot of migrations tested in this file at once; so we usually
|
|
|
|
# delete the old migration tests when adding a new one, so this file
|
|
|
|
# always has a single migration test in it as an example.
|
|
|
|
#
|
|
|
|
# The error you get with multiple similar tests doing migrations on
|
|
|
|
# the same table is this (table name may vary):
|
|
|
|
#
|
|
|
|
# django.db.utils.OperationalError: cannot ALTER TABLE
|
|
|
|
# "zerver_subscription" because it has pending trigger events
|
2019-05-13 07:04:31 +02:00
|
|
|
#
|
|
|
|
# As a result, we generally mark these tests as skipped once they have
|
|
|
|
# been tested for a migration being merged.
|
2019-05-06 16:34:31 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2023-08-30 21:38:59 +02:00
|
|
|
@skip("Cannot be run because there is a non-atomic migration that has been merged after it")
|
2023-07-18 12:18:56 +02:00
|
|
|
class ScheduledEmailData(MigrationsTestCase):
|
|
|
|
migrate_from = "0467_rename_extradata_realmauditlog_extra_data_json"
|
|
|
|
migrate_to = "0468_rename_followup_day_email_templates"
|
2022-03-02 02:19:34 +01:00
|
|
|
|
|
|
|
@use_db_models
|
|
|
|
def setUpBeforeMigration(self, apps: StateApps) -> None:
|
2023-05-27 04:47:37 +02:00
|
|
|
iago = self.example_user("iago")
|
2023-07-18 12:18:56 +02:00
|
|
|
ScheduledEmail = apps.get_model("zerver", "ScheduledEmail")
|
|
|
|
send_date = datetime(2025, 1, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
|
|
|
|
|
|
templates = [
|
|
|
|
["zerver/emails/followup_day1", "a", True, 10],
|
|
|
|
["zerver/emails/followup_day2", "b", False, 20],
|
|
|
|
["zerver/emails/onboarding_zulip_guide", "c", True, 30],
|
2022-10-05 21:18:45 +02:00
|
|
|
]
|
2023-07-18 12:18:56 +02:00
|
|
|
|
|
|
|
for template in templates:
|
|
|
|
email_fields = {
|
|
|
|
"template_prefix": template[0],
|
|
|
|
"string_context": template[1],
|
|
|
|
"boolean_context": template[2],
|
|
|
|
"integer_context": template[3],
|
|
|
|
}
|
|
|
|
|
|
|
|
email = ScheduledEmail.objects.create(
|
|
|
|
type=1,
|
2023-05-27 04:47:37 +02:00
|
|
|
realm=iago.realm,
|
2023-07-18 12:18:56 +02:00
|
|
|
scheduled_timestamp=send_date,
|
|
|
|
data=orjson.dumps(email_fields).decode(),
|
|
|
|
)
|
|
|
|
email.users.add(iago.id)
|
|
|
|
|
|
|
|
def test_updated_email_templates(self) -> None:
|
|
|
|
ScheduledEmail = self.apps.get_model("zerver", "ScheduledEmail")
|
|
|
|
send_date = datetime(2025, 1, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
|
|
|
|
|
|
old_templates = [
|
|
|
|
"zerver/emails/followup_day1",
|
|
|
|
"zerver/emails/followup_day2",
|
2023-05-27 04:47:37 +02:00
|
|
|
]
|
2023-07-15 18:43:23 +02:00
|
|
|
|
2023-07-18 12:18:56 +02:00
|
|
|
current_templates = [
|
|
|
|
"zerver/emails/account_registered",
|
|
|
|
"zerver/emails/onboarding_zulip_guide",
|
|
|
|
"zerver/emails/onboarding_zulip_topics",
|
|
|
|
]
|
|
|
|
|
|
|
|
email_data = [
|
|
|
|
["zerver/emails/account_registered", "a", True, 10],
|
|
|
|
["zerver/emails/onboarding_zulip_topics", "b", False, 20],
|
|
|
|
["zerver/emails/onboarding_zulip_guide", "c", True, 30],
|
|
|
|
]
|
|
|
|
|
|
|
|
scheduled_emails = ScheduledEmail.objects.all()
|
|
|
|
self.assert_length(scheduled_emails, 3)
|
|
|
|
|
|
|
|
checked_emails = []
|
|
|
|
for email in scheduled_emails:
|
|
|
|
self.assertEqual(email.type, 1)
|
|
|
|
self.assertEqual(email.scheduled_timestamp, send_date)
|
|
|
|
|
|
|
|
updated_data = orjson.loads(email.data)
|
|
|
|
template_prefix = updated_data["template_prefix"]
|
|
|
|
self.assertFalse(template_prefix in old_templates)
|
|
|
|
for data in email_data:
|
|
|
|
if template_prefix == data[0]:
|
|
|
|
self.assertEqual(updated_data["string_context"], data[1])
|
|
|
|
self.assertEqual(updated_data["boolean_context"], data[2])
|
|
|
|
self.assertEqual(updated_data["integer_context"], data[3])
|
|
|
|
checked_emails.append(template_prefix)
|
|
|
|
|
|
|
|
self.assertEqual(current_templates, sorted(checked_emails))
|