test_api.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from django.test import TestCase
  2. from django.urls import reverse
  3. from model_bakery import baker
  4. from apps.organizations_ext.models import OrganizationUserRole
  5. from glitchtip.test_utils.test_case import GlitchTipTestCaseMixin
  6. from ..models import Release
  7. class ReleaseAPITestCase(GlitchTipTestCaseMixin, TestCase):
  8. def setUp(self):
  9. self.create_logged_in_user()
  10. def test_create(self):
  11. url = reverse("api:create_release", args=[self.organization.slug])
  12. data = {"version": "1.0", "projects": [self.project.slug]}
  13. res = self.client.post(url, data, content_type="application/json")
  14. self.assertContains(res, data["version"], status_code=201)
  15. self.assertTrue(Release.objects.filter(version=data["version"]).exists())
  16. def test_list(self):
  17. url = reverse(
  18. "api:list_releases",
  19. kwargs={"organization_slug": self.organization.slug},
  20. )
  21. release1 = baker.make("releases.Release", organization=self.organization)
  22. release2 = baker.make("releases.Release")
  23. organization2 = baker.make("organizations_ext.Organization")
  24. organization2.add_user(self.user, OrganizationUserRole.ADMIN)
  25. release3 = baker.make("releases.Release", organization=organization2)
  26. res = self.client.get(url)
  27. self.assertContains(res, release1.version)
  28. self.assertNotContains(res, release2.version) # User not in org
  29. self.assertNotContains(res, release3.version) # Filtered our by url
  30. def test_retrieve(self):
  31. release = baker.make("releases.Release", organization=self.organization)
  32. url = reverse(
  33. "api:get_release",
  34. kwargs={
  35. "organization_slug": self.organization.slug,
  36. "version": release.version,
  37. },
  38. )
  39. res = self.client.get(url)
  40. self.assertContains(res, release.version)
  41. def test_finalize(self):
  42. release = baker.make("releases.Release", organization=self.organization)
  43. url = reverse(
  44. "api:update_release",
  45. kwargs={
  46. "organization_slug": release.organization.slug,
  47. "version": release.version,
  48. },
  49. )
  50. data = {"dateReleased": "2021-09-04T14:08:57.388525996Z"}
  51. res = self.client.put(url, data, content_type="application/json")
  52. self.assertContains(res, data["dateReleased"][:14])
  53. def test_destroy_org_release(self):
  54. release1 = baker.make("releases.Release", organization=self.organization)
  55. url = reverse(
  56. "api:delete_organization_release",
  57. kwargs={
  58. "organization_slug": release1.organization.slug,
  59. "version": release1.version,
  60. },
  61. )
  62. res = self.client.delete(url)
  63. self.assertEqual(res.status_code, 204)
  64. self.assertEqual(Release.objects.all().count(), 0)
  65. release2 = baker.make("releases.Release")
  66. url = reverse(
  67. "api:delete_organization_release",
  68. kwargs={
  69. "organization_slug": release2.organization.slug,
  70. "version": release2.version,
  71. },
  72. )
  73. res = self.client.delete(url)
  74. self.assertEqual(res.status_code, 404)
  75. self.assertEqual(Release.objects.all().count(), 1)
  76. def test_project_list(self):
  77. url = reverse(
  78. "api:list_project_releases",
  79. kwargs={
  80. "organization_slug": self.organization.slug,
  81. "project_slug": self.project.slug,
  82. },
  83. )
  84. project2 = baker.make("projects.Project", organization=self.organization)
  85. release1 = baker.make(
  86. "releases.Release",
  87. organization=self.organization,
  88. projects=[self.project, project2],
  89. )
  90. release2 = baker.make("releases.Release", organization=self.organization)
  91. res = self.client.get(url)
  92. self.assertContains(res, release1.version)
  93. self.assertNotContains(res, release2.version) # User not in project
  94. self.assertEqual(len(res.json()), 1)
  95. def test_destroy_project_release(self):
  96. release = baker.make(
  97. "releases.Release", organization=self.organization, projects=[self.project]
  98. )
  99. other_project= baker.make("projects.Project", organization=self.organization)
  100. url = reverse(
  101. "api:delete_project_release",
  102. kwargs={
  103. "organization_slug": release.organization.slug,
  104. "project_slug": other_project.slug,
  105. "version": release.version,
  106. },
  107. )
  108. res = self.client.delete(url)
  109. self.assertEqual(res.status_code, 404)
  110. self.assertEqual(Release.objects.all().count(), 1)
  111. url = reverse(
  112. "api:delete_project_release",
  113. kwargs={
  114. "organization_slug": release.organization.slug,
  115. "project_slug": self.project.slug,
  116. "version": release.version,
  117. },
  118. )
  119. res = self.client.delete(url)
  120. self.assertEqual(res.status_code, 204)
  121. self.assertEqual(Release.objects.all().count(), 0)