test_api.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. from unittest import mock
  2. from django.shortcuts import reverse
  3. from django.utils import timezone
  4. from freezegun import freeze_time
  5. from model_bakery import baker
  6. from glitchtip.test_utils.test_case import GlitchTipTestCase
  7. from glitchtip.uptime.models import Monitor
  8. class UptimeAPITestCase(GlitchTipTestCase):
  9. def setUp(self):
  10. self.create_user_and_project()
  11. self.list_url = reverse(
  12. "organization-monitors-list",
  13. kwargs={"organization_slug": self.organization.slug},
  14. )
  15. def test_list(self):
  16. monitor = baker.make(
  17. "uptime.Monitor", organization=self.organization, url="http://example.com"
  18. )
  19. baker.make(
  20. "uptime.MonitorCheck",
  21. monitor=monitor,
  22. is_up=False,
  23. start_check="2021-09-19T15:39:31Z",
  24. )
  25. baker.make(
  26. "uptime.MonitorCheck",
  27. monitor=monitor,
  28. is_up=True,
  29. start_check="2021-09-19T15:40:31Z",
  30. )
  31. res = self.client.get(self.list_url)
  32. self.assertContains(res, monitor.name)
  33. self.assertEqual(res.data[0]["isUp"], True)
  34. self.assertEqual(res.data[0]["lastChange"], "2021-09-19T15:39:31Z")
  35. def test_list_aggregation(self):
  36. """Test up and down event aggregations"""
  37. monitor = baker.make(
  38. "uptime.Monitor", organization=self.organization, url="http://example.com"
  39. )
  40. start_time = timezone.now()
  41. # Make 100 events, 50 up and then 50 up and down every minute
  42. for i in range(99):
  43. is_up = i % 2
  44. if i < 50:
  45. is_up = True
  46. current_time = start_time + timezone.timedelta(minutes=i)
  47. with freeze_time(current_time):
  48. baker.make(
  49. "uptime.MonitorCheck",
  50. monitor=monitor,
  51. is_up=is_up,
  52. start_check=current_time,
  53. )
  54. with freeze_time(current_time):
  55. res = self.client.get(self.list_url)
  56. self.assertEqual(len(res.data[0]["checks"]), 60)
  57. def test_create(self):
  58. data = {
  59. "monitorType": "Ping",
  60. "name": "Test",
  61. "url": "https://www.google.com",
  62. "expectedStatus": 200,
  63. "interval": "00:01:00",
  64. "project": self.project.pk,
  65. }
  66. res = self.client.post(self.list_url, data)
  67. self.assertEqual(res.status_code, 201)
  68. monitor = Monitor.objects.all().first()
  69. self.assertEqual(monitor.name, data["name"])
  70. self.assertEqual(monitor.organization, self.organization)
  71. self.assertEqual(monitor.project, self.project)
  72. def test_create_invalid(self):
  73. data = {
  74. "monitorType": "Ping",
  75. "name": "Test",
  76. "url": "",
  77. "expectedStatus": 200,
  78. "interval": "00:01:00",
  79. "project": self.project.pk,
  80. }
  81. res = self.client.post(self.list_url, data)
  82. self.assertEqual(res.status_code, 400)
  83. def test_create_expected_status(self):
  84. data = {
  85. "monitorType": "Ping",
  86. "name": "Test",
  87. "url": "http://example.com",
  88. "expectedStatus": None,
  89. "interval": "00:01:00",
  90. "project": self.project.pk,
  91. }
  92. res = self.client.post(self.list_url, data, format="json")
  93. self.assertEqual(res.status_code, 201)
  94. self.assertTrue(Monitor.objects.filter(expected_status=None).exists())
  95. def test_monitor_retrieve(self):
  96. """Test monitor details endpoint. Unlike the list view,
  97. checks here should include response time for the frontend graph"""
  98. environment = baker.make(
  99. "environments.Environment", organization=self.organization
  100. )
  101. monitor = baker.make(
  102. "uptime.Monitor",
  103. organization=self.organization,
  104. url="http://example.com",
  105. environment=environment,
  106. )
  107. baker.make(
  108. "uptime.MonitorCheck",
  109. monitor=monitor,
  110. is_up=False,
  111. start_check="2021-09-19T15:39:31Z",
  112. )
  113. baker.make(
  114. "uptime.MonitorCheck",
  115. monitor=monitor,
  116. is_up=True,
  117. start_check="2021-09-19T15:40:31Z",
  118. )
  119. url = reverse(
  120. "organization-monitors-detail",
  121. kwargs={"organization_slug": self.organization.slug, "pk": monitor.pk},
  122. )
  123. res = self.client.get(url)
  124. self.assertEqual(res.data["isUp"], True)
  125. self.assertEqual(res.data["lastChange"], "2021-09-19T15:39:31Z")
  126. self.assertEqual(res.data["environment"], environment.pk)
  127. self.assertTrue(res.data["checks"][0]["responseTime"])
  128. def test_monitor_checks_list(self):
  129. monitor = baker.make(
  130. "uptime.Monitor",
  131. organization=self.organization,
  132. url="http://example.com",
  133. )
  134. baker.make(
  135. "uptime.MonitorCheck",
  136. monitor=monitor,
  137. is_up=False,
  138. start_check="2021-09-19T15:39:31Z",
  139. )
  140. url = reverse(
  141. "organization-monitor-checks-list",
  142. kwargs={
  143. "organization_slug": self.organization.slug,
  144. "monitor_pk": monitor.pk,
  145. },
  146. )
  147. res = self.client.get(url)
  148. self.assertContains(res, "2021-09-19T15:39:31Z")
  149. def test_monitor_update(self):
  150. monitor = baker.make(
  151. "uptime.Monitor",
  152. organization=self.organization,
  153. url="http://example.com",
  154. interval="60",
  155. monitor_type="Ping",
  156. expected_status="200",
  157. )
  158. url = reverse(
  159. "organization-monitors-detail",
  160. kwargs={"organization_slug": self.organization.slug, "pk": monitor.pk},
  161. )
  162. data = {
  163. "name": "New name",
  164. "url": "https://differentexample.com",
  165. "monitorType": "GET",
  166. "expectedStatus": "200",
  167. "interval": "60",
  168. }
  169. res = self.client.put(url, data, format="json")
  170. self.assertEqual(res.data["monitorType"], "GET")
  171. self.assertEqual(res.data["url"], "https://differentexample.com")
  172. @mock.patch("glitchtip.uptime.tasks.perform_checks.run")
  173. def test_list_isolation(self, _):
  174. """Users should only access monitors in their organization"""
  175. user2 = baker.make("users.user")
  176. org2 = baker.make("organizations_ext.Organization")
  177. org2.add_user(user2)
  178. monitor1 = baker.make("uptime.Monitor", organization=self.organization)
  179. monitor2 = baker.make("uptime.Monitor", organization=org2)
  180. res = self.client.get(self.list_url)
  181. self.assertContains(res, monitor1.name)
  182. self.assertNotContains(res, monitor2.name)
  183. def test_create_isolation(self):
  184. """Users should only make monitors in their organization"""
  185. org2 = baker.make("organizations_ext.Organization")
  186. url = reverse(
  187. "organization-monitors-list",
  188. kwargs={"organization_slug": org2.slug},
  189. )
  190. data = {
  191. "monitorType": "Ping",
  192. "name": "Test",
  193. "url": "https://www.google.com",
  194. "expectedStatus": 200,
  195. "interval": "00:01:00",
  196. "project": self.project.pk,
  197. }
  198. res = self.client.post(url, data)
  199. self.assertEqual(res.status_code, 400)