test_emails.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import re
  2. from urllib.parse import urlencode
  3. from selenium.webdriver.common.by import By
  4. from sentry.testutils import AcceptanceTestCase
  5. from sentry.testutils.factories import get_fixture_path
  6. EMAILS = (
  7. ("/debug/mail/assigned/", "assigned"),
  8. ("/debug/mail/assigned/self/", "assigned self"),
  9. ("/debug/mail/note/", "note"),
  10. ("/debug/mail/regression/", "regression"),
  11. ("/debug/mail/regression/release/", "regression with version"),
  12. ("/debug/mail/new-release/", "release"),
  13. ("/debug/mail/resolved/", "resolved"),
  14. ("/debug/mail/resolved-in-release/", "resolved in release"),
  15. ("/debug/mail/resolved-in-release/upcoming/", "resolved in release upcoming"),
  16. ("/debug/mail/unassigned/", "unassigned"),
  17. ("/debug/mail/unable-to-fetch-commits/", "unable to fetch commits"),
  18. ("/debug/mail/unable-to-delete-repo/", "unable to delete repo"),
  19. ("/debug/mail/error-alert/", "alert"),
  20. ("/debug/mail/performance-alert/transaction-n-plus-one", "performance"),
  21. ("/debug/mail/performance-alert/transaction-n-plus-one-api-call/", "n1 api call"),
  22. ("/debug/mail/digest/", "digest"),
  23. ("/debug/mail/invalid-identity/", "invalid identity"),
  24. ("/debug/mail/invitation/", "invitation"),
  25. ("/debug/mail/mfa-added/", "mfa added"),
  26. ("/debug/mail/mfa-removed/", "mfa removed"),
  27. ("/debug/mail/recovery-codes-regenerated/", "recovery codes regenerated"),
  28. ("/debug/mail/password-changed/", "password changed"),
  29. ("/debug/mail/sso-linked", "sso linked"),
  30. ("/debug/mail/sso-unlinked", "sso unlinked"),
  31. ("/debug/mail/sso-unlinked/no-password", "sso unlinked without password"),
  32. )
  33. def read_txt_email_fixture(name: str) -> str:
  34. # "sso unlinked without password"
  35. # => "sso_unlinked_without_password.txt"
  36. filename = name.replace(" ", "_") + ".txt"
  37. with open(get_fixture_path("emails", filename)) as f:
  38. return f.read()
  39. def build_url(path: str, format: str = "html") -> str:
  40. return f"{path}?{urlencode({'format': format, 'seed': b'123', 'is_test': True})}"
  41. def redact_ids(text: str) -> str:
  42. issues_re = re.compile("(testserver/organizations/sentry/issues/[0-9]+/)")
  43. match = issues_re.search(text)
  44. if match:
  45. for g in match.groups():
  46. text = text.replace(g, "testserver/organizations/sentry/issues/x/")
  47. return text
  48. class EmailTestCase(AcceptanceTestCase):
  49. def setUp(self):
  50. super().setUp()
  51. # This email address is required to match FIXTURES.
  52. self.user = self.create_user("foo@example.com")
  53. self.login_as(self.user)
  54. def test_emails(self):
  55. for url, name in EMAILS:
  56. # HTML output is captured as a snapshot
  57. self.browser.get(build_url(url, "html"))
  58. self.browser.wait_until("#preview")
  59. self.browser.snapshot(f"{name} email html")
  60. # Text output is asserted against static fixture files
  61. self.browser.get(build_url(url, "txt"))
  62. self.browser.wait_until("#preview")
  63. elem = self.browser.find_element(by=By.CSS_SELECTOR, value="#preview pre")
  64. text_src = elem.get_attribute("innerHTML")
  65. # Avoid relying on IDs as this can cause flakey tests
  66. text_src = redact_ids(text_src)
  67. fixture_src = read_txt_email_fixture(name)
  68. assert fixture_src == text_src