tests: Create new test mode for generating stripe fixtures.

This commit is contained in:
Vishnu KS 2020-03-13 20:15:47 +05:30 committed by Tim Abbott
parent ca74cd6e37
commit a48845c8fb
5 changed files with 25 additions and 6 deletions

View File

@ -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,6 +78,8 @@ 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:
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:
@ -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:

View File

@ -17,8 +17,7 @@ Stripe makes pretty regular updates to their API. The process for upgrading
our code is:
* Go to <https://dashboard.stripe.com/developers> 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.

View File

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

View File

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

View File

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