Ejemplo n.º 1
0
 def test_dbuser_query_missing_user(self):
     try:
         user = DatabaseUser('somefunkynonexistinguser', create=False)
         # Remove the user, so it doesn't get stuck in the DB
         user.delete()
         self.fail("User shouldn't exist.")
     except CommandError:
         pass
Ejemplo n.º 2
0
 def test_dbuser_delete_fail(self, mock_exists):
     mock_exists.return_value = True
     try:
         user = DatabaseUser(TEST_USER_NAME1, create=False)
         user.delete()
         self.fail('Deleting an user was successful.')
     except CommandError, err:
         self.assertEqual(str(err),
             "User cannot be deleted: %s" % TEST_USER_NAME1)
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        check_database_engine()
        username, rolename = parse_username_and_rolename(args)

        role = DatabaseRole(rolename)
        user = DatabaseUser(username, create=False)

        role.revoke(user)
        user.delete()

        print "User '%s' deleted successfully." % username
Ejemplo n.º 4
0
 def test_dbuser_create_fail(self, mock_exists):
     mock_exists.return_value = False
     try:
         DatabaseUser('dummy')
         self.fail('Creating an user was successful.')
     except CommandError, err:
         self.assertEqual(str(err), "User couldn't be created: dummy")
Ejemplo n.º 5
0
    def _delete_roles_and_users(self):
        # clean up everything
        if DatabaseUser.exists(TEST_USER_NAME1):
            if DatabaseRole.exists(MASTER_ROLE_NAME):
                self.master_role.revoke(self.test1)
            self.test1.delete()
        if DatabaseUser.exists(TEST_USER_NAME2):
            if DatabaseRole.exists(MASTER_ROLE_NAME):
                self.master_role.revoke(self.test2)
            self.test2.delete()
        if DatabaseRole.exists(MASTER_ROLE_NAME):
            self.master_role.delete()
        if DatabaseRole.exists(EMPTY_ROLE_NAME):
            self.empty_role.delete()

        # make sure everything has been cleaned up
        self.assertFalse(DatabaseUser.exists(TEST_USER_NAME1))
        self.assertFalse(DatabaseUser.exists(TEST_USER_NAME2))
        self.assertFalse(DatabaseRole.exists(MASTER_ROLE_NAME))
        self.assertFalse(DatabaseRole.exists(EMPTY_ROLE_NAME))
Ejemplo n.º 6
0
    def _create_roles_and_users(self):
        # Create master role
        if not DatabaseRole.exists(MASTER_ROLE_NAME):
            self.master_role = DatabaseRole(MASTER_ROLE_NAME, create=True)
        else:
            self.master_role = DatabaseRole(MASTER_ROLE_NAME, create=False)
        # Create standalone role (test1)
        if not DatabaseRole.exists(EMPTY_ROLE_NAME):
            self.empty_role = DatabaseRole(EMPTY_ROLE_NAME, create=True)
        else:
            self.empty_role = DatabaseRole(EMPTY_ROLE_NAME, create=False)
        # Create users based on master tole (test2 and test3)
        if not DatabaseUser.exists(TEST_USER_NAME1):
            self.test1 = DatabaseUser(TEST_USER_NAME1, 'testpass', create=True)
        else:
            self.test1 = DatabaseUser(TEST_USER_NAME1, 'testpass', create=False)
        self.master_role.grant(self.test1)

        if not DatabaseUser.exists(TEST_USER_NAME2):
            self.test2 = DatabaseUser(TEST_USER_NAME2, create=True)
        else:
            self.test2 = DatabaseUser(TEST_USER_NAME2, create=False)
        self.master_role.grant(self.test2)
Ejemplo n.º 7
0
    def handle(self, *args, **options):
        check_database_engine()
        username, rolename = parse_username_and_rolename(args)

        password = options.get('password')
        no_password = options.get('no_password')
        if not no_password and password is None:
            self._ask_password()

        role = DatabaseRole(rolename)
        user = DatabaseUser(username, password)
        role.grant(user)

        print "User '%s' created successfully based on role '%s'." % (username,
                                                                      rolename)
