invites: _max_invites is currently never None.

dc1eeef30a made the column nullable, with the meaning for null of
"use the current `settings.INVITES_DEFAULT_REALM_DAILY_MAX`."
However, 8a95526ced switched to calling `do_change_plan_type` during
realm creation, which sets `realm.max_invites` based on the plan type,
thus ensuring that no new realms have their `_max_invites` set to
null.

Check `max_invites` instead of `_max_invites`.  This requires test
adjustments for the fact that `apply_invite_realm_heuristics` is now
run.
This commit is contained in:
Alex Vandiver 2023-03-07 19:14:09 +00:00 committed by Tim Abbott
parent 75138102f4
commit 36da7783ce
2 changed files with 25 additions and 18 deletions

View File

@ -1382,26 +1382,30 @@ class TestLoggingCountStats(AnalyticsTestCase):
stream, _ = self.create_stream_with_recipient()
invite_expires_in_minutes = 2 * 24 * 60
do_invite_users(
user,
["user1@domain.tld", "user2@domain.tld"],
[stream],
invite_expires_in_minutes=invite_expires_in_minutes,
)
with mock.patch("zerver.actions.invites.apply_invite_realm_heuristics"):
do_invite_users(
user,
["user1@domain.tld", "user2@domain.tld"],
[stream],
invite_expires_in_minutes=invite_expires_in_minutes,
)
assertInviteCountEquals(2)
# We currently send emails when re-inviting users that haven't
# turned into accounts, so count them towards the total
do_invite_users(
user,
["user1@domain.tld", "user2@domain.tld"],
[stream],
invite_expires_in_minutes=invite_expires_in_minutes,
)
with mock.patch("zerver.actions.invites.apply_invite_realm_heuristics"):
do_invite_users(
user,
["user1@domain.tld", "user2@domain.tld"],
[stream],
invite_expires_in_minutes=invite_expires_in_minutes,
)
assertInviteCountEquals(4)
# Test mix of good and malformed invite emails
with self.assertRaises(InvitationError):
with self.assertRaises(InvitationError), mock.patch(
"zerver.actions.invites.apply_invite_realm_heuristics"
):
do_invite_users(
user,
["user3@domain.tld", "malformed"],
@ -1411,7 +1415,9 @@ class TestLoggingCountStats(AnalyticsTestCase):
assertInviteCountEquals(4)
# Test inviting existing users
with self.assertRaises(InvitationError):
with self.assertRaises(InvitationError), mock.patch(
"zerver.actions.invites.apply_invite_realm_heuristics"
):
do_invite_users(
user,
["first@domain.tld", "user4@domain.tld"],
@ -1427,7 +1433,8 @@ class TestLoggingCountStats(AnalyticsTestCase):
assertInviteCountEquals(5)
# Resending invite should cost you
do_resend_user_invite_email(assert_is_not_none(PreregistrationUser.objects.first()))
with mock.patch("zerver.actions.invites.apply_invite_realm_heuristics"):
do_resend_user_invite_email(assert_is_not_none(PreregistrationUser.objects.first()))
assertInviteCountEquals(6)
def test_messages_read_hour(self) -> None:

View File

@ -82,10 +82,10 @@ def estimate_recent_invites(realms: Collection[Realm], *, days: int) -> int:
return recent_invites
def apply_invite_realm_heuristics(realm: Realm) -> None:
def apply_invite_realm_heuristics(realm: Realm, recent_invites: int, num_invitees: int) -> None:
if realm.plan_type != Realm.PLAN_TYPE_LIMITED:
return
if realm._max_invites is not None:
if realm.max_invites != settings.INVITES_DEFAULT_REALM_DAILY_MAX:
return
# If they're a non-paid plan with default invitation limits,
@ -160,7 +160,7 @@ def check_invite_limit(realm: Realm, num_invitees: int) -> None:
daily_limit_reached=True,
)
apply_invite_realm_heuristics(realm)
apply_invite_realm_heuristics(realm, recent_invites, num_invitees)
default_max = settings.INVITES_DEFAULT_REALM_DAILY_MAX
newrealm_age = datetime.timedelta(days=settings.INVITES_NEW_REALM_DAYS)