tests.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from django.shortcuts import reverse
  2. from model_bakery import baker
  3. from rest_framework.test import APITestCase
  4. from glitchtip import test_utils # pylint: disable=unused-import
  5. class OrgTeamTestCase(APITestCase):
  6. """Tests nested under /organizations/"""
  7. def setUp(self):
  8. self.user = baker.make("users.user")
  9. self.organization = baker.make("organizations_ext.Organization")
  10. self.organization.add_user(self.user)
  11. self.client.force_login(self.user)
  12. self.url = reverse("organization-teams-list", args=[self.organization.slug])
  13. def test_list(self):
  14. team = baker.make("teams.Team", organization=self.organization)
  15. other_organization = baker.make("organizations_ext.Organization")
  16. other_organization.add_user(self.user)
  17. other_team = baker.make("teams.Team", organization=other_organization)
  18. res = self.client.get(self.url)
  19. self.assertContains(res, team.slug)
  20. self.assertNotContains(res, other_team.slug)
  21. def test_create(self):
  22. data = {"slug": "team"}
  23. res = self.client.post(self.url, data)
  24. self.assertContains(res, data["slug"], status_code=201)
  25. def test_unauthorized_create(self):
  26. """Only admins can create teams for that org"""
  27. data = {"slug": "team"}
  28. organization = baker.make("organizations_ext.Organization")
  29. url = reverse("organization-teams-list", args=[organization.slug])
  30. res = self.client.post(url, data)
  31. # Not even in this org
  32. self.assertEqual(res.status_code, 400)
  33. admin_user = baker.make("users.user")
  34. organization.add_user(admin_user) # First user is always admin
  35. organization.add_user(self.user)
  36. res = self.client.post(url, data)
  37. # Not an admin
  38. self.assertEqual(res.status_code, 400)
  39. def test_invalid_create(self):
  40. url = reverse("organization-teams-list", args=["haha"])
  41. data = {"slug": "team"}
  42. res = self.client.post(url, data)
  43. self.assertEqual(res.status_code, 400)
  44. class TeamTestCase(APITestCase):
  45. def setUp(self):
  46. self.user = baker.make("users.user")
  47. self.organization = baker.make("organizations_ext.Organization")
  48. self.org_user = self.organization.add_user(self.user)
  49. self.client.force_login(self.user)
  50. self.url = reverse("team-list")
  51. def test_list(self):
  52. team = baker.make("teams.Team", organization=self.organization)
  53. other_team = baker.make("teams.Team")
  54. res = self.client.get(self.url)
  55. self.assertContains(res, team.slug)
  56. self.assertNotContains(res, other_team.slug)
  57. def test_retrieve(self):
  58. team = baker.make("teams.Team", organization=self.organization)
  59. team.members.add(self.org_user)
  60. url = reverse(
  61. "team-detail",
  62. kwargs={
  63. "pk": f"{self.organization.slug}/{team.slug}",
  64. },
  65. )
  66. res = self.client.get(url)
  67. self.assertContains(res, team.slug)
  68. self.assertTrue(res.data["isMember"])
  69. def test_invalid_retrieve(self):
  70. team = baker.make("teams.Team")
  71. url = reverse(
  72. "team-detail",
  73. kwargs={
  74. "pk": f"{self.organization.slug}/{team.slug}",
  75. },
  76. )
  77. res = self.client.get(url)
  78. self.assertEqual(res.status_code, 404)