2018-12-28 07:20:30 +01:00
|
|
|
from datetime import datetime, timedelta
|
2018-12-12 19:41:03 +01:00
|
|
|
from decimal import Decimal
|
2018-12-28 07:20:30 +01:00
|
|
|
from functools import partial, wraps
|
2018-10-18 05:50:52 +02:00
|
|
|
from mock import Mock, patch
|
2018-10-17 08:23:13 +02:00
|
|
|
import operator
|
2018-03-31 04:13:44 +02:00
|
|
|
import os
|
2018-08-08 16:35:33 +02:00
|
|
|
import re
|
2018-10-17 08:23:13 +02:00
|
|
|
import sys
|
2018-11-12 17:18:36 +01:00
|
|
|
from typing import Any, Callable, Dict, List, Optional, TypeVar, Tuple, cast
|
2018-10-17 08:23:13 +02:00
|
|
|
import ujson
|
2018-10-29 07:36:50 +01:00
|
|
|
import json
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-07-13 17:34:39 +02:00
|
|
|
from django.core import signing
|
2018-10-17 08:23:13 +02:00
|
|
|
from django.core.management import call_command
|
2018-11-01 11:26:29 +01:00
|
|
|
from django.core.urlresolvers import get_resolver
|
2018-08-08 16:35:33 +02:00
|
|
|
from django.http import HttpResponse
|
2018-07-03 21:49:55 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2018-07-13 17:34:39 +02:00
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
import stripe
|
|
|
|
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.lib.actions import do_deactivate_user, do_create_user, \
|
2018-12-24 09:59:52 +01:00
|
|
|
do_activate_user, do_reactivate_user, do_create_realm
|
2018-03-31 04:13:44 +02:00
|
|
|
from zerver.lib.test_classes import ZulipTestCase
|
2018-07-03 21:49:55 +02:00
|
|
|
from zerver.lib.timestamp import timestamp_to_datetime, datetime_to_timestamp
|
2018-06-28 00:48:51 +02:00
|
|
|
from zerver.models import Realm, UserProfile, get_realm, RealmAuditLog
|
2018-12-15 09:33:25 +01:00
|
|
|
from corporate.lib.stripe import catch_stripe_errors, attach_discount_to_realm, \
|
|
|
|
get_seat_count, sign_string, unsign_string, \
|
2018-09-08 00:49:54 +02:00
|
|
|
BillingError, StripeCardError, StripeConnectionError, stripe_get_customer, \
|
2019-01-29 06:34:31 +01:00
|
|
|
DEFAULT_INVOICE_DAYS_UNTIL_DUE, MIN_INVOICED_LICENSES, do_create_stripe_customer, \
|
2018-12-15 09:33:25 +01:00
|
|
|
add_months, next_month, next_renewal_date, renewal_amount, \
|
2018-12-28 07:20:30 +01:00
|
|
|
compute_plan_parameters, update_or_create_stripe_customer, \
|
2019-01-26 02:36:37 +01:00
|
|
|
process_initial_upgrade, add_plan_renewal_to_license_ledger_if_needed, \
|
2019-01-28 22:57:29 +01:00
|
|
|
update_license_ledger_if_needed, update_license_ledger_for_automanaged_plan, \
|
|
|
|
invoice_plan, invoice_plans_as_needed
|
2018-12-28 07:20:30 +01:00
|
|
|
from corporate.models import Customer, CustomerPlan, LicenseLedger
|
2018-09-08 00:49:54 +02:00
|
|
|
from corporate.views import payment_method_string
|
2018-11-01 11:26:29 +01:00
|
|
|
import corporate.urls
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-10-17 08:23:13 +02:00
|
|
|
CallableT = TypeVar('CallableT', bound=Callable[..., Any])
|
|
|
|
|
|
|
|
GENERATE_STRIPE_FIXTURES = False
|
2018-11-09 08:15:44 +01:00
|
|
|
STRIPE_FIXTURES_DIR = "corporate/tests/stripe_fixtures"
|
2018-07-26 16:10:07 +02:00
|
|
|
|
2018-10-17 08:23:13 +02:00
|
|
|
# TODO: check that this creates a token similar to what is created by our
|
|
|
|
# actual Stripe Checkout flows
|
2018-10-18 20:04:45 +02:00
|
|
|
def stripe_create_token(card_number: str="4242424242424242") -> stripe.Token:
|
2018-10-17 08:23:13 +02:00
|
|
|
return stripe.Token.create(
|
|
|
|
card={
|
2018-10-18 20:04:45 +02:00
|
|
|
"number": card_number,
|
2018-10-17 08:23:13 +02:00
|
|
|
"exp_month": 3,
|
|
|
|
"exp_year": 2033,
|
|
|
|
"cvc": "333",
|
|
|
|
"name": "Ada Starr",
|
|
|
|
"address_line1": "Under the sea,",
|
|
|
|
"address_city": "Pacific",
|
|
|
|
"address_zip": "33333",
|
|
|
|
"address_country": "United States",
|
|
|
|
})
|
|
|
|
|
|
|
|
def stripe_fixture_path(decorated_function_name: str, mocked_function_name: str, call_count: int) -> str:
|
|
|
|
# Make the eventual filename a bit shorter, and also we conventionally
|
|
|
|
# use test_* for the python test files
|
|
|
|
if decorated_function_name[:5] == 'test_':
|
|
|
|
decorated_function_name = decorated_function_name[5:]
|
2018-11-09 08:15:44 +01:00
|
|
|
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)
|
2018-11-26 23:58:10 +01:00
|
|
|
if f.startswith(decorated_function_name + ':')])
|
2018-10-17 08:23:13 +02:00
|
|
|
|
|
|
|
def generate_and_save_stripe_fixture(decorated_function_name: str, mocked_function_name: str,
|
|
|
|
mocked_function: CallableT) -> Callable[[Any, Any], Any]: # nocoverage
|
|
|
|
def _generate_and_save_stripe_fixture(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
# Note that mock is not the same as mocked_function, even though their
|
|
|
|
# definitions look the same
|
|
|
|
mock = operator.attrgetter(mocked_function_name)(sys.modules[__name__])
|
|
|
|
fixture_path = stripe_fixture_path(decorated_function_name, mocked_function_name, mock.call_count)
|
2018-10-29 07:36:50 +01:00
|
|
|
try:
|
|
|
|
# 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__
|
|
|
|
error_dict["headers"] = dict(error_dict["headers"])
|
|
|
|
f.write(json.dumps(error_dict, indent=2, separators=(',', ': '), sort_keys=True) + "\n")
|
|
|
|
raise e
|
2018-10-17 08:23:13 +02:00
|
|
|
with open(fixture_path, 'w') as f:
|
2018-11-16 16:49:40 +01:00
|
|
|
if stripe_object is not None:
|
|
|
|
f.write(str(stripe_object) + "\n")
|
|
|
|
else:
|
|
|
|
f.write("{}\n")
|
2018-10-17 08:23:13 +02:00
|
|
|
return stripe_object
|
|
|
|
return _generate_and_save_stripe_fixture
|
|
|
|
|
|
|
|
def read_stripe_fixture(decorated_function_name: str,
|
|
|
|
mocked_function_name: str) -> Callable[[Any, Any], Any]:
|
|
|
|
def _read_stripe_fixture(*args: Any, **kwargs: Any) -> Any:
|
|
|
|
mock = operator.attrgetter(mocked_function_name)(sys.modules[__name__])
|
|
|
|
fixture_path = stripe_fixture_path(decorated_function_name, mocked_function_name, mock.call_count)
|
2018-10-29 07:36:50 +01:00
|
|
|
fixture = ujson.load(open(fixture_path, 'r'))
|
|
|
|
# Check for StripeError fixtures
|
|
|
|
if "json_body" in fixture:
|
|
|
|
requestor = stripe.api_requestor.APIRequestor()
|
|
|
|
# This function will raise the relevant StripeError according to the fixture
|
|
|
|
requestor.interpret_response(fixture["http_body"], fixture["http_status"], fixture["headers"])
|
|
|
|
return stripe.util.convert_to_stripe_object(fixture)
|
2018-10-17 08:23:13 +02:00
|
|
|
return _read_stripe_fixture
|
|
|
|
|
2018-11-26 23:07:36 +01:00
|
|
|
def delete_fixture_data(decorated_function: CallableT) -> None: # nocoverage
|
|
|
|
for fixture_file in fixture_files_for_function(decorated_function):
|
|
|
|
os.remove(fixture_file)
|
|
|
|
|
2018-12-04 00:29:23 +01:00
|
|
|
def normalize_fixture_data(decorated_function: CallableT,
|
|
|
|
tested_timestamp_fields: List[str]=[]) -> None: # nocoverage
|
2018-11-09 08:15:44 +01:00
|
|
|
# stripe ids are all of the form cus_D7OT2jf5YAtZQ2
|
|
|
|
id_lengths = [
|
2018-12-01 16:57:52 +01:00
|
|
|
('cus', 14), ('sub', 14), ('si', 14), ('sli', 14), ('req', 14), ('tok', 24), ('card', 24),
|
|
|
|
('txn', 24), ('ch', 24), ('in', 24), ('ii', 24), ('test', 12), ('src_client_secret', 24),
|
2018-12-23 09:10:57 +01:00
|
|
|
('src', 24), ('invst', 26)]
|
2018-11-09 08:15:44 +01:00
|
|
|
# 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({
|
2018-12-03 22:06:08 +01:00
|
|
|
'"invoice_prefix": "([A-Za-z0-9]{7})"': 'NORMA%02d',
|
|
|
|
'"fingerprint": "([A-Za-z0-9]{16})"': 'NORMALIZED%06d',
|
|
|
|
'"number": "([A-Za-z0-9]{7}-[A-Za-z0-9]{4})"': 'NORMALI-%04d',
|
|
|
|
'"address": "([A-Za-z0-9]{9}-test_[A-Za-z0-9]{12})"': '000000000-test_NORMALIZED%02d',
|
2018-12-03 22:16:09 +01:00
|
|
|
# Don't use (..) notation, since the matched strings may be small integers that will also match
|
|
|
|
# elsewhere in the file
|
|
|
|
'"realm_id": "[0-9]+"': '"realm_id": "%d"',
|
2018-11-09 08:15:44 +01:00
|
|
|
})
|
2018-12-04 00:29:23 +01:00
|
|
|
# Normalizing across all timestamps still causes a lot of variance run to run, which is
|
|
|
|
# why we're doing something a bit more complicated
|
|
|
|
for i, timestamp_field in enumerate(tested_timestamp_fields):
|
|
|
|
# Don't use (..) notation, since the matched timestamp can easily appear in other fields
|
|
|
|
pattern_translations[
|
|
|
|
'"%s": 1[5-9][0-9]{8}(?![0-9-])' % (timestamp_field,)
|
|
|
|
] = '"%s": 1%02d%%07d' % (timestamp_field, i+1)
|
2018-11-09 08:15:44 +01:00
|
|
|
|
|
|
|
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])
|
2018-12-03 22:06:08 +01:00
|
|
|
file_content = re.sub(r'(?<="risk_score": )(\d+)', '00', file_content)
|
|
|
|
file_content = re.sub(r'(?<="times_redeemed": )(\d+)', '00', file_content)
|
2018-12-01 16:57:52 +01:00
|
|
|
# Dates
|
2018-12-03 22:16:09 +01:00
|
|
|
file_content = re.sub(r'(?<="Date": )"(.* GMT)"', '"NORMALIZED DATETIME"', file_content)
|
2018-12-01 16:57:52 +01:00
|
|
|
file_content = re.sub(r'[0-3]\d [A-Z][a-z]{2} 20[1-2]\d', 'NORMALIZED DATE', file_content)
|
|
|
|
# IP addresses
|
2018-11-09 08:15:44 +01:00
|
|
|
file_content = re.sub(r'"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"', '"0.0.0.0"', file_content)
|
2018-12-04 00:29:23 +01:00
|
|
|
# All timestamps not in tested_timestamp_fields
|
|
|
|
file_content = re.sub(r': (1[5-9][0-9]{8})(?![0-9-])', ': 1000000000', file_content)
|
2018-12-01 16:57:52 +01:00
|
|
|
|
2018-11-09 08:15:44 +01:00
|
|
|
with open(fixture_file, "w") as f:
|
|
|
|
f.write(file_content)
|
|
|
|
|
2018-12-03 19:23:13 +01:00
|
|
|
MOCKED_STRIPE_FUNCTION_NAMES = ["stripe.{}".format(name) for name in [
|
2018-12-15 09:33:25 +01:00
|
|
|
"Charge.create", "Charge.list",
|
2018-12-03 19:23:13 +01:00
|
|
|
"Coupon.create",
|
|
|
|
"Customer.create", "Customer.retrieve", "Customer.save",
|
2018-12-15 09:33:25 +01:00
|
|
|
"Invoice.create", "Invoice.finalize_invoice", "Invoice.list", "Invoice.upcoming",
|
|
|
|
"InvoiceItem.create", "InvoiceItem.list",
|
2018-12-03 19:23:13 +01:00
|
|
|
"Plan.create",
|
|
|
|
"Product.create",
|
|
|
|
"Subscription.create", "Subscription.delete", "Subscription.retrieve", "Subscription.save",
|
|
|
|
"Token.create",
|
|
|
|
]]
|
|
|
|
|
2018-12-04 18:22:03 +01:00
|
|
|
def mock_stripe(tested_timestamp_fields: List[str]=[],
|
2018-11-12 17:18:36 +01:00
|
|
|
generate: Optional[bool]=None) -> Callable[[CallableT], CallableT]:
|
|
|
|
def _mock_stripe(decorated_function: CallableT) -> CallableT:
|
2018-11-07 11:28:36 +01:00
|
|
|
generate_fixture = generate
|
2018-10-30 11:33:24 +01:00
|
|
|
if generate_fixture is None:
|
|
|
|
generate_fixture = GENERATE_STRIPE_FIXTURES
|
2018-12-03 19:23:13 +01:00
|
|
|
for mocked_function_name in MOCKED_STRIPE_FUNCTION_NAMES:
|
2018-11-07 11:28:36 +01:00
|
|
|
mocked_function = operator.attrgetter(mocked_function_name)(sys.modules[__name__])
|
|
|
|
if generate_fixture:
|
|
|
|
side_effect = generate_and_save_stripe_fixture(
|
|
|
|
decorated_function.__name__, mocked_function_name, mocked_function) # nocoverage
|
|
|
|
else:
|
|
|
|
side_effect = read_stripe_fixture(decorated_function.__name__, mocked_function_name)
|
|
|
|
decorated_function = patch(mocked_function_name, side_effect=side_effect)(decorated_function)
|
2018-10-17 08:23:13 +02:00
|
|
|
|
|
|
|
@wraps(decorated_function)
|
|
|
|
def wrapped(*args: Any, **kwargs: Any) -> Any:
|
2018-11-09 08:15:44 +01:00
|
|
|
if generate_fixture: # nocoverage
|
2018-11-26 23:07:36 +01:00
|
|
|
delete_fixture_data(decorated_function)
|
|
|
|
val = decorated_function(*args, **kwargs)
|
2018-12-04 00:29:23 +01:00
|
|
|
normalize_fixture_data(decorated_function, tested_timestamp_fields)
|
2018-11-26 23:07:36 +01:00
|
|
|
return val
|
|
|
|
else:
|
|
|
|
return decorated_function(*args, **kwargs)
|
2018-11-12 17:18:36 +01:00
|
|
|
return cast(CallableT, wrapped)
|
2018-10-17 08:23:13 +02:00
|
|
|
return _mock_stripe
|
|
|
|
|
2018-08-09 21:38:22 +02:00
|
|
|
# A Kandra is a fictional character that can become anything. Used as a
|
|
|
|
# wildcard when testing for equality.
|
2018-12-15 09:33:25 +01:00
|
|
|
class Kandra(object): # nocoverage: TODO
|
2018-08-09 21:38:22 +02:00
|
|
|
def __eq__(self, other: Any) -> bool:
|
|
|
|
return True
|
|
|
|
|
2019-01-27 21:16:02 +01:00
|
|
|
class StripeTestCase(ZulipTestCase):
|
2018-12-02 19:05:34 +01:00
|
|
|
def setUp(self, *mocks: Mock) -> None:
|
2018-12-15 09:33:25 +01:00
|
|
|
# TODO
|
2018-11-29 03:24:19 +01:00
|
|
|
# Unfortunately this test suite is likely not robust to users being
|
2018-11-29 08:08:27 +01:00
|
|
|
# added in populate_db. A quick hack for now to ensure get_seat_count is 8
|
|
|
|
# for these tests (8, since that's what it was when the tests were written).
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
seat_count = get_seat_count(get_realm('zulip'))
|
|
|
|
assert(seat_count >= 8)
|
|
|
|
if seat_count > 8: # nocoverage
|
|
|
|
for user in UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False) \
|
|
|
|
.exclude(email__in=[
|
|
|
|
self.example_email('hamlet'),
|
2018-11-30 06:33:04 +01:00
|
|
|
self.example_email('iago')])[6:]:
|
2018-11-29 08:08:27 +01:00
|
|
|
user.is_active = False
|
|
|
|
user.save(update_fields=['is_active'])
|
2018-11-30 06:33:04 +01:00
|
|
|
self.assertEqual(get_seat_count(get_realm('zulip')), 8)
|
2018-11-29 03:24:19 +01:00
|
|
|
self.seat_count = 8
|
|
|
|
self.signed_seat_count, self.salt = sign_string(str(self.seat_count))
|
2018-12-15 09:33:25 +01:00
|
|
|
# Choosing dates with corresponding timestamps below 1500000000 so that they are
|
|
|
|
# not caught by our timestamp normalization regex in normalize_fixture_data
|
|
|
|
self.now = datetime(2012, 1, 2, 3, 4, 5).replace(tzinfo=timezone_utc)
|
|
|
|
self.next_month = datetime(2012, 2, 2, 3, 4, 5).replace(tzinfo=timezone_utc)
|
|
|
|
self.next_year = datetime(2013, 1, 2, 3, 4, 5).replace(tzinfo=timezone_utc)
|
2018-03-31 04:13:44 +02:00
|
|
|
|
2018-08-08 16:35:33 +02:00
|
|
|
def get_signed_seat_count_from_response(self, response: HttpResponse) -> Optional[str]:
|
|
|
|
match = re.search(r'name=\"signed_seat_count\" value=\"(.+)\"', response.content.decode("utf-8"))
|
|
|
|
return match.group(1) if match else None
|
|
|
|
|
|
|
|
def get_salt_from_response(self, response: HttpResponse) -> Optional[str]:
|
|
|
|
match = re.search(r'name=\"salt\" value=\"(\w+)\"', response.content.decode("utf-8"))
|
|
|
|
return match.group(1) if match else None
|
|
|
|
|
2018-11-29 03:15:27 +01:00
|
|
|
def upgrade(self, invoice: bool=False, talk_to_stripe: bool=True,
|
2018-12-22 05:29:25 +01:00
|
|
|
realm: Optional[Realm]=None, del_args: List[str]=[],
|
|
|
|
**kwargs: Any) -> HttpResponse:
|
2018-11-29 03:15:27 +01:00
|
|
|
host_args = {}
|
2018-12-15 09:33:25 +01:00
|
|
|
if realm is not None: # nocoverage: TODO
|
2018-11-29 03:15:27 +01:00
|
|
|
host_args['HTTP_HOST'] = realm.host
|
|
|
|
response = self.client_get("/upgrade/", **host_args)
|
|
|
|
params = {
|
2018-12-22 05:29:25 +01:00
|
|
|
'schedule': 'annual',
|
2018-11-29 03:15:27 +01:00
|
|
|
'signed_seat_count': self.get_signed_seat_count_from_response(response),
|
2018-12-22 05:29:25 +01:00
|
|
|
'salt': self.get_salt_from_response(response)} # type: Dict[str, Any]
|
2018-11-29 03:15:27 +01:00
|
|
|
if invoice: # send_invoice
|
|
|
|
params.update({
|
2018-12-22 05:29:25 +01:00
|
|
|
'billing_modality': 'send_invoice',
|
|
|
|
'licenses': 123})
|
2018-11-29 03:15:27 +01:00
|
|
|
else: # charge_automatically
|
|
|
|
stripe_token = None
|
|
|
|
if not talk_to_stripe:
|
|
|
|
stripe_token = 'token'
|
|
|
|
stripe_token = kwargs.get('stripe_token', stripe_token)
|
|
|
|
if stripe_token is None:
|
|
|
|
stripe_token = stripe_create_token().id
|
|
|
|
params.update({
|
|
|
|
'billing_modality': 'charge_automatically',
|
2018-12-22 05:29:25 +01:00
|
|
|
'license_management': 'automatic',
|
|
|
|
'stripe_token': stripe_token,
|
2018-11-29 03:15:27 +01:00
|
|
|
})
|
2018-12-07 18:43:22 +01:00
|
|
|
|
2018-11-29 03:15:27 +01:00
|
|
|
params.update(kwargs)
|
2018-12-22 05:29:25 +01:00
|
|
|
for key in del_args:
|
|
|
|
if key in params:
|
|
|
|
del params[key]
|
2018-12-07 18:43:22 +01:00
|
|
|
for key, value in params.items():
|
|
|
|
params[key] = ujson.dumps(value)
|
|
|
|
return self.client_post("/json/billing/upgrade", params, **host_args)
|
2018-11-29 03:15:27 +01:00
|
|
|
|
2019-01-27 21:16:02 +01:00
|
|
|
# Upgrade without talking to Stripe
|
|
|
|
def local_upgrade(self, *args: Any) -> None:
|
|
|
|
class StripeMock(object):
|
|
|
|
def __init__(self, depth: int=1):
|
|
|
|
self.id = 'id'
|
|
|
|
self.created = '1000'
|
|
|
|
self.last4 = '4242'
|
|
|
|
if depth == 1:
|
|
|
|
self.source = StripeMock(depth=2)
|
|
|
|
|
|
|
|
def upgrade_func(*args: Any) -> Any:
|
|
|
|
return process_initial_upgrade(self.example_user('hamlet'), *args[:4])
|
|
|
|
|
|
|
|
for mocked_function_name in MOCKED_STRIPE_FUNCTION_NAMES:
|
|
|
|
upgrade_func = patch(mocked_function_name, return_value=StripeMock())(upgrade_func)
|
|
|
|
upgrade_func(*args)
|
|
|
|
|
|
|
|
class StripeTest(StripeTestCase):
|
2018-10-18 05:50:52 +02:00
|
|
|
@patch("corporate.lib.stripe.billing_logger.error")
|
|
|
|
def test_catch_stripe_errors(self, mock_billing_logger_error: Mock) -> None:
|
2018-03-31 04:13:44 +02:00
|
|
|
@catch_stripe_errors
|
|
|
|
def raise_invalid_request_error() -> None:
|
2018-08-06 23:07:26 +02:00
|
|
|
raise stripe.error.InvalidRequestError(
|
2018-10-22 10:13:41 +02:00
|
|
|
"message", "param", "code", json_body={})
|
2018-08-06 06:16:29 +02:00
|
|
|
with self.assertRaises(BillingError) as context:
|
2018-03-31 04:13:44 +02:00
|
|
|
raise_invalid_request_error()
|
2018-08-06 06:16:29 +02:00
|
|
|
self.assertEqual('other stripe error', context.exception.description)
|
2018-03-31 04:13:44 +02:00
|
|
|
mock_billing_logger_error.assert_called()
|
|
|
|
|
|
|
|
@catch_stripe_errors
|
|
|
|
def raise_card_error() -> None:
|
|
|
|
error_message = "The card number is not a valid credit card number."
|
|
|
|
json_body = {"error": {"message": error_message}}
|
|
|
|
raise stripe.error.CardError(error_message, "number", "invalid_number",
|
|
|
|
json_body=json_body)
|
2018-08-06 23:07:26 +02:00
|
|
|
with self.assertRaises(StripeCardError) as context:
|
2018-03-31 04:13:44 +02:00
|
|
|
raise_card_error()
|
2018-08-06 06:16:29 +02:00
|
|
|
self.assertIn('not a valid credit card', context.exception.message)
|
|
|
|
self.assertEqual('card error', context.exception.description)
|
2018-03-31 04:13:44 +02:00
|
|
|
mock_billing_logger_error.assert_called()
|
|
|
|
|
2018-10-18 20:31:01 +02:00
|
|
|
def test_billing_not_enabled(self) -> None:
|
|
|
|
with self.settings(BILLING_ENABLED=False):
|
|
|
|
self.login(self.example_email("iago"))
|
|
|
|
response = self.client_get("/upgrade/")
|
|
|
|
self.assert_in_success_response(["Page not found (404)"], response)
|
|
|
|
|
2018-12-04 00:29:23 +01:00
|
|
|
@mock_stripe(tested_timestamp_fields=["created"])
|
2018-12-15 09:33:25 +01:00
|
|
|
def test_upgrade_by_card(self, *mocks: Mock) -> None:
|
2018-07-25 16:37:07 +02:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login(user.email)
|
2018-03-31 04:13:44 +02:00
|
|
|
response = self.client_get("/upgrade/")
|
2018-11-18 10:18:14 +01:00
|
|
|
self.assert_in_success_response(['Pay annually'], response)
|
2018-10-24 06:09:01 +02:00
|
|
|
self.assertNotEqual(user.realm.plan_type, Realm.STANDARD)
|
2018-10-17 08:23:13 +02:00
|
|
|
self.assertFalse(Customer.objects.filter(realm=user.realm).exists())
|
2018-08-08 16:35:33 +02:00
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
# Click "Make payment" in Stripe Checkout
|
2018-12-15 09:33:25 +01:00
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.upgrade()
|
2018-10-17 08:23:13 +02:00
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check that we correctly created a Customer object in Stripe
|
2018-10-17 08:23:13 +02:00
|
|
|
stripe_customer = stripe_get_customer(Customer.objects.get(realm=user.realm).stripe_customer_id)
|
|
|
|
self.assertEqual(stripe_customer.default_source.id[:5], 'card_')
|
|
|
|
self.assertEqual(stripe_customer.description, "zulip (Zulip Dev)")
|
|
|
|
self.assertEqual(stripe_customer.discount, None)
|
|
|
|
self.assertEqual(stripe_customer.email, user.email)
|
|
|
|
self.assertEqual(dict(stripe_customer.metadata),
|
|
|
|
{'realm_id': str(user.realm.id), 'realm_str': 'zulip'})
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check Charges in Stripe
|
|
|
|
stripe_charges = [charge for charge in stripe.Charge.list(customer=stripe_customer.id)]
|
|
|
|
self.assertEqual(len(stripe_charges), 1)
|
|
|
|
self.assertEqual(stripe_charges[0].amount, 8000 * self.seat_count)
|
|
|
|
# TODO: fix Decimal
|
|
|
|
self.assertEqual(stripe_charges[0].description,
|
|
|
|
"Upgrade to Zulip Standard, $80.0 x {}".format(self.seat_count))
|
|
|
|
self.assertEqual(stripe_charges[0].receipt_email, user.email)
|
|
|
|
self.assertEqual(stripe_charges[0].statement_descriptor, "Zulip Standard")
|
|
|
|
# Check Invoices in Stripe
|
|
|
|
stripe_invoices = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer.id)]
|
|
|
|
self.assertEqual(len(stripe_invoices), 1)
|
|
|
|
self.assertIsNotNone(stripe_invoices[0].finalized_at)
|
|
|
|
invoice_params = {
|
|
|
|
# auto_advance is False because the invoice has been paid
|
|
|
|
'amount_due': 0, 'amount_paid': 0, 'auto_advance': False, 'billing': 'charge_automatically',
|
|
|
|
'charge': None, 'status': 'paid', 'total': 0}
|
|
|
|
for key, value in invoice_params.items():
|
|
|
|
self.assertEqual(stripe_invoices[0].get(key), value)
|
|
|
|
# Check Line Items on Stripe Invoice
|
|
|
|
stripe_line_items = [item for item in stripe_invoices[0].lines]
|
|
|
|
self.assertEqual(len(stripe_line_items), 2)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': 8000 * self.seat_count, 'description': 'Zulip Standard', 'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'end': datetime_to_timestamp(self.next_year),
|
|
|
|
'start': datetime_to_timestamp(self.now)},
|
|
|
|
# There's no unit_amount on Line Items, probably because it doesn't show up on the
|
|
|
|
# user-facing invoice. We could pull the Invoice Item instead and test unit_amount there,
|
|
|
|
# but testing the amount and quantity seems sufficient.
|
|
|
|
'plan': None, 'proration': False, 'quantity': self.seat_count}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[0].get(key), value)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': -8000 * self.seat_count, 'description': 'Payment (Card ending in 4242)',
|
|
|
|
'discountable': False, 'plan': None, 'proration': False, 'quantity': 1}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[1].get(key), value)
|
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
# Check that we correctly populated Customer, CustomerPlan, and LicenseLedger in Zulip
|
|
|
|
customer = Customer.objects.get(stripe_customer_id=stripe_customer.id, realm=user.realm)
|
|
|
|
plan = CustomerPlan.objects.get(
|
|
|
|
customer=customer, automanage_licenses=True,
|
2018-12-15 09:33:25 +01:00
|
|
|
price_per_license=8000, fixed_price=None, discount=None, billing_cycle_anchor=self.now,
|
2019-01-28 14:18:21 +01:00
|
|
|
billing_schedule=CustomerPlan.ANNUAL, invoiced_through=LicenseLedger.objects.first(),
|
|
|
|
next_invoice_date=self.next_month, tier=CustomerPlan.STANDARD,
|
2018-12-28 07:20:30 +01:00
|
|
|
status=CustomerPlan.ACTIVE)
|
|
|
|
LicenseLedger.objects.get(
|
|
|
|
plan=plan, is_renewal=True, event_time=self.now, licenses=self.seat_count,
|
|
|
|
licenses_at_next_renewal=self.seat_count)
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check RealmAuditLog
|
2018-07-25 16:37:07 +02:00
|
|
|
audit_log_entries = list(RealmAuditLog.objects.filter(acting_user=user)
|
2018-06-28 00:48:51 +02:00
|
|
|
.values_list('event_type', 'event_time').order_by('id'))
|
|
|
|
self.assertEqual(audit_log_entries, [
|
2018-10-17 08:23:13 +02:00
|
|
|
(RealmAuditLog.STRIPE_CUSTOMER_CREATED, timestamp_to_datetime(stripe_customer.created)),
|
|
|
|
(RealmAuditLog.STRIPE_CARD_CHANGED, timestamp_to_datetime(stripe_customer.created)),
|
2018-12-15 09:33:25 +01:00
|
|
|
(RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now),
|
2018-12-13 07:54:43 +01:00
|
|
|
# TODO: Check for REALM_PLAN_TYPE_CHANGED
|
|
|
|
# (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, Kandra()),
|
2018-06-28 00:48:51 +02:00
|
|
|
])
|
2018-12-15 09:33:25 +01:00
|
|
|
self.assertEqual(ujson.loads(RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED).values_list(
|
2018-12-28 07:20:30 +01:00
|
|
|
'extra_data', flat=True).first())['automanage_licenses'], True)
|
2018-06-28 00:48:51 +02:00
|
|
|
# Check that we correctly updated Realm
|
|
|
|
realm = get_realm("zulip")
|
2018-10-24 06:09:01 +02:00
|
|
|
self.assertEqual(realm.plan_type, Realm.STANDARD)
|
|
|
|
self.assertEqual(realm.max_invites, Realm.INVITES_STANDARD_REALM_DAILY_MAX)
|
2018-03-31 04:13:44 +02:00
|
|
|
# Check that we can no longer access /upgrade
|
|
|
|
response = self.client_get("/upgrade/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/billing/', response.url)
|
|
|
|
|
2018-12-23 09:10:57 +01:00
|
|
|
# Check /billing has the correct information
|
2019-01-26 20:45:26 +01:00
|
|
|
with patch('corporate.views.timezone_now', return_value=self.now):
|
|
|
|
response = self.client_get("/billing/")
|
2018-12-23 09:10:57 +01:00
|
|
|
self.assert_not_in_success_response(['Pay annually'], response)
|
|
|
|
for substring in [
|
|
|
|
'Zulip Standard', str(self.seat_count),
|
2019-01-29 16:38:24 +01:00
|
|
|
'You are using', '%s of %s licenses' % (self.seat_count, self.seat_count),
|
2018-12-23 09:10:57 +01:00
|
|
|
'Your plan will renew on', 'January 2, 2013', '$%s.00' % (80 * self.seat_count,),
|
|
|
|
'Visa ending in 4242',
|
|
|
|
'Update card']:
|
|
|
|
self.assert_in_response(substring, response)
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
@mock_stripe(tested_timestamp_fields=["created"])
|
|
|
|
def test_upgrade_by_invoice(self, *mocks: Mock) -> None:
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login(user.email)
|
|
|
|
# Click "Make payment" in Stripe Checkout
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.upgrade(invoice=True)
|
|
|
|
# Check that we correctly created a Customer in Stripe
|
|
|
|
stripe_customer = stripe_get_customer(Customer.objects.get(realm=user.realm).stripe_customer_id)
|
|
|
|
# It can take a second for Stripe to attach the source to the customer, and in
|
|
|
|
# particular it may not be attached at the time stripe_get_customer is called above,
|
|
|
|
# causing test flakes.
|
|
|
|
# So commenting the next line out, but leaving it here so future readers know what
|
|
|
|
# is supposed to happen here
|
|
|
|
# self.assertEqual(stripe_customer.default_source.type, 'ach_credit_transfer')
|
|
|
|
|
|
|
|
# Check Charges in Stripe
|
|
|
|
self.assertFalse(stripe.Charge.list(customer=stripe_customer.id))
|
|
|
|
# Check Invoices in Stripe
|
|
|
|
stripe_invoices = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer.id)]
|
|
|
|
self.assertEqual(len(stripe_invoices), 1)
|
|
|
|
self.assertIsNotNone(stripe_invoices[0].due_date)
|
|
|
|
self.assertIsNotNone(stripe_invoices[0].finalized_at)
|
|
|
|
invoice_params = {
|
|
|
|
'amount_due': 8000 * 123, 'amount_paid': 0, 'attempt_count': 0,
|
|
|
|
'auto_advance': True, 'billing': 'send_invoice', 'statement_descriptor': 'Zulip Standard',
|
|
|
|
'status': 'open', 'total': 8000 * 123}
|
|
|
|
for key, value in invoice_params.items():
|
|
|
|
self.assertEqual(stripe_invoices[0].get(key), value)
|
|
|
|
# Check Line Items on Stripe Invoice
|
|
|
|
stripe_line_items = [item for item in stripe_invoices[0].lines]
|
|
|
|
self.assertEqual(len(stripe_line_items), 1)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': 8000 * 123, 'description': 'Zulip Standard', 'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'end': datetime_to_timestamp(self.next_year),
|
|
|
|
'start': datetime_to_timestamp(self.now)},
|
|
|
|
'plan': None, 'proration': False, 'quantity': 123}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[0].get(key), value)
|
|
|
|
|
2018-12-28 07:20:30 +01:00
|
|
|
# Check that we correctly populated Customer, CustomerPlan and LicenseLedger in Zulip
|
|
|
|
customer = Customer.objects.get(stripe_customer_id=stripe_customer.id, realm=user.realm)
|
|
|
|
plan = CustomerPlan.objects.get(
|
|
|
|
customer=customer, automanage_licenses=False, charge_automatically=False,
|
2018-12-15 09:33:25 +01:00
|
|
|
price_per_license=8000, fixed_price=None, discount=None, billing_cycle_anchor=self.now,
|
2019-01-28 14:18:21 +01:00
|
|
|
billing_schedule=CustomerPlan.ANNUAL, invoiced_through=LicenseLedger.objects.first(),
|
|
|
|
next_invoice_date=self.next_year, tier=CustomerPlan.STANDARD,
|
2018-12-28 07:20:30 +01:00
|
|
|
status=CustomerPlan.ACTIVE)
|
|
|
|
LicenseLedger.objects.get(
|
|
|
|
plan=plan, is_renewal=True, event_time=self.now, licenses=123, licenses_at_next_renewal=123)
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check RealmAuditLog
|
|
|
|
audit_log_entries = list(RealmAuditLog.objects.filter(acting_user=user)
|
|
|
|
.values_list('event_type', 'event_time').order_by('id'))
|
|
|
|
self.assertEqual(audit_log_entries, [
|
|
|
|
(RealmAuditLog.STRIPE_CUSTOMER_CREATED, timestamp_to_datetime(stripe_customer.created)),
|
|
|
|
(RealmAuditLog.CUSTOMER_PLAN_CREATED, self.now),
|
|
|
|
# TODO: Check for REALM_PLAN_TYPE_CHANGED
|
|
|
|
# (RealmAuditLog.REALM_PLAN_TYPE_CHANGED, Kandra()),
|
|
|
|
])
|
|
|
|
self.assertEqual(ujson.loads(RealmAuditLog.objects.filter(
|
|
|
|
event_type=RealmAuditLog.CUSTOMER_PLAN_CREATED).values_list(
|
2018-12-28 07:20:30 +01:00
|
|
|
'extra_data', flat=True).first())['automanage_licenses'], False)
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check that we correctly updated Realm
|
|
|
|
realm = get_realm("zulip")
|
|
|
|
self.assertEqual(realm.plan_type, Realm.STANDARD)
|
|
|
|
self.assertEqual(realm.max_invites, Realm.INVITES_STANDARD_REALM_DAILY_MAX)
|
|
|
|
# Check that we can no longer access /upgrade
|
|
|
|
response = self.client_get("/upgrade/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/billing/', response.url)
|
|
|
|
|
2018-12-23 09:10:57 +01:00
|
|
|
# Check /billing has the correct information
|
2019-01-26 20:45:26 +01:00
|
|
|
with patch('corporate.views.timezone_now', return_value=self.now):
|
|
|
|
response = self.client_get("/billing/")
|
2018-12-23 09:10:57 +01:00
|
|
|
self.assert_not_in_success_response(['Pay annually', 'Update card'], response)
|
|
|
|
for substring in [
|
|
|
|
'Zulip Standard', str(123),
|
2019-01-29 16:38:24 +01:00
|
|
|
'You are using', '%s of %s licenses' % (self.seat_count, 123),
|
2018-12-23 09:10:57 +01:00
|
|
|
'Your plan will renew on', 'January 2, 2013', '$9,840.00', # 9840 = 80 * 123
|
|
|
|
'Billed by invoice']:
|
|
|
|
self.assert_in_response(substring, response)
|
2018-11-05 22:37:22 +01:00
|
|
|
|
2018-12-03 19:23:13 +01:00
|
|
|
@mock_stripe()
|
2018-12-02 19:05:34 +01:00
|
|
|
def test_billing_page_permissions(self, *mocks: Mock) -> None:
|
2018-07-11 16:36:52 +02:00
|
|
|
# Check that non-admins can access /upgrade via /billing, when there is no Customer object
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
response = self.client_get("/billing/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/upgrade/', response.url)
|
|
|
|
# Check that non-admins can sign up and pay
|
2018-11-29 03:15:27 +01:00
|
|
|
self.upgrade()
|
2018-07-11 16:36:52 +02:00
|
|
|
# Check that the non-admin hamlet can still access /billing
|
|
|
|
response = self.client_get("/billing/")
|
2018-06-29 16:51:36 +02:00
|
|
|
self.assert_in_success_response(["for billing history or to make changes"], response)
|
2018-08-22 07:49:48 +02:00
|
|
|
# Check admins can access billing, even though they are not a billing admin
|
2018-07-11 16:36:52 +02:00
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
response = self.client_get("/billing/")
|
2018-06-29 16:51:36 +02:00
|
|
|
self.assert_in_success_response(["for billing history or to make changes"], response)
|
2018-08-22 07:49:48 +02:00
|
|
|
# Check that a non-admin, non-billing admin user does not have access
|
2018-07-11 16:36:52 +02:00
|
|
|
self.login(self.example_email("cordelia"))
|
|
|
|
response = self.client_get("/billing/")
|
|
|
|
self.assert_in_success_response(["You must be an organization administrator"], response)
|
|
|
|
|
2018-12-04 00:29:23 +01:00
|
|
|
@mock_stripe(tested_timestamp_fields=["created"])
|
2018-12-15 09:33:25 +01:00
|
|
|
def test_upgrade_by_card_with_outdated_seat_count(self, *mocks: Mock) -> None:
|
2018-07-25 16:37:07 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2018-12-15 09:33:25 +01:00
|
|
|
new_seat_count = 23
|
2018-06-28 00:48:51 +02:00
|
|
|
# Change the seat count while the user is going through the upgrade flow
|
2018-10-18 05:50:52 +02:00
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=new_seat_count):
|
2018-11-29 03:15:27 +01:00
|
|
|
self.upgrade()
|
2018-12-15 09:33:25 +01:00
|
|
|
stripe_customer_id = Customer.objects.first().stripe_customer_id
|
|
|
|
# Check that the Charge used the old quantity, not new_seat_count
|
|
|
|
self.assertEqual(8000 * self.seat_count,
|
|
|
|
[charge for charge in stripe.Charge.list(customer=stripe_customer_id)][0].amount)
|
|
|
|
# Check that the invoice has a credit for the old amount and a charge for the new one
|
|
|
|
stripe_invoice = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer_id)][0]
|
|
|
|
self.assertEqual([8000 * new_seat_count, -8000 * self.seat_count],
|
|
|
|
[item.amount for item in stripe_invoice.lines])
|
2018-12-28 07:20:30 +01:00
|
|
|
# Check LicenseLedger has the new amount
|
|
|
|
self.assertEqual(LicenseLedger.objects.first().licenses, new_seat_count)
|
|
|
|
self.assertEqual(LicenseLedger.objects.first().licenses_at_next_renewal, new_seat_count)
|
2018-06-28 00:48:51 +02:00
|
|
|
|
2018-12-03 19:23:13 +01:00
|
|
|
@mock_stripe()
|
2018-12-15 09:33:25 +01:00
|
|
|
def test_upgrade_where_first_card_fails(self, *mocks: Mock) -> None:
|
2018-08-14 03:33:31 +02:00
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login(user.email)
|
2018-10-18 20:04:45 +02:00
|
|
|
# From https://stripe.com/docs/testing#cards: Attaching this card to
|
|
|
|
# a Customer object succeeds, but attempts to charge the customer fail.
|
2018-12-15 09:33:25 +01:00
|
|
|
with patch("corporate.lib.stripe.billing_logger.error") as mock_billing_logger:
|
|
|
|
self.upgrade(stripe_token=stripe_create_token('4000000000000341').id)
|
|
|
|
mock_billing_logger.assert_called()
|
|
|
|
# Check that we created a Customer object but no CustomerPlan
|
|
|
|
stripe_customer_id = Customer.objects.get(realm=get_realm('zulip')).stripe_customer_id
|
|
|
|
self.assertFalse(CustomerPlan.objects.exists())
|
|
|
|
# Check that we created a Customer in stripe, a failed Charge, and no Invoices or Invoice Items
|
|
|
|
self.assertTrue(stripe_get_customer(stripe_customer_id))
|
|
|
|
stripe_charges = [charge for charge in stripe.Charge.list(customer=stripe_customer_id)]
|
|
|
|
self.assertEqual(len(stripe_charges), 1)
|
|
|
|
self.assertEqual(stripe_charges[0].failure_code, 'card_declined')
|
|
|
|
# TODO: figure out what these actually are
|
|
|
|
self.assertFalse(stripe.Invoice.list(customer=stripe_customer_id))
|
|
|
|
self.assertFalse(stripe.InvoiceItem.list(customer=stripe_customer_id))
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we correctly populated RealmAuditLog
|
|
|
|
audit_log_entries = list(RealmAuditLog.objects.filter(acting_user=user)
|
|
|
|
.values_list('event_type', flat=True).order_by('id'))
|
|
|
|
self.assertEqual(audit_log_entries, [RealmAuditLog.STRIPE_CUSTOMER_CREATED,
|
2018-09-05 09:40:29 +02:00
|
|
|
RealmAuditLog.STRIPE_CARD_CHANGED])
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we did not update Realm
|
|
|
|
realm = get_realm("zulip")
|
2018-12-15 09:33:25 +01:00
|
|
|
self.assertNotEqual(realm.plan_type, Realm.STANDARD)
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we still get redirected to /upgrade
|
|
|
|
response = self.client_get("/billing/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/upgrade/', response.url)
|
|
|
|
|
2018-12-15 09:33:25 +01:00
|
|
|
# Try again, with a valid card, after they added a few users
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=23):
|
|
|
|
with patch('corporate.views.get_seat_count', return_value=23):
|
|
|
|
self.upgrade()
|
2018-10-18 20:04:45 +02:00
|
|
|
customer = Customer.objects.get(realm=get_realm('zulip'))
|
2018-12-15 09:33:25 +01:00
|
|
|
# It's impossible to create two Customers, but check that we didn't
|
|
|
|
# change stripe_customer_id
|
|
|
|
self.assertEqual(customer.stripe_customer_id, stripe_customer_id)
|
2018-12-28 07:20:30 +01:00
|
|
|
# Check that we successfully added a CustomerPlan, and have the right number of licenses
|
|
|
|
plan = CustomerPlan.objects.get(customer=customer)
|
|
|
|
ledger_entry = LicenseLedger.objects.get(plan=plan)
|
|
|
|
self.assertEqual(ledger_entry.licenses, 23)
|
|
|
|
self.assertEqual(ledger_entry.licenses_at_next_renewal, 23)
|
2018-12-15 09:33:25 +01:00
|
|
|
# Check the Charges and Invoices in Stripe
|
|
|
|
self.assertEqual(8000 * 23, [charge for charge in
|
|
|
|
stripe.Charge.list(customer=stripe_customer_id)][0].amount)
|
|
|
|
stripe_invoice = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer_id)][0]
|
|
|
|
self.assertEqual([8000 * 23, -8000 * 23],
|
|
|
|
[item.amount for item in stripe_invoice.lines])
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we correctly populated RealmAuditLog
|
|
|
|
audit_log_entries = list(RealmAuditLog.objects.filter(acting_user=user)
|
|
|
|
.values_list('event_type', flat=True).order_by('id'))
|
2018-12-13 07:54:43 +01:00
|
|
|
# TODO: Test for REALM_PLAN_TYPE_CHANGED as the last entry
|
2018-08-14 03:33:31 +02:00
|
|
|
self.assertEqual(audit_log_entries, [RealmAuditLog.STRIPE_CUSTOMER_CREATED,
|
2018-09-05 09:40:29 +02:00
|
|
|
RealmAuditLog.STRIPE_CARD_CHANGED,
|
|
|
|
RealmAuditLog.STRIPE_CARD_CHANGED,
|
2018-12-15 09:33:25 +01:00
|
|
|
RealmAuditLog.CUSTOMER_PLAN_CREATED])
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we correctly updated Realm
|
|
|
|
realm = get_realm("zulip")
|
2018-12-15 09:33:25 +01:00
|
|
|
self.assertEqual(realm.plan_type, Realm.STANDARD)
|
2018-08-14 03:33:31 +02:00
|
|
|
# Check that we can no longer access /upgrade
|
|
|
|
response = self.client_get("/upgrade/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/billing/', response.url)
|
|
|
|
|
2018-07-13 13:33:05 +02:00
|
|
|
def test_upgrade_with_tampered_seat_count(self) -> None:
|
2018-07-25 16:37:07 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2018-11-29 03:15:27 +01:00
|
|
|
response = self.upgrade(talk_to_stripe=False, salt='badsalt')
|
2018-12-07 18:43:22 +01:00
|
|
|
self.assert_json_error_contains(response, "Something went wrong. Please contact")
|
|
|
|
self.assertEqual(ujson.loads(response.content)['error_description'], 'tampered seat count')
|
2018-07-13 13:33:05 +02:00
|
|
|
|
2019-01-29 16:01:31 +01:00
|
|
|
def test_upgrade_race_condition(self) -> None:
|
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
with patch("corporate.lib.stripe.billing_logger.warning") as mock_billing_logger:
|
|
|
|
with self.assertRaises(BillingError) as context:
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
self.assertEqual('subscribing with existing subscription', context.exception.description)
|
|
|
|
mock_billing_logger.assert_called()
|
|
|
|
|
2018-12-22 05:29:25 +01:00
|
|
|
def test_check_upgrade_parameters(self) -> None:
|
|
|
|
# Tests all the error paths except 'not enough licenses'
|
|
|
|
def check_error(error_description: str, upgrade_params: Dict[str, Any],
|
|
|
|
del_args: List[str]=[]) -> None:
|
|
|
|
response = self.upgrade(talk_to_stripe=False, del_args=del_args, **upgrade_params)
|
|
|
|
self.assert_json_error_contains(response, "Something went wrong. Please contact")
|
|
|
|
self.assertEqual(ujson.loads(response.content)['error_description'], error_description)
|
|
|
|
|
2018-07-25 16:37:07 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2018-12-22 05:29:25 +01:00
|
|
|
check_error('unknown billing_modality', {'billing_modality': 'invalid'})
|
|
|
|
check_error('unknown schedule', {'schedule': 'invalid'})
|
|
|
|
check_error('unknown license_management', {'license_management': 'invalid'})
|
|
|
|
check_error('autopay with no card', {}, del_args=['stripe_token'])
|
|
|
|
|
|
|
|
def test_upgrade_license_counts(self) -> None:
|
|
|
|
def check_error(invoice: bool, licenses: Optional[int], min_licenses_in_response: int,
|
|
|
|
upgrade_params: Dict[str, Any]={}) -> None:
|
|
|
|
if licenses is None:
|
|
|
|
del_args = ['licenses']
|
|
|
|
else:
|
|
|
|
del_args = []
|
|
|
|
upgrade_params['licenses'] = licenses
|
|
|
|
response = self.upgrade(invoice=invoice, talk_to_stripe=False,
|
|
|
|
del_args=del_args, **upgrade_params)
|
|
|
|
self.assert_json_error_contains(response, "at least {} users".format(min_licenses_in_response))
|
|
|
|
self.assertEqual(ujson.loads(response.content)['error_description'], 'not enough licenses')
|
|
|
|
|
|
|
|
def check_success(invoice: bool, licenses: Optional[int], upgrade_params: Dict[str, Any]={}) -> None:
|
|
|
|
if licenses is None:
|
|
|
|
del_args = ['licenses']
|
|
|
|
else:
|
|
|
|
del_args = []
|
|
|
|
upgrade_params['licenses'] = licenses
|
|
|
|
with patch('corporate.views.process_initial_upgrade'):
|
|
|
|
response = self.upgrade(invoice=invoice, talk_to_stripe=False,
|
|
|
|
del_args=del_args, **upgrade_params)
|
|
|
|
self.assert_json_success(response)
|
2018-07-22 17:23:57 +02:00
|
|
|
|
2018-09-08 00:49:54 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
2018-12-22 05:29:25 +01:00
|
|
|
# Autopay with licenses < seat count
|
|
|
|
check_error(False, self.seat_count - 1, self.seat_count, {'license_management': 'manual'})
|
|
|
|
# Autopay with not setting licenses
|
|
|
|
check_error(False, None, self.seat_count, {'license_management': 'manual'})
|
|
|
|
# Invoice with licenses < MIN_INVOICED_LICENSES
|
|
|
|
check_error(True, MIN_INVOICED_LICENSES - 1, MIN_INVOICED_LICENSES)
|
|
|
|
# Invoice with licenses < seat count
|
2018-12-22 01:43:44 +01:00
|
|
|
with patch("corporate.views.MIN_INVOICED_LICENSES", 3):
|
2018-12-22 05:29:25 +01:00
|
|
|
check_error(True, 4, self.seat_count)
|
|
|
|
# Invoice with not setting licenses
|
|
|
|
check_error(True, None, MIN_INVOICED_LICENSES)
|
|
|
|
|
|
|
|
# Autopay with automatic license_management
|
|
|
|
check_success(False, None)
|
|
|
|
# Autopay with automatic license_management, should just ignore the licenses entry
|
|
|
|
check_success(False, self.seat_count)
|
|
|
|
# Autopay
|
|
|
|
check_success(False, self.seat_count, {'license_management': 'manual'})
|
|
|
|
# Invoice
|
|
|
|
check_success(True, self.seat_count + MIN_INVOICED_LICENSES)
|
2018-09-08 00:49:54 +02:00
|
|
|
|
2018-10-22 12:41:14 +02:00
|
|
|
@patch("corporate.lib.stripe.billing_logger.error")
|
2018-12-02 19:05:34 +01:00
|
|
|
def test_upgrade_with_uncaught_exception(self, mock_: Mock) -> None:
|
2018-10-22 12:41:14 +02:00
|
|
|
self.login(self.example_email("hamlet"))
|
|
|
|
with patch("corporate.views.process_initial_upgrade", side_effect=Exception):
|
2018-11-29 03:15:27 +01:00
|
|
|
response = self.upgrade(talk_to_stripe=False)
|
2018-12-07 18:43:22 +01:00
|
|
|
self.assert_json_error_contains(response, "Something went wrong. Please contact zulip-admin@example.com.")
|
|
|
|
self.assertEqual(ujson.loads(response.content)['error_description'], 'uncaught exception during upgrade')
|
2018-10-22 12:41:14 +02:00
|
|
|
|
2018-11-28 10:49:16 +01:00
|
|
|
def test_redirect_for_billing_home(self) -> None:
|
2018-08-22 07:49:48 +02:00
|
|
|
user = self.example_user("iago")
|
2018-07-25 16:37:07 +02:00
|
|
|
self.login(user.email)
|
2018-03-31 04:13:44 +02:00
|
|
|
# No Customer yet; check that we are redirected to /upgrade
|
|
|
|
response = self.client_get("/billing/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/upgrade/', response.url)
|
|
|
|
|
2018-11-28 10:49:16 +01:00
|
|
|
# Customer, but no billing relationship; check that we are still redirected to /upgrade
|
|
|
|
Customer.objects.create(
|
2018-11-28 23:23:03 +01:00
|
|
|
realm=user.realm, stripe_customer_id='cus_123', has_billing_relationship=False)
|
2018-11-05 22:37:22 +01:00
|
|
|
response = self.client_get("/billing/")
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
self.assertEqual('/upgrade/', response.url)
|
|
|
|
|
2018-03-31 04:13:44 +02:00
|
|
|
def test_get_seat_count(self) -> None:
|
2018-07-25 16:37:07 +02:00
|
|
|
realm = get_realm("zulip")
|
|
|
|
initial_count = get_seat_count(realm)
|
|
|
|
user1 = UserProfile.objects.create(realm=realm, email='user1@zulip.com', pointer=-1)
|
|
|
|
user2 = UserProfile.objects.create(realm=realm, email='user2@zulip.com', pointer=-1)
|
|
|
|
self.assertEqual(get_seat_count(realm), initial_count + 2)
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
# Test that bots aren't counted
|
|
|
|
user1.is_bot = True
|
|
|
|
user1.save(update_fields=['is_bot'])
|
2018-07-25 16:37:07 +02:00
|
|
|
self.assertEqual(get_seat_count(realm), initial_count + 1)
|
2018-03-31 04:13:44 +02:00
|
|
|
|
|
|
|
# Test that inactive users aren't counted
|
|
|
|
do_deactivate_user(user2)
|
2018-07-25 16:37:07 +02:00
|
|
|
self.assertEqual(get_seat_count(realm), initial_count)
|
2018-06-28 00:48:51 +02:00
|
|
|
|
2018-07-13 17:34:39 +02:00
|
|
|
def test_sign_string(self) -> None:
|
|
|
|
string = "abc"
|
|
|
|
signed_string, salt = sign_string(string)
|
|
|
|
self.assertEqual(string, unsign_string(signed_string, salt))
|
|
|
|
|
|
|
|
with self.assertRaises(signing.BadSignature):
|
|
|
|
unsign_string(signed_string, "randomsalt")
|
|
|
|
|
2018-09-08 00:49:54 +02:00
|
|
|
# This tests both the payment method string, and also is a very basic
|
|
|
|
# test that the various upgrade paths involving non-standard payment
|
|
|
|
# histories don't throw errors
|
2018-12-03 19:23:13 +01:00
|
|
|
@mock_stripe()
|
2018-12-02 19:05:34 +01:00
|
|
|
def test_payment_method_string(self, *mocks: Mock) -> None:
|
2018-12-15 09:33:25 +01:00
|
|
|
pass
|
2018-09-08 00:49:54 +02:00
|
|
|
# If you signup with a card, we should show your card as the payment method
|
|
|
|
# Already tested in test_initial_upgrade
|
|
|
|
|
|
|
|
# If you pay by invoice, your payment method should be
|
|
|
|
# "Billed by invoice", even if you have a card on file
|
2018-12-15 09:33:25 +01:00
|
|
|
# user = self.example_user("hamlet")
|
2019-01-29 06:34:31 +01:00
|
|
|
# do_create_stripe_customer(user, stripe_create_token().id)
|
2018-12-15 09:33:25 +01:00
|
|
|
# self.login(user.email)
|
|
|
|
# self.upgrade(invoice=True)
|
|
|
|
# stripe_customer = stripe_get_customer(Customer.objects.get(realm=user.realm).stripe_customer_id)
|
|
|
|
# self.assertEqual('Billed by invoice', payment_method_string(stripe_customer))
|
2018-09-08 00:49:54 +02:00
|
|
|
|
|
|
|
# If you signup with a card and then downgrade, we still have your
|
|
|
|
# card on file, and should show it
|
2018-12-12 07:47:53 +01:00
|
|
|
# TODO
|
2018-09-08 00:49:54 +02:00
|
|
|
|
2018-12-03 19:23:13 +01:00
|
|
|
@mock_stripe()
|
2018-12-02 19:05:34 +01:00
|
|
|
def test_attach_discount_to_realm(self, *mocks: Mock) -> None:
|
2018-11-23 12:37:01 +01:00
|
|
|
# Attach discount before Stripe customer exists
|
2018-08-23 07:45:19 +02:00
|
|
|
user = self.example_user('hamlet')
|
2019-01-29 06:34:31 +01:00
|
|
|
attach_discount_to_realm(user.realm, Decimal(85))
|
2018-11-23 12:37:01 +01:00
|
|
|
self.login(user.email)
|
2018-11-30 02:27:01 +01:00
|
|
|
# Check that the discount appears in page_params
|
|
|
|
self.assert_in_success_response(['85'], self.client_get("/upgrade/"))
|
2018-11-23 12:37:01 +01:00
|
|
|
# Check that the customer was charged the discounted amount
|
2019-01-29 06:34:31 +01:00
|
|
|
self.upgrade()
|
|
|
|
stripe_customer_id = Customer.objects.values_list('stripe_customer_id', flat=True).first()
|
|
|
|
self.assertEqual(1200 * self.seat_count,
|
|
|
|
[charge for charge in stripe.Charge.list(customer=stripe_customer_id)][0].amount)
|
|
|
|
stripe_invoice = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer_id)][0]
|
|
|
|
self.assertEqual([1200 * self.seat_count, -1200 * self.seat_count],
|
|
|
|
[item.amount for item in stripe_invoice.lines])
|
|
|
|
# Check CustomerPlan reflects the discount
|
|
|
|
plan = CustomerPlan.objects.get(price_per_license=1200, discount=Decimal(85))
|
|
|
|
|
2018-11-23 12:37:01 +01:00
|
|
|
# Attach discount to existing Stripe customer
|
2019-01-29 06:34:31 +01:00
|
|
|
plan.status = CustomerPlan.ENDED
|
|
|
|
plan.save(update_fields=['status'])
|
|
|
|
attach_discount_to_realm(user.realm, Decimal(25))
|
|
|
|
process_initial_upgrade(user, self.seat_count, True, CustomerPlan.ANNUAL, stripe_create_token().id)
|
|
|
|
self.assertEqual(6000 * self.seat_count,
|
|
|
|
[charge for charge in stripe.Charge.list(customer=stripe_customer_id)][0].amount)
|
|
|
|
stripe_invoice = [invoice for invoice in stripe.Invoice.list(customer=stripe_customer_id)][0]
|
|
|
|
self.assertEqual([6000 * self.seat_count, -6000 * self.seat_count],
|
|
|
|
[item.amount for item in stripe_invoice.lines])
|
|
|
|
plan = CustomerPlan.objects.get(price_per_license=6000, discount=Decimal(25))
|
2018-08-23 07:45:19 +02:00
|
|
|
|
2018-12-03 19:23:13 +01:00
|
|
|
@mock_stripe()
|
2018-12-02 19:05:34 +01:00
|
|
|
def test_replace_payment_source(self, *mocks: Mock) -> None:
|
2018-11-28 09:07:21 +01:00
|
|
|
user = self.example_user("hamlet")
|
2018-10-22 14:21:48 +02:00
|
|
|
self.login(user.email)
|
2018-11-29 03:15:27 +01:00
|
|
|
self.upgrade()
|
2018-11-28 09:07:21 +01:00
|
|
|
# Try replacing with a valid card
|
|
|
|
stripe_token = stripe_create_token(card_number='5555555555554444').id
|
|
|
|
response = self.client_post("/json/billing/sources/change",
|
|
|
|
{'stripe_token': ujson.dumps(stripe_token)})
|
|
|
|
self.assert_json_success(response)
|
|
|
|
number_of_sources = 0
|
|
|
|
for stripe_source in stripe_get_customer(Customer.objects.first().stripe_customer_id).sources:
|
|
|
|
self.assertEqual(cast(stripe.Card, stripe_source).last4, '4444')
|
|
|
|
number_of_sources += 1
|
|
|
|
self.assertEqual(number_of_sources, 1)
|
|
|
|
audit_log_entry = RealmAuditLog.objects.order_by('-id') \
|
|
|
|
.values_list('acting_user', 'event_type').first()
|
|
|
|
self.assertEqual(audit_log_entry, (user.id, RealmAuditLog.STRIPE_CARD_CHANGED))
|
|
|
|
RealmAuditLog.objects.filter(acting_user=user).delete()
|
|
|
|
|
|
|
|
# Try replacing with an invalid card
|
|
|
|
stripe_token = stripe_create_token(card_number='4000000000009987').id
|
|
|
|
with patch("corporate.lib.stripe.billing_logger.error") as mock_billing_logger:
|
2018-10-22 14:21:48 +02:00
|
|
|
response = self.client_post("/json/billing/sources/change",
|
2018-11-28 09:07:21 +01:00
|
|
|
{'stripe_token': ujson.dumps(stripe_token)})
|
|
|
|
mock_billing_logger.assert_called()
|
|
|
|
self.assertEqual(ujson.loads(response.content)['error_description'], 'card error')
|
|
|
|
self.assert_json_error_contains(response, 'Your card was declined')
|
|
|
|
number_of_sources = 0
|
|
|
|
for stripe_source in stripe_get_customer(Customer.objects.first().stripe_customer_id).sources:
|
|
|
|
self.assertEqual(cast(stripe.Card, stripe_source).last4, '4444')
|
|
|
|
number_of_sources += 1
|
|
|
|
self.assertEqual(number_of_sources, 1)
|
|
|
|
self.assertFalse(RealmAuditLog.objects.filter(event_type=RealmAuditLog.STRIPE_CARD_CHANGED).exists())
|
2018-10-22 14:21:48 +02:00
|
|
|
|
2018-11-01 11:26:29 +01:00
|
|
|
class RequiresBillingAccessTest(ZulipTestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
hamlet = self.example_user("hamlet")
|
|
|
|
hamlet.is_billing_admin = True
|
|
|
|
hamlet.save(update_fields=["is_billing_admin"])
|
|
|
|
|
|
|
|
# mocked_function_name will typically be something imported from
|
|
|
|
# stripe.py. In theory we could have endpoints that need to mock
|
|
|
|
# multiple functions, but we'll cross that bridge when we get there.
|
|
|
|
def _test_endpoint(self, url: str, mocked_function_name: str,
|
|
|
|
request_data: Optional[Dict[str, Any]]={}) -> None:
|
|
|
|
# Normal users do not have access
|
|
|
|
self.login(self.example_email('cordelia'))
|
|
|
|
response = self.client_post(url, request_data)
|
2018-11-01 22:17:36 +01:00
|
|
|
self.assert_json_error_contains(response, "Must be a billing administrator or an organization")
|
2018-11-01 11:26:29 +01:00
|
|
|
|
|
|
|
# Billing admins have access
|
|
|
|
self.login(self.example_email('hamlet'))
|
|
|
|
with patch("corporate.views.{}".format(mocked_function_name)) as mocked1:
|
|
|
|
response = self.client_post(url, request_data)
|
|
|
|
self.assert_json_success(response)
|
|
|
|
mocked1.assert_called()
|
|
|
|
|
|
|
|
# Realm admins have access, even if they are not billing admins
|
|
|
|
self.login(self.example_email('iago'))
|
|
|
|
with patch("corporate.views.{}".format(mocked_function_name)) as mocked2:
|
|
|
|
response = self.client_post(url, request_data)
|
|
|
|
self.assert_json_success(response)
|
|
|
|
mocked2.assert_called()
|
|
|
|
|
|
|
|
def test_json_endpoints(self) -> None:
|
|
|
|
params = [
|
|
|
|
("/json/billing/sources/change", "do_replace_payment_source",
|
|
|
|
{'stripe_token': ujson.dumps('token')}),
|
2018-12-12 07:47:53 +01:00
|
|
|
# TODO: second argument should be something like "process_downgrade"
|
2018-12-07 18:43:22 +01:00
|
|
|
("/json/billing/downgrade", "process_downgrade", {}),
|
2018-11-01 11:26:29 +01:00
|
|
|
] # type: List[Tuple[str, str, Dict[str, Any]]]
|
|
|
|
|
|
|
|
for (url, mocked_function_name, data) in params:
|
|
|
|
self._test_endpoint(url, mocked_function_name, data)
|
|
|
|
|
|
|
|
# Make sure that we are testing all the JSON endpoints
|
|
|
|
# Quite a hack, but probably fine for now
|
|
|
|
string_with_all_endpoints = str(get_resolver('corporate.urls').reverse_dict)
|
|
|
|
json_endpoints = set([word.strip("\"'()[],$") for word in string_with_all_endpoints.split()
|
|
|
|
if 'json' in word])
|
2018-12-07 18:43:22 +01:00
|
|
|
# No need to test upgrade endpoint as it only requires user to be logged in.
|
|
|
|
json_endpoints.remove("json/billing/upgrade")
|
|
|
|
|
2018-11-01 11:26:29 +01:00
|
|
|
self.assertEqual(len(json_endpoints), len(params))
|
2018-12-15 09:33:25 +01:00
|
|
|
|
|
|
|
class BillingHelpersTest(ZulipTestCase):
|
|
|
|
def test_next_month(self) -> None:
|
|
|
|
anchor = datetime(2019, 12, 31, 1, 2, 3).replace(tzinfo=timezone_utc)
|
|
|
|
period_boundaries = [
|
|
|
|
anchor,
|
|
|
|
datetime(2020, 1, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
# Test that this is the 28th even during leap years
|
|
|
|
datetime(2020, 2, 28, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 3, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 4, 30, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 5, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 6, 30, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 7, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 8, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 9, 30, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 10, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 11, 30, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2020, 12, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2021, 1, 31, 1, 2, 3).replace(tzinfo=timezone_utc),
|
|
|
|
datetime(2021, 2, 28, 1, 2, 3).replace(tzinfo=timezone_utc)]
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
add_months(anchor, -1)
|
|
|
|
# Explictly test add_months for each value of MAX_DAY_FOR_MONTH and
|
|
|
|
# for crossing a year boundary
|
|
|
|
for i, boundary in enumerate(period_boundaries):
|
|
|
|
self.assertEqual(add_months(anchor, i), boundary)
|
|
|
|
# Test next_month for small values
|
|
|
|
for last, next_ in zip(period_boundaries[:-1], period_boundaries[1:]):
|
|
|
|
self.assertEqual(next_month(anchor, last), next_)
|
|
|
|
# Test next_month for large values
|
|
|
|
period_boundaries = [dt.replace(year=dt.year+100) for dt in period_boundaries]
|
|
|
|
for last, next_ in zip(period_boundaries[:-1], period_boundaries[1:]):
|
|
|
|
self.assertEqual(next_month(anchor, last), next_)
|
|
|
|
|
|
|
|
def test_compute_plan_parameters(self) -> None:
|
|
|
|
# TODO: test rounding down microseconds
|
|
|
|
anchor = datetime(2019, 12, 31, 1, 2, 3).replace(tzinfo=timezone_utc)
|
|
|
|
month_later = datetime(2020, 1, 31, 1, 2, 3).replace(tzinfo=timezone_utc)
|
|
|
|
year_later = datetime(2020, 12, 31, 1, 2, 3).replace(tzinfo=timezone_utc)
|
|
|
|
test_cases = [
|
|
|
|
# TODO test with Decimal(85), not 85
|
|
|
|
# TODO fix the mypy error by specifying the exact type
|
|
|
|
# test all possibilities, since there aren't that many
|
|
|
|
[(True, CustomerPlan.ANNUAL, None), (anchor, month_later, year_later, 8000)], # lint:ignore
|
|
|
|
[(True, CustomerPlan.ANNUAL, 85), (anchor, month_later, year_later, 1200)], # lint:ignore
|
|
|
|
[(True, CustomerPlan.MONTHLY, None), (anchor, month_later, month_later, 800)], # lint:ignore
|
|
|
|
[(True, CustomerPlan.MONTHLY, 85), (anchor, month_later, month_later, 120)], # lint:ignore
|
|
|
|
[(False, CustomerPlan.ANNUAL, None), (anchor, year_later, year_later, 8000)], # lint:ignore
|
|
|
|
[(False, CustomerPlan.ANNUAL, 85), (anchor, year_later, year_later, 1200)], # lint:ignore
|
|
|
|
[(False, CustomerPlan.MONTHLY, None), (anchor, month_later, month_later, 800)], # lint:ignore
|
|
|
|
[(False, CustomerPlan.MONTHLY, 85), (anchor, month_later, month_later, 120)], # lint:ignore
|
|
|
|
# test exact math of Decimals; 800 * (1 - 87.25) = 101.9999999..
|
|
|
|
[(False, CustomerPlan.MONTHLY, 87.25), (anchor, month_later, month_later, 102)],
|
|
|
|
# test dropping of fractional cents; without the int it's 102.8
|
|
|
|
[(False, CustomerPlan.MONTHLY, 87.15), (anchor, month_later, month_later, 102)]]
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=anchor):
|
|
|
|
for input_, output in test_cases:
|
|
|
|
output_ = compute_plan_parameters(*input_) # type: ignore # TODO
|
|
|
|
self.assertEqual(output_, output)
|
|
|
|
|
|
|
|
def test_update_or_create_stripe_customer_logic(self) -> None:
|
|
|
|
user = self.example_user('hamlet')
|
|
|
|
# No existing Customer object
|
2019-01-29 06:34:31 +01:00
|
|
|
with patch('corporate.lib.stripe.do_create_stripe_customer', return_value='returned') as mocked1:
|
2018-12-15 09:33:25 +01:00
|
|
|
returned = update_or_create_stripe_customer(user, stripe_token='token')
|
|
|
|
mocked1.assert_called()
|
|
|
|
self.assertEqual(returned, 'returned')
|
|
|
|
# Customer exists, replace payment source
|
|
|
|
Customer.objects.create(realm=get_realm('zulip'), stripe_customer_id='cus_12345')
|
|
|
|
with patch('corporate.lib.stripe.do_replace_payment_source') as mocked2:
|
|
|
|
customer = update_or_create_stripe_customer(self.example_user('hamlet'), 'token')
|
|
|
|
mocked2.assert_called()
|
|
|
|
self.assertTrue(isinstance(customer, Customer))
|
|
|
|
# Customer exists, do nothing
|
|
|
|
with patch('corporate.lib.stripe.do_replace_payment_source') as mocked3:
|
|
|
|
customer = update_or_create_stripe_customer(self.example_user('hamlet'), None)
|
|
|
|
mocked3.assert_not_called()
|
|
|
|
self.assertTrue(isinstance(customer, Customer))
|
2018-12-28 07:20:30 +01:00
|
|
|
|
2019-01-27 21:16:02 +01:00
|
|
|
class LicenseLedgerTest(StripeTestCase):
|
2018-12-28 07:20:30 +01:00
|
|
|
def test_add_plan_renewal_if_needed(self) -> None:
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 1)
|
|
|
|
plan = CustomerPlan.objects.get()
|
|
|
|
# Plan hasn't renewed yet
|
2019-01-26 20:45:26 +01:00
|
|
|
add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year - timedelta(days=1))
|
2018-12-28 07:20:30 +01:00
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 1)
|
|
|
|
# Plan needs to renew
|
|
|
|
# TODO: do_deactivate_user for a user, so that licenses_at_next_renewal != licenses
|
2019-01-26 20:45:26 +01:00
|
|
|
ledger_entry = add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year)
|
2018-12-28 07:20:30 +01:00
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 2)
|
|
|
|
ledger_params = {
|
|
|
|
'plan': plan, 'is_renewal': True, 'event_time': self.next_year,
|
|
|
|
'licenses': self.seat_count, 'licenses_at_next_renewal': self.seat_count}
|
|
|
|
for key, value in ledger_params.items():
|
|
|
|
self.assertEqual(getattr(ledger_entry, key), value)
|
|
|
|
# Plan needs to renew, but we already added the plan_renewal ledger entry
|
2019-01-26 20:45:26 +01:00
|
|
|
add_plan_renewal_to_license_ledger_if_needed(plan, self.next_year + timedelta(days=1))
|
2018-12-28 07:20:30 +01:00
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 2)
|
2019-01-26 02:36:37 +01:00
|
|
|
|
|
|
|
def test_update_license_ledger_if_needed(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
# Test no Customer
|
|
|
|
update_license_ledger_if_needed(realm, self.now)
|
|
|
|
self.assertFalse(LicenseLedger.objects.exists())
|
|
|
|
# Test plan not automanaged
|
|
|
|
self.local_upgrade(self.seat_count + 1, False, CustomerPlan.ANNUAL, 'token')
|
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 1)
|
|
|
|
update_license_ledger_if_needed(realm, self.now)
|
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 1)
|
|
|
|
# Test no active plan
|
|
|
|
plan = CustomerPlan.objects.get()
|
|
|
|
plan.automanage_licenses = True
|
|
|
|
plan.status = CustomerPlan.ENDED
|
|
|
|
plan.save(update_fields=['automanage_licenses', 'status'])
|
|
|
|
update_license_ledger_if_needed(realm, self.now)
|
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 1)
|
|
|
|
# Test update needed
|
|
|
|
plan.status = CustomerPlan.ACTIVE
|
|
|
|
plan.save(update_fields=['status'])
|
|
|
|
update_license_ledger_if_needed(realm, self.now)
|
|
|
|
self.assertEqual(LicenseLedger.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_update_license_ledger_for_automanaged_plan(self) -> None:
|
|
|
|
realm = get_realm('zulip')
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
# Simple increase
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=23):
|
|
|
|
update_license_ledger_for_automanaged_plan(realm, plan, self.now)
|
|
|
|
# Decrease
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=20):
|
|
|
|
update_license_ledger_for_automanaged_plan(realm, plan, self.now)
|
|
|
|
# Increase, but not past high watermark
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=21):
|
|
|
|
update_license_ledger_for_automanaged_plan(realm, plan, self.now)
|
|
|
|
# Increase, but after renewal date, and below last year's high watermark
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=22):
|
|
|
|
update_license_ledger_for_automanaged_plan(realm, plan, self.next_year + timedelta(seconds=1))
|
|
|
|
|
|
|
|
ledger_entries = list(LicenseLedger.objects.values_list(
|
|
|
|
'is_renewal', 'event_time', 'licenses', 'licenses_at_next_renewal').order_by('id'))
|
|
|
|
self.assertEqual(ledger_entries,
|
|
|
|
[(True, self.now, self.seat_count, self.seat_count),
|
|
|
|
(False, self.now, 23, 23),
|
|
|
|
(False, self.now, 23, 20),
|
|
|
|
(False, self.now, 23, 21),
|
|
|
|
(True, self.next_year, 21, 21),
|
|
|
|
(False, self.next_year + timedelta(seconds=1), 22, 22)])
|
|
|
|
|
|
|
|
def test_user_changes(self) -> None:
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
user = do_create_user('email', 'password', get_realm('zulip'), 'name', 'name')
|
|
|
|
do_deactivate_user(user)
|
|
|
|
do_reactivate_user(user)
|
|
|
|
# Not a proper use of do_activate_user, but fine for this test
|
|
|
|
do_activate_user(user)
|
|
|
|
ledger_entries = list(LicenseLedger.objects.values_list(
|
|
|
|
'is_renewal', 'licenses', 'licenses_at_next_renewal').order_by('id'))
|
|
|
|
self.assertEqual(ledger_entries,
|
|
|
|
[(True, self.seat_count, self.seat_count),
|
|
|
|
(False, self.seat_count + 1, self.seat_count + 1),
|
|
|
|
(False, self.seat_count + 1, self.seat_count),
|
|
|
|
(False, self.seat_count + 1, self.seat_count + 1),
|
|
|
|
(False, self.seat_count + 1, self.seat_count + 1)])
|
2019-01-28 22:57:29 +01:00
|
|
|
|
|
|
|
class InvoiceTest(StripeTestCase):
|
|
|
|
def test_invoicing_status_is_started(self) -> None:
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
plan.invoicing_status = CustomerPlan.STARTED
|
|
|
|
plan.save(update_fields=['invoicing_status'])
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
invoice_plan(CustomerPlan.objects.first(), self.now)
|
|
|
|
|
|
|
|
@mock_stripe()
|
|
|
|
def test_invoice_plan(self, *mocks: Mock) -> None:
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login(user.email)
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.upgrade()
|
|
|
|
# Increase
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=self.seat_count + 3):
|
|
|
|
update_license_ledger_if_needed(get_realm('zulip'), self.now + timedelta(days=100))
|
|
|
|
# Decrease
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=self.seat_count):
|
|
|
|
update_license_ledger_if_needed(get_realm('zulip'), self.now + timedelta(days=200))
|
|
|
|
# Increase, but not past high watermark
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=self.seat_count + 1):
|
|
|
|
update_license_ledger_if_needed(get_realm('zulip'), self.now + timedelta(days=300))
|
|
|
|
# Increase, but after renewal date, and below last year's high watermark
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=self.seat_count + 2):
|
|
|
|
update_license_ledger_if_needed(get_realm('zulip'), self.now + timedelta(days=400))
|
|
|
|
# Increase, but after event_time
|
|
|
|
with patch('corporate.lib.stripe.get_seat_count', return_value=self.seat_count + 3):
|
|
|
|
update_license_ledger_if_needed(get_realm('zulip'), self.now + timedelta(days=500))
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
invoice_plan(plan, self.now + timedelta(days=400))
|
|
|
|
|
|
|
|
stripe_invoices = [invoice for invoice in stripe.Invoice.list(
|
|
|
|
customer=plan.customer.stripe_customer_id)]
|
|
|
|
self.assertEqual(len(stripe_invoices), 2)
|
|
|
|
self.assertIsNotNone(stripe_invoices[0].finalized_at)
|
|
|
|
stripe_line_items = [item for item in stripe_invoices[0].lines]
|
|
|
|
self.assertEqual(len(stripe_line_items), 3)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': int(8000 * (1 - ((400-366) / 365)) + .5),
|
|
|
|
'description': 'Additional license (Feb 5, 2013 - Jan 2, 2014)',
|
|
|
|
'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'start': datetime_to_timestamp(self.now + timedelta(days=400)),
|
|
|
|
'end': datetime_to_timestamp(self.now + timedelta(days=2*365 + 1))},
|
|
|
|
'quantity': 1}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[0].get(key), value)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': 8000 * (self.seat_count + 1),
|
|
|
|
'description': 'Zulip Standard - renewal',
|
|
|
|
'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'start': datetime_to_timestamp(self.now + timedelta(days=366)),
|
|
|
|
'end': datetime_to_timestamp(self.now + timedelta(days=2*365 + 1))},
|
|
|
|
'quantity': (self.seat_count + 1)}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[1].get(key), value)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': 3 * int(8000 * (366-100) / 366 + .5),
|
|
|
|
'description': 'Additional license (Apr 11, 2012 - Jan 2, 2013)',
|
|
|
|
'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'start': datetime_to_timestamp(self.now + timedelta(days=100)),
|
|
|
|
'end': datetime_to_timestamp(self.now + timedelta(days=366))},
|
|
|
|
'quantity': 3}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[2].get(key), value)
|
|
|
|
|
|
|
|
@mock_stripe()
|
|
|
|
def test_fixed_price_plans(self, *mocks: Mock) -> None:
|
|
|
|
# Also tests charge_automatically=False
|
|
|
|
user = self.example_user("hamlet")
|
|
|
|
self.login(user.email)
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.upgrade(invoice=True)
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
plan.fixed_price = 100
|
|
|
|
plan.price_per_license = 0
|
|
|
|
plan.save(update_fields=['fixed_price', 'price_per_license'])
|
|
|
|
invoice_plan(plan, self.next_year)
|
|
|
|
stripe_invoices = [invoice for invoice in stripe.Invoice.list(
|
|
|
|
customer=plan.customer.stripe_customer_id)]
|
|
|
|
self.assertEqual(len(stripe_invoices), 2)
|
|
|
|
self.assertEqual(stripe_invoices[0].billing, 'send_invoice')
|
|
|
|
stripe_line_items = [item for item in stripe_invoices[0].lines]
|
|
|
|
self.assertEqual(len(stripe_line_items), 1)
|
|
|
|
line_item_params = {
|
|
|
|
'amount': 100,
|
|
|
|
'description': 'Zulip Standard - renewal',
|
|
|
|
'discountable': False,
|
|
|
|
'period': {
|
|
|
|
'start': datetime_to_timestamp(self.next_year),
|
|
|
|
'end': datetime_to_timestamp(self.next_year + timedelta(days=365))},
|
|
|
|
'quantity': 1}
|
|
|
|
for key, value in line_item_params.items():
|
|
|
|
self.assertEqual(stripe_line_items[0].get(key), value)
|
|
|
|
|
|
|
|
def test_no_invoice_needed(self) -> None:
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
self.assertEqual(plan.next_invoice_date, self.next_month)
|
|
|
|
# Test this doesn't make any calls to stripe.Invoice or stripe.InvoiceItem
|
|
|
|
invoice_plan(plan, self.next_month)
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
# Test that we still update next_invoice_date
|
|
|
|
self.assertEqual(plan.next_invoice_date, self.next_month + timedelta(days=29))
|
|
|
|
|
|
|
|
def test_invoice_plans_as_needed(self) -> None:
|
|
|
|
with patch('corporate.lib.stripe.timezone_now', return_value=self.now):
|
|
|
|
self.local_upgrade(self.seat_count, True, CustomerPlan.ANNUAL, 'token')
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
self.assertEqual(plan.next_invoice_date, self.next_month)
|
|
|
|
# Test nothing needed to be done
|
|
|
|
with patch('corporate.lib.stripe.invoice_plan') as mocked:
|
|
|
|
invoice_plans_as_needed(self.next_month - timedelta(days=1))
|
|
|
|
mocked.assert_not_called()
|
|
|
|
# Test something needing to be done
|
|
|
|
invoice_plans_as_needed(self.next_month)
|
|
|
|
plan = CustomerPlan.objects.first()
|
|
|
|
self.assertEqual(plan.next_invoice_date, self.next_month + timedelta(days=29))
|