You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.4 KiB

  1. from hc.api.models import Check, Ping
  2. from hc.test import BaseTestCase
  3. PLAINTEXT_EMAIL = """Content-Type: multipart/alternative; boundary=bbb
  4. --bbb
  5. Content-Type: text/plain;charset=utf-8
  6. Content-Transfer-Encoding: base64
  7. aGVsbG8gd29ybGQ=
  8. --bbb
  9. """
  10. BAD_BASE64_EMAIL = """Content-Type: multipart/alternative; boundary=bbb
  11. --bbb
  12. Content-Type: text/plain;charset=utf-8
  13. Content-Transfer-Encoding: base64
  14. !!!
  15. --bbb
  16. """
  17. HTML_EMAIL = """Content-Type: multipart/alternative; boundary=bbb
  18. --bbb
  19. Content-Type: text/html;charset=utf-8
  20. Content-Transfer-Encoding: base64
  21. PGI+aGVsbG88L2I+
  22. --bbb
  23. """
  24. class PingDetailsTestCase(BaseTestCase):
  25. def setUp(self):
  26. super().setUp()
  27. self.check = Check.objects.create(project=self.project)
  28. self.url = "/checks/%s/last_ping/" % self.check.code
  29. def test_it_works(self):
  30. Ping.objects.create(owner=self.check, body="this is body")
  31. self.client.login(username="[email protected]", password="password")
  32. r = self.client.get(self.url)
  33. self.assertContains(r, "this is body", status_code=200)
  34. def test_it_requires_logged_in_user(self):
  35. Ping.objects.create(owner=self.check, body="this is body")
  36. r = self.client.get(self.url)
  37. self.assertRedirects(r, "/accounts/login/?next=" + self.url)
  38. def test_it_shows_fail(self):
  39. Ping.objects.create(owner=self.check, kind="fail")
  40. self.client.login(username="[email protected]", password="password")
  41. r = self.client.get(self.url)
  42. self.assertContains(r, "/fail", status_code=200)
  43. def test_it_shows_start(self):
  44. Ping.objects.create(owner=self.check, kind="start")
  45. self.client.login(username="[email protected]", password="password")
  46. r = self.client.get(self.url)
  47. self.assertContains(r, "/start", status_code=200)
  48. def test_it_accepts_n(self):
  49. # remote_addr, scheme, method, ua, body:
  50. self.check.ping("1.2.3.4", "http", "post", "tester", "foo-123", "success")
  51. self.check.ping("1.2.3.4", "http", "post", "tester", "bar-456", "success")
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get("/checks/%s/pings/1/" % self.check.code)
  54. self.assertContains(r, "foo-123", status_code=200)
  55. r = self.client.get("/checks/%s/pings/2/" % self.check.code)
  56. self.assertContains(r, "bar-456", status_code=200)
  57. def test_it_allows_cross_team_access(self):
  58. Ping.objects.create(owner=self.check, body="this is body")
  59. self.client.login(username="[email protected]", password="password")
  60. r = self.client.get(self.url)
  61. self.assertEqual(r.status_code, 200)
  62. def test_it_handles_missing_ping(self):
  63. self.client.login(username="[email protected]", password="password")
  64. r = self.client.get("/checks/%s/pings/123/" % self.check.code)
  65. self.assertContains(r, "No additional information is", status_code=200)
  66. def test_it_shows_nonzero_exitstatus(self):
  67. Ping.objects.create(owner=self.check, kind="fail", exitstatus=42)
  68. self.client.login(username="[email protected]", password="password")
  69. r = self.client.get(self.url)
  70. self.assertContains(r, "(failure, exit status 42)", status_code=200)
  71. def test_it_shows_zero_exitstatus(self):
  72. Ping.objects.create(owner=self.check, exitstatus=0)
  73. self.client.login(username="[email protected]", password="password")
  74. r = self.client.get(self.url)
  75. self.assertContains(r, "(exit status 0)", status_code=200)
  76. def test_it_decodes_plaintext_email_body(self):
  77. Ping.objects.create(owner=self.check, scheme="email", body=PLAINTEXT_EMAIL)
  78. self.client.login(username="[email protected]", password="password")
  79. r = self.client.get(self.url)
  80. self.assertContains(r, "email-body-plain", status_code=200)
  81. self.assertNotContains(r, "email-body-html")
  82. # aGVsbG8gd29ybGQ= is base64("hello world")
  83. self.assertContains(r, "aGVsbG8gd29ybGQ=")
  84. self.assertContains(r, "hello world")
  85. def test_it_handles_bad_base64_in_email_body(self):
  86. Ping.objects.create(owner=self.check, scheme="email", body=BAD_BASE64_EMAIL)
  87. self.client.login(username="[email protected]", password="password")
  88. r = self.client.get(self.url)
  89. self.assertContains(r, "!!!", status_code=200)
  90. self.assertNotContains(r, "email-body-plain")
  91. self.assertNotContains(r, "email-body-html")
  92. def test_it_decodes_html_email_body(self):
  93. Ping.objects.create(owner=self.check, scheme="email", body=HTML_EMAIL)
  94. self.client.login(username="[email protected]", password="password")
  95. r = self.client.get(self.url)
  96. self.assertNotContains(r, "email-body-plain", status_code=200)
  97. self.assertContains(r, "email-body-html")
  98. # PGI+aGVsbG88L2I+ is base64("<b>hello</b>")
  99. self.assertContains(r, "PGI+aGVsbG88L2I+")
  100. self.assertContains(r, "&lt;b&gt;hello&lt;/b&gt;")
  101. def test_it_decodes_email_subject(self):
  102. Ping.objects.create(
  103. owner=self.check,
  104. scheme="email",
  105. body="Subject: =?UTF-8?B?aGVsbG8gd29ybGQ=?=",
  106. )
  107. self.client.login(username="[email protected]", password="password")
  108. r = self.client.get(self.url)
  109. # aGVsbG8gd29ybGQ= is base64("hello world")
  110. self.assertContains(r, "hello world", status_code=200)