From 4ea8ecaac8a52e85e020d0cf6bb61bea9ac44260 Mon Sep 17 00:00:00 2001 From: Daniel Abramov Date: Fri, 20 Oct 2023 16:06:57 +0200 Subject: [PATCH] widget: Add missing unit tests for error cases --- .../src/widget/machine/tests/capabilities.rs | 73 ++++++++++++++++++ .../src/widget/machine/tests/openid.rs | 74 +++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/crates/matrix-sdk/src/widget/machine/tests/capabilities.rs b/crates/matrix-sdk/src/widget/machine/tests/capabilities.rs index 369dc550f..080e80dab 100644 --- a/crates/matrix-sdk/src/widget/machine/tests/capabilities.rs +++ b/crates/matrix-sdk/src/widget/machine/tests/capabilities.rs @@ -65,6 +65,79 @@ fn machine_can_request_capabilities_on_content_load() { assert_capabilities_dance(&mut machine, &mut actions_recv); } +#[test] +fn capabilities_failure_results_into_empty_capabilities() { + let (mut machine, mut actions_recv) = + WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), false); + + // Ask widget to provide desired capabilities. + { + let action = actions_recv.try_recv().unwrap(); + let msg = assert_matches!(action, Action::SendToWidget(msg) => msg); + let (msg, request_id) = parse_msg(&msg); + assert_eq!( + msg, + json!({ + "api": "toWidget", + "widgetId": WIDGET_ID, + "action": "capabilities", + "data": {}, + }), + ); + + machine.process(IncomingMessage::WidgetMessage(json_string!({ + "api": "toWidget", + "widgetId": WIDGET_ID, + "requestId": request_id, + "action": "capabilities", + "data": {}, + "response": { + "capabilities": ["org.matrix.msc2762.receive.state_event:m.room.member"], + }, + }))); + } + + // Try to acquire capabilities by sending a request to a matrix driver. + { + let action = actions_recv.try_recv().unwrap(); + let (request_id, capabilities) = assert_matches!( + action, + Action::MatrixDriverRequest { + request_id, + data: MatrixDriverRequestData::AcquireCapabilities(data) + } => (request_id, data.desired_capabilities) + ); + assert_eq!( + capabilities, + from_value(json!(["org.matrix.msc2762.receive.state_event:m.room.member"])).unwrap() + ); + + machine.process(IncomingMessage::MatrixDriverResponse { + request_id, + response: Err("OHMG!".into()), + }); + } + + // Inform the widget about the new capabilities, or lack of thereof :) + let action = actions_recv.try_recv().unwrap(); + let msg = assert_matches!(action, Action::SendToWidget(msg) => msg); + let (msg, _request_id) = parse_msg(&msg); + assert_eq!( + msg, + json!({ + "api": "toWidget", + "widgetId": WIDGET_ID, + "action": "notify_capabilities", + "data": { + "requested": ["org.matrix.msc2762.receive.state_event:m.room.member"], + "approved": [], + }, + }), + ); + + assert_matches!(actions_recv.try_recv(), Err(_)); +} + pub(super) fn assert_capabilities_dance( machine: &mut WidgetMachine, actions_recv: &mut UnboundedReceiver, diff --git a/crates/matrix-sdk/src/widget/machine/tests/openid.rs b/crates/matrix-sdk/src/widget/machine/tests/openid.rs index 9832bb5bd..bbd523d09 100644 --- a/crates/matrix-sdk/src/widget/machine/tests/openid.rs +++ b/crates/matrix-sdk/src/widget/machine/tests/openid.rs @@ -107,3 +107,77 @@ fn openid_request_handling_works() { // No further actions expected. assert_matches!(actions_recv.try_recv(), Err(_)); } + +#[test] +fn openid_fail_results_in_response_blocked() { + let (mut machine, mut actions_recv) = + WidgetMachine::new(WIDGET_ID.to_owned(), owned_room_id!("!a98sd12bjh:example.org"), true); + + // Widget requests an open ID token, since we don't have any caching yet, + // we reply with a pending response right away. + { + machine.process(IncomingMessage::WidgetMessage(json_string!({ + "api": "fromWidget", + "widgetId": WIDGET_ID, + "requestId": "openid-request-id", + "action": "get_openid", + "data": {}, + }))); + + let action = actions_recv.try_recv().unwrap(); + let msg = assert_matches!(action, Action::SendToWidget(msg) => msg); + let (msg, request_id) = parse_msg(&msg); + assert_eq!(request_id, "openid-request-id"); + assert_eq!( + msg, + json!({ + "api": "fromWidget", + "widgetId": WIDGET_ID, + "action": "get_openid", + "data": {}, + "response": { + "state": "request", + }, + }), + ); + } + + // Then we send an OpenID request to the driver and expect a fail. + { + let action = actions_recv.try_recv().unwrap(); + let request_id = assert_matches!( + action, + Action::MatrixDriverRequest { + request_id, + data: MatrixDriverRequestData::GetOpenId, + } => request_id + ); + + machine.process(IncomingMessage::MatrixDriverResponse { + request_id, + response: Err("Unlucky one".into()), + }); + } + + // We inform the widget about the new OpenID token. + { + let action = actions_recv.try_recv().unwrap(); + let msg = assert_matches!(action, Action::SendToWidget(msg) => msg); + let (msg, _request_id) = parse_msg(&msg); + assert_eq!( + msg, + json!({ + "api": "toWidget", + "widgetId": WIDGET_ID, + "action": "openid_credentials", + "data": { + "state": "blocked", + "original_request_id": "openid-request-id", + }, + }), + ); + } + + // No further actions expected. + assert_matches!(actions_recv.try_recv(), Err(_)); +}