Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
billing_session: Push nocoverage comments down a layer.
Tim Abbottf281199d2f85
diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.pyindex dad6a8890a6..2ebadd49e6f 100644--- a/corporate/lib/stripe.py+++ b/corporate/lib/stripe.py@@ -2987,7 +2987,7 @@ class RealmBillingSession(BillingSession):pass-class RemoteRealmBillingSession(BillingSession): # nocoverage+class RemoteRealmBillingSession(BillingSession):def __init__(self,remote_realm: RemoteRealm,@@ -2996,7 +2996,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage) -> None:self.remote_realm = remote_realmself.remote_billing_user = remote_billing_user- if support_staff is not None:+ if support_staff is not None: # nocoverageassert support_staff.is_staffself.support_session = Trueelse:@@ -3004,12 +3004,12 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@override@property- def billing_entity_display_name(self) -> str:+ def billing_entity_display_name(self) -> str: # nocoveragereturn self.remote_realm.name@override@property- def billing_session_url(self) -> str:+ def billing_session_url(self) -> str: # nocoveragereturn f"{settings.EXTERNAL_URI_SCHEME}{settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN}.{settings.EXTERNAL_HOST}/realm/{self.remote_realm.uuid}"@override@@ -3018,7 +3018,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoveragereturn f"/realm/{self.remote_realm.uuid}"@override- def support_url(self) -> str:+ def support_url(self) -> str: # nocoveragereturn build_support_url("remote_servers_support", self.remote_realm.server.hostname)@override@@ -3039,7 +3039,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage)return remote_realm_counts.non_guest_user_count + remote_realm_counts.guest_user_count- def missing_data_error_page(self, request: HttpRequest) -> HttpResponse:+ def missing_data_error_page(self, request: HttpRequest) -> HttpResponse: # nocoverage# The RemoteRealm error page code path should not really be# possible, in that the self-hosted server will have uploaded# current audit log data as needed as part of logging the user@@ -3053,7 +3053,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage)@override- def get_audit_log_event(self, event_type: AuditLogEventType) -> int:+ def get_audit_log_event(self, event_type: AuditLogEventType) -> int: # nocoverageif event_type is AuditLogEventType.STRIPE_CUSTOMER_CREATED:return RemoteRealmAuditLog.STRIPE_CUSTOMER_CREATEDelif event_type is AuditLogEventType.STRIPE_CARD_CHANGED:@@ -3084,7 +3084,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverageevent_time: datetime,*,extra_data: Optional[Dict[str, Any]] = None,- ) -> None:+ ) -> None: # nocoverage# These audit logs don't use all the fields of `RemoteRealmAuditLog`:## * remote_id is None because this is not synced from a remote table.@@ -3104,7 +3104,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverageRemoteRealmAuditLog.objects.create(**log_data)@override- def get_data_for_stripe_customer(self) -> StripeCustomerData:+ def get_data_for_stripe_customer(self) -> StripeCustomerData: # nocoverage# Support requests do not set any stripe billing information.assert self.support_session is Falsemetadata: Dict[str, Any] = {}@@ -3120,7 +3120,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@overridedef update_data_for_checkout_session_and_payment_intent(self, metadata: Dict[str, Any]- ) -> Dict[str, Any]:+ ) -> Dict[str, Any]: # nocoverage# TODO: Figure out what this should do.updated_metadata = dict(**metadata,@@ -3130,7 +3130,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@overridedef update_or_create_customer(self, stripe_customer_id: Optional[str] = None, *, defaults: Optional[Dict[str, Any]] = None- ) -> Customer:+ ) -> Customer: # nocoverageif stripe_customer_id is not None:# Support requests do not set any stripe billing information.assert self.support_session is False@@ -3146,7 +3146,9 @@ class RemoteRealmBillingSession(BillingSession): # nocoveragereturn customer@override- def do_change_plan_type(self, *, tier: Optional[int], is_sponsored: bool = False) -> None:+ def do_change_plan_type(+ self, *, tier: Optional[int], is_sponsored: bool = False+ ) -> None: # nocoverageif is_sponsored:plan_type = RemoteRealm.PLAN_TYPE_COMMUNITYelif tier == CustomerPlan.TIER_SELF_HOSTED_BUSINESS:@@ -3171,7 +3173,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage)@override- def approve_sponsorship(self) -> str:+ def approve_sponsorship(self) -> str: # nocoverage# Sponsorship approval is only a support admin action.assert self.support_session@@ -3211,7 +3213,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoveragereturn self.remote_realm.plan_type == self.remote_realm.PLAN_TYPE_COMMUNITY@override- def get_metadata_for_stripe_update_card(self) -> Dict[str, Any]:+ def get_metadata_for_stripe_update_card(self) -> Dict[str, Any]: # nocoverageassert self.remote_billing_user is not Nonereturn {"type": "card_update", "remote_realm_user_id": str(self.remote_billing_user.id)}@@ -3228,7 +3230,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage)@override- def process_downgrade(self, plan: CustomerPlan) -> None:+ def process_downgrade(self, plan: CustomerPlan) -> None: # nocoveragewith transaction.atomic():old_plan_type = self.remote_realm.plan_typenew_plan_type = RemoteRealm.PLAN_TYPE_SELF_HOSTED@@ -3246,7 +3248,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@overridedef get_type_of_plan_tier_change(self, current_plan_tier: int, new_plan_tier: int- ) -> PlanTierChangeType:+ ) -> PlanTierChangeType: # nocoveragevalid_plan_tiers = [CustomerPlan.TIER_SELF_HOSTED_LEGACY,CustomerPlan.TIER_SELF_HOSTED_BUSINESS,@@ -3274,7 +3276,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoveragereturn PlanTierChangeType.DOWNGRADE@override- def has_billing_access(self) -> bool:+ def has_billing_access(self) -> bool: # nocoverage# We don't currently have a way to authenticate a remote# session that isn't authorized for billing access.return True@@ -3285,7 +3287,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage]@override- def on_paid_plan(self) -> bool:+ def on_paid_plan(self) -> bool: # nocoveragereturn self.remote_realm.plan_type in self.PAID_PLANS@override@@ -3306,7 +3308,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage@overridedef get_sponsorship_request_session_specific_context(self,- ) -> SponsorshipRequestSessionSpecificContext:+ ) -> SponsorshipRequestSessionSpecificContext: # nocoverageassert self.remote_billing_user is not Nonereturn SponsorshipRequestSessionSpecificContext(realm_user=None,@@ -3321,13 +3323,13 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage)@override- def save_org_type_from_request_sponsorship_session(self, org_type: int) -> None:+ def save_org_type_from_request_sponsorship_session(self, org_type: int) -> None: # nocoverageif self.remote_realm.org_type != org_type:self.remote_realm.org_type = org_typeself.remote_realm.save(update_fields=["org_type"])@override- def sync_license_ledger_if_needed(self) -> None:+ def sync_license_ledger_if_needed(self) -> None: # nocoveragelast_ledger = self.get_last_ledger_for_automanaged_plan_if_exists()if last_ledger is None:return@@ -3375,7 +3377,7 @@ class RemoteRealmBillingSession(BillingSession): # nocoverage}-class RemoteServerBillingSession(BillingSession): # nocoverage+class RemoteServerBillingSession(BillingSession):"""Billing session for pre-8.0 servers that do not yet supportcreating RemoteRealm objects."""@@ -3387,7 +3389,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage) -> None:self.remote_server = remote_serverself.remote_billing_user = remote_billing_user- if support_staff is not None:+ if support_staff is not None: # nocoverageassert support_staff.is_staffself.support_session = Trueelse:@@ -3395,12 +3397,12 @@ class RemoteServerBillingSession(BillingSession): # nocoverage@override@property- def billing_entity_display_name(self) -> str:+ def billing_entity_display_name(self) -> str: # nocoveragereturn self.remote_server.hostname@override@property- def billing_session_url(self) -> str:+ def billing_session_url(self) -> str: # nocoveragereturn f"{settings.EXTERNAL_URI_SCHEME}{settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN}.{settings.EXTERNAL_HOST}/server/{self.remote_server.uuid}"@override@@ -3409,7 +3411,7 @@ class RemoteServerBillingSession(BillingSession): # nocoveragereturn f"/server/{self.remote_server.uuid}"@override- def support_url(self) -> str:+ def support_url(self) -> str: # nocoveragereturn build_support_url("remote_servers_support", self.remote_server.hostname)@override@@ -3422,7 +3424,9 @@ class RemoteServerBillingSession(BillingSession): # nocoveragereturn self.remote_billing_user.email@override- def current_count_for_billed_licenses(self, event_time: datetime = timezone_now()) -> int:+ def current_count_for_billed_licenses(+ self, event_time: datetime = timezone_now()+ ) -> int: # nocoverageif has_stale_audit_log(self.remote_server):raise MissingDataErrorremote_server_counts = get_remote_server_guest_and_non_guest_count(@@ -3430,7 +3434,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage)return remote_server_counts.non_guest_user_count + remote_server_counts.guest_user_count- def missing_data_error_page(self, request: HttpRequest) -> HttpResponse:+ def missing_data_error_page(self, request: HttpRequest) -> HttpResponse: # nocoverage# The remedy for a RemoteZulipServer login is usually# upgrading to Zulip 8.0 or enabling SUBMIT_USAGE_STATISTICS.missing_data_context = {@@ -3442,7 +3446,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage)@override- def get_audit_log_event(self, event_type: AuditLogEventType) -> int:+ def get_audit_log_event(self, event_type: AuditLogEventType) -> int: # nocoverageif event_type is AuditLogEventType.STRIPE_CUSTOMER_CREATED:return RemoteZulipServerAuditLog.STRIPE_CUSTOMER_CREATEDelif event_type is AuditLogEventType.STRIPE_CARD_CHANGED:@@ -3473,7 +3477,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverageevent_time: datetime,*,extra_data: Optional[Dict[str, Any]] = None,- ) -> None:+ ) -> None: # nocoverageaudit_log_event = self.get_audit_log_event(event_type)log_data = {"server": self.remote_server,@@ -3487,7 +3491,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverageRemoteZulipServerAuditLog.objects.create(**log_data)@override- def get_data_for_stripe_customer(self) -> StripeCustomerData:+ def get_data_for_stripe_customer(self) -> StripeCustomerData: # nocoverage# Support requests do not set any stripe billing information.assert self.support_session is Falsemetadata: Dict[str, Any] = {}@@ -3503,7 +3507,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage@overridedef update_data_for_checkout_session_and_payment_intent(self, metadata: Dict[str, Any]- ) -> Dict[str, Any]:+ ) -> Dict[str, Any]: # nocoverageupdated_metadata = dict(server=self.remote_server,email=self.get_email(),@@ -3514,7 +3518,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage@overridedef update_or_create_customer(self, stripe_customer_id: Optional[str] = None, *, defaults: Optional[Dict[str, Any]] = None- ) -> Customer:+ ) -> Customer: # nocoverageif stripe_customer_id is not None:# Support requests do not set any stripe billing information.assert self.support_session is False@@ -3530,7 +3534,9 @@ class RemoteServerBillingSession(BillingSession): # nocoveragereturn customer@override- def do_change_plan_type(self, *, tier: Optional[int], is_sponsored: bool = False) -> None:+ def do_change_plan_type(+ self, *, tier: Optional[int], is_sponsored: bool = False+ ) -> None: # nocoverage# TODO: Create actual plan types.# This function needs to translate between the different@@ -3558,7 +3564,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage)@override- def approve_sponsorship(self) -> str:+ def approve_sponsorship(self) -> str: # nocoverage# Sponsorship approval is only a support admin action.assert self.support_session@@ -3588,7 +3594,7 @@ class RemoteServerBillingSession(BillingSession): # nocoveragereturn f"Sponsorship approved for {self.billing_entity_display_name}"@override- def process_downgrade(self, plan: CustomerPlan) -> None:+ def process_downgrade(self, plan: CustomerPlan) -> None: # nocoveragewith transaction.atomic():old_plan_type = self.remote_server.plan_typenew_plan_type = RemoteZulipServer.PLAN_TYPE_SELF_HOSTED@@ -3608,7 +3614,7 @@ class RemoteServerBillingSession(BillingSession): # nocoveragereturn self.remote_server.plan_type == self.remote_server.PLAN_TYPE_COMMUNITY@override- def get_metadata_for_stripe_update_card(self) -> Dict[str, Any]:+ def get_metadata_for_stripe_update_card(self) -> Dict[str, Any]: # nocoverageassert self.remote_billing_user is not Nonereturn {"type": "card_update", "remote_server_user_id": str(self.remote_billing_user.id)}@@ -3627,7 +3633,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage@overridedef get_type_of_plan_tier_change(self, current_plan_tier: int, new_plan_tier: int- ) -> PlanTierChangeType:+ ) -> PlanTierChangeType: # nocoveragevalid_plan_tiers = [CustomerPlan.TIER_SELF_HOSTED_LEGACY,CustomerPlan.TIER_SELF_HOSTED_BUSINESS,@@ -3672,11 +3678,11 @@ class RemoteServerBillingSession(BillingSession): # nocoverage]@override- def on_paid_plan(self) -> bool:+ def on_paid_plan(self) -> bool: # nocoveragereturn self.remote_server.plan_type in self.PAID_PLANS@override- def add_sponsorship_info_to_context(self, context: Dict[str, Any]) -> None:+ def add_sponsorship_info_to_context(self, context: Dict[str, Any]) -> None: # nocoveragecontext.update(realm_org_type=self.remote_server.org_type,sorted_org_types=sorted(@@ -3693,7 +3699,7 @@ class RemoteServerBillingSession(BillingSession): # nocoverage@overridedef get_sponsorship_request_session_specific_context(self,- ) -> SponsorshipRequestSessionSpecificContext:+ ) -> SponsorshipRequestSessionSpecificContext: # nocoverageassert self.remote_billing_user is not Nonereturn SponsorshipRequestSessionSpecificContext(realm_user=None,@@ -3708,13 +3714,13 @@ class RemoteServerBillingSession(BillingSession): # nocoverage)@override- def save_org_type_from_request_sponsorship_session(self, org_type: int) -> None:+ def save_org_type_from_request_sponsorship_session(self, org_type: int) -> None: # nocoverageif self.remote_server.org_type != org_type:self.remote_server.org_type = org_typeself.remote_server.save(update_fields=["org_type"])@override- def sync_license_ledger_if_needed(self) -> None:+ def sync_license_ledger_if_needed(self) -> None: # nocoveragelast_ledger = self.get_last_ledger_for_automanaged_plan_if_exists()if last_ledger is None:return