| 1 | n/a | import getpass |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import unittest |
|---|
| 4 | n/a | from io import BytesIO, StringIO, TextIOWrapper |
|---|
| 5 | n/a | from unittest import mock |
|---|
| 6 | n/a | from test import support |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | try: |
|---|
| 9 | n/a | import termios |
|---|
| 10 | n/a | except ImportError: |
|---|
| 11 | n/a | termios = None |
|---|
| 12 | n/a | try: |
|---|
| 13 | n/a | import pwd |
|---|
| 14 | n/a | except ImportError: |
|---|
| 15 | n/a | pwd = None |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | @mock.patch('os.environ') |
|---|
| 18 | n/a | class GetpassGetuserTest(unittest.TestCase): |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | def test_username_takes_username_from_env(self, environ): |
|---|
| 21 | n/a | expected_name = 'some_name' |
|---|
| 22 | n/a | environ.get.return_value = expected_name |
|---|
| 23 | n/a | self.assertEqual(expected_name, getpass.getuser()) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | def test_username_priorities_of_env_values(self, environ): |
|---|
| 26 | n/a | environ.get.return_value = None |
|---|
| 27 | n/a | try: |
|---|
| 28 | n/a | getpass.getuser() |
|---|
| 29 | n/a | except ImportError: # in case there's no pwd module |
|---|
| 30 | n/a | pass |
|---|
| 31 | n/a | self.assertEqual( |
|---|
| 32 | n/a | environ.get.call_args_list, |
|---|
| 33 | n/a | [mock.call(x) for x in ('LOGNAME', 'USER', 'LNAME', 'USERNAME')]) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | def test_username_falls_back_to_pwd(self, environ): |
|---|
| 36 | n/a | expected_name = 'some_name' |
|---|
| 37 | n/a | environ.get.return_value = None |
|---|
| 38 | n/a | if pwd: |
|---|
| 39 | n/a | with mock.patch('os.getuid') as uid, \ |
|---|
| 40 | n/a | mock.patch('pwd.getpwuid') as getpw: |
|---|
| 41 | n/a | uid.return_value = 42 |
|---|
| 42 | n/a | getpw.return_value = [expected_name] |
|---|
| 43 | n/a | self.assertEqual(expected_name, |
|---|
| 44 | n/a | getpass.getuser()) |
|---|
| 45 | n/a | getpw.assert_called_once_with(42) |
|---|
| 46 | n/a | else: |
|---|
| 47 | n/a | self.assertRaises(ImportError, getpass.getuser) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | class GetpassRawinputTest(unittest.TestCase): |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def test_flushes_stream_after_prompt(self): |
|---|
| 53 | n/a | # see issue 1703 |
|---|
| 54 | n/a | stream = mock.Mock(spec=StringIO) |
|---|
| 55 | n/a | input = StringIO('input_string') |
|---|
| 56 | n/a | getpass._raw_input('some_prompt', stream, input=input) |
|---|
| 57 | n/a | stream.flush.assert_called_once_with() |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | def test_uses_stderr_as_default(self): |
|---|
| 60 | n/a | input = StringIO('input_string') |
|---|
| 61 | n/a | prompt = 'some_prompt' |
|---|
| 62 | n/a | with mock.patch('sys.stderr') as stderr: |
|---|
| 63 | n/a | getpass._raw_input(prompt, input=input) |
|---|
| 64 | n/a | stderr.write.assert_called_once_with(prompt) |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | @mock.patch('sys.stdin') |
|---|
| 67 | n/a | def test_uses_stdin_as_default_input(self, mock_input): |
|---|
| 68 | n/a | mock_input.readline.return_value = 'input_string' |
|---|
| 69 | n/a | getpass._raw_input(stream=StringIO()) |
|---|
| 70 | n/a | mock_input.readline.assert_called_once_with() |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | @mock.patch('sys.stdin') |
|---|
| 73 | n/a | def test_uses_stdin_as_different_locale(self, mock_input): |
|---|
| 74 | n/a | stream = TextIOWrapper(BytesIO(), encoding="ascii") |
|---|
| 75 | n/a | mock_input.readline.return_value = "HasÃ
âo: " |
|---|
| 76 | n/a | getpass._raw_input(prompt="HasÃ
âo: ",stream=stream) |
|---|
| 77 | n/a | mock_input.readline.assert_called_once_with() |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def test_raises_on_empty_input(self): |
|---|
| 81 | n/a | input = StringIO('') |
|---|
| 82 | n/a | self.assertRaises(EOFError, getpass._raw_input, input=input) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def test_trims_trailing_newline(self): |
|---|
| 85 | n/a | input = StringIO('test\n') |
|---|
| 86 | n/a | self.assertEqual('test', getpass._raw_input(input=input)) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | # Some of these tests are a bit white-box. The functional requirement is that |
|---|
| 90 | n/a | # the password input be taken directly from the tty, and that it not be echoed |
|---|
| 91 | n/a | # on the screen, unless we are falling back to stderr/stdin. |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | # Some of these might run on platforms without termios, but play it safe. |
|---|
| 94 | n/a | @unittest.skipUnless(termios, 'tests require system with termios') |
|---|
| 95 | n/a | class UnixGetpassTest(unittest.TestCase): |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | def test_uses_tty_directly(self): |
|---|
| 98 | n/a | with mock.patch('os.open') as open, \ |
|---|
| 99 | n/a | mock.patch('io.FileIO') as fileio, \ |
|---|
| 100 | n/a | mock.patch('io.TextIOWrapper') as textio: |
|---|
| 101 | n/a | # By setting open's return value to None the implementation will |
|---|
| 102 | n/a | # skip code we don't care about in this test. We can mock this out |
|---|
| 103 | n/a | # fully if an alternate implementation works differently. |
|---|
| 104 | n/a | open.return_value = None |
|---|
| 105 | n/a | getpass.unix_getpass() |
|---|
| 106 | n/a | open.assert_called_once_with('/dev/tty', |
|---|
| 107 | n/a | os.O_RDWR | os.O_NOCTTY) |
|---|
| 108 | n/a | fileio.assert_called_once_with(open.return_value, 'w+') |
|---|
| 109 | n/a | textio.assert_called_once_with(fileio.return_value) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def test_resets_termios(self): |
|---|
| 112 | n/a | with mock.patch('os.open') as open, \ |
|---|
| 113 | n/a | mock.patch('io.FileIO'), \ |
|---|
| 114 | n/a | mock.patch('io.TextIOWrapper'), \ |
|---|
| 115 | n/a | mock.patch('termios.tcgetattr') as tcgetattr, \ |
|---|
| 116 | n/a | mock.patch('termios.tcsetattr') as tcsetattr: |
|---|
| 117 | n/a | open.return_value = 3 |
|---|
| 118 | n/a | fake_attrs = [255, 255, 255, 255, 255] |
|---|
| 119 | n/a | tcgetattr.return_value = list(fake_attrs) |
|---|
| 120 | n/a | getpass.unix_getpass() |
|---|
| 121 | n/a | tcsetattr.assert_called_with(3, mock.ANY, fake_attrs) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | def test_falls_back_to_fallback_if_termios_raises(self): |
|---|
| 124 | n/a | with mock.patch('os.open') as open, \ |
|---|
| 125 | n/a | mock.patch('io.FileIO') as fileio, \ |
|---|
| 126 | n/a | mock.patch('io.TextIOWrapper') as textio, \ |
|---|
| 127 | n/a | mock.patch('termios.tcgetattr'), \ |
|---|
| 128 | n/a | mock.patch('termios.tcsetattr') as tcsetattr, \ |
|---|
| 129 | n/a | mock.patch('getpass.fallback_getpass') as fallback: |
|---|
| 130 | n/a | open.return_value = 3 |
|---|
| 131 | n/a | fileio.return_value = BytesIO() |
|---|
| 132 | n/a | tcsetattr.side_effect = termios.error |
|---|
| 133 | n/a | getpass.unix_getpass() |
|---|
| 134 | n/a | fallback.assert_called_once_with('Password: ', |
|---|
| 135 | n/a | textio.return_value) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | def test_flushes_stream_after_input(self): |
|---|
| 138 | n/a | # issue 7208 |
|---|
| 139 | n/a | with mock.patch('os.open') as open, \ |
|---|
| 140 | n/a | mock.patch('io.FileIO'), \ |
|---|
| 141 | n/a | mock.patch('io.TextIOWrapper'), \ |
|---|
| 142 | n/a | mock.patch('termios.tcgetattr'), \ |
|---|
| 143 | n/a | mock.patch('termios.tcsetattr'): |
|---|
| 144 | n/a | open.return_value = 3 |
|---|
| 145 | n/a | mock_stream = mock.Mock(spec=StringIO) |
|---|
| 146 | n/a | getpass.unix_getpass(stream=mock_stream) |
|---|
| 147 | n/a | mock_stream.flush.assert_called_with() |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def test_falls_back_to_stdin(self): |
|---|
| 150 | n/a | with mock.patch('os.open') as os_open, \ |
|---|
| 151 | n/a | mock.patch('sys.stdin', spec=StringIO) as stdin: |
|---|
| 152 | n/a | os_open.side_effect = IOError |
|---|
| 153 | n/a | stdin.fileno.side_effect = AttributeError |
|---|
| 154 | n/a | with support.captured_stderr() as stderr: |
|---|
| 155 | n/a | with self.assertWarns(getpass.GetPassWarning): |
|---|
| 156 | n/a | getpass.unix_getpass() |
|---|
| 157 | n/a | stdin.readline.assert_called_once_with() |
|---|
| 158 | n/a | self.assertIn('Warning', stderr.getvalue()) |
|---|
| 159 | n/a | self.assertIn('Password:', stderr.getvalue()) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | if __name__ == "__main__": |
|---|
| 163 | n/a | unittest.main() |
|---|