mirror of
https://github.com/ansible/awx.git
synced 2026-01-13 02:50:02 -03:30
Set created_by and modified_by on default org to superuser (if found). Fixes https://trello.com/c/gERRKtXa
This commit is contained in:
parent
07205bcb88
commit
8eb69b96d1
@ -2,8 +2,8 @@
|
||||
# All Rights Reserved
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from awx.main.models import Organization
|
||||
from crum import impersonate
|
||||
from awx.main.models import User, Organization
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
@ -17,7 +17,11 @@ class Command(BaseCommand):
|
||||
if Organization.objects.count():
|
||||
return
|
||||
|
||||
# Create a default organization.
|
||||
org, new = Organization.objects.get_or_create(name='Default')
|
||||
if new:
|
||||
print('Default organization added.')
|
||||
# Create a default organization as the first superuser found.
|
||||
try:
|
||||
superuser = User.objects.filter(is_superuser=True, is_active=True).order_by('pk')[0]
|
||||
except IndexError:
|
||||
superuser = None
|
||||
with impersonate(superuser):
|
||||
org = Organization.objects.create(name='Default')
|
||||
print('Default organization added.')
|
||||
|
||||
@ -29,8 +29,9 @@ from awx.main.tests.base import BaseTest, BaseLiveServerTest
|
||||
if not hasattr(unittest, 'skipIf'):
|
||||
import unittest2 as unittest
|
||||
|
||||
__all__ = ['DumpDataTest', 'CleanupDeletedTest', 'CleanupJobsTest',
|
||||
'CleanupActivityStreamTest', 'InventoryImportTest']
|
||||
__all__ = ['CreateDefaultOrgTest', 'DumpDataTest', 'CleanupDeletedTest',
|
||||
'CleanupJobsTest', 'CleanupActivityStreamTest',
|
||||
'InventoryImportTest']
|
||||
|
||||
TEST_PLAYBOOK = '''- hosts: test-group
|
||||
gather_facts: False
|
||||
@ -175,6 +176,39 @@ class BaseCommandMixin(object):
|
||||
sys.stderr = original_stderr
|
||||
return result, captured_stdout, captured_stderr
|
||||
|
||||
class CreateDefaultOrgTest(BaseCommandMixin, BaseTest):
|
||||
'''
|
||||
Test cases for create_default_org management command.
|
||||
'''
|
||||
|
||||
def setUp(self):
|
||||
super(CreateDefaultOrgTest, self).setUp()
|
||||
|
||||
def test_create_default_org(self):
|
||||
self.setup_users()
|
||||
self.assertEqual(Organization.objects.count(), 0)
|
||||
result, stdout, stderr = self.run_command('create_default_org')
|
||||
self.assertEqual(result, None)
|
||||
self.assertTrue('Default organization added' in stdout)
|
||||
self.assertEqual(Organization.objects.count(), 1)
|
||||
org = Organization.objects.all()[0]
|
||||
self.assertEqual(org.created_by, self.super_django_user)
|
||||
self.assertEqual(org.modified_by, self.super_django_user)
|
||||
result, stdout, stderr = self.run_command('create_default_org')
|
||||
self.assertEqual(result, None)
|
||||
self.assertFalse('Default organization added' in stdout)
|
||||
self.assertEqual(Organization.objects.count(), 1)
|
||||
|
||||
def test_create_default_org_when_no_superuser_exists(self):
|
||||
self.assertEqual(Organization.objects.count(), 0)
|
||||
result, stdout, stderr = self.run_command('create_default_org')
|
||||
self.assertEqual(result, None)
|
||||
self.assertTrue('Default organization added' in stdout)
|
||||
self.assertEqual(Organization.objects.count(), 1)
|
||||
org = Organization.objects.all()[0]
|
||||
self.assertEqual(org.created_by, None)
|
||||
self.assertEqual(org.modified_by, None)
|
||||
|
||||
class DumpDataTest(BaseCommandMixin, BaseTest):
|
||||
'''
|
||||
Test cases for dumpdata management command.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user