| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import argparse |
| 14 |
from unittest import mock |
| 15 |
import uuid |
| 16 |
|
| 17 |
|
| 18 |
from keystoneclient.auth.identity.generic import cli |
| 19 |
from keystoneclient import exceptions |
| 20 |
from keystoneclient.tests.unit import utils |
| 21 |
|
| 22 |
|
| 23 |
class DefaultCliTests(utils.TestCase): |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
super(DefaultCliTests, self).setUp() |
| 27 |
self.deprecations.expect_deprecations() |
| 28 |
|
| 29 |
def new_plugin(self, argv): |
| 30 |
parser = argparse.ArgumentParser() |
| 31 |
cli.DefaultCLI.register_argparse_arguments(parser) |
| 32 |
opts = parser.parse_args(argv) |
| 33 |
return cli.DefaultCLI.load_from_argparse_arguments(opts) |
| 34 |
|
| 35 |
def test_endpoint_override(self): |
| 36 |
password = uuid.uuid4().hex |
| 37 |
url = uuid.uuid4().hex |
| 38 |
|
| 39 |
p = self.new_plugin(['--os-auth-url', 'url', |
| 40 |
'--os-endpoint', url, |
| 41 |
'--os-password', password]) |
| 42 |
|
| 43 |
self.assertEqual(url, p.get_endpoint(None)) |
| 44 |
self.assertEqual(password, p._password) |
| 45 |
|
| 46 |
def test_token_only_override(self): |
| 47 |
self.assertRaises(exceptions.CommandError, |
| 48 |
self.new_plugin, |
| 49 |
['--os-token', uuid.uuid4().hex]) |
| 50 |
|
| 51 |
def test_token_endpoint_override(self): |
| 52 |
token = uuid.uuid4().hex |
| 53 |
endpoint = uuid.uuid4().hex |
| 54 |
|
| 55 |
p = self.new_plugin(['--os-endpoint', endpoint, |
| 56 |
'--os-token', token]) |
| 57 |
|
| 58 |
self.assertEqual(endpoint, p.get_endpoint(None)) |
| 59 |
self.assertEqual(token, p.get_token(None)) |
| 60 |
|
| 61 |
def test_no_auth_url(self): |
| 62 |
exc = self.assertRaises(exceptions.CommandError, |
| 63 |
self.new_plugin, |
| 64 |
['--os-username', uuid.uuid4().hex]) |
| 65 |
|
| 66 |
self.assertIn('auth-url', str(exc)) |
| 67 |
|
| 68 |
@mock.patch('sys.stdin', autospec=True) |
| 69 |
@mock.patch('getpass.getpass') |
| 70 |
def test_prompt_password(self, mock_getpass, mock_stdin): |
| 71 |
password = uuid.uuid4().hex |
| 72 |
|
| 73 |
mock_stdin.isatty = lambda: True |
| 74 |
mock_getpass.return_value = password |
| 75 |
|
| 76 |
p = self.new_plugin(['--os-auth-url', uuid.uuid4().hex, |
| 77 |
'--os-username', uuid.uuid4().hex]) |
| 78 |
|
| 79 |
self.assertEqual(password, p._password) |
| 80 |
|
| 81 |
@mock.patch('sys.stdin', autospec=True) |
| 82 |
@mock.patch('getpass.getpass') |
| 83 |
def test_prompt_no_password(self, mock_getpass, mock_stdin): |
| 84 |
mock_stdin.isatty = lambda: True |
| 85 |
mock_getpass.return_value = '' |
| 86 |
|
| 87 |
exc = self.assertRaises(exceptions.CommandError, |
| 88 |
self.new_plugin, |
| 89 |
['--os-auth-url', uuid.uuid4().hex, |
| 90 |
'--os-username', uuid.uuid4().hex]) |
| 91 |
|
| 92 |
self.assertIn('password', str(exc)) |