audit-log: Move remote server event types to AuditLogEventType enum.

Event types moved: REMOTE_SERVER_DEACTIVATED, REMOTE_SERVER_REACTIVATED
REMOTE_SERVER_PLAN_TYPE_CHANGED, REMOTE_SERVER_DISCOUNT_CHANGED
REMOTE_SERVER_SPONSORSHIP_APPROVED, REMOTE_SERVER_BILLING_MODALITY_CHANGED
REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED, REMOTE_SERVER_CREATED
This commit is contained in:
Lauryn Menard 2024-09-06 17:29:04 +02:00 committed by Tim Abbott
parent d431a5aad6
commit aad93e149e
10 changed files with 42 additions and 37 deletions

View File

@ -4210,19 +4210,19 @@ class RemoteRealmBillingSession(BillingSession):
elif event_type is BillingSessionEventType.CUSTOMER_PLAN_CREATED:
return AuditLogEventType.CUSTOMER_PLAN_CREATED
elif event_type is BillingSessionEventType.DISCOUNT_CHANGED:
return RemoteRealmAuditLog.REMOTE_SERVER_DISCOUNT_CHANGED
return AuditLogEventType.REMOTE_SERVER_DISCOUNT_CHANGED
elif event_type is BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED:
return AuditLogEventType.CUSTOMER_PROPERTY_CHANGED # nocoverage
elif event_type is BillingSessionEventType.SPONSORSHIP_APPROVED:
return RemoteRealmAuditLog.REMOTE_SERVER_SPONSORSHIP_APPROVED
return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_APPROVED
elif event_type is BillingSessionEventType.SPONSORSHIP_PENDING_STATUS_CHANGED:
return RemoteRealmAuditLog.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED
return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED
elif event_type is BillingSessionEventType.BILLING_MODALITY_CHANGED:
return RemoteRealmAuditLog.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage
return AuditLogEventType.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage
elif event_type is BillingSessionEventType.CUSTOMER_PLAN_PROPERTY_CHANGED:
return AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED
elif event_type is BillingSessionEventType.BILLING_ENTITY_PLAN_TYPE_CHANGED:
return RemoteRealmAuditLog.REMOTE_SERVER_PLAN_TYPE_CHANGED
return AuditLogEventType.REMOTE_SERVER_PLAN_TYPE_CHANGED
elif (
event_type is BillingSessionEventType.CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN
): # nocoverage
@ -4653,19 +4653,19 @@ class RemoteServerBillingSession(BillingSession):
elif event_type is BillingSessionEventType.CUSTOMER_PLAN_CREATED:
return AuditLogEventType.CUSTOMER_PLAN_CREATED
elif event_type is BillingSessionEventType.DISCOUNT_CHANGED:
return RemoteZulipServerAuditLog.REMOTE_SERVER_DISCOUNT_CHANGED # nocoverage
return AuditLogEventType.REMOTE_SERVER_DISCOUNT_CHANGED # nocoverage
elif event_type is BillingSessionEventType.CUSTOMER_PROPERTY_CHANGED:
return AuditLogEventType.CUSTOMER_PROPERTY_CHANGED # nocoverage
elif event_type is BillingSessionEventType.SPONSORSHIP_APPROVED:
return RemoteZulipServerAuditLog.REMOTE_SERVER_SPONSORSHIP_APPROVED
return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_APPROVED
elif event_type is BillingSessionEventType.SPONSORSHIP_PENDING_STATUS_CHANGED:
return RemoteZulipServerAuditLog.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED
return AuditLogEventType.REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED
elif event_type is BillingSessionEventType.BILLING_MODALITY_CHANGED:
return RemoteZulipServerAuditLog.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage
return AuditLogEventType.REMOTE_SERVER_BILLING_MODALITY_CHANGED # nocoverage
elif event_type is BillingSessionEventType.CUSTOMER_PLAN_PROPERTY_CHANGED:
return AuditLogEventType.CUSTOMER_PLAN_PROPERTY_CHANGED # nocoverage
elif event_type is BillingSessionEventType.BILLING_ENTITY_PLAN_TYPE_CHANGED:
return RemoteZulipServerAuditLog.REMOTE_SERVER_PLAN_TYPE_CHANGED
return AuditLogEventType.REMOTE_SERVER_PLAN_TYPE_CHANGED
elif (
event_type is BillingSessionEventType.CUSTOMER_SWITCHED_FROM_MONTHLY_TO_ANNUAL_PLAN
): # nocoverage
@ -5168,7 +5168,7 @@ def do_reactivate_remote_server(remote_server: RemoteZulipServer) -> None:
remote_server.deactivated = False
remote_server.save(update_fields=["deactivated"])
RemoteZulipServerAuditLog.objects.create(
event_type=RealmAuditLog.REMOTE_SERVER_REACTIVATED,
event_type=AuditLogEventType.REMOTE_SERVER_REACTIVATED,
server=remote_server,
event_time=timezone_now(),
)
@ -5218,7 +5218,7 @@ def do_deactivate_remote_server(
remote_server.deactivated = True
remote_server.save(update_fields=["deactivated"])
RemoteZulipServerAuditLog.objects.create(
event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED,
event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED,
server=remote_server,
event_time=timezone_now(),
)

View File

@ -29,6 +29,7 @@ from corporate.models import (
get_current_plan_by_customer,
)
from zerver.models import Realm
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import get_org_type_display_name, get_realm
from zilencer.lib.remote_counts import MissingDataError
from zilencer.models import (
@ -410,7 +411,7 @@ def get_data_for_remote_support_view(billing_session: BillingSession) -> RemoteS
user_data = get_remote_server_guest_and_non_guest_count(billing_session.remote_server.id)
stale_audit_log_data = has_stale_audit_log(billing_session.remote_server)
date_created = RemoteZulipServerAuditLog.objects.get(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server__id=billing_session.remote_server.id,
).event_time
mobile_data = get_mobile_push_data(billing_session.remote_server)

View File

@ -170,7 +170,7 @@ class ActivityTest(ZulipTestCase):
contact_email="email@example.com",
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=server,
event_time=server.last_updated,
)
@ -303,7 +303,7 @@ class ActivityTest(ZulipTestCase):
hostname=hostname, contact_email=f"admin@{hostname}", uuid=uuid.uuid4()
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=remote_server,
event_time=remote_server.last_updated,
)

