test_api.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. environment = baker.make(
  97. "environments.Environment", organization=self.organization
  98. )
  99. monitor = baker.make(
  100. "uptime.Monitor",
  101. organization=self.organization,
  102. url="http://example.com",
  103. environment=environment,
  104. )
  105. baker.make(
  106. "uptime.MonitorCheck",
  107. monitor=monitor,
  108. is_up=False,
  109. start_check="2021-09-19T15:39:31Z",
  110. )
  111. baker.make(
  112. "uptime.MonitorCheck",
  113. monitor=monitor,
  114. is_up=True,
  115. start_check="2021-09-19T15:40:31Z",
  116. )
  117. url = reverse(
  118. "organization-monitors-detail",
  119. kwargs={"organization_slug": self.organization.slug, "pk": monitor.pk},
  120. )
  121. res = self.client.get(url)
  122. self.assertEqual(res.data["isUp"], True)
  123. self.assertEqual(res.data["lastChange"], "2021-09-19T15:39:31Z")
  124. self.assertEqual(res.data["environment"], environment.pk)
  125. def test_monitor_checks_list(self):
  126. monitor = baker.make(
  127. "uptime.Monitor",
  128. organization=self.organization,
  129. url="http://example.com",
  130. )
  131. baker.make(
  132. "uptime.MonitorCheck",
  133. monitor=monitor,
  134. is_up=False,
  135. start_check="2021-09-19T15:39:31Z",
  136. )
  137. url = reverse(
  138. "organization-monitor-checks-list",
  139. kwargs={
  140. "organization_slug": self.organization.slug,
  141. "monitor_pk": monitor.pk,
  142. },
  143. )
  144. res = self.client.get(url)
  145. self.assertContains(res, "2021-09-19T15:39:31Z")
  146. def test_monitor_update(self):
  147. monitor = baker.make(
  148. "uptime.Monitor",
  149. organization=self.organization,
  150. url="http://example.com",
  151. interval="60",
  152. monitor_type="Ping",
  153. expected_status="200",
  154. )
  155. url = reverse(
  156. "organization-monitors-detail",
  157. kwargs={"organization_slug": self.organization.slug, "pk": monitor.pk},
  158. )
  159. data = {
  160. "name": "New name",
  161. "url": "https://differentexample.com",
  162. "monitorType": "GET",
  163. "expectedStatus": "200",
  164. "interval": "60",
  165. }
  166. res = self.client.put(url, data, format="json")
  167. self.assertEqual(res.data["monitorType"], "GET")
  168. self.assertEqual(res.data["url"], "https://differentexample.com")
  169. @mock.patch("glitchtip.uptime.tasks.perform_checks.run")
  170. def test_list_isolation(self, _):
  171. """Users should only access monitors in their organization"""
  172. user2 = baker.make("users.user")
  173. org2 = baker.make("organizations_ext.Organization")
  174. org2.add_user(user2)
  175. monitor1 = baker.make("uptime.Monitor", organization=self.organization)
  176. monitor2 = baker.make("uptime.Monitor", organization=org2)
  177. res = self.client.get(self.list_url)
  178. self.assertContains(res, monitor1.name)
  179. self.assertNotContains(res, monitor2.name)
  180. def test_create_isolation(self):
  181. """Users should only make monitors in their organization"""
  182. org2 = baker.make("organizations_ext.Organization")
  183. url = reverse(
  184. "organization-monitors-list",
  185. kwargs={"organization_slug": org2.slug},
  186. )
  187. data = {
  188. "monitorType": "Ping",
  189. "name": "Test",
  190. "url": "https://www.google.com",
  191. "expectedStatus": 200,
  192. "interval": "00:01:00",
  193. "project": self.project.pk,
  194. }
  195. res = self.client.post(url, data)
  196. self.assertEqual(res.status_code, 400)