support: Create RealmAuditLog when updating realm_subdomain.

This commit is contained in:
Siddharth Asthana 2020-12-07 00:34:33 +05:30 committed by Tim Abbott
parent 80c9243c6a
commit 47e478945b
5 changed files with 25 additions and 7 deletions

View File

@ -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}"

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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 <a href="http://new-name-2.testserver">http://new-name-2.testserver</a>.',
@ -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"