diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.create.1.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.create.1.json index a6ba159bb3..d217ce9388 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.create.1.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.1.json index aefeb64664..f928190118 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.1.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.1.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.2.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.2.json index aefeb64664..f928190118 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.2.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Customer.retrieve.2.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.1.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.1.json index 4b5af0aaa7..dd2ffb5019 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.1.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.1.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.2.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.2.json index 4b5af0aaa7..dd2ffb5019 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.2.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Invoice.upcoming.2.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Subscription.create.1.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Subscription.create.1.json index 6461595870..31d6122f6d 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Subscription.create.1.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Subscription.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/billing_page_permissions:Token.create.1.json b/corporate/tests/stripe_fixtures/billing_page_permissions:Token.create.1.json index 4a0c68702b..76d62e1987 100644 Binary files a/corporate/tests/stripe_fixtures/billing_page_permissions:Token.create.1.json and b/corporate/tests/stripe_fixtures/billing_page_permissions:Token.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.create.1.json b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.create.1.json index ff1df030ab..20fbd9cbe1 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.create.1.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.1.json index d6b823021c..c6be34d2b2 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.1.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.1.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.2.json b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.2.json index d6b823021c..c6be34d2b2 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.2.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Customer.retrieve.2.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Invoice.upcoming.1.json b/corporate/tests/stripe_fixtures/initial_upgrade:Invoice.upcoming.1.json index 3aac71b73c..f109cc0050 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Invoice.upcoming.1.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Invoice.upcoming.1.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Subscription.create.1.json b/corporate/tests/stripe_fixtures/initial_upgrade:Subscription.create.1.json index 6d3319c72c..09329294c4 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Subscription.create.1.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Subscription.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/initial_upgrade:Token.create.1.json b/corporate/tests/stripe_fixtures/initial_upgrade:Token.create.1.json index 5684625671..7ad4eaeee3 100644 Binary files a/corporate/tests/stripe_fixtures/initial_upgrade:Token.create.1.json and b/corporate/tests/stripe_fixtures/initial_upgrade:Token.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.create.1.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.create.1.json index 38a8a59318..f1fdbd7528 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.1.json index 6344d23e96..85f0b358b6 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.1.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.2.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.2.json index 6344d23e96..85f0b358b6 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.2.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.2.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.3.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.3.json index b86de47eb0..1936e0a77b 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.3.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.retrieve.3.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.save.1.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.save.1.json index 528e101e27..bd4b418de1 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.save.1.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Customer.save.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.1.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.1.json index 0213d64607..01d597a849 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.2.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.2.json index 6674c1b3a0..4ae825cb71 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.2.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Subscription.create.2.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.1.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.1.json index 3b3e0060d6..0136fa6d1f 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.2.json b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.2.json index 8ce7bf6e2c..bdec363ab6 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.2.json and b/corporate/tests/stripe_fixtures/upgrade_where_subscription_save_fails_at_first:Token.create.2.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.create.1.json b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.create.1.json index a6f9f917d4..493bf9fb4a 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.retrieve.1.json b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.retrieve.1.json index 72dcc92089..9a2562d2cd 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.retrieve.1.json and b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Customer.retrieve.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Subscription.create.1.json b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Subscription.create.1.json index b70a10187b..974a594ff6 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Subscription.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Subscription.create.1.json differ diff --git a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Token.create.1.json b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Token.create.1.json index 0ff96f1c56..7f10d68a2c 100644 Binary files a/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Token.create.1.json and b/corporate/tests/stripe_fixtures/upgrade_with_outdated_seat_count:Token.create.1.json differ diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index e593b211b0..d267c288c7 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -34,6 +34,7 @@ CallableT = TypeVar('CallableT', bound=Callable[..., Any]) GENERATE_STRIPE_FIXTURES = False +STRIPE_FIXTURES_DIR = "corporate/tests/stripe_fixtures" fixture_data_file = open(os.path.join(os.path.dirname(__file__), 'stripe_fixtures.json'), 'r') fixture_data = ujson.load(fixture_data_file) @@ -95,8 +96,15 @@ def stripe_fixture_path(decorated_function_name: str, mocked_function_name: str, # use test_* for the python test files if decorated_function_name[:5] == 'test_': decorated_function_name = decorated_function_name[5:] - return "corporate/tests/stripe_fixtures/{}:{}.{}.json".format( - decorated_function_name, mocked_function_name[7:], call_count) + return "{}/{}:{}.{}.json".format( + STRIPE_FIXTURES_DIR, decorated_function_name, mocked_function_name[7:], call_count) + +def fixture_files_for_function(decorated_function: CallableT) -> List[str]: # nocoverage + decorated_function_name = decorated_function.__name__ + if decorated_function_name[:5] == 'test_': + decorated_function_name = decorated_function_name[5:] + return sorted(['{}/{}'.format(STRIPE_FIXTURES_DIR, f) for f in os.listdir(STRIPE_FIXTURES_DIR) + if f.startswith(decorated_function_name)]) def generate_and_save_stripe_fixture(decorated_function_name: str, mocked_function_name: str, mocked_function: CallableT) -> Callable[[Any, Any], Any]: # nocoverage @@ -133,6 +141,37 @@ def read_stripe_fixture(decorated_function_name: str, return stripe.util.convert_to_stripe_object(fixture) return _read_stripe_fixture +def normalize_fixture_data(decorated_function: CallableT) -> None: # nocoverage + # stripe ids are all of the form cus_D7OT2jf5YAtZQ2 + id_lengths = [ + ('cus', 14), ('sub', 14), ('si', 14), ('sli', 14), ('req', 14), ('tok', 24), ('card', 24)] + # We'll replace cus_D7OT2jf5YAtZQ2 with something like cus_NORMALIZED0001 + pattern_translations = { + "%s_[A-Za-z0-9]{%d}" % (prefix, length): "%s_NORMALIZED%%0%dd" % (prefix, length - 10) + for prefix, length in id_lengths + } + # We'll replace "invoice_prefix": "A35BC4Q" with something like "invoice_prefix": "NORMA01" + pattern_translations.update({ + '"invoice_prefix": "[A-Za-z0-9]{7}"': '"invoice_prefix": "NORMA%02d"', + '"fingerprint": "[A-Za-z0-9]{16}"': '"fingerprint": "NORMALIZED%06d"', + '"number": "[A-Za-z0-9]{7}-[A-Za-z0-9]{4}"': '"number": "NORMALI-%04d"', + }) + + normalized_values = {pattern: {} + for pattern in pattern_translations.keys()} # type: Dict[str, Dict[str, str]] + for fixture_file in fixture_files_for_function(decorated_function): + with open(fixture_file, "r") as f: + file_content = f.read() + for pattern, translation in pattern_translations.items(): + for match in re.findall(pattern, file_content): + if match not in normalized_values[pattern]: + normalized_values[pattern][match] = translation % (len(normalized_values[pattern]) + 1,) + file_content = file_content.replace(match, normalized_values[pattern][match]) + # Overwrite all IP addresses + file_content = re.sub(r'"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"', '"0.0.0.0"', file_content) + with open(fixture_file, "w") as f: + f.write(file_content) + def mock_stripe(*mocked_function_names: str, generate: Optional[bool]=None) -> Callable[[CallableT], Callable[..., Any]]: def _mock_stripe(decorated_function: CallableT) -> Callable[..., Any]: @@ -151,7 +190,10 @@ def mock_stripe(*mocked_function_names: str, @wraps(decorated_function) def wrapped(*args: Any, **kwargs: Any) -> Any: - return decorated_function(*args, **kwargs) + val = decorated_function(*args, **kwargs) + if generate_fixture: # nocoverage + normalize_fixture_data(decorated_function) + return val return wrapped return _mock_stripe