def test_try_seed_permissions_update_description(self): """ Test permission seeding update description, skip string configs. """ permission_string = "approval_local_test:templates:read" with tenant_context(self.tenant): self.assertFalse(len(Permission.objects.all())) Permission.objects.create(permission=permission_string) try: seed_permissions(self.tenant) except Exception: self.fail(msg="seed_permissions encountered an exception") with tenant_context(self.tenant): self.assertEqual( Permission.objects.exclude(permission=permission_string). values_list("description").distinct().first(), ("", ), ) permission = Permission.objects.filter( permission=permission_string) self.assertEqual(len(permission), 1) self.assertEqual(permission.first().description, "Approval local test templates read.") # Previous string verb still works self.assertEqual( Permission.objects.filter( permission="catalog_local_test:approval_requests:read"). count(), 1)
def get_tenant(self, model, hostname, request): """Override the tenant selection logic.""" connections["default"].set_schema_to_public() tenant_schema = create_schema_name(request.user.account) tenant = TENANTS.get_tenant(tenant_schema) if tenant is None: if request.user.system: try: tenant = Tenant.objects.get(schema_name=tenant_schema) except Tenant.DoesNotExist: raise Http404() else: with transaction.atomic(): try: tenant = Tenant.objects.get(schema_name=tenant_schema) except Tenant.DoesNotExist: cursor = transaction.get_connection().cursor() cursor.execute("LOCK TABLE public.api_tenant in SHARE ROW EXCLUSIVE MODE") tenant, created = Tenant.objects.get_or_create(schema_name=tenant_schema) if created: seed_permissions(tenant=tenant) seed_roles(tenant=tenant) seed_group(tenant=tenant) TENANTS.save_tenant(tenant) return tenant
def try_seed_permissions(self): """ Test permission seeding. """ with tenant_context(self.tenant): self.assertFalse(len(Permission.objects.all())) try: seed_permissions(self.tenant) except Exception: self.fail(msg="seed_permissions encountered an exception") with tenant_context(self.tenant): self.assertTrue(len(Permission.objects.all())) permission = Permission.objects.first() self.assertTrue(permission.application) self.assertTrue(permission.resource_type) self.assertTrue(permission.verb) self.assertTrue(permission.permission)
def get_tenant(self, model, hostname, request): """Override the tenant selection logic.""" connections["default"].set_schema_to_public() if request.user.account not in TENANTS: if request.user.system: try: tenant = Tenant.objects.get( schema_name=create_schema_name(request.user.account)) except Tenant.DoesNotExist: raise Http404() else: with transaction.atomic(): tenant, created = Tenant.objects.get_or_create( schema_name=create_schema_name(request.user.account)) if created: seed_permissions(tenant=tenant) seed_roles(tenant=tenant) seed_group(tenant=tenant) TENANTS[request.user.account] = tenant return TENANTS[request.user.account]