From a48845c8fbba04b70acfab695b5a419b5a52af72 Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Fri, 13 Mar 2020 20:15:47 +0530 Subject: [PATCH] tests: Create new test mode for generating stripe fixtures. --- corporate/tests/test_stripe.py | 11 +++++++---- docs/subsystems/billing.md | 3 +-- tools/test-backend | 12 ++++++++++++ zproject/settings.py | 2 ++ zproject/test_settings.py | 3 +++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index c8bbcd7274..79309ca297 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -9,11 +9,13 @@ import sys from typing import Any, Callable, Dict, List, Optional, TypeVar, Tuple, cast import ujson import json +import responses from django.core import signing from django.urls.resolvers import get_resolver from django.http import HttpResponse from django.utils.timezone import utc as timezone_utc +from django.conf import settings import stripe @@ -35,7 +37,6 @@ from corporate.models import Customer, CustomerPlan, LicenseLedger CallableT = TypeVar('CallableT', bound=Callable[..., Any]) -GENERATE_STRIPE_FIXTURES = False STRIPE_FIXTURES_DIR = "corporate/tests/stripe_fixtures" # TODO: check that this creates a token similar to what is created by our @@ -77,8 +78,10 @@ def generate_and_save_stripe_fixture(decorated_function_name: str, mocked_functi mock = operator.attrgetter(mocked_function_name)(sys.modules[__name__]) fixture_path = stripe_fixture_path(decorated_function_name, mocked_function_name, mock.call_count) try: - # Talk to Stripe - stripe_object = mocked_function(*args, **kwargs) + with responses.RequestsMock() as request_mock: + request_mock.add_passthru("https://api.stripe.com") + # Talk to Stripe + stripe_object = mocked_function(*args, **kwargs) except stripe.error.StripeError as e: with open(fixture_path, 'w') as f: error_dict = e.__dict__ @@ -183,7 +186,7 @@ def mock_stripe(tested_timestamp_fields: List[str]=[], def _mock_stripe(decorated_function: CallableT) -> CallableT: generate_fixture = generate if generate_fixture is None: - generate_fixture = GENERATE_STRIPE_FIXTURES + generate_fixture = settings.GENERATE_STRIPE_FIXTURES for mocked_function_name in MOCKED_STRIPE_FUNCTION_NAMES: mocked_function = operator.attrgetter(mocked_function_name)(sys.modules[__name__]) if generate_fixture: diff --git a/docs/subsystems/billing.md b/docs/subsystems/billing.md index a2e61ced0a..1d9c743e10 100644 --- a/docs/subsystems/billing.md +++ b/docs/subsystems/billing.md @@ -17,8 +17,7 @@ Stripe makes pretty regular updates to their API. The process for upgrading our code is: * Go to in your Stripe account. * Upgrade the API version. -* Set `GENERATE_STRIPE_FIXTURES = True` in `test_stripe.py`. -* Run `tools/test-backend corporate/tests/test_stripe.py` +* Run `tools/test-backend --generate-stripe-fixtures` * Fix any failing tests, and manually look through `git diff` to understand the changes. * If there are no material changes, commit the diff, and open a PR. diff --git a/tools/test-backend b/tools/test-backend index f4210b1566..e9bf749d87 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -259,6 +259,10 @@ def main() -> None: action="store_true", default=False, help=("Include webhook tests. By default, they are skipped for performance.")) + parser.add_argument('--generate-stripe-fixtures', dest="generate_stripe_fixtures", + action="store_true", + default=False, + help=("Generate Stripe test fixtures by making requests to Stripe test network")) parser.add_argument('args', nargs='*') options = parser.parse_args() @@ -344,6 +348,14 @@ def main() -> None: if full_suite and include_webhooks: suites.append("zerver.webhooks") + if options.generate_stripe_fixtures: + if full_suite: + suites = [ + "corporate.tests.test_stripe", + ] + full_suite = False + os.environ["GENERATE_STRIPE_FIXTURES"] = "1" + assert_provisioning_status_ok(options.force) if options.coverage: diff --git a/zproject/settings.py b/zproject/settings.py index 787c365a26..90b6ae11d6 100644 --- a/zproject/settings.py +++ b/zproject/settings.py @@ -68,6 +68,8 @@ TUTORIAL_ENABLED = True CASPER_TESTS = False # This is overridden in test_settings.py for the test suites RUNNING_OPENAPI_CURL_TEST = False +# This is overridden in test_settings.py for the test suites +GENERATE_STRIPE_FIXTURES = False # Google Compute Engine has an /etc/boto.cfg that is "nicely # configured" to work with GCE's storage service. However, their diff --git a/zproject/test_settings.py b/zproject/test_settings.py index 45fdb58004..8d8d086c3c 100644 --- a/zproject/test_settings.py +++ b/zproject/test_settings.py @@ -54,6 +54,9 @@ if "CASPER_TESTS" in os.environ: if "RUNNING_OPENAPI_CURL_TEST" in os.environ: RUNNING_OPENAPI_CURL_TEST = True +if "GENERATE_STRIPE_FIXTURES" in os.environ: + GENERATE_STRIPE_FIXTURES = True + # Decrease the get_updates timeout to 1 second. # This allows CasperJS to proceed quickly to the next test step. POLL_TIMEOUT = 1000