Skip to content

Commit

Permalink
fix tests [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jan 13, 2024
1 parent dadb3d1 commit 0922789
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 48 deletions.
26 changes: 17 additions & 9 deletions core/tauri-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,23 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
}
};

let acl_file = std::fs::read_to_string(out_dir.join(PLUGIN_MANIFESTS_FILE_NAME))
.expect("failed to read plugin manifest map");
let acl: HashMap<String, Manifest> =
serde_json::from_str(&acl_file).expect("failed to parse plugin manifest map");

let capabilities_file = std::fs::read_to_string(out_dir.join(CAPABILITIES_FILE_NAME))
.expect("failed to read capabilities");
let capabilities: HashMap<String, Capability> =
serde_json::from_str(&capabilities_file).expect("failed to parse capabilities");
let acl_file_path = out_dir.join(PLUGIN_MANIFESTS_FILE_NAME);
let acl: HashMap<String, Manifest> = if acl_file_path.exists() {
let acl_file =
std::fs::read_to_string(acl_file_path).expect("failed to read plugin manifest map");
serde_json::from_str(&acl_file).expect("failed to parse plugin manifest map")
} else {
Default::default()
};

let capabilities_file_path = out_dir.join(CAPABILITIES_FILE_NAME);
let capabilities: HashMap<String, Capability> = if capabilities_file_path.exists() {
let capabilities_file =
std::fs::read_to_string(capabilities_file_path).expect("failed to read capabilities");
serde_json::from_str(&capabilities_file).expect("failed to parse capabilities")
} else {
Default::default()
};

let resolved_act = resolve_acl::resolve(acl, capabilities).expect("failed to resolve ACL");

Expand Down
141 changes: 102 additions & 39 deletions core/tauri/src/scope/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,53 @@ impl Scope {

#[cfg(test)]
mod tests {
use tauri_utils::acl::{
resolved::{CommandKey, ResolvedCommand},
ExecutionContext,
};

use super::RemoteDomainAccessScope;
use crate::{
ipc::CallbackFn,
test::{assert_ipc_response, mock_app, MockRuntime},
test::{assert_ipc_response, mock_builder, mock_context, noop_assets, MockRuntime},
window::InvokeRequest,
App, Manager, Window, WindowBuilder,
};

const PLUGIN_NAME: &str = "test";

fn test_context(scopes: Vec<RemoteDomainAccessScope>) -> (App<MockRuntime>, Window<MockRuntime>) {
let app = mock_app();
fn test_context(
request: &InvokeRequest,
scopes: Vec<RemoteDomainAccessScope>,
) -> (App<MockRuntime>, Window<MockRuntime>) {
let mut ctx = mock_context(noop_assets());

ctx.resolved_acl.allowed_commands.insert(
CommandKey {
name: request.cmd.clone(),
context: ExecutionContext::Local,
},
ResolvedCommand {
windows: vec!["main".into()],
scope: 0,
},
);
for scope in &scopes {
ctx.resolved_acl.allowed_commands.insert(
CommandKey {
name: request.cmd.clone(),
context: ExecutionContext::Remote {
domain: scope.domain.clone(),
},
},
ResolvedCommand {
windows: vec!["main".into()],
scope: 0,
},
);
}

let app = mock_builder().build(ctx).unwrap();
let window = WindowBuilder::new(&app, "main", Default::default())
.build()
.unwrap();
Expand Down Expand Up @@ -223,14 +258,18 @@ mod tests {

#[test]
fn scope_not_defined() {
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("app.tauri.app")
.add_window("other")
.add_plugin("path")]);
let request = path_is_absolute_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("app.tauri.app")
.add_window("other")
.add_plugin("path")],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
path_is_absolute_request(),
request,
Err(crate::window::ipc_scope_not_found_error_message(
"main",
"https://tauri.app/",
Expand All @@ -240,28 +279,37 @@ mod tests {

#[test]
fn scope_not_defined_for_window() {
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("second")
.add_plugin("path")]);
let request = path_is_absolute_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("second")
.add_plugin("path")],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
path_is_absolute_request(),
request,
Err(crate::window::ipc_scope_window_error_message("main")),
);
}

#[test]
fn scope_not_defined_for_url() {
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("github.com")
.add_window("main")
.add_plugin("path")]);
let request = path_is_absolute_request();

let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("github.com")
.add_window("main")
.add_plugin("path")],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
path_is_absolute_request(),
request,
Err(crate::window::ipc_scope_domain_error_message(
"https://tauri.app/",
)),
Expand All @@ -270,14 +318,17 @@ mod tests {

#[test]
fn subdomain_is_not_allowed() {
let (_app, mut window) = test_context(vec![
RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path"),
RemoteDomainAccessScope::new("sub.tauri.app")
.add_window("main")
.add_plugin("path"),
]);
let (_app, mut window) = test_context(
&path_is_absolute_request(),
vec![
RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path"),
RemoteDomainAccessScope::new("sub.tauri.app")
.add_window("main")
.add_plugin("path"),
],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(&window, path_is_absolute_request(), Ok(true));
Expand Down Expand Up @@ -308,52 +359,64 @@ mod tests {

#[test]
fn subpath_is_allowed() {
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path")]);
let request = path_is_absolute_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin("path")],
);

window.navigate("https://tauri.app/inner/path".parse().unwrap());
assert_ipc_response(&window, path_is_absolute_request(), Ok(true));
assert_ipc_response(&window, request, Ok(true));
}

#[test]
fn tauri_api_not_allowed() {
let (_app, mut window) = test_context(vec![
RemoteDomainAccessScope::new("tauri.app").add_window("main")
]);
let request = path_is_absolute_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("tauri.app").add_window("main")],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
path_is_absolute_request(),
request,
Err(crate::window::IPC_SCOPE_DOES_NOT_ALLOW),
);
}

#[test]
fn plugin_allowed() {
let (_app, mut window) = test_context(vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin(PLUGIN_NAME)]);
let request = plugin_test_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("tauri.app")
.add_window("main")
.add_plugin(PLUGIN_NAME)],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
plugin_test_request(),
request,
Err(format!("plugin {PLUGIN_NAME} not found")),
);
}

#[test]
fn plugin_not_allowed() {
let (_app, mut window) = test_context(vec![
RemoteDomainAccessScope::new("tauri.app").add_window("main")
]);
let request = plugin_test_request();
let (_app, mut window) = test_context(
&request,
vec![RemoteDomainAccessScope::new("tauri.app").add_window("main")],
);

window.navigate("https://tauri.app".parse().unwrap());
assert_ipc_response(
&window,
plugin_test_request(),
request,
Err(crate::window::IPC_SCOPE_DOES_NOT_ALLOW),
);
}
Expand Down
6 changes: 6 additions & 0 deletions core/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use crate::{
App, Builder, Context, Pattern, Window,
};
use tauri_utils::{
acl::resolved::Resolved,
assets::{AssetKey, Assets, CspHash},
config::{Config, PatternKind, TauriConfig},
};
Expand Down Expand Up @@ -125,6 +126,11 @@ pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
},
_info_plist: (),
pattern: Pattern::Brownfield(std::marker::PhantomData),
resolved_acl: Resolved {
allowed_commands: Default::default(),
denied_commands: Default::default(),
scope: Default::default(),
},
}
}

Expand Down

0 comments on commit 0922789

Please sign in to comment.