Ejemplo n.º 8
0
class CommandTestCase(TestCase):

    def setUp(self):
        test_settings = {
            'DATABASE_USER': '******',
            'DATABASE_PASSWORD': '',
        }
        if hasattr(settings, 'DATABASES'):
            test_settings['DATABASES'] = settings.DATABASES.copy()
            test_settings['DATABASES']['default'].update({
                'USER': '******',
                'PASSWORD': '',
            })
        self.old_settings = switch_settings(**test_settings)

        self._refresh_connection()
        self._capture_stdout_stderr()
        self._create_roles_and_users()

        # Make sure that dummy role doesn't exist before testing starts.
        if DatabaseRole.exists('dummy'):
            DatabaseRole('dummy', create=False).delete()

    def _refresh_connection(self):
        for attname in ['HOST', 'PORT', 'NAME', 'USER', 'PASSWORD']:
            if hasattr(settings, 'DATABASES'):
                attvalue = settings.DATABASES['default'].get(attname, '')
                # need to update the DatabaseWrapper cached settings_dict
                # to keep up to date with the new db setting
                connection.settings_dict[attname] = attvalue
            elif hasattr(connection, 'settings_dict'):
                setting_name = "DATABASE_%s" % attname
                attvalue = getattr(settings, setting_name, '')
                # need to update the DatabaseWrapper cached settings_dict
                # to keep up to date with the new db setting
                connection.settings_dict[setting_name] = attvalue
            else:
                # Django 1.0 doesn't have setting cache, so there's nothing to
                # refresh
                pass
        connection.close()

    def _capture_stdout_stderr(self):
        # patch stdout and stderr to avoid polluting the console
        sys.stdout = StringIO()
        sys.stderr = StringIO()

    def _create_roles_and_users(self):
        # Create master role
        if not DatabaseRole.exists(MASTER_ROLE_NAME):
            self.master_role = DatabaseRole(MASTER_ROLE_NAME, create=True)
        else:
            self.master_role = DatabaseRole(MASTER_ROLE_NAME, create=False)
        # Create standalone role (test1)
        if not DatabaseRole.exists(EMPTY_ROLE_NAME):
            self.empty_role = DatabaseRole(EMPTY_ROLE_NAME, create=True)
        else:
            self.empty_role = DatabaseRole(EMPTY_ROLE_NAME, create=False)
        # Create users based on master tole (test2 and test3)
        if not DatabaseUser.exists(TEST_USER_NAME1):
            self.test1 = DatabaseUser(TEST_USER_NAME1, 'testpass', create=True)
        else:
            self.test1 = DatabaseUser(TEST_USER_NAME1, 'testpass', create=False)
        self.master_role.grant(self.test1)

        if not DatabaseUser.exists(TEST_USER_NAME2):
            self.test2 = DatabaseUser(TEST_USER_NAME2, create=True)
        else:
            self.test2 = DatabaseUser(TEST_USER_NAME2, create=False)
        self.master_role.grant(self.test2)

    def tearDown(self):
        self._delete_roles_and_users()
        self._release_stdout_stderr()
        switch_settings(**self.old_settings)

    def _delete_roles_and_users(self):
        # clean up everything
        if DatabaseUser.exists(TEST_USER_NAME1):
            if DatabaseRole.exists(MASTER_ROLE_NAME):
                self.master_role.revoke(self.test1)
            self.test1.delete()
        if DatabaseUser.exists(TEST_USER_NAME2):
            if DatabaseRole.exists(MASTER_ROLE_NAME):
                self.master_role.revoke(self.test2)
            self.test2.delete()
        if DatabaseRole.exists(MASTER_ROLE_NAME):
            self.master_role.delete()
        if DatabaseRole.exists(EMPTY_ROLE_NAME):
            self.empty_role.delete()

        # make sure everything has been cleaned up
        self.assertFalse(DatabaseUser.exists(TEST_USER_NAME1))
        self.assertFalse(DatabaseUser.exists(TEST_USER_NAME2))
        self.assertFalse(DatabaseRole.exists(MASTER_ROLE_NAME))
        self.assertFalse(DatabaseRole.exists(EMPTY_ROLE_NAME))

    def _release_stdout_stderr(self):
        # reset stdout/stderr
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
Ejemplo n.º 9
0
 def test_dbuser_create_existing_user(self):
     try:
         DatabaseUser(TEST_USER_NAME1)
         self.fail('Creating an existing user was successful.')
     except CommandError:
         pass