View File

@ -5486,7 +5486,7 @@ class BillingHelpersTest(ZulipTestCase):
remote_server = RemoteZulipServer.objects.get(uuid=server_uuid)
remote_realm_audit_log = RemoteZulipServerAuditLog.objects.filter(
event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED
event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED
).last()
assert remote_realm_audit_log is not None
self.assertTrue(remote_server.deactivated)
@ -5506,7 +5506,9 @@ class BillingHelpersTest(ZulipTestCase):
remote_server.refresh_from_db()
self.assertFalse(remote_server.deactivated)
remote_realm_audit_log = RemoteZulipServerAuditLog.objects.latest("id")
self.assertEqual(remote_realm_audit_log.event_type, RealmAuditLog.REMOTE_SERVER_REACTIVATED)
self.assertEqual(
remote_realm_audit_log.event_type, AuditLogEventType.REMOTE_SERVER_REACTIVATED
)
self.assertEqual(remote_realm_audit_log.server, remote_server)
with self.assertLogs("corporate.stripe", "WARN") as warning_log:

View File

@ -116,7 +116,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):
hostname=hostname, contact_email=f"admin@{hostname}", uuid=uuid.uuid4()
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=remote_server,
event_time=remote_server.last_updated,
)
@ -688,7 +688,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):
remote_server_no_upgrade.refresh_from_db()
self.assertTrue(remote_server_no_upgrade.deactivated)
audit_log = RemoteZulipServerAuditLog.objects.filter(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_DEACTIVATED
event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED
).last()
assert audit_log is not None
self.assertEqual(audit_log.server, remote_server_no_upgrade)
@ -709,7 +709,7 @@ class TestRemoteServerSupportEndpoint(ZulipTestCase):
remote_server.refresh_from_db()
self.assertFalse(remote_server.deactivated)
audit_log = RemoteZulipServerAuditLog.objects.filter(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_REACTIVATED
event_type=AuditLogEventType.REMOTE_SERVER_REACTIVATED
).last()
assert audit_log is not None
self.assertEqual(audit_log.server, remote_server)

View File

