Skip to content

Commit 59da53c

Browse files
committed
Add test
1 parent 977389a commit 59da53c

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lib/test/test_getpass.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,32 @@ def test_falls_back_to_stdin(self):
161161
self.assertIn('Warning', stderr.getvalue())
162162
self.assertIn('Password:', stderr.getvalue())
163163

164+
def test_echochar_replaces_input_with_asterisks(self):
165+
mock_result = '*************'
166+
with mock.patch('os.open') as os_open, \
167+
mock.patch('io.FileIO'), \
168+
mock.patch('io.TextIOWrapper') as textio, \
169+
mock.patch('termios.tcgetattr'), \
170+
mock.patch('termios.tcsetattr'), \
171+
mock.patch('getpass._input_with_echochar') as mock_input:
172+
os_open.return_value = 3
173+
mock_input.return_value = mock_result
174+
175+
result = getpass.unix_getpass(echochar='*')
176+
mock_input.assert_called_once_with('Password: ', textio(), textio(), '*')
177+
self.assertEqual(result, mock_result)
178+
179+
def test_input_with_echochar(self):
180+
passwd = 'my1pa$$word!'
181+
mock_input = StringIO(f'{passwd}\n')
182+
mock_output = StringIO()
183+
with mock.patch('sys.stdin', mock_input), \
184+
mock.patch('sys.stdout', mock_output):
185+
result = getpass._input_with_echochar('Password: ', mock_output,
186+
mock_input, '*')
187+
self.assertEqual(result, passwd)
188+
self.assertEqual('Password: ************', mock_output.getvalue())
189+
164190

165191
if __name__ == "__main__":
166192
unittest.main()

0 commit comments

Comments
 (0)