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.

172 lines
6.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 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.slug = "alice-was-here"
  10. self.check.save()
  11. self.url = f"/projects/{self.project.code}/checks/"
  12. def test_it_works(self):
  13. for email in ("[email protected]", "[email protected]"):
  14. self.client.login(username=email, password="password")
  15. r = self.client.get(self.url)
  16. self.assertContains(r, "Alice Was Here", status_code=200)
  17. self.assertContains(r, str(self.check.code), status_code=200)
  18. # The pause button:
  19. self.assertContains(r, "btn btn-default pause", status_code=200)
  20. # last_active_date should have been set
  21. self.profile.refresh_from_db()
  22. self.assertTrue(self.profile.last_active_date)
  23. def test_it_bumps_last_active_date(self):
  24. self.profile.last_active_date = timezone.now() - td(days=10)
  25. self.profile.save()
  26. self.client.login(username="[email protected]", password="password")
  27. self.client.get(self.url)
  28. # last_active_date should have been bumped
  29. self.profile.refresh_from_db()
  30. delta = timezone.now() - self.profile.last_active_date
  31. self.assertTrue(delta.total_seconds() < 1)
  32. def test_it_updates_session(self):
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.get(self.url)
  35. self.assertEqual(r.status_code, 200)
  36. self.assertEqual(self.client.session["last_project_id"], self.project.id)
  37. def test_it_checks_access(self):
  38. self.client.login(username="[email protected]", password="password")
  39. r = self.client.get(self.url)
  40. self.assertEqual(r.status_code, 404)
  41. def test_it_shows_green_check(self):
  42. self.check.last_ping = timezone.now()
  43. self.check.status = "up"
  44. self.check.save()
  45. self.client.login(username="[email protected]", password="password")
  46. r = self.client.get(self.url)
  47. self.assertContains(r, "ic-up")
  48. def test_it_shows_red_check(self):
  49. self.check.last_ping = timezone.now() - td(days=3)
  50. self.check.status = "up"
  51. self.check.save()
  52. self.client.login(username="[email protected]", password="password")
  53. r = self.client.get(self.url)
  54. self.assertContains(r, "ic-down")
  55. def test_it_shows_amber_check(self):
  56. self.check.last_ping = timezone.now() - td(days=1, minutes=30)
  57. self.check.status = "up"
  58. self.check.save()
  59. self.client.login(username="[email protected]", password="password")
  60. r = self.client.get(self.url)
  61. self.assertContains(r, "ic-grace")
  62. def test_it_hides_add_check_button(self):
  63. self.profile.check_limit = 0
  64. self.profile.save()
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.get(self.url)
  67. self.assertContains(r, "Check limit reached", status_code=200)
  68. def test_it_saves_sort_field(self):
  69. self.client.login(username="[email protected]", password="password")
  70. self.client.get(self.url + "?sort=name")
  71. self.profile.refresh_from_db()
  72. self.assertEqual(self.profile.sort, "name")
  73. def test_it_ignores_bad_sort_value(self):
  74. self.client.login(username="[email protected]", password="password")
  75. self.client.get(self.url + "?sort=invalid")
  76. self.profile.refresh_from_db()
  77. self.assertEqual(self.profile.sort, "created")
  78. def test_it_shows_started_but_down_badge(self):
  79. self.check.last_start = timezone.now()
  80. self.check.tags = "foo"
  81. self.check.status = "down"
  82. self.check.save()
  83. self.client.login(username="[email protected]", password="password")
  84. r = self.client.get(self.url)
  85. self.assertContains(r, """<div class="btn btn-xs down ">foo</div>""")
  86. def test_it_shows_grace_badge(self):
  87. self.check.last_ping = timezone.now() - td(days=1, minutes=10)
  88. self.check.tags = "foo"
  89. self.check.status = "up"
  90. self.check.save()
  91. self.client.login(username="[email protected]", password="password")
  92. r = self.client.get(self.url)
  93. self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
  94. def test_it_shows_grace_started_badge(self):
  95. self.check.last_start = timezone.now()
  96. self.check.last_ping = timezone.now() - td(days=1, minutes=10)
  97. self.check.tags = "foo"
  98. self.check.status = "up"
  99. self.check.save()
  100. self.client.login(username="[email protected]", password="password")
  101. r = self.client.get(self.url)
  102. self.assertContains(r, """<div class="btn btn-xs grace ">foo</div>""")
  103. def test_it_hides_actions_from_readonly_users(self):
  104. self.bobs_membership.role = "r"
  105. self.bobs_membership.save()
  106. self.client.login(username="[email protected]", password="password")
  107. r = self.client.get(self.url)
  108. self.assertNotContains(r, "Add Check", status_code=200)
  109. # The pause button:
  110. self.assertNotContains(r, "btn btn-default pause", status_code=200)
  111. def test_it_shows_slugs(self):
  112. self.project.show_slugs = True
  113. self.project.save()
  114. self.client.login(username="[email protected]", password="password")
  115. r = self.client.get(self.url)
  116. self.assertContains(r, "alice-was-here")
  117. self.assertNotContains(r, "(not unique)")
  118. def test_it_shows_not_unique_note(self):
  119. self.project.show_slugs = True
  120. self.project.save()
  121. dupe = Check(project=self.project, name="Alice Was Here")
  122. dupe.slug = "alice-was-here"
  123. dupe.save()
  124. self.client.login(username="[email protected]", password="password")
  125. r = self.client.get(self.url)
  126. self.assertContains(r, "alice-was-here")
  127. self.assertContains(r, "(not unique)")
  128. def test_it_saves_url_format_preference(self):
  129. self.client.login(username="[email protected]", password="password")
  130. self.client.get(self.url + "?urls=slug")
  131. self.project.refresh_from_db()
  132. self.assertTrue(self.project.show_slugs)