Browse Source

fix(core): test invoke key, closes #9960 (#9972)

Lucas Fernandes Nogueira 1 year ago
parent
commit
07b02c9f96
3 changed files with 18 additions and 1 deletions
  1. 5 0
      .changes/fix-test-invoke-key.md
  2. 5 1
      core/tauri/src/app.rs
  3. 8 0
      core/tauri/src/test/mod.rs

+ 5 - 0
.changes/fix-test-invoke-key.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch:bug
+---
+
+Added `test::INVOKE_KEY` to be used in `tauri::InvokePayload` when testing using `test::assert_ipc_response` and `test::get_ipc_response`.

+ 5 - 1
core/tauri/src/app.rs

@@ -989,6 +989,9 @@ pub struct Builder<R: Runtime> {
   /// The script that initializes the `window.__TAURI_POST_MESSAGE__` function.
   invoke_initialization_script: String,
 
+  /// Invoke key. Used to secure IPC calls.
+  pub(crate) invoke_key: String,
+
   /// The setup hook.
   setup: SetupHook<R>,
 
@@ -1047,6 +1050,7 @@ impl<R: Runtime> Builder<R> {
       invoke_responder: Arc::new(window_invoke_responder),
       invoke_initialization_script:
         format!("Object.defineProperty(window, '__TAURI_POST_MESSAGE__', {{ value: (message) => window.ipc.postMessage({}(message)) }})", crate::manager::STRINGIFY_IPC_MESSAGE_FN),
+      invoke_key: crate::generate_invoke_key().expect("failed to generate invoke key"),
       on_page_load: Box::new(|_, _| ()),
       pending_windows: Default::default(),
       plugins: PluginStore::default(),
@@ -1569,7 +1573,7 @@ impl<R: Runtime> Builder<R> {
       self.window_event_listeners,
       (self.menu, self.menu_event_listeners),
       (self.invoke_responder, self.invoke_initialization_script),
-      crate::generate_invoke_key()?,
+      self.invoke_key,
     );
 
     let http_scheme = manager.config().tauri.security.dangerous_use_http_scheme;

+ 8 - 0
core/tauri/src/test/mod.rs

@@ -50,6 +50,7 @@
 //!         callback: tauri::api::ipc::CallbackFn(0),
 //!         error: tauri::api::ipc::CallbackFn(1),
 //!         inner: serde_json::Value::Null,
+//!         invoke_key: Some(tauri::test::INVOKE_KEY.into()),
 //!       },
 //!       Ok(())
 //!     );
@@ -85,6 +86,9 @@ use tauri_utils::{
   config::{CliConfig, Config, PatternKind, TauriConfig},
 };
 
+/// The invoke key used for tests.
+pub const INVOKE_KEY: &str = "__invoke-key__";
+
 /// A key for an [`Ipc`] call.
 #[derive(Eq, PartialEq)]
 struct IpcKey {
@@ -197,6 +201,8 @@ pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
 pub fn mock_builder() -> Builder<MockRuntime> {
   let mut builder = Builder::<MockRuntime>::new().manage(Ipc(Default::default()));
 
+  builder.invoke_key = INVOKE_KEY.to_string();
+
   builder.invoke_responder = Arc::new(|window, response, callback, error| {
     let window_ = window.clone();
     let ipc = window_.state::<Ipc>();
@@ -250,6 +256,7 @@ pub fn mock_app() -> App<MockRuntime> {
 ///       callback: tauri::api::ipc::CallbackFn(0),
 ///       error: tauri::api::ipc::CallbackFn(1),
 ///       inner: serde_json::Value::Null,
+///       invoke_key: Some(tauri::test::INVOKE_KEY.into()),
 ///     },
 ///     // the expected response is a success with the "pong" payload
 ///     // we could also use Err("error message") here to ensure the command failed
@@ -303,6 +310,7 @@ pub fn assert_ipc_response<T: Serialize + Debug>(
 ///     callback: tauri::api::ipc::CallbackFn(0),
 ///     error: tauri::api::ipc::CallbackFn(1),
 ///     inner: serde_json::Value::Null,
+///     invoke_key: Some(tauri::test::INVOKE_KEY.into()),
 ///   });
 /// assert_eq!(res, Ok("pong".into()))
 /// ```