test_emails.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import os.path
  2. from os.path import dirname, join
  3. from urllib.parse import urlencode
  4. from sentry.testutils import AcceptanceTestCase
  5. EMAILS = (
  6. ("/debug/mail/assigned/", "assigned"),
  7. ("/debug/mail/assigned/self/", "assigned self"),
  8. ("/debug/mail/note/", "note"),
  9. ("/debug/mail/regression/", "regression"),
  10. ("/debug/mail/regression/release/", "regression with version"),
  11. ("/debug/mail/new-release/", "release"),
  12. ("/debug/mail/resolved/", "resolved"),
  13. ("/debug/mail/resolved-in-release/", "resolved in release"),
  14. ("/debug/mail/resolved-in-release/upcoming/", "resolved in release upcoming"),
  15. ("/debug/mail/unassigned/", "unassigned"),
  16. ("/debug/mail/unable-to-fetch-commits/", "unable to fetch commits"),
  17. ("/debug/mail/unable-to-delete-repo/", "unable to delete repo"),
  18. ("/debug/mail/alert/", "alert"),
  19. ("/debug/mail/digest/", "digest"),
  20. ("/debug/mail/invalid-identity/", "invalid identity"),
  21. ("/debug/mail/invitation/", "invitation"),
  22. ("/debug/mail/report/", "report"),
  23. ("/debug/mail/mfa-added/", "mfa added"),
  24. ("/debug/mail/mfa-removed/", "mfa removed"),
  25. ("/debug/mail/recovery-codes-regenerated/", "recovery codes regenerated"),
  26. ("/debug/mail/password-changed/", "password changed"),
  27. ("/debug/mail/sso-linked", "sso linked"),
  28. ("/debug/mail/sso-unlinked", "sso unlinked"),
  29. ("/debug/mail/sso-unlinked/no-password", "sso unlinked without password"),
  30. )
  31. def read_txt_email_fixture(name: str) -> str:
  32. # "sso unlinked without password"
  33. # => "sso_unlinked_without_password.txt"
  34. filename = name.replace(" ", "_") + ".txt"
  35. path = join(dirname(__file__), os.pardir, "fixtures", "emails", filename)
  36. with open(path) as f:
  37. return f.read()
  38. def build_url(path: str, format: str = "html") -> str:
  39. return f"{path}?{urlencode({'format': format, 'seed': b'123'})}"
  40. class EmailTestCase(AcceptanceTestCase):
  41. def setUp(self):
  42. super().setUp()
  43. # This email address is required to match FIXTURES.
  44. self.user = self.create_user("foo@example.com")
  45. self.login_as(self.user)
  46. def test_emails(self):
  47. for url, name in EMAILS:
  48. # HTML output is captured as a snapshot
  49. self.browser.get(build_url(url, "html"))
  50. self.browser.wait_until("#preview")
  51. self.browser.snapshot(f"{name} email html")
  52. # Text output is asserted against static fixture files
  53. self.browser.get(build_url(url, "txt"))
  54. self.browser.wait_until("#preview")
  55. elem = self.browser.find_element_by_css_selector("#preview pre")
  56. text_src = elem.get_attribute("innerHTML")
  57. fixture_src = read_txt_email_fixture(name)
  58. assert fixture_src == text_src