test_emails.py 3.4 KB

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