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.

141 lines
5.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
  1. from hc.api.models import Check
  2. from hc.test import BaseTestCase
  3. from datetime import timedelta as td
  4. from django.utils import timezone
  5. class MyChecksTestCase(BaseTestCase):
  6. def setUp(self):
  7. super().setUp()
  8. self.check = Check(project=self.project, name="Alice Was Here")
  9. self.check.save()
  10. self.url = "/projects/%s/checks/" % self.project.code
  11. def test_it_works(self):
  12. for email in ("[email protected]", "[email protected]"):
  13. self.client.login(username=email, password="password")
  14. r = self.client.get(self.url)
  15. self.assertContains(r, "Alice Was Here", status_code=200)
  16. # The pause button:
  17. self.assertContains(r, "btn btn-default pause", status_code=200)
  18. # last_active_date should have been set
  19. self.profile.refresh_from_db()
  20. self.assertTrue(self.profile.last_active_date)
  21. def test_it_bumps_last_active_date(self):
  22. self.profile.last_active_date = timezone.now() - td(days=10)
  23. self.profile.save()
  24. self.client.login(username="[email protected]", password="password")
  25. self.client.get(self.url)
  26. # last_active_date should have been bumped
  27. self.profile.refresh_from_db()
  28. delta = timezone.now() - self.profile.last_active_date
  29. self.assertTrue(delta.total_seconds() < 1)
  30. def test_it_updates_session(self):
  31. self.client.login(username="[email protected]", password="password")
  32. r = self.client.get(self.url)
  33. self.assertEqual(r.status_code, 200)
  34. self.assertEqual(self.client.session["last_project_id"], self.project.id)
  35. def test_it_checks_access(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_green_check(self):
  40. self.check.last_ping = timezone.now()
  41. self.check.status = "up"
  42. self.check.save()
  43. self.client.login(username="[email protected]", password="password")
  44. r = self.client.get(self.url)
  45. self.assertContains(r, "icon-up")
  46. def test_it_shows_red_check(self):
  47. self.check.last_ping = timezone.now() - td(days=3)
  48. self.check.status = "up"
  49. self.check.save()
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.get(self.url)
  52. self.assertContains(r, "icon-down")
  53. def test_it_shows_amber_check(self):
  54. self.check.last_ping = timezone.now() - td(days=1, minutes=30)
  55. self.check.status = "up"
  56. self.check.save()
  57. self.client.login(username="[email protected]", password="password")
  58. r = self.client.get(self.url)
  59. self.assertContains(r, "icon-grace")
  60. def test_it_hides_add_check_button(self):
  61. self.profile.check_limit = 0
  62. self.profile.save()
  63. self.client.login(username="[email protected]", password="password")
  64. r = self.client.get(self.url)
  65. self.assertContains(r, "Check limit reached", status_code=200)
  66. def test_it_saves_sort_field(self):
  67. self.client.login(username="[email protected]", password="password")
  68. self.client.get(self.url + "?sort=name")
  69. self.profile.refresh_from_db()
  70. self.assertEqual(self.profile.sort, "name")
  71. def test_it_ignores_bad_sort_value(self):
  72. self.client.login(username="[email protected]", password="password")
  73. self.client.get(self.url + "?sort=invalid")
  74. self.profile.refresh_from_db()
  75. self.assertEqual(self.profile.sort, "created")
  76. def test_it_shows_started_but_down_badge(self):
  77. self.check.last_start = timezone.now()
  78. self.check.tags = "foo"
  79. self.check.status = "down"
  80. self.check.save()
  81. self.client.login(username="[email protected]", password="password")
  82. r = self.client.get(self.url)
  83. self.assertContains(r, """<div class="btn btn-xs down ">foo</div>""")
  84. def test_it_shows_grace_badge(self):
  85. self.check.last_ping = timezone.now() - td(days=1, minutes=10)
  86. self.check.tags = "foo"
  87. self.check.status = "up"
  88. self.check.save()
  89. self.client.login(username="[email protected]", password="password")
  90. r = self.client.get(self.url)
  91. self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
  92. def test_it_shows_grace_started_badge(self):
  93. self.check.last_start = timezone.now()
  94. self.check.last_ping = timezone.now() - td(days=1, minutes=10)
  95. self.check.tags = "foo"
  96. self.check.status = "up"
  97. self.check.save()
  98. self.client.login(username="[email protected]", password="password")
  99. r = self.client.get(self.url)
  100. self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
  101. def test_it_hides_actions_from_readonly_users(self):
  102. self.bobs_membership.rw = False
  103. self.bobs_membership.save()
  104. self.client.login(username="[email protected]", password="password")
  105. r = self.client.get(self.url)
  106. self.assertNotContains(r, "Add Check", status_code=200)
  107. # The pause button:
  108. self.assertNotContains(r, "btn btn-default pause", status_code=200)