Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

470 prefect webhook emails should only go to admins #478

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion ddpui/tests/api_tests/test_webhook_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
email_flowrun_logs_to_orgusers,
)
from ddpui.models.org import Org, OrgPrefectBlock
from ddpui.models.org_user import OrgUser, User, OrgUserRole
from ddpui.models.org_user import OrgUser, User, OrgUserRole, UserAttributes
from ddpui.settings import PRODUCTION

pytestmark = pytest.mark.django_db
Expand Down Expand Up @@ -126,6 +126,7 @@ def test_email_orgusers():
org = Org.objects.create(name="temp", slug="temp")
user = User.objects.create(username="username", email="useremail")
OrgUser.objects.create(org=org, role=OrgUserRole.ACCOUNT_MANAGER, user=user)
UserAttributes.objects.create(user=user, is_platform_admin=True)
with patch(
"ddpui.utils.webhook_helpers.send_text_message"
) as mock_send_text_message:
Expand All @@ -135,6 +136,18 @@ def test_email_orgusers():
mock_send_text_message.assert_called_once_with("useremail", subject, "hello")


def test_email_orgusers_not_non_admins():
"""tests the email_orgusers function"""
org = Org.objects.create(name="temp", slug="temp")
user = User.objects.create(username="username", email="useremail")
OrgUser.objects.create(org=org, role=OrgUserRole.ACCOUNT_MANAGER, user=user)
with patch(
"ddpui.utils.webhook_helpers.send_text_message"
) as mock_send_text_message:
email_orgusers(org, "hello")
mock_send_text_message.assert_not_called()


def test_email_orgusers_not_to_report_viewers():
"""tests the email_orgusers function"""
org = Org.objects.create(name="temp", slug="temp")
Expand Down
8 changes: 6 additions & 2 deletions ddpui/utils/webhook_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ddpui.utils.awsses import send_text_message
from ddpui.ddpprefect import prefect_service
from ddpui.settings import PRODUCTION
from ddpui.models.org_user import UserAttributes

logger = CustomLogger("ddpui")

Expand Down Expand Up @@ -97,8 +98,11 @@ def email_orgusers(org: Org, email_body: str):
OrgUserRole.PIPELINE_MANAGER,
],
):
logger.info(f"sending prefect-notification email to {orguser.user.email}")
send_text_message(orguser.user.email, subject, email_body)
if UserAttributes.objects.filter(
user=orguser.user, is_platform_admin=True
).exists():
logger.info(f"sending prefect-notification email to {orguser.user.email}")
send_text_message(orguser.user.email, subject, email_body)


def email_flowrun_logs_to_orgusers(org: Org, flow_run_id: str):
Expand Down
Loading