tests.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from django.test import TestCase, override_settings
  2. from django.urls import reverse
  3. from model_bakery import baker
  4. class OrganizationModelTestCase(TestCase):
  5. def test_email(self):
  6. """Billing email address"""
  7. user = baker.make("users.user")
  8. organization = baker.make("organizations_ext.Organization")
  9. organization.add_user(user)
  10. # Org 1 has two users and only one of which is an owner
  11. user2 = baker.make("users.user")
  12. organization2 = baker.make("organizations_ext.Organization")
  13. organization2.add_user(user2)
  14. organization.add_user(user2)
  15. self.assertEqual(organization.email, user.email)
  16. self.assertEqual(organization.users.count(), 2)
  17. self.assertEqual(organization.owners.count(), 1)
  18. def test_slug_reserved_words(self):
  19. """Reserve some words for frontend routing needs"""
  20. word = "login"
  21. organization = baker.make("organizations_ext.Organization", name=word)
  22. self.assertNotEqual(organization.slug, word)
  23. organization = baker.make("organizations_ext.Organization", name=word)
  24. class OrganizationRegistrationSettingQueryTestCase(TestCase):
  25. def setUp(self):
  26. self.user = baker.make("users.user")
  27. self.client.force_login(self.user)
  28. self.url = reverse("api:list_organizations")
  29. @override_settings(ENABLE_ORGANIZATION_CREATION=False)
  30. def test_organizations_closed_registration_first_organization_create(self):
  31. data = {"name": "test"}
  32. res = self.client.post(self.url, data, content_type="application/json")
  33. self.assertEqual(res.status_code, 201)
  34. class OrganizationsFilterTestCase(TestCase):
  35. def setUp(self):
  36. self.user = baker.make("users.user")
  37. self.client.force_login(self.user)
  38. self.url = reverse("api:list_organizations")
  39. def test_default_ordering(self):
  40. organizationA = baker.make(
  41. "organizations_ext.Organization", name="A Organization"
  42. )
  43. organizationZ = baker.make(
  44. "organizations_ext.Organization", name="Z Organization"
  45. )
  46. organizationB = baker.make(
  47. "organizations_ext.Organization", name="B Organization"
  48. )
  49. organizationA.add_user(self.user)
  50. organizationB.add_user(self.user)
  51. organizationZ.add_user(self.user)
  52. res = self.client.get(self.url)
  53. data = res.json()
  54. self.assertEqual(data[0]["name"], organizationA.name)
  55. self.assertEqual(data[2]["name"], organizationZ.name)