123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- from django.conf import settings
- from django.shortcuts import reverse
- from django.test import TestCase, RequestFactory
- from rest_framework.test import APITestCase
- from model_bakery import baker
- from organizations_ext.models import OrganizationUser, OrganizationUserRole
- from glitchtip import test_utils # pylint: disable=unused-import
- class OrganizationModelTestCase(TestCase):
- def test_email(self):
- """ Billing email address """
- user = baker.make("users.user")
- organization = baker.make("organizations_ext.Organization")
- organization.add_user(user)
- # Org 1 has two users and only one of which is an owner
- user2 = baker.make("users.user")
- organization2 = baker.make("organizations_ext.Organization")
- organization2.add_user(user2)
- organization.add_user(user2)
- self.assertEqual(organization.email, user.email)
- self.assertEqual(organization.users.count(), 2)
- self.assertEqual(organization.owners.count(), 1)
- def test_organization_request_callback(self):
- user = baker.make("users.user")
- organization = baker.make("organizations_ext.Organization")
- organization.add_user(user)
- factory = RequestFactory()
- request = factory.get("/")
- request.user = user
- callback = settings.DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK
- self.assertEqual(callback(request), organization)
- def test_slug_reserved_words(self):
- """ Reserve some words for frontend routing needs """
- word = "login"
- organization = baker.make("organizations_ext.Organization", name=word)
- self.assertNotEqual(organization.slug, word)
- organization = baker.make("organizations_ext.Organization", name=word)
- class OrganizationsAPITestCase(APITestCase):
- def setUp(self):
- self.user = baker.make("users.user")
- self.organization = baker.make("organizations_ext.Organization")
- self.organization.add_user(self.user)
- self.client.force_login(self.user)
- self.url = reverse("organization-list")
- def test_organizations_list(self):
- not_my_organization = baker.make("organizations_ext.Organization")
- res = self.client.get(self.url)
- self.assertContains(res, self.organization.slug)
- self.assertNotContains(res, not_my_organization.slug)
- self.assertFalse(
- "teams" in res.data[0].keys(), "List view shouldn't contain teams"
- )
- def test_organizations_retrieve(self):
- project = baker.make("projects.Project", organization=self.organization)
- url = reverse("organization-detail", args=[self.organization.slug])
- res = self.client.get(url)
- self.assertContains(res, self.organization.name)
- self.assertContains(res, project.name)
- self.assertTrue(
- "teams" in res.data.keys(), "Retrieve view should contain teams"
- )
- def test_organizations_create(self):
- data = {"name": "test"}
- res = self.client.post(self.url, data)
- self.assertContains(res, data["name"], status_code=201)
- self.assertEqual(
- OrganizationUser.objects.filter(organization__name=data["name"]).count(), 1
- )
- def test_organizations_create_closed_registration(self):
- data = {"name": "test"}
- with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
- res = self.client.post(self.url, data)
- self.assertEqual(res.status_code, 201)
- def test_organizations_create_closed_registration_failure(self):
- data = {"name": "test"}
- self.organization.organization_users.all().update(
- role=OrganizationUserRole.MANAGER
- )
- with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
- res = self.client.post(self.url, data)
- self.assertEqual(res.status_code, 403)
- self.user.is_superuser = True
- self.user.save()
- with self.settings(ENABLE_OPEN_USER_REGISTRATION=False):
- res = self.client.post(self.url, data)
- self.assertEqual(res.status_code, 201)
|