test_api.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from django.urls import reverse
  2. from model_bakery import baker
  3. from apps.organizations_ext.constants import OrganizationUserRole
  4. from glitchtip.test_utils.test_case import GlitchTestCase
  5. from ..models import Release
  6. class ReleaseAPITestCase(GlitchTestCase):
  7. @classmethod
  8. def setUpTestData(cls):
  9. cls.create_user()
  10. def setUp(self):
  11. self.client.force_login(self.user)
  12. def test_create(self):
  13. url = reverse("api:create_release", args=[self.organization.slug])
  14. data = {"version": "1.0", "projects": [self.project.slug]}
  15. res = self.client.post(url, data, content_type="application/json")
  16. self.assertContains(res, data["version"], status_code=201)
  17. self.assertTrue(Release.objects.filter(version=data["version"]).exists())
  18. def test_list(self):
  19. url = reverse(
  20. "api:list_releases",
  21. kwargs={"organization_slug": self.organization.slug},
  22. )
  23. release1 = baker.make("releases.Release", organization=self.organization)
  24. release2 = baker.make("releases.Release")
  25. organization2 = baker.make("organizations_ext.Organization")
  26. organization2.add_user(self.user, OrganizationUserRole.ADMIN)
  27. release3 = baker.make("releases.Release", organization=organization2)
  28. res = self.client.get(url)
  29. self.assertContains(res, release1.version)
  30. self.assertNotContains(res, release2.version) # User not in org
  31. self.assertNotContains(res, release3.version) # Filtered our by url
  32. def test_retrieve(self):
  33. release = baker.make(
  34. "releases.Release", organization=self.organization, version="@1.1.1"
  35. )
  36. url = reverse(
  37. "api:get_release",
  38. kwargs={
  39. "organization_slug": self.organization.slug,
  40. "version": release.version,
  41. },
  42. )
  43. res = self.client.get(url)
  44. self.assertContains(res, release.version)
  45. def test_finalize(self):
  46. release = baker.make("releases.Release", organization=self.organization)
  47. url = reverse(
  48. "api:update_release",
  49. kwargs={
  50. "organization_slug": release.organization.slug,
  51. "version": release.version,
  52. },
  53. )
  54. data = {"dateReleased": "2021-09-04T14:08:57.388525996Z"}
  55. res = self.client.put(url, data, content_type="application/json")
  56. self.assertContains(res, data["dateReleased"][:14])
  57. def test_destroy_org_release(self):
  58. release1 = baker.make(
  59. "releases.Release", organization=self.organization, version="@1.1.1"
  60. )
  61. url = reverse(
  62. "api:delete_organization_release",
  63. kwargs={
  64. "organization_slug": release1.organization.slug,
  65. "version": release1.version,
  66. },
  67. )
  68. res = self.client.delete(url)
  69. self.assertEqual(res.status_code, 204)
  70. self.assertEqual(Release.objects.all().count(), 0)
  71. release2 = baker.make("releases.Release")
  72. url = reverse(
  73. "api:delete_organization_release",
  74. kwargs={
  75. "organization_slug": release2.organization.slug,
  76. "version": release2.version,
  77. },
  78. )
  79. res = self.client.delete(url)
  80. self.assertEqual(res.status_code, 404)
  81. self.assertEqual(Release.objects.all().count(), 1)
  82. def test_project_list(self):
  83. url = reverse(
  84. "api:list_project_releases",
  85. kwargs={
  86. "organization_slug": self.organization.slug,
  87. "project_slug": self.project.slug,
  88. },
  89. )
  90. project2 = baker.make("projects.Project", organization=self.organization)
  91. release1 = baker.make(
  92. "releases.Release",
  93. organization=self.organization,
  94. projects=[self.project, project2],
  95. )
  96. release2 = baker.make("releases.Release", organization=self.organization)
  97. res = self.client.get(url)
  98. self.assertContains(res, release1.version)
  99. self.assertNotContains(res, release2.version) # User not in project
  100. self.assertEqual(len(res.json()), 1)
  101. def test_finalize_project_release(self):
  102. release = baker.make(
  103. "releases.Release", organization=self.organization, projects=[self.project]
  104. )
  105. url = reverse(
  106. "api:update_project_release",
  107. kwargs={
  108. "organization_slug": release.organization.slug,
  109. "project_slug": self.project.slug,
  110. "version": release.version,
  111. },
  112. )
  113. data = {"dateReleased": "2021-09-04T14:08:57.388525996Z"}
  114. res = self.client.put(url, data, content_type="application/json")
  115. self.assertContains(res, data["dateReleased"][:14])
  116. def test_destroy_project_release(self):
  117. release = baker.make(
  118. "releases.Release",
  119. organization=self.organization,
  120. projects=[self.project],
  121. version="@1.1.1",
  122. )
  123. other_project = baker.make("projects.Project", organization=self.organization)
  124. url = reverse(
  125. "api:delete_project_release",
  126. kwargs={
  127. "organization_slug": release.organization.slug,
  128. "project_slug": other_project.slug,
  129. "version": release.version,
  130. },
  131. )
  132. res = self.client.delete(url)
  133. self.assertEqual(res.status_code, 404)
  134. self.assertEqual(Release.objects.all().count(), 1)
  135. url = reverse(
  136. "api:delete_project_release",
  137. kwargs={
  138. "organization_slug": release.organization.slug,
  139. "project_slug": self.project.slug,
  140. "version": release.version,
  141. },
  142. )
  143. res = self.client.delete(url)
  144. self.assertEqual(res.status_code, 204)
  145. self.assertEqual(Release.objects.all().count(), 0)
  146. def test_assemble(self):
  147. version = "app@v1"
  148. baker.make("releases.Release", version=version, organization=self.organization)
  149. url = reverse("api:assemble_release", args=[self.organization.slug, version])
  150. data = {
  151. "checksum": "94bc085fe32db9b4b1b82236214d65eeeeeeeeee",
  152. "chunks": ["94bc085fe32db9b4b1b82236214d65eeeeeeeeee"],
  153. }
  154. res = self.client.post(url, data, content_type="application/json")
  155. self.assertEqual(res.status_code, 200)