def test_enqueue_new_session_message(self, _1, _2, patched_config): def get_option(name): if name == "server.runOnSave": # Just to avoid starting the watcher for no reason. return False return config.get_option(name) patched_config.get_option.side_effect = get_option patched_config.get_options_for_section.side_effect = ( _mock_get_options_for_section() ) # Create a AppSession with some mocked bits rs = AppSession( self.io_loop, SessionData("mock_report.py", ""), UploadedFileManager(), lambda: None, MagicMock(), ) orig_ctx = get_script_run_ctx() ctx = ScriptRunContext( "TestSessionID", rs._session_data.enqueue, "", None, None ) add_script_run_ctx(ctx=ctx) rs._on_scriptrunner_event(ScriptRunnerEvent.SCRIPT_STARTED) sent_messages = rs._session_data._browser_queue._queue self.assertEqual(len(sent_messages), 2) # NewApp and SessionState messages # Note that we're purposefully not very thoroughly testing new_session # fields below to avoid getting to the point where we're just # duplicating code in tests. new_session_msg = sent_messages[0].new_session self.assertEqual("mock_scriptrun_id", new_session_msg.script_run_id) self.assertEqual(new_session_msg.HasField("config"), True) self.assertEqual( new_session_msg.config.allow_run_on_save, config.get_option("server.allowRunOnSave"), ) self.assertEqual(new_session_msg.HasField("custom_theme"), True) self.assertEqual(new_session_msg.custom_theme.text_color, "black") init_msg = new_session_msg.initialize self.assertEqual(init_msg.HasField("user_info"), True) add_script_run_ctx(ctx=orig_ctx)
def test_enqueue_new_session_message(self, patched_config): """The SCRIPT_STARTED event should enqueue a 'new_session' message.""" def get_option(name): if name == "server.runOnSave": # Just to avoid starting the watcher for no reason. return False return config.get_option(name) patched_config.get_option.side_effect = get_option patched_config.get_options_for_section.side_effect = ( _mock_get_options_for_section() ) # Create a AppSession with some mocked bits session = AppSession( ioloop=self.io_loop, session_data=SessionData("mock_report.py", ""), uploaded_file_manager=UploadedFileManager(), message_enqueued_callback=lambda: None, local_sources_watcher=MagicMock(), ) orig_ctx = get_script_run_ctx() ctx = ScriptRunContext( session_id="TestSessionID", enqueue=session._session_data.enqueue, query_string="", session_state=MagicMock(), uploaded_file_mgr=MagicMock(), ) add_script_run_ctx(ctx=ctx) # Send a mock SCRIPT_STARTED event. session._on_scriptrunner_event( sender=MagicMock(), event=ScriptRunnerEvent.SCRIPT_STARTED ) sent_messages = session._session_data._browser_queue._queue self.assertEqual(2, len(sent_messages)) # NewApp and SessionState messages # Note that we're purposefully not very thoroughly testing new_session # fields below to avoid getting to the point where we're just # duplicating code in tests. new_session_msg = sent_messages[0].new_session self.assertEqual("mock_scriptrun_id", new_session_msg.script_run_id) self.assertTrue(new_session_msg.HasField("config")) self.assertEqual( config.get_option("server.allowRunOnSave"), new_session_msg.config.allow_run_on_save, ) self.assertTrue(new_session_msg.HasField("custom_theme")) self.assertEqual("black", new_session_msg.custom_theme.text_color) init_msg = new_session_msg.initialize self.assertTrue(init_msg.HasField("user_info")) add_script_run_ctx(ctx=orig_ctx)