tests.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from django.conf import settings
  2. from django.shortcuts import reverse
  3. from django.test import TestCase, RequestFactory
  4. from rest_framework.test import APITestCase
  5. from model_bakery import baker
  6. from organizations_ext.models import OrganizationUser, OrganizationUserRole
  7. from glitchtip import test_utils # pylint: disable=unused-import
  8. class OrganizationModelTestCase(TestCase):
  9. def test_email(self):
  10. """ Billing email address """
  11. user = baker.make("users.user")
  12. organization = baker.make("organizations_ext.Organization")
  13. organization.add_user(user)
  14. # Org 1 has two users and only one of which is an owner
  15. user2 = baker.make("users.user")
  16. organization2 = baker.make("organizations_ext.Organization")
  17. organization2.add_user(user2)
  18. organization.add_user(user2)
  19. self.assertEqual(organization.email, user.email)
  20. self.assertEqual(organization.users.count(), 2)
  21. self.assertEqual(organization.owners.count(), 1)
  22. def test_organization_request_callback(self):
  23. user = baker.make("users.user")
  24. organization = baker.make("organizations_ext.Organization")
  25. organization.add_user(user)
  26. factory = RequestFactory()
  27. request = factory.get("/")
  28. request.user = user
  29. callback = settings.DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK
  30. self.assertEqual(callback(request), organization)
  31. def test_slug_reserved_words(self):
  32. """ Reserve some words for frontend routing needs """
  33. word = "login"
  34. organization = baker.make("organizations_ext.Organization", name=word)
  35. self.assertNotEqual(organization.slug, word)
  36. organization = baker.make("organizations_ext.Organization", name=word)
  37. class OrganizationsAPITestCase(APITestCase):
  38. def setUp(self):
  39. self.user = baker.make("users.user")
  40. self.organization = baker.make("organizations_ext.Organization")
  41. self.organization.add_user(self.user)
  42. self.client.force_login(self.user)
  43. self.url = reverse("organization-list")
  44. def test_organizations_list(self):
  45. not_my_organization = baker.make("organizations_ext.Organization")
  46. res = self.client.get(self.url)
  47. self.assertContains(res, self.organization.slug)
  48. self.assertNotContains(res, not_my_organization.slug)
  49. self.assertFalse(
  50. "teams" in res.data[0].keys(), "List view shouldn't contain teams"
  51. )
  52. def test_organizations_retrieve(self):
  53. project = baker.make("projects.Project", organization=self.organization)
  54. url = reverse("organization-detail", args=[self.organization.slug])
  55. res = self.client.get(url)
  56. self.assertContains(res, self.organization.name)
  57. self.assertContains(res, project.name)
  58. self.assertTrue(
  59. "teams" in res.data.keys(), "Retrieve view should contain teams"
  60. )
  61. def test_organizations_create(self):
  62. data = {"name": "test"}
  63. res = self.client.post(self.url, data)
  64. self.assertContains(res, data["name"], status_code=201)
  65. self.assertEqual(
  66. OrganizationUser.objects.filter(organization__name=data["name"]).count(), 1
  67. )
  68. def test_organizations_create_closed_registration(self):
  69. data = {"name": "test"}
  70. with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
  71. res = self.client.post(self.url, data)
  72. self.assertEqual(res.status_code, 201)
  73. def test_organizations_create_closed_registration_failure(self):
  74. data = {"name": "test"}
  75. self.organization.organization_users.all().update(
  76. role=OrganizationUserRole.MANAGER
  77. )
  78. with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
  79. res = self.client.post(self.url, data)
  80. self.assertEqual(res.status_code, 403)
  81. self.user.is_superuser = True
  82. self.user.save()
  83. with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
  84. res = self.client.post(self.url, data)
  85. self.assertEqual(res.status_code, 201)