mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-02 21:13:36 +00:00 
			
		
		
		
	demo-orgs: Delete expired demo orgs in archive_messages cron job.
Adds delete_expired_demo_organizations to the archive_messages management command, which is run as a cron job. Adds "demo_expired" as a `RealmDeactivationReasonType` to be used for this specific case of calling do_deactivate_realm. The function loops through non-deactivated realms that have a demo organization scheduled deletion datetime set that is less than the current datetime.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							0fa5b158df
						
					
				
				
					commit
					c797c481b3
				
			@@ -26,6 +26,7 @@ from zerver.actions.message_send import (
 | 
			
		||||
)
 | 
			
		||||
from zerver.actions.realm_settings import (
 | 
			
		||||
    clean_deactivated_realm_data,
 | 
			
		||||
    delete_expired_demo_organizations,
 | 
			
		||||
    do_add_deactivated_redirect,
 | 
			
		||||
    do_change_realm_max_invites,
 | 
			
		||||
    do_change_realm_org_type,
 | 
			
		||||
@@ -1225,6 +1226,64 @@ class RealmTest(ZulipTestCase):
 | 
			
		||||
            clean_deactivated_realm_data()
 | 
			
		||||
            mock_scrub_realm.assert_called_once_with(zephyr, acting_user=None)
 | 
			
		||||
 | 
			
		||||
    def test_delete_expired_demo_organizations(self) -> None:
 | 
			
		||||
        zulip = get_realm("zulip")
 | 
			
		||||
        assert not zulip.deactivated
 | 
			
		||||
        assert zulip.demo_organization_scheduled_deletion_date is None
 | 
			
		||||
 | 
			
		||||
        with mock.patch(
 | 
			
		||||
            "zerver.actions.realm_settings.do_deactivate_realm"
 | 
			
		||||
        ) as mock_deactivate_realm:
 | 
			
		||||
            delete_expired_demo_organizations()
 | 
			
		||||
            mock_deactivate_realm.assert_not_called()
 | 
			
		||||
 | 
			
		||||
        # Add scheduled demo organization deletion date
 | 
			
		||||
        zulip.demo_organization_scheduled_deletion_date = timezone_now() + timedelta(days=4)
 | 
			
		||||
        zulip.save()
 | 
			
		||||
 | 
			
		||||
        # Before deletion date
 | 
			
		||||
        with mock.patch(
 | 
			
		||||
            "zerver.actions.realm_settings.do_deactivate_realm"
 | 
			
		||||
        ) as mock_deactivate_realm:
 | 
			
		||||
            delete_expired_demo_organizations()
 | 
			
		||||
            mock_deactivate_realm.assert_not_called()
 | 
			
		||||
 | 
			
		||||
        # After deletion date, when owner email is set.
 | 
			
		||||
        with (
 | 
			
		||||
            time_machine.travel(timezone_now() + timedelta(days=5), tick=False),
 | 
			
		||||
            mock.patch(
 | 
			
		||||
                "zerver.actions.realm_settings.do_deactivate_realm"
 | 
			
		||||
            ) as mock_deactivate_realm,
 | 
			
		||||
        ):
 | 
			
		||||
            delete_expired_demo_organizations()
 | 
			
		||||
            mock_deactivate_realm.assert_called_once_with(
 | 
			
		||||
                realm=zulip,
 | 
			
		||||
                acting_user=None,
 | 
			
		||||
                deactivation_reason="demo_expired",
 | 
			
		||||
                deletion_delay_days=0,
 | 
			
		||||
                email_owners=True,
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        # After deletion date, when owner email is not set.
 | 
			
		||||
        desdemona = self.example_user("desdemona")
 | 
			
		||||
        desdemona.delivery_email = ""
 | 
			
		||||
        desdemona.save()
 | 
			
		||||
 | 
			
		||||
        with (
 | 
			
		||||
            time_machine.travel(timezone_now() + timedelta(days=5), tick=False),
 | 
			
		||||
            mock.patch(
 | 
			
		||||
                "zerver.actions.realm_settings.do_deactivate_realm"
 | 
			
		||||
            ) as mock_deactivate_realm,
 | 
			
		||||
        ):
 | 
			
		||||
            delete_expired_demo_organizations()
 | 
			
		||||
            mock_deactivate_realm.assert_called_once_with(
 | 
			
		||||
                realm=zulip,
 | 
			
		||||
                acting_user=None,
 | 
			
		||||
                deactivation_reason="demo_expired",
 | 
			
		||||
                deletion_delay_days=0,
 | 
			
		||||
                email_owners=False,
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    def test_initial_plan_type(self) -> None:
 | 
			
		||||
        with self.settings(BILLING_ENABLED=True):
 | 
			
		||||
            self.assertEqual(do_create_realm("hosted", "hosted").plan_type, Realm.PLAN_TYPE_LIMITED)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user