Files
sentinel/tests/sentry/api/endpoints/test_user_notification_email.py
2024-02-15 17:01:50 +00:00

77 lines
2.8 KiB
Python

from sentry.models.options.user_option import UserOption
from sentry.models.useremail import UserEmail
from sentry.testutils.cases import APITestCase
from sentry.testutils.silo import control_silo_test
class UserNotificationEmailTestBase(APITestCase):
endpoint = "sentry-api-0-user-notifications-email"
def setUp(self):
self.organization2 = self.create_organization(name="Another Org", owner=self.user)
self.project2 = self.create_project(
organization=self.organization, teams=[self.team], name="Another Name"
)
self.login_as(user=self.user)
@control_silo_test
class UserNotificationEmailGetTest(UserNotificationEmailTestBase):
def test_populates_useroptions_for_email(self):
UserEmail.objects.create(user=self.user, email="[email protected]", is_verified=True).save()
UserEmail.objects.create(
user=self.user, email="[email protected]", is_verified=True
).save()
UserOption.objects.set_value(
user=self.user, project=self.project, key="mail:email", value="[email protected]"
)
UserOption.objects.set_value(
user=self.user, project=self.project2, key="mail:email", value="[email protected]"
)
response = self.get_success_response("me")
assert response.data == {
str(self.project.id): "[email protected]",
str(self.project2.id): "[email protected]",
}
@control_silo_test()
class UserNotificationEmailTest(UserNotificationEmailTestBase):
method = "put"
def test_saves_and_returns_email_routing(self):
UserEmail.objects.create(user=self.user, email="[email protected]", is_verified=True).save()
email = self.user.email
data = {str(self.project.id): email, str(self.project2.id): "[email protected]"}
self.get_success_response("me", status_code=204, **data)
value1 = UserOption.objects.get(
user=self.user, project_id=self.project.id, key="mail:email"
).value
value2 = UserOption.objects.get(
user=self.user, project_id=self.project2.id, key="mail:email"
).value
assert value1 == email
assert value2 == "[email protected]"
def test_email_routing_emails_must_be_verified(self):
UserEmail.objects.create(
user=self.user, email="[email protected]", is_verified=False
).save()
data = {str(self.project.id): "[email protected]"}
self.get_error_response("me", status_code=400, **data)
def test_email_routing_emails_must_be_valid(self):
new_user = self.create_user(email="[email protected]")
UserEmail.objects.create(user=new_user, email="[email protected]", is_verified=True).save()
data = {str(self.project2.id): "[email protected]"}
self.get_error_response("me", status_code=400, **data)