widget: Add missing unit tests for error cases

This commit is contained in:
Daniel Abramov
2023-10-20 16:06:57 +02:00
committed by Jonas Platte
parent 71627a29a6
commit 4ea8ecaac8
2 changed files with 147 additions and 0 deletions

View File

@@ -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<Action>,

View File

@@ -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(_));
}