test_emails.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from __future__ import absolute_import
  2. import six
  3. import os.path
  4. from six.moves.urllib.parse import urlencode
  5. from os.path import join, dirname
  6. from sentry.testutils import AcceptanceTestCase
  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/alert/", "alert"),
  21. ("/debug/mail/digest/", "digest"),
  22. ("/debug/mail/invalid-identity/", "invalid identity"),
  23. ("/debug/mail/invitation/", "invitation"),
  24. ("/debug/mail/report/", "report"),
  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):
  34. # XXX(python3): We have different fixtures for python2 vs python3 tests
  35. # because of the differences in how the random module works betwen 2 and 3.
  36. # NOTE that we _cannot_ just set the version of the seed, as there are more
  37. # differences. See [0].
  38. #
  39. # [0]: https://stackoverflow.com/questions/55647936/random-randint-shows-different-output-in-python-2-x-and-python-3-x-with-same-see/55648073
  40. version_suffix = "_py2" if six.PY2 else ""
  41. # "sso unlinked without password"
  42. # => "sso_unlinked_without_password.txt"
  43. filename = name.replace(" ", "_") + version_suffix + ".txt"
  44. path = join(dirname(__file__), os.pardir, "fixtures", "emails", filename)
  45. fixture = None
  46. with open(path, "r") as f:
  47. fixture = f.read()
  48. return fixture
  49. class EmailTestCase(AcceptanceTestCase):
  50. def setUp(self):
  51. super(EmailTestCase, self).setUp()
  52. self.user = self.create_user("foo@example.com")
  53. self.login_as(self.user)
  54. def build_url(self, path, format="html"):
  55. return u"{}?{}".format(path, urlencode({"format": format, "seed": b"123"}))
  56. def test_emails(self):
  57. for url, name in EMAILS:
  58. # HTML output is captured as a snapshot
  59. self.browser.get(self.build_url(url, "html"))
  60. self.browser.wait_until("#preview")
  61. self.browser.snapshot(u"{} email html".format(name))
  62. # Text output is asserted against static fixture files
  63. self.browser.get(self.build_url(url, "txt"))
  64. self.browser.wait_until("#preview")
  65. elem = self.browser.find_element_by_css_selector("#preview pre")
  66. text_src = elem.get_attribute("innerHTML")
  67. fixture_src = read_txt_email_fixture(name)
  68. assert fixture_src == text_src