From fe91c73fd2df472409698d3b96108d5dec1540ce Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Wed, 20 Jan 2016 09:26:09 -0500 Subject: [PATCH] testable playbook w/ state wanted pattern * remove complicated redis config assembly * backup and restore discover credentials rather than requiring * backup strategy no longer drops the db. This allows awx user to restore without creatdb persmission. * removed mongo from backup/restore * added password change support for redis, pg, munin * added update_password command and respective tests * tests for munin, pg, redis password change * unit tests for munin, redis password change * tests for postgres host (ip and socket) change * tests for postgres backup/restore on remote db --- .../management/commands/update_password.py | 45 +++++++++++++++++++ awx/main/tests/commands/__init__.py | 1 + awx/main/tests/commands/update_password.py | 37 +++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 awx/main/management/commands/update_password.py create mode 100644 awx/main/tests/commands/update_password.py diff --git a/awx/main/management/commands/update_password.py b/awx/main/management/commands/update_password.py new file mode 100644 index 0000000000..fe45799776 --- /dev/null +++ b/awx/main/management/commands/update_password.py @@ -0,0 +1,45 @@ +# Copyright (c) 2016 Ansible, Inc. +# All Rights Reserved + +# Python +from optparse import make_option + +# Django +from django.core.management.base import BaseCommand +from django.core.management.base import CommandError +from django.contrib.auth.models import User + +class UpdatePassword(object): + def update_password(self, username, password): + changed = False + u = User.objects.get(username=username) + if not u: + raise RuntimeError("User not found") + check = u.check_password(password) + if not check: + u.set_password(password) + u.save() + changed = True + return changed + +class Command(BaseCommand): + option_list = BaseCommand.option_list + ( + make_option('--username', dest='username', action='store', type='string', default=None, + help='username to change the password for'), + make_option('--password', dest='password', action='store', type='string', default=None, + help='new password for user'), + ) + + def handle(self, *args, **options): + if not options['username']: + raise CommandError('username required') + if not options['password']: + raise CommandError('password required') + + cp = UpdatePassword() + res = cp.update_password(options['username'], options['password']) + if res: + return "Password updated" + return "Password not updated" + + diff --git a/awx/main/tests/commands/__init__.py b/awx/main/tests/commands/__init__.py index dc89a6f8b6..84212dd2fb 100644 --- a/awx/main/tests/commands/__init__.py +++ b/awx/main/tests/commands/__init__.py @@ -9,4 +9,5 @@ from .cleanup_facts import * # noqa from .age_deleted import * # noqa from .remove_instance import * # noqa from .run_socketio_service import * # noqa +from .update_password import * # noqa diff --git a/awx/main/tests/commands/update_password.py b/awx/main/tests/commands/update_password.py new file mode 100644 index 0000000000..60eb04de88 --- /dev/null +++ b/awx/main/tests/commands/update_password.py @@ -0,0 +1,37 @@ +# Copyright (c) 2015 Ansible, Inc. +# All Rights Reserved + +# AWX +from awx.main.tests.base import BaseTest +from awx.main.tests.commands.base import BaseCommandMixin + +# Django +from django.core.management.base import CommandError + +__all__ = ['UpdatePasswordCommandFunctionalTest'] + +class UpdatePasswordCommandFunctionalTest(BaseCommandMixin, BaseTest): + def setUp(self): + super(UpdatePasswordCommandFunctionalTest, self).setUp() + self.create_test_license_file() + self.setup_instances() + self.setup_users() + + def test_updated_ok(self): + result, stdout, stderr = self.run_command('update_password', username='admin', password='dingleberry') + self.assertEqual(stdout, 'Password updated\n') + + def test_same_password(self): + result, stdout, stderr = self.run_command('update_password', username='admin', password='admin') + self.assertEqual(stdout, 'Password not updated\n') + + def test_error_username_required(self): + result, stdout, stderr = self.run_command('update_password', password='foo') + self.assertIsInstance(result, CommandError) + self.assertEqual(str(result), 'username required') + + def test_error_password_required(self): + result, stdout, stderr = self.run_command('update_password', username='admin') + self.assertIsInstance(result, CommandError) + self.assertEqual(str(result), 'password required') +