»Core Development>Code coverage>Lib/test/test_getpass.py

Python code coverage for Lib/test/test_getpass.py

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