@ -107,6 +107,19 @@ class AuditLogEventType(IntEnum):
USER_GROUP_DESCRIPTION_CHANGED = 721
USER_GROUP_GROUP_BASED_SETTING_CHANGED = 722
# The following values are only for remote server/realm logs.
# Values should be exactly 10000 greater than the corresponding
# value used for the same purpose in realm audit logs (e.g.,
# REALM_DEACTIVATED = 201, and REMOTE_SERVER_DEACTIVATED = 10201).
REMOTE_SERVER_DEACTIVATED = 10201
REMOTE_SERVER_REACTIVATED = 10202
REMOTE_SERVER_PLAN_TYPE_CHANGED = 10204
REMOTE_SERVER_DISCOUNT_CHANGED = 10209
REMOTE_SERVER_SPONSORSHIP_APPROVED = 10210
REMOTE_SERVER_BILLING_MODALITY_CHANGED = 10211
REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED = 10213
REMOTE_SERVER_CREATED = 10215
class AbstractRealmAuditLog(models.Model):
"""Defines fields common to RealmAuditLog and RemoteRealmAuditLog."""
@ -128,19 +141,6 @@ class AbstractRealmAuditLog(models.Model):
# Event types
# The following values are only for RemoteZulipServerAuditLog
# Values should be exactly 10000 greater than the corresponding
# value used for the same purpose in RealmAuditLog (e.g.
# REALM_DEACTIVATED = 201, and REMOTE_SERVER_DEACTIVATED = 10201).
REMOTE_SERVER_DEACTIVATED = 10201
REMOTE_SERVER_REACTIVATED = 10202
REMOTE_SERVER_PLAN_TYPE_CHANGED = 10204
REMOTE_SERVER_DISCOUNT_CHANGED = 10209
REMOTE_SERVER_SPONSORSHIP_APPROVED = 10210
REMOTE_SERVER_BILLING_MODALITY_CHANGED = 10211
REMOTE_SERVER_SPONSORSHIP_PENDING_STATUS_CHANGED = 10213
REMOTE_SERVER_CREATED = 10215
# This value is for RemoteRealmAuditLog entries tracking changes to the
# RemoteRealm model resulting from modified realm information sent to us
# via send_server_data_to_push_bouncer.

View File

@ -5164,7 +5164,7 @@ class PushBouncerSignupTest(ZulipTestCase):
server = RemoteZulipServer.objects.get(uuid=zulip_org_id)
remote_realm_audit_log = RemoteZulipServerAuditLog.objects.filter(
event_type=RealmAuditLog.REMOTE_SERVER_DEACTIVATED
event_type=AuditLogEventType.REMOTE_SERVER_DEACTIVATED
).last()
assert remote_realm_audit_log is not None
self.assertTrue(server.deactivated)

View File

@ -28,6 +28,7 @@ from zerver.lib.management import ZulipBaseCommand
from zerver.lib.remote_server import get_realms_info_for_push_bouncer
from zerver.lib.streams import create_stream_if_needed
from zerver.models import Realm, UserProfile
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import get_realm
from zilencer.models import (
RemoteRealm,
@ -449,7 +450,7 @@ def populate_remote_server(customer_profile: CustomerProfile) -> dict[str, str]:
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=remote_server,
event_time=remote_server.last_updated,
)

View File

@ -406,7 +406,7 @@ class Command(ZulipBaseCommand):
contact_email="remotezulipserver@zulip.com",
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=server,
event_time=server.last_updated,
)

View File

@ -77,6 +77,7 @@ from zerver.lib.typed_endpoint import (
)
from zerver.lib.typed_endpoint_validators import check_string_fixed_length
from zerver.lib.types import RemoteRealmDictValue
from zerver.models.realm_audit_logs import AuditLogEventType
from zerver.models.realms import DisposableEmailError
from zerver.views.push_notifications import validate_token
from zilencer.auth import InvalidZulipServerKeyError
@ -226,7 +227,7 @@ def register_remote_server(
last_request_datetime=timezone_now(),
)
RemoteZulipServerAuditLog.objects.create(
event_type=RemoteZulipServerAuditLog.REMOTE_SERVER_CREATED,
event_type=AuditLogEventType.REMOTE_SERVER_CREATED,
server=remote_server,
event_time=remote_server.last_updated,
)