test_api.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 OrganizationUser
  5. class OrganizationsAPITestCase(TestCase):
  6. @classmethod
  7. def setUpTestData(cls):
  8. cls.user = baker.make("users.user")
  9. cls.organization = baker.make("organizations_ext.Organization")
  10. cls.organization.add_user(cls.user)
  11. cls.url = reverse("api:list_organizations")
  12. def setUp(self):
  13. self.client.force_login(self.user)
  14. def test_organizations_list(self):
  15. not_my_organization = baker.make("organizations_ext.Organization")
  16. res = self.client.get(self.url)
  17. self.assertContains(res, self.organization.slug)
  18. self.assertNotContains(res, not_my_organization.slug)
  19. self.assertFalse(
  20. "teams" in res.json()[0].keys(), "List view shouldn't contain teams"
  21. )
  22. def test_organizations_retrieve(self):
  23. project = baker.make("projects.Project", organization=self.organization)
  24. team = baker.make("teams.Team", organization=self.organization)
  25. url = reverse("api:get_organization", args=[self.organization.slug])
  26. res = self.client.get(url)
  27. self.assertContains(res, self.organization.name)
  28. self.assertContains(res, project.name)
  29. data = res.json()
  30. self.assertTrue("teams" in data.keys(), "Retrieve view should contain teams")
  31. self.assertTrue(
  32. "projects" in data.keys(), "Retrieve view should contain projects"
  33. )
  34. self.assertContains(res, team.slug)
  35. self.assertTrue(
  36. "teams" in data["projects"][0].keys(),
  37. "Org projects should contain teams id/name",
  38. )
  39. def test_organizations_create(self):
  40. data = {"name": "test"}
  41. res = self.client.post(self.url, data, content_type="application/json")
  42. self.assertContains(res, data["name"], status_code=201)
  43. self.assertEqual(
  44. OrganizationUser.objects.filter(organization__name=data["name"]).count(), 1
  45. )
  46. def test_organizations_create_closed_registration_superuser(self):
  47. data = {"name": "test"}
  48. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  49. res = self.client.post(self.url, data, content_type="application/json")
  50. self.assertEqual(res.status_code, 403)
  51. self.user.is_superuser = True
  52. self.user.save()
  53. with self.settings(ENABLE_ORGANIZATION_CREATION=False):
  54. with self.assertNumQueries(9):
  55. res = self.client.post(self.url, data, content_type="application/json")
  56. self.assertEqual(res.status_code, 201)
  57. def test_organizations_update(self):
  58. data = {"name": "edit"}
  59. url = reverse("api:get_organization", args=[self.organization.slug])
  60. res = self.client.put(url, data, content_type="application/json")
  61. self.assertContains(res, data["name"])
  62. self.assertTrue(
  63. OrganizationUser.objects.filter(organization__name=data["name"]).exists()
  64. )