test_api.py 6.7 KB

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