tests.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from django.shortcuts import reverse
  2. from rest_framework.test import APITestCase
  3. from model_bakery import baker
  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", kwargs={"pk": f"{self.organization.slug}/{team.slug}",},
  62. )
  63. res = self.client.get(url)
  64. self.assertContains(res, team.slug)
  65. self.assertTrue(res.data["isMember"])
  66. def test_invalid_retrieve(self):
  67. team = baker.make("teams.Team")
  68. url = reverse(
  69. "team-detail", kwargs={"pk": f"{self.organization.slug}/{team.slug}",},
  70. )
  71. res = self.client.get(url)
  72. self.assertEqual(res.status_code, 404)