-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
write tests for low balance email check, add indexes, reduce cuttoff …
…to 200 credits avoid hardcoding urls and dates
- Loading branch information
Showing
8 changed files
with
198 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import smtplib | ||
import sys | ||
import typing | ||
from email.mime.application import MIMEApplication | ||
from email.mime.multipart import MIMEMultipart | ||
|
@@ -12,6 +13,7 @@ | |
from daras_ai_v2 import settings | ||
from daras_ai_v2.settings import templates | ||
from gooey_ui import UploadedFile | ||
from routers.billing import account_url | ||
|
||
|
||
def send_reported_run_email( | ||
|
@@ -51,8 +53,9 @@ def send_low_balance_email( | |
recipeints = "[email protected], [email protected]" | ||
html_body = templates.get_template("low_balance_email.html").render( | ||
user=user, | ||
url="https://gooey.ai/account", | ||
url=account_url, | ||
total_credits_consumed=total_credits_consumed, | ||
settings=settings, | ||
) | ||
send_email_via_postmark( | ||
from_address=settings.SUPPORT_EMAIL, | ||
|
@@ -63,6 +66,10 @@ def send_low_balance_email( | |
) | ||
|
||
|
||
is_running_pytest = "pytest" in sys.modules | ||
pytest_outbox = [] | ||
|
||
|
||
def send_email_via_postmark( | ||
*, | ||
from_address: str, | ||
|
@@ -76,6 +83,21 @@ def send_email_via_postmark( | |
"outbound", "gooey-ai-workflows", "announcements" | ||
] = "outbound", | ||
): | ||
if is_running_pytest: | ||
pytest_outbox.append( | ||
dict( | ||
from_address=from_address, | ||
to_address=to_address, | ||
cc=cc, | ||
bcc=bcc, | ||
subject=subject, | ||
html_body=html_body, | ||
text_body=text_body, | ||
message_stream=message_stream, | ||
), | ||
) | ||
return | ||
|
||
r = requests.post( | ||
"https://api.postmarkapp.com/email", | ||
headers={ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
from django.utils import timezone | ||
|
||
from app_users.models import AppUserTransaction | ||
from bots.models import AppUser | ||
from celeryapp.tasks import run_low_balance_email_check | ||
from daras_ai_v2 import settings | ||
from daras_ai_v2.send_email import pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_feature_is_disabled(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=True, balance=0, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = False | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_is_not_paying(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=False, balance=0, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_has_enough_balance(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", is_paying=True, balance=500, is_anonymous=False | ||
) | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_dont_send_email_if_user_has_been_emailed_recently(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=66, | ||
is_anonymous=False, | ||
low_balance_email_sent_at=timezone.now(), | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_send_email_if_user_has_been_email_recently_but_made_a_purchase( | ||
transactional_db, | ||
): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=22, | ||
is_anonymous=False, | ||
low_balance_email_sent_at=timezone.now(), | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_1", | ||
user=user, | ||
amount=100, | ||
created_at=timezone.now(), | ||
end_balance=100, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_2", | ||
user=user, | ||
amount=-78, | ||
created_at=timezone.now(), | ||
end_balance=22, | ||
) | ||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
run_low_balance_email_check(user.uid) | ||
|
||
assert len(pytest_outbox) == 1 | ||
assert " 22" in pytest_outbox[0]["html_body"] | ||
assert " 78" in pytest_outbox[0]["html_body"] | ||
|
||
pytest_outbox.clear() | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox | ||
|
||
|
||
def test_send_email(transactional_db): | ||
user = AppUser.objects.create( | ||
uid="test_user", | ||
is_paying=True, | ||
balance=66, | ||
is_anonymous=False, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_1", | ||
user=user, | ||
amount=-100, | ||
created_at=timezone.now() - timezone.timedelta(days=2), | ||
end_balance=150 + 66, | ||
) | ||
AppUserTransaction.objects.create( | ||
invoice_id="test_invoice_2", | ||
user=user, | ||
amount=-150, | ||
created_at=timezone.now(), | ||
end_balance=66, | ||
) | ||
|
||
settings.LOW_BALANCE_EMAIL_ENABLED = True | ||
settings.LOW_BALANCE_EMAIL_DAYS = 1 | ||
settings.LOW_BALANCE_EMAIL_CREDITS = 100 | ||
|
||
run_low_balance_email_check(user.uid) | ||
assert len(pytest_outbox) == 1 | ||
body = pytest_outbox[0]["html_body"] | ||
assert " 66" in body | ||
assert " 150" in body | ||
assert " pm:unsubscribe" in body | ||
assert " 100" not in body | ||
|
||
pytest_outbox.clear() | ||
run_low_balance_email_check(user.uid) | ||
assert not pytest_outbox |