def _check_default_password_function(self, password_func=None): if password_func: self.assertEqual(App.get_configuration().password_function, password_func) else: self.assertEqual(App.get_configuration().password_function, DEFAULT_PASSWORD_FUNC)
def test_clear_password_function(self): self._check_default_password_function() test_func = MagicMock() App.get_configuration().password_function = test_func self._check_default_password_function(test_func) App.get_configuration().clear_password_function() self._check_default_password_function()
def test_clear_width(self): self._check_default_width() test_width = 150 App.get_configuration().width = test_width self._check_default_width(test_width) App.get_configuration().clear_width() self._check_default_width()
def setup(self, data): TextUserInterface.setup(self, data) # Make sure custom getpass() from multi-tty handler is used instead of regular getpass. # This needs to be done as the default getpass() implementation cant work with arbitrary # consoles and always defaults to /dev/tty for input. configuration = App.get_configuration() configuration.password_function = self.multi_tty_handler.custom_getpass
def test_create_instance_with_custom_everything(self): event_loop = CustomEventLoop() App.initialize(event_loop=event_loop, scheduler=CustomScreenScheduler(event_loop), global_configuration=CustomGlobalConfiguration()) self.assertTrue(isinstance(App.get_event_loop(), CustomEventLoop)) self.assertTrue(isinstance(App.get_scheduler(), CustomScreenScheduler)) self.assertTrue(isinstance(App.get_configuration(), CustomGlobalConfiguration))
def test_width(self): self._check_default_width() App.initialize() App.get_configuration().width = 100 self._check_default_width(100) App.initialize() self._check_default_width()
def test_zero_width_no_separator(self, stdout_mock): ui_screen = EmptyScreenMock() width = 0 App.get_configuration().width = width App.get_scheduler().schedule_screen(ui_screen) App.run() self.assertEqual("\n\n", stdout_mock.getvalue())
def test_password_function(self): self._check_default_password_function() test_mock = MagicMock() App.initialize() App.get_configuration().password_function = test_mock self._check_default_password_function(test_mock) App.initialize() self._check_default_password_function()
def test_other_width_separator(self, stdout_mock): ui_screen = EmptyScreenMock() width = 60 App.get_configuration().width = width App.get_scheduler().schedule_screen(ui_screen) App.run() self.assertEqual(self.calculate_separator(width), stdout_mock.getvalue())
def __init__(self, callback=None, source=None): """Class to handle hidden password input from the terminal. This class is designed to be instantiated on place where it should be used. The main method is `get_input()` which is non-blocking asynchronous call. It can be used as synchronous call be calling the `wait_on_input` method. To get result from this class use the `value` property. :param callback: You can specify callback which will be called when user give input. :type callback: Callback function with one argument which will be user input. :param source: Source of this input. It will be helpful in case of debugging an issue. :type source: Class which will process an input from this InputHandler. """ super().__init__(callback=callback, source=source) self._getpass_func = App.get_configuration().password_function
def test_reinitialize(self): event_loop1 = CustomEventLoop() event_loop2 = CustomEventLoop() scheduler1 = CustomScreenScheduler(event_loop1) scheduler2 = CustomScreenScheduler(event_loop2) configuration1 = CustomGlobalConfiguration() configuration2 = CustomGlobalConfiguration() App.initialize(event_loop=event_loop1, scheduler=scheduler1, global_configuration=configuration1) self._check_app_settings(event_loop1, scheduler1, configuration1) App.initialize(event_loop=event_loop2, scheduler=scheduler2, global_configuration=configuration2) self._check_app_settings(event_loop2, scheduler2, configuration2) App.initialize() self.assertNotEqual(App.get_event_loop(), event_loop2) self.assertNotEqual(App.get_scheduler(), scheduler2) self.assertNotEqual(App.get_configuration(), configuration2)
def test_run_shortcut(self, run_mock): App.initialize() App.get_configuration().should_run_with_empty_stack = True App.run() self.assertTrue(run_mock.called)
def test_create_instance_with_configuration(self): App.initialize(global_configuration=CustomGlobalConfiguration()) self.assertTrue(isinstance(App.get_configuration(), CustomGlobalConfiguration))
def test_create_instance(self): App.initialize() self.assertTrue(isinstance(App.get_scheduler(), ScreenScheduler)) self.assertTrue(isinstance(App.get_event_loop(), MainLoop)) self.assertTrue(isinstance(App.get_configuration(), GlobalConfiguration))
def create_thread_object(self, prompt): """Return PasswordInputThread for getting user password.""" return PasswordInputHandlerRequest(App.get_configuration().width, prompt, self, self._getpass_func)
def create_thread_object(self, prompt): """Create thread object containing all the information how to get user input. :returns: Instance of class inherited from `simpleline.input.InputThread`. """ return InputHandlerRequest(App.get_configuration().width, prompt, self)
def show_all(self): """Print WindowContainer in `self.window` with all its content.""" self.window.render(App.get_configuration().width) self._print_widget(self.window)
def _check_app_settings(self, event_loop, scheduler, configuration): self.assertEqual(App.get_event_loop(), event_loop) self.assertEqual(App.get_scheduler(), scheduler) self.assertEqual(App.get_configuration(), configuration)
def _spacer(): return "\n".join(2 * [App.get_configuration().width * "="])
def _check_default_width(self, width=DEFAULT_WIDTH): self.assertEqual(App.get_configuration().width, width)