@@ -85,7 +85,7 @@ async fn run_test_driver(
8585
8686async fn recv_message ( driver_handle : & WidgetDriverHandle ) -> JsonObject {
8787 let fut = pin ! ( driver_handle. recv( ) ) ;
88- let msg = timeout ( fut, Duration :: from_secs ( 1 ) ) . await . unwrap ( ) ;
88+ let msg = timeout ( fut, Duration :: from_secs ( 20 ) ) . await . unwrap ( ) ;
8989 serde_json:: from_str ( & msg. unwrap ( ) ) . unwrap ( )
9090}
9191
@@ -95,15 +95,15 @@ async fn send_request(
9595 action : & str ,
9696 data : impl Serialize ,
9797) {
98- let sent = driver_handle
99- . send ( json_string ! ( {
100- "api ": "fromWidget" ,
101- "widgetId ": WIDGET_ID ,
102- "requestId ": request_id ,
103- "action ": action ,
104- "data" : data ,
105- } ) )
106- . await ;
98+ let json_string = json_string ! ( {
99+ "api" : "fromWidget" ,
100+ "widgetId ": WIDGET_ID ,
101+ "requestId ": request_id ,
102+ "action ": action ,
103+ "data ": data ,
104+ } ) ;
105+ println ! ( "Json string sent from the widget {}" , json_string ) ;
106+ let sent = driver_handle . send ( json_string ) . await ;
107107 assert ! ( sent) ;
108108}
109109
@@ -381,6 +381,8 @@ async fn test_receive_live_events() {
381381 "org.matrix.msc2762.receive.event:m.room.message#m.text" ,
382382 "org.matrix.msc2762.receive.state_event:m.room.name#" ,
383383 "org.matrix.msc2762.receive.state_event:m.room.member#@example:localhost" ,
384+ "org.matrix.msc2762.receive.state_event:m.room.member#@example:localhost" ,
385+ "org.matrix.msc3819.receive.to_device:my.custom.to.device"
384386 ] ) ,
385387 )
386388 . await ;
@@ -417,32 +419,63 @@ async fn test_receive_live_events() {
417419 // set room name - matches filter #3
418420 . add_timeline_event ( f. room_name ( "New Room Name" ) . sender ( & BOB ) ) ,
419421 ) ;
422+ // to device message - doesn't match
423+ sync_builder. add_to_device_event ( json ! ( {
424+ "sender" : "@alice:example.com" ,
425+ "type" : "m.not_matching.to.device" ,
426+ "content" : {
427+ "a" : "test" ,
428+ }
429+ } ) ) ;
430+ // to device message - matches filter #6
431+ sync_builder. add_to_device_event ( json ! ( {
432+ "sender" : "@alice:example.com" ,
433+ "type" : "my.custom.to.device" ,
434+ "content" : {
435+ "a" : "test" ,
436+ }
437+ }
438+ ) ) ;
420439 } )
421440 . await ;
422441
423- let msg = recv_message ( & driver_handle) . await ;
424- assert_eq ! ( msg[ "api" ] , "toWidget" ) ;
425- assert_eq ! ( msg[ "action" ] , "send_event" ) ;
426- assert_eq ! ( msg[ "data" ] [ "type" ] , "m.room.message" ) ;
427- assert_eq ! ( msg[ "data" ] [ "room_id" ] , ROOM_ID . as_str( ) ) ;
428- assert_eq ! ( msg[ "data" ] [ "content" ] [ "msgtype" ] , "m.text" ) ;
429- assert_eq ! ( msg[ "data" ] [ "content" ] [ "body" ] , "simple text message" ) ;
430-
431- let msg = recv_message ( & driver_handle) . await ;
432- assert_eq ! ( msg[ "api" ] , "toWidget" ) ;
433- assert_eq ! ( msg[ "action" ] , "send_event" ) ;
434- assert_eq ! ( msg[ "data" ] [ "type" ] , "m.room.member" ) ;
435- assert_eq ! ( msg[ "data" ] [ "room_id" ] , ROOM_ID . as_str( ) ) ;
436- assert_eq ! ( msg[ "data" ] [ "state_key" ] , "@example:localhost" ) ;
437- assert_eq ! ( msg[ "data" ] [ "content" ] [ "membership" ] , "join" ) ;
438- assert_eq ! ( msg[ "data" ] [ "unsigned" ] [ "prev_content" ] [ "membership" ] , "join" ) ;
442+ // The to device and room events are racing -> we dont know the order and just
443+ // need to store them separately.
444+ let mut to_device: JsonObject = JsonObject :: new ( ) ;
445+ let mut events = vec ! [ ] ;
446+ for _ in 0 ..4 {
447+ let msg = recv_message ( & driver_handle) . await ;
448+ if msg[ "action" ] == "send_to_device" {
449+ to_device = msg;
450+ } else {
451+ events. push ( msg) ;
452+ }
453+ }
439454
440- let msg = recv_message ( & driver_handle) . await ;
441- assert_eq ! ( msg[ "api" ] , "toWidget" ) ;
442- assert_eq ! ( msg[ "action" ] , "send_event" ) ;
443- assert_eq ! ( msg[ "data" ] [ "type" ] , "m.room.name" ) ;
444- assert_eq ! ( msg[ "data" ] [ "sender" ] , BOB . as_str( ) ) ;
445- assert_eq ! ( msg[ "data" ] [ "content" ] [ "name" ] , "New Room Name" ) ;
455+ assert_eq ! ( events[ 0 ] [ "api" ] , "toWidget" ) ;
456+ assert_eq ! ( events[ 0 ] [ "action" ] , "send_event" ) ;
457+ assert_eq ! ( events[ 0 ] [ "data" ] [ "type" ] , "m.room.message" ) ;
458+ assert_eq ! ( events[ 0 ] [ "data" ] [ "room_id" ] , ROOM_ID . as_str( ) ) ;
459+ assert_eq ! ( events[ 0 ] [ "data" ] [ "content" ] [ "msgtype" ] , "m.text" ) ;
460+ assert_eq ! ( events[ 0 ] [ "data" ] [ "content" ] [ "body" ] , "simple text message" ) ;
461+
462+ assert_eq ! ( events[ 1 ] [ "api" ] , "toWidget" ) ;
463+ assert_eq ! ( events[ 1 ] [ "action" ] , "send_event" ) ;
464+ assert_eq ! ( events[ 1 ] [ "data" ] [ "type" ] , "m.room.member" ) ;
465+ assert_eq ! ( events[ 1 ] [ "data" ] [ "room_id" ] , ROOM_ID . as_str( ) ) ;
466+ assert_eq ! ( events[ 1 ] [ "data" ] [ "state_key" ] , "@example:localhost" ) ;
467+ assert_eq ! ( events[ 1 ] [ "data" ] [ "content" ] [ "membership" ] , "join" ) ;
468+ assert_eq ! ( events[ 1 ] [ "data" ] [ "unsigned" ] [ "prev_content" ] [ "membership" ] , "join" ) ;
469+
470+ assert_eq ! ( events[ 2 ] [ "api" ] , "toWidget" ) ;
471+ assert_eq ! ( events[ 2 ] [ "action" ] , "send_event" ) ;
472+ assert_eq ! ( events[ 2 ] [ "data" ] [ "type" ] , "m.room.name" ) ;
473+ assert_eq ! ( events[ 2 ] [ "data" ] [ "sender" ] , BOB . as_str( ) ) ;
474+ assert_eq ! ( events[ 2 ] [ "data" ] [ "content" ] [ "name" ] , "New Room Name" ) ;
475+
476+ assert_eq ! ( to_device[ "api" ] , "toWidget" ) ;
477+ assert_eq ! ( to_device[ "action" ] , "send_to_device" ) ;
478+ assert_eq ! ( to_device[ "data" ] [ "type" ] , "my.custom.to.device" ) ;
446479
447480 // No more messages from the driver
448481 assert_matches ! ( recv_message( & driver_handle) . now_or_never( ) , None ) ;
@@ -816,7 +849,6 @@ async fn test_send_redaction() {
816849 ] ) ,
817850 )
818851 . await ;
819-
820852 mock_server. mock_room_redact ( ) . ok ( event_id ! ( "$redact_event_id" ) ) . mock_once ( ) . mount ( ) . await ;
821853
822854 send_request (
@@ -843,6 +875,104 @@ async fn test_send_redaction() {
843875 assert_eq ! ( redact_room_id, "!a98sd12bjh:example.org" ) ;
844876}
845877
878+ async fn send_to_device_test_helper (
879+ request_id : & str ,
880+ data : JsonValue ,
881+ expected_response : JsonValue ,
882+ calls : u64 ,
883+ ) -> JsonValue {
884+ let ( _, mock_server, driver_handle) = run_test_driver ( false ) . await ;
885+
886+ negotiate_capabilities (
887+ & driver_handle,
888+ json ! ( [
889+ "org.matrix.msc3819.send.to_device:my.custom.to_device_type" ,
890+ "org.matrix.msc3819.send.to_device:my.other_type"
891+ ] ) ,
892+ )
893+ . await ;
894+
895+ mock_server. mock_send_to_device ( ) . ok ( ) . expect ( calls) . mount ( ) . await ;
896+
897+ send_request ( & driver_handle, request_id, "send_to_device" , data) . await ;
898+
899+ // Receive the response
900+ let msg = recv_message ( & driver_handle) . await ;
901+ assert_eq ! ( msg[ "api" ] , "fromWidget" ) ;
902+ assert_eq ! ( msg[ "action" ] , "send_to_device" ) ;
903+ let response = msg[ "response" ] . clone ( ) ;
904+ assert_eq ! (
905+ serde_json:: to_string( & response) . unwrap( ) ,
906+ serde_json:: to_string( & expected_response) . unwrap( )
907+ ) ;
908+
909+ response
910+ }
911+
912+ #[ async_test]
913+ async fn test_send_to_device_event ( ) {
914+ send_to_device_test_helper (
915+ "id_my.custom.to_device_type" ,
916+ json ! ( {
917+ "type" : "my.custom.to_device_type" ,
918+ "encrypted" : false ,
919+ "messages" : {
920+ "@username:test.org" : {
921+ "DEVICEID" : {
922+ "param1" : "test" ,
923+ } ,
924+ } ,
925+ }
926+ } ) ,
927+ json ! { { } } ,
928+ 1 ,
929+ )
930+ . await ;
931+ }
932+
933+ #[ async_test]
934+ async fn test_error_to_device_event_no_permission ( ) {
935+ send_to_device_test_helper (
936+ "id_my.unallowed_type" ,
937+ json ! ( {
938+ "type" : "my.unallowed_type" ,
939+ "encrypted" : false ,
940+ "messages" : {
941+ "@username:test.org" : {
942+ "DEVICEID" : {
943+ "param1" : "test" ,
944+ } ,
945+ } ,
946+ }
947+ } ) ,
948+ // this means the server did not get the correct event type
949+ json ! { { "error" : { "message" : "Not allowed to send to-device message of type: my.unallowed_type" } } } ,
950+ 0
951+ )
952+ . await ;
953+ }
954+
955+ #[ async_test]
956+ async fn test_send_encrypted_to_device_event ( ) {
957+ send_to_device_test_helper (
958+ "my.custom.to_device_type" ,
959+ json ! ( {
960+ "type" : "my.custom.to_device_type" ,
961+ "encrypted" : true ,
962+ "messages" : {
963+ "@username:test.org" : {
964+ "DEVICEID" : {
965+ "param1" : "test" ,
966+ } ,
967+ } ,
968+ }
969+ } ) ,
970+ json ! { { } } ,
971+ 1 ,
972+ )
973+ . await ;
974+ }
975+
846976async fn negotiate_capabilities ( driver_handle : & WidgetDriverHandle , caps : JsonValue ) {
847977 {
848978 // Receive toWidget capabilities request
0 commit comments