From 47e478945b82f051060e021a51f7c6c1d534b9c5 Mon Sep 17 00:00:00 2001 From: Siddharth Asthana Date: Mon, 7 Dec 2020 00:34:33 +0530 Subject: [PATCH] support: Create RealmAuditLog when updating realm_subdomain. --- analytics/views.py | 2 +- zerver/lib/actions.py | 11 ++++++++++- zerver/models.py | 1 + zerver/tests/test_realm.py | 10 +++++++++- zerver/tests/test_signup.py | 8 ++++---- 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/analytics/views.py b/analytics/views.py index d5bd20bd43..81d67c35cd 100644 --- a/analytics/views.py +++ b/analytics/views.py @@ -1298,7 +1298,7 @@ def support(request: HttpRequest) -> HttpResponse: except ValidationError as error: context["error_message"] = error.message else: - do_change_realm_subdomain(realm, new_subdomain) + do_change_realm_subdomain(realm, new_subdomain, acting_user=request.user) request.session[ "success_message" ] = f"Subdomain changed from {old_subdomain} to {new_subdomain}" diff --git a/zerver/lib/actions.py b/zerver/lib/actions.py index de061d182c..b6ba260a88 100644 --- a/zerver/lib/actions.py +++ b/zerver/lib/actions.py @@ -1035,11 +1035,20 @@ def do_reactivate_realm(realm: Realm) -> None: ) -def do_change_realm_subdomain(realm: Realm, new_subdomain: str) -> None: +def do_change_realm_subdomain( + realm: Realm, new_subdomain: str, *, acting_user: Optional[UserProfile] +) -> None: old_subdomain = realm.subdomain old_uri = realm.uri realm.string_id = new_subdomain realm.save(update_fields=["string_id"]) + RealmAuditLog.objects.create( + realm=realm, + event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED, + event_time=timezone_now(), + acting_user=acting_user, + extra_data={"old_subdomain": old_subdomain, "new_subdomain": new_subdomain}, + ) # If a realm if being renamed multiple times, we should find all the placeholder # realms and reset their deactivated_redirect field to point to the new realm uri diff --git a/zerver/models.py b/zerver/models.py index 764edd3b7a..cdf9a982c3 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -3192,6 +3192,7 @@ class AbstractRealmAuditLog(models.Model): REALM_BILLING_METHOD_CHANGED = 211 REALM_REACTIVATION_EMAIL_SENT = 212 REALM_SPONSORSHIP_PENDING_STATUS_CHANGED = 213 + REALM_SUBDOMAIN_CHANGED = 214 SUBSCRIPTION_CREATED = 301 SUBSCRIPTION_ACTIVATED = 302 diff --git a/zerver/tests/test_realm.py b/zerver/tests/test_realm.py index 841244a47e..8b27b21d7d 100644 --- a/zerver/tests/test_realm.py +++ b/zerver/tests/test_realm.py @@ -194,7 +194,8 @@ class RealmTest(ZulipTestCase): hamlet_id = self.example_user("hamlet").id user = get_user_profile_by_id(hamlet_id) realm = get_realm("zulip") - do_change_realm_subdomain(realm, "newzulip") + iago = self.example_user("iago") + do_change_realm_subdomain(realm, "newzulip", acting_user=iago) user = get_user_profile_by_id(hamlet_id) self.assertEqual(user.realm.string_id, "newzulip") @@ -202,6 +203,13 @@ class RealmTest(ZulipTestCase): self.assertTrue(placeholder_realm.deactivated) self.assertEqual(placeholder_realm.deactivated_redirect, user.realm.uri) + realm_audit_log = RealmAuditLog.objects.filter( + event_type=RealmAuditLog.REALM_SUBDOMAIN_CHANGED, acting_user=iago + ).last() + expected_extra_data = {"old_subdomain": "zulip", "new_subdomain": "newzulip"} + self.assertEqual(realm_audit_log.extra_data, str(expected_extra_data)) + self.assertEqual(realm_audit_log.acting_user, iago) + def test_do_deactivate_realm_clears_scheduled_jobs(self) -> None: user = self.example_user("hamlet") send_future_email( diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index 414e4a8a4b..1a50f2d505 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -201,7 +201,7 @@ class DeactivationNoticeTestCase(ZulipTestCase): def test_deactivation_notice_when_realm_subdomain_is_changed(self) -> None: realm = get_realm("zulip") - do_change_realm_subdomain(realm, "new-subdomain-name") + do_change_realm_subdomain(realm, "new-subdomain-name", acting_user=None) result = self.client_get("/accounts/deactivated/") self.assertIn( @@ -213,7 +213,7 @@ class DeactivationNoticeTestCase(ZulipTestCase): self, ) -> None: realm = get_realm("zulip") - do_change_realm_subdomain(realm, "new-name-1") + do_change_realm_subdomain(realm, "new-name-1", acting_user=None) result = self.client_get("/accounts/deactivated/") self.assertIn( @@ -222,7 +222,7 @@ class DeactivationNoticeTestCase(ZulipTestCase): ) realm = get_realm("new-name-1") - do_change_realm_subdomain(realm, "new-name-2") + do_change_realm_subdomain(realm, "new-name-2", acting_user=None) result = self.client_get("/accounts/deactivated/") self.assertIn( 'It has moved to http://new-name-2.testserver.', @@ -2881,7 +2881,7 @@ class RealmCreationTest(ZulipTestCase): @override_settings(OPEN_REALM_CREATION=True) def test_create_realm_using_old_subdomain_of_a_realm(self) -> None: realm = get_realm("zulip") - do_change_realm_subdomain(realm, "new-name") + do_change_realm_subdomain(realm, "new-name", acting_user=None) password = "test" email = "user1@test.com"