test_api.py 6.3 KB

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