From a37558b021235b1e15423d5f96524b51b659eb16 Mon Sep 17 00:00:00 2001 From: Rishi Gupta Date: Wed, 30 Jan 2019 10:04:32 -0800 Subject: [PATCH] billing: Update get_seat_count to incorporate guests. --- corporate/lib/stripe.py | 7 ++++++- corporate/tests/test_stripe.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/corporate/lib/stripe.py b/corporate/lib/stripe.py index 0693e98913..72dc35e5de 100644 --- a/corporate/lib/stripe.py +++ b/corporate/lib/stripe.py @@ -2,6 +2,7 @@ from datetime import datetime from decimal import Decimal from functools import wraps import logging +import math import os from typing import Any, Callable, Dict, Optional, TypeVar, Tuple, cast import ujson @@ -39,7 +40,11 @@ MIN_INVOICED_LICENSES = 30 DEFAULT_INVOICE_DAYS_UNTIL_DUE = 30 def get_seat_count(realm: Realm) -> int: - return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count() + non_guests = UserProfile.objects.filter( + realm=realm, is_active=True, is_bot=False, is_guest=False).count() + guests = UserProfile.objects.filter( + realm=realm, is_active=True, is_bot=False, is_guest=True).count() + return max(non_guests, math.ceil(guests / 5)) def sign_string(string: str) -> Tuple[str, str]: salt = generate_random_token(64) diff --git a/corporate/tests/test_stripe.py b/corporate/tests/test_stripe.py index 3289a9d894..9cba751621 100644 --- a/corporate/tests/test_stripe.py +++ b/corporate/tests/test_stripe.py @@ -735,6 +735,21 @@ class StripeTest(StripeTestCase): do_deactivate_user(user2) self.assertEqual(get_seat_count(realm), initial_count) + # Test guests + # Adding a guest to a realm with a lot of members shouldn't change anything + UserProfile.objects.create(realm=realm, email='user3@zulip.com', pointer=-1, is_guest=True) + self.assertEqual(get_seat_count(realm), initial_count) + # Test 1 member and 5 guests + realm = Realm.objects.create(string_id='second', name='second') + UserProfile.objects.create(realm=realm, email='member@second.com', pointer=-1) + for i in range(5): + UserProfile.objects.create(realm=realm, email='guest{}@second.com'.format(i), + pointer=-1, is_guest=True) + self.assertEqual(get_seat_count(realm), 1) + # Test 1 member and 6 guests + UserProfile.objects.create(realm=realm, email='guest5@second.com', pointer=-1, is_guest=True) + self.assertEqual(get_seat_count(realm), 2) + def test_sign_string(self) -> None: string = "abc" signed_string, salt = sign_string(string)