diff --git a/corporate/lib/__init__.py b/corporate/lib/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/zilencer/lib/stripe.py b/corporate/lib/stripe.py similarity index 99% rename from zilencer/lib/stripe.py rename to corporate/lib/stripe.py index 7f28f405ff..426192bc9d 100644 --- a/zilencer/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -28,7 +28,7 @@ BILLING_LOG_PATH = os.path.join('/var/log/zulip' if not settings.DEVELOPMENT else settings.DEVELOPMENT_LOG_DIRECTORY, 'billing.log') -billing_logger = logging.getLogger('zilencer.stripe') +billing_logger = logging.getLogger('corporate.stripe') log_to_file(billing_logger, BILLING_LOG_PATH) log_to_file(logging.getLogger('stripe'), BILLING_LOG_PATH) diff --git a/corporate/views.py b/corporate/views.py index 4363523fcd..329eb0fed1 100644 --- a/corporate/views.py +++ b/corporate/views.py @@ -15,7 +15,7 @@ from zerver.lib.response import json_error, json_success from zerver.lib.validator import check_string from zerver.lib.timestamp import timestamp_to_datetime from zerver.models import UserProfile, Realm -from zilencer.lib.stripe import STRIPE_PUBLISHABLE_KEY, \ +from corporate.lib.stripe import STRIPE_PUBLISHABLE_KEY, \ stripe_get_customer, stripe_get_upcoming_invoice, get_seat_count, \ extract_current_subscription, process_initial_upgrade, sign_string, \ unsign_string, BillingError, process_downgrade, do_replace_payment_source diff --git a/docs/subsystems/billing.md b/docs/subsystems/billing.md index 3c8c5705bf..249bcc54d8 100644 --- a/docs/subsystems/billing.md +++ b/docs/subsystems/billing.md @@ -18,7 +18,7 @@ Nearly all the billing-relevant code lives in `zilencer/`. Notes: * Anything that talks directly to Stripe should go in - `zilencer/lib/stripe.py`. + `corporate/lib/stripe.py`. * We generally try to store billing-related data in Stripe, rather than in Zulip database tables. We'd rather pay the penalty of making extra stripe API requests than deal with keeping two sources of data in sync. diff --git a/tools/test-backend b/tools/test-backend index 091c65ad27..aa7806527e 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -29,7 +29,7 @@ target_fully_covered = {path for target in [ 'zerver/*.py', 'zerver/lib/*.py', # stripe.py handles billing - 'zilencer/lib/stripe.py', + 'corporate/lib/stripe.py', # Test files should have 100% coverage; test code that isn't run # is likely a bug in the test. 'zerver/tests/*.py', diff --git a/zilencer/management/commands/process_billing_updates.py b/zilencer/management/commands/process_billing_updates.py index 9d17acee86..5391bcf521 100644 --- a/zilencer/management/commands/process_billing_updates.py +++ b/zilencer/management/commands/process_billing_updates.py @@ -15,7 +15,7 @@ from django.core.management.base import BaseCommand from zerver.lib.context_managers import lockfile from zerver.lib.management import sleep_forever -from zilencer.lib.stripe import StripeConnectionError, \ +from corporate.lib.stripe import StripeConnectionError, \ run_billing_processor_one_step from zilencer.models import BillingProcessor diff --git a/zilencer/tests/test_stripe.py b/zilencer/tests/test_stripe.py index 88f585a670..d08062c814 100644 --- a/zilencer/tests/test_stripe.py +++ b/zilencer/tests/test_stripe.py @@ -16,7 +16,7 @@ from zerver.lib.actions import do_deactivate_user, do_create_user, \ from zerver.lib.test_classes import ZulipTestCase from zerver.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp from zerver.models import Realm, UserProfile, get_realm, RealmAuditLog -from zilencer.lib.stripe import catch_stripe_errors, \ +from corporate.lib.stripe import catch_stripe_errors, \ do_subscribe_customer_to_plan, attach_discount_to_realm, \ get_seat_count, extract_current_subscription, sign_string, unsign_string, \ get_next_billing_log_entry, run_billing_processor_one_step, \ @@ -95,7 +95,7 @@ class StripeTest(ZulipTestCase): match = re.search(r'name=\"salt\" value=\"(\w+)\"', response.content.decode("utf-8")) return match.group(1) if match else None - @mock.patch("zilencer.lib.stripe.billing_logger.error") + @mock.patch("corporate.lib.stripe.billing_logger.error") def test_catch_stripe_errors(self, mock_billing_logger_error: mock.Mock) -> None: @catch_stripe_errors def raise_invalid_request_error() -> None: @@ -210,7 +210,7 @@ class StripeTest(ZulipTestCase): new_seat_count = 123 # Change the seat count while the user is going through the upgrade flow response = self.client_get("/upgrade/") - with mock.patch('zilencer.lib.stripe.get_seat_count', return_value=new_seat_count): + with mock.patch('corporate.lib.stripe.get_seat_count', return_value=new_seat_count): self.client_post("/upgrade/", { 'stripeToken': self.token, 'signed_seat_count': self.get_signed_seat_count_from_response(response), @@ -545,7 +545,7 @@ class StripeTest(ZulipTestCase): # Test STRIPE_PLAN_QUANTITY_RESET new_seat_count = 123 # change the seat count while the user is going through the upgrade flow - with mock.patch('zilencer.lib.stripe.get_seat_count', return_value=new_seat_count): + with mock.patch('corporate.lib.stripe.get_seat_count', return_value=new_seat_count): self.client_post("/upgrade/", {'stripeToken': self.token, 'signed_seat_count': self.signed_seat_count, 'salt': self.salt, @@ -682,14 +682,14 @@ class BillingProcessorTest(ZulipTestCase): realm=second_realm, log_row=entry1, state=BillingProcessor.STARTED) Customer.objects.create(realm=get_realm('zulip'), stripe_customer_id='cust_1') Customer.objects.create(realm=second_realm, stripe_customer_id='cust_2') - with mock.patch('zilencer.lib.stripe.do_adjust_subscription_quantity'): + with mock.patch('corporate.lib.stripe.do_adjust_subscription_quantity'): # test return values self.assertTrue(run_billing_processor_one_step(processor)) self.assertTrue(run_billing_processor_one_step(realm_processor)) # test no processors get added or deleted self.assertEqual(2, BillingProcessor.objects.count()) - @mock.patch("zilencer.lib.stripe.billing_logger.error") + @mock.patch("corporate.lib.stripe.billing_logger.error") def test_run_billing_processor_with_card_error(self, mock_billing_logger_error: mock.Mock) -> None: second_realm = Realm.objects.create(string_id='second', name='second') entry1 = self.add_log_entry(realm=second_realm) @@ -699,7 +699,7 @@ class BillingProcessorTest(ZulipTestCase): Customer.objects.create(realm=second_realm, stripe_customer_id='cust_2') # card error on global processor should create a new realm processor - with mock.patch('zilencer.lib.stripe.do_adjust_subscription_quantity', + with mock.patch('corporate.lib.stripe.do_adjust_subscription_quantity', side_effect=stripe.error.CardError('message', 'param', 'code', json_body={})): self.assertTrue(run_billing_processor_one_step(processor)) self.assertEqual(2, BillingProcessor.objects.count()) @@ -713,7 +713,7 @@ class BillingProcessorTest(ZulipTestCase): realm_processor = BillingProcessor.objects.filter(realm=second_realm).first() realm_processor.state = BillingProcessor.STARTED realm_processor.save() - with mock.patch('zilencer.lib.stripe.do_adjust_subscription_quantity', + with mock.patch('corporate.lib.stripe.do_adjust_subscription_quantity', side_effect=stripe.error.CardError('message', 'param', 'code', json_body={})): self.assertTrue(run_billing_processor_one_step(realm_processor)) self.assertEqual(2, BillingProcessor.objects.count()) @@ -721,7 +721,7 @@ class BillingProcessorTest(ZulipTestCase): realm=second_realm, log_row=entry1, state=BillingProcessor.STALLED).exists()) mock_billing_logger_error.assert_called() - @mock.patch("zilencer.lib.stripe.billing_logger.error") + @mock.patch("corporate.lib.stripe.billing_logger.error") def test_run_billing_processor_with_uncaught_error(self, mock_billing_logger_error: mock.Mock) -> None: # This tests three different things: # * That run_billing_processor_one_step passes through exceptions that @@ -733,7 +733,7 @@ class BillingProcessorTest(ZulipTestCase): processor = BillingProcessor.objects.create( log_row=entry1, state=BillingProcessor.DONE) Customer.objects.create(realm=get_realm('zulip'), stripe_customer_id='cust_1') - with mock.patch('zilencer.lib.stripe.do_adjust_subscription_quantity', + with mock.patch('corporate.lib.stripe.do_adjust_subscription_quantity', side_effect=stripe.error.StripeError('message', 'param', 'code', json_body={})): with self.assertRaises(BillingError): run_billing_processor_one_step(processor)