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.

98 lines
3.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
  1. import json
  2. from hc.api.models import Channel, Check, Notification, Ping
  3. from hc.test import BaseTestCase
  4. class LogTestCase(BaseTestCase):
  5. def setUp(self):
  6. super(LogTestCase, self).setUp()
  7. self.check = Check.objects.create(project=self.project)
  8. ping = Ping.objects.create(owner=self.check)
  9. # Older MySQL versions don't store microseconds. This makes sure
  10. # the ping is older than any notifications we may create later:
  11. ping.created = "2000-01-01T00:00:00+00:00"
  12. ping.save()
  13. self.url = "/checks/%s/log/" % self.check.code
  14. def test_it_works(self):
  15. self.client.login(username="[email protected]", password="password")
  16. r = self.client.get(self.url)
  17. self.assertContains(r, "Local Time", status_code=200)
  18. def test_team_access_works(self):
  19. # Logging in as bob, not alice. Bob has team access so this
  20. # should work.
  21. self.client.login(username="[email protected]", password="password")
  22. r = self.client.get(self.url)
  23. self.assertEqual(r.status_code, 200)
  24. def test_it_handles_bad_uuid(self):
  25. url = "/checks/not-uuid/log/"
  26. self.client.login(username="[email protected]", password="password")
  27. r = self.client.get(url)
  28. self.assertEqual(r.status_code, 404)
  29. def test_it_handles_missing_uuid(self):
  30. # Valid UUID but there is no check for it:
  31. url = "/checks/6837d6ec-fc08-4da5-a67f-08a9ed1ccf62/log/"
  32. self.client.login(username="[email protected]", password="password")
  33. r = self.client.get(url)
  34. self.assertEqual(r.status_code, 404)
  35. def test_it_checks_ownership(self):
  36. self.client.login(username="[email protected]", password="password")
  37. r = self.client.get(self.url)
  38. self.assertEqual(r.status_code, 404)
  39. def test_it_shows_email_notification(self):
  40. ch = Channel(kind="email", project=self.project)
  41. ch.value = json.dumps({
  42. "value": "[email protected]",
  43. "up": True,
  44. "down": True
  45. })
  46. ch.save()
  47. Notification(owner=self.check, channel=ch, check_status="down").save()
  48. self.client.login(username="[email protected]", password="password")
  49. r = self.client.get(self.url)
  50. self.assertContains(r, "Sent email alert to [email protected]",
  51. status_code=200)
  52. def test_it_shows_pushover_notification(self):
  53. ch = Channel.objects.create(kind="po", project=self.project)
  54. Notification(owner=self.check, channel=ch, check_status="down").save()
  55. self.client.login(username="[email protected]", password="password")
  56. r = self.client.get(self.url)
  57. self.assertContains(r, "Sent a Pushover notification", status_code=200)
  58. def test_it_shows_webhook_notification(self):
  59. ch = Channel(kind="webhook", project=self.project)
  60. ch.value = "foo/$NAME"
  61. ch.save()
  62. Notification(owner=self.check, channel=ch, check_status="down").save()
  63. self.client.login(username="[email protected]", password="password")
  64. r = self.client.get(self.url)
  65. self.assertContains(r, "Called webhook foo/$NAME", status_code=200)
  66. def test_it_allows_cross_team_access(self):
  67. self.bobs_profile.current_project = None
  68. self.bobs_profile.save()
  69. self.client.login(username="[email protected]", password="password")
  70. r = self.client.get(self.url)
  71. self.assertEqual(r.status_code, 200)