From 102b8a4f91beb329de8367fc7113dda0bb9911c2 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Tue, 10 May 2016 10:37:20 -0400 Subject: [PATCH] refector old update_password tests --- .../functional/commands/test_commands.py | 52 +++++++++++++++++++ .../tests/old/commands/update_password.py | 37 ------------- 2 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 awx/main/tests/functional/commands/test_commands.py delete mode 100644 awx/main/tests/old/commands/update_password.py diff --git a/awx/main/tests/functional/commands/test_commands.py b/awx/main/tests/functional/commands/test_commands.py new file mode 100644 index 0000000000..5d661ca80e --- /dev/null +++ b/awx/main/tests/functional/commands/test_commands.py @@ -0,0 +1,52 @@ +import sys +import pytest + +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +from django.core.management import call_command + +from awx.main.management.commands.update_password import UpdatePassword + +def run_command(name, *args, **options): + command_runner = options.pop('command_runner', call_command) + stdin_fileobj = options.pop('stdin_fileobj', None) + options.setdefault('verbosity', 1) + options.setdefault('interactive', False) + original_stdin = sys.stdin + original_stdout = sys.stdout + original_stderr = sys.stderr + if stdin_fileobj: + sys.stdin = stdin_fileobj + sys.stdout = StringIO() + sys.stderr = StringIO() + result = None + try: + result = command_runner(name, *args, **options) + except Exception as e: + result = e + finally: + captured_stdout = sys.stdout.getvalue() + captured_stderr = sys.stderr.getvalue() + sys.stdin = original_stdin + sys.stdout = original_stdout + sys.stderr = original_stderr + return result, captured_stdout, captured_stderr + +@pytest.mark.parametrize( + "command,username,password,expected,changed", [ + ('update_password', 'admin', 'dingleberry', 'Password updated\n', True), + ('update_password', 'admin', 'admin', 'Password not updated\n', False), + ('update_password', None, 'foo', 'username required', False), + ('update_password', 'admin', None, 'password required', False), + ] +) +def test_run_command(mocker, command, username, password, expected, changed): + with mocker.patch.object(UpdatePassword, 'update_password', return_value=changed): + result, stdout, stderr = run_command(command, username=username, password=password) + if result is None: + assert stdout == expected + else: + assert str(result) == expected diff --git a/awx/main/tests/old/commands/update_password.py b/awx/main/tests/old/commands/update_password.py deleted file mode 100644 index cecbb1667d..0000000000 --- a/awx/main/tests/old/commands/update_password.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (c) 2015 Ansible, Inc. -# All Rights Reserved - -# AWX -from awx.main.tests.base import BaseTest -from command_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') -