Example #1
0
    def handle(self, *args, **options):
        """
        utilizes the rename_user function in the management_utils module
        :param args: <old_username> <new_username>
        :param options: no options supported
        """
        if len(args) != 2:
            command_name = '.'.join(basename(__file__).split('.')[:-1])
            raise CommandError(
                "Usage is {command_name} {command_args}".format(
                    command_name=command_name,
                    command_args=self.args,
                )
            )

        try:
            management_utils.rename_user(*args)
        except (User.DoesNotExist, IntegrityError, PyMongoError):
            log.exception('FAILED TO MODIFY USERNAME FOR USER: {old_username}'.format(
                old_username=args[0]
            ))
        else:
            print "Changed username of user: {old_username} to {new_username}".format(
                old_username=args[0],
                new_username=args[1],
            )
    def test_rename_not_matching(self):
        """
        ensure that the username for the second user does not change when renaming the first user
        """
        # check that the username for user2 is initially different from the new username
        self.assertNotEqual(self.sql_users[1].username, TestManagementUtils.NEW_USERNAME)
        mongo_user2 = self.db.users.find_one({'username': self.sql_users[1].username})
        mongo_comment2 = self.db.contents.find_one({'author_username': self.sql_users[1].username})
        self.assertEqual(mongo_user2['username'], self.sql_users[1].username)
        self.assertEqual(mongo_comment2['author_username'], self.sql_users[1].username)

        # perform the rename only for the first user
        result = rename_user(self.sql_users[0].username, TestManagementUtils.NEW_USERNAME)
        # ensure that another user was successfully changed
        self.assertTrue(result)

        # re-query the second user
        self.sql_users[1] = User.objects.get(id=self.sql_users[1].id)
        mongo_user2 = self.db.users.find_one({'_id': mongo_user2['_id']})
        mongo_comment2 = self.db.contents.find_one({'_id': mongo_comment2['_id']})

        # check that the rename operation did not change the username for user2 to the new username
        self.assertNotEqual(self.sql_users[1].username, TestManagementUtils.NEW_USERNAME)
        self.assertNotEqual(mongo_user2['username'], TestManagementUtils.NEW_USERNAME)
        self.assertNotEqual(mongo_comment2['author_username'], TestManagementUtils.NEW_USERNAME)
    def test_rename_user(self):
        """
        ensures that the rename_user utility successfully renames a user
        """
        # ensure that the starting sql username is different from the target username
        self.assertNotEqual(self.sql_users[0].username, TestManagementUtils.NEW_USERNAME)

        # ensure that the starting mongo username matches the starting sql username
        mongo_user = self.db.users.find_one({'username': self.sql_users[0].username})
        self.assertEqual(mongo_user['username'], self.sql_users[0].username)
        mongo_comment = self.db.contents.find_one({'author_username': self.sql_users[0].username})
        self.assertEqual(mongo_comment['author_username'], self.sql_users[0].username)

        # perform the rename
        result = rename_user(self.sql_users[0].username, TestManagementUtils.NEW_USERNAME)

        self.assertTrue(result)
        # check that the sql username now matches the target username
        self.sql_users[0] = User.objects.get(id=self.sql_users[0].id)
        self.assertEqual(self.sql_users[0].username, TestManagementUtils.NEW_USERNAME)

        # check that the mongo username now matches the target username
        mongo_user = self.db.users.find_one({'_id': mongo_user['_id']})
        self.assertEqual(mongo_user['username'], TestManagementUtils.NEW_USERNAME)

        mongo_comment = self.db.contents.find_one({'_id': mongo_comment['_id']})
        self.assertEqual(mongo_comment['author_username'], TestManagementUtils.NEW_USERNAME)
    def test_rename_not_matching(self):
        """
        ensure that the username for the second user does not change when renaming the first user
        """
        # check that the username for user2 is initially different from the new username
        self.assertNotEqual(self.sql_users[1].username,
                            TestManagementUtils.NEW_USERNAME)
        mongo_user2 = self.db.users.find_one(
            {'username': self.sql_users[1].username})
        mongo_comment2 = self.db.contents.find_one(
            {'author_username': self.sql_users[1].username})
        self.assertEqual(mongo_user2['username'], self.sql_users[1].username)
        self.assertEqual(mongo_comment2['author_username'],
                         self.sql_users[1].username)

        # perform the rename only for the first user
        result = rename_user(self.sql_users[0].username,
                             TestManagementUtils.NEW_USERNAME)
        # ensure that another user was successfully changed
        self.assertTrue(result)

        # re-query the second user
        self.sql_users[1] = User.objects.get(id=self.sql_users[1].id)
        mongo_user2 = self.db.users.find_one({'_id': mongo_user2['_id']})
        mongo_comment2 = self.db.contents.find_one(
            {'_id': mongo_comment2['_id']})

        # check that the rename operation did not change the username for user2 to the new username
        self.assertNotEqual(self.sql_users[1].username,
                            TestManagementUtils.NEW_USERNAME)
        self.assertNotEqual(mongo_user2['username'],
                            TestManagementUtils.NEW_USERNAME)
        self.assertNotEqual(mongo_comment2['author_username'],
                            TestManagementUtils.NEW_USERNAME)
Example #5
0
    def handle(self, *args, **options):
        """
        utilizes the rename_user function in the management_utils module
        :param args: <old_username> <new_username>
        :param options: no options supported
        """
        if len(args) != 2:
            command_name = '.'.join(basename(__file__).split('.')[:-1])
            raise CommandError("Usage is {command_name} {command_args}".format(
                command_name=command_name,
                command_args=self.args,
            ))

        try:
            management_utils.rename_user(*args)
        except (User.DoesNotExist, IntegrityError, PyMongoError):
            log.exception(
                'FAILED TO MODIFY USERNAME FOR USER: {old_username}'.format(
                    old_username=args[0]))
        else:
            print "Changed username of user: {old_username} to {new_username}".format(
                old_username=args[0],
                new_username=args[1],
            )
    def test_rename_user(self):
        """
        ensures that the rename_user utility successfully renames a user
        """
        # ensure that the starting sql username is different from the target username
        self.assertNotEqual(self.sql_users[0].username,
                            TestManagementUtils.NEW_USERNAME)

        # ensure that the starting mongo username matches the starting sql username
        mongo_user = self.db.users.find_one(
            {'username': self.sql_users[0].username})
        self.assertEqual(mongo_user['username'], self.sql_users[0].username)
        mongo_comment = self.db.contents.find_one(
            {'author_username': self.sql_users[0].username})
        self.assertEqual(mongo_comment['author_username'],
                         self.sql_users[0].username)

        # perform the rename
        result = rename_user(self.sql_users[0].username,
                             TestManagementUtils.NEW_USERNAME)

        self.assertTrue(result)
        # check that the sql username now matches the target username
        self.sql_users[0] = User.objects.get(id=self.sql_users[0].id)
        self.assertEqual(self.sql_users[0].username,
                         TestManagementUtils.NEW_USERNAME)

        # check that the mongo username now matches the target username
        mongo_user = self.db.users.find_one({'_id': mongo_user['_id']})
        self.assertEqual(mongo_user['username'],
                         TestManagementUtils.NEW_USERNAME)

        mongo_comment = self.db.contents.find_one(
            {'_id': mongo_comment['_id']})
        self.assertEqual(mongo_comment['author_username'],
                         TestManagementUtils.NEW_USERNAME)