Эх сурвалжийг харах

HUE-8530 [organization] Port to suppor the make_logged_in_client test util

Romain 5 жил өмнө
parent
commit
48c59d7f42

+ 3 - 0
apps/useradmin/src/useradmin/organization.py

@@ -199,8 +199,11 @@ class UserManager(BaseUserManager):
     """Create and save a regular User with the given email and password."""
     if extra_fields.get('username'):
       email = extra_fields.pop('username')
+
     if not extra_fields.get('organization'):
       extra_fields['organization'] = get_user_request_organization()
+      if not extra_fields['organization']:
+        extra_fields['organization'] = get_organization(email=email)
 
     extra_fields.setdefault('is_staff', False)
     extra_fields.setdefault('is_superuser', False)

+ 11 - 4
desktop/core/src/desktop/lib/django_test_util.py

@@ -24,6 +24,8 @@ import nose.tools
 
 from useradmin.models import User, Group, Organization
 
+from desktop.conf import ENABLE_ORGANIZATIONS
+
 
 class Client(django.test.client.Client):
   """
@@ -48,8 +50,8 @@ def make_logged_in_client(username="test", password="test", is_superuser=True, r
   """
   Create a client with a user already logged in.
 
-  Sometimes we recreate the user, because some tests like to
-  mess with is_active and such.
+  Sometimes we recreate the user, because some tests like to mess with is_active and such.
+  Note: could be combined with backend.create_user and other standart utils.
   """
   try:
     user = User.objects.get(username=username)
@@ -66,8 +68,13 @@ def make_logged_in_client(username="test", password="test", is_superuser=True, r
       user.save()
 
   if groupname is not None:
-    group, created = Group.objects.get_or_create(name=groupname)
-    if not user.groups.filter(name=group.name).exists():
+    attributes = {'name': groupname}
+
+    if ENABLE_ORGANIZATIONS.get():
+      attributes['organization'] = user.organization
+
+    group, created = Group.objects.get_or_create(**attributes)
+    if not user.groups.filter(**attributes).exists():
       user.groups.add(group)
       user.save()