@@ -32,6 +32,7 @@ import { Widget } from '../src/models/Widget';
3232import { PostmessageTransport } from '../src/transport/PostmessageTransport' ;
3333import {
3434 IDownloadFileActionFromWidgetActionRequest ,
35+ IGetOpenIDActionRequest ,
3536 IMatrixApiError ,
3637 INavigateActionRequest ,
3738 IReadEventFromWidgetActionRequest ,
@@ -40,9 +41,13 @@ import {
4041 IUpdateDelayedEventFromWidgetActionRequest ,
4142 IUploadFileActionFromWidgetActionRequest ,
4243 IWidgetApiErrorResponseDataDetails ,
44+ OpenIDRequestState ,
45+ SimpleObservable ,
46+ Symbols ,
4347 UpdateDelayedEventAction ,
4448} from '../src' ;
4549import { IGetMediaConfigActionFromWidgetActionRequest } from '../src/interfaces/GetMediaConfigAction' ;
50+ import { IReadRoomAccountDataFromWidgetActionRequest } from '../src/interfaces/ReadRoomAccountDataAction' ;
4651
4752jest . mock ( '../src/transport/PostmessageTransport' ) ;
4853
@@ -122,6 +127,8 @@ describe('ClientWidgetApi', () => {
122127 sendDelayedEvent : jest . fn ( ) ,
123128 updateDelayedEvent : jest . fn ( ) ,
124129 sendToDevice : jest . fn ( ) ,
130+ askOpenID : jest . fn ( ) ,
131+ readRoomAccountData : jest . fn ( ) ,
125132 validateCapabilities : jest . fn ( ) ,
126133 searchUserDirectory : jest . fn ( ) ,
127134 getMediaConfig : jest . fn ( ) ,
@@ -1119,6 +1126,144 @@ describe('ClientWidgetApi', () => {
11191126 } ) ;
11201127 } ) ;
11211128
1129+ describe ( 'get_openid action' , ( ) => {
1130+ it ( 'gets info' , async ( ) => {
1131+ driver . askOpenID . mockImplementation ( ( observable ) => {
1132+ observable . update ( {
1133+ state : OpenIDRequestState . Allowed ,
1134+ token : {
1135+ access_token : "access_token" ,
1136+ } ,
1137+ } ) ;
1138+ } ) ;
1139+
1140+ const event : IGetOpenIDActionRequest = {
1141+ api : WidgetApiDirection . FromWidget ,
1142+ widgetId : 'test' ,
1143+ requestId : '0' ,
1144+ action : WidgetApiFromWidgetAction . GetOpenIDCredentials ,
1145+ data : { } ,
1146+ } ;
1147+
1148+ await loadIframe ( [ ] ) ;
1149+
1150+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1151+
1152+ await waitFor ( ( ) => {
1153+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1154+ state : OpenIDRequestState . Allowed ,
1155+ access_token : "access_token" ,
1156+ } ) ;
1157+ } ) ;
1158+
1159+ expect ( driver . askOpenID ) . toHaveBeenCalledWith ( expect . any ( SimpleObservable ) ) ;
1160+ } ) ;
1161+
1162+ it ( 'fails when client provided invalid token' , async ( ) => {
1163+ driver . askOpenID . mockImplementation ( ( observable ) => {
1164+ observable . update ( {
1165+ state : OpenIDRequestState . Allowed ,
1166+ } ) ;
1167+ } ) ;
1168+
1169+ const event : IGetOpenIDActionRequest = {
1170+ api : WidgetApiDirection . FromWidget ,
1171+ widgetId : 'test' ,
1172+ requestId : '0' ,
1173+ action : WidgetApiFromWidgetAction . GetOpenIDCredentials ,
1174+ data : { } ,
1175+ } ;
1176+
1177+ await loadIframe ( [ ] ) ;
1178+
1179+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1180+
1181+ await waitFor ( ( ) => {
1182+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1183+ error : { message : 'client provided invalid OIDC token for an allowed request' } ,
1184+ } ) ;
1185+ } ) ;
1186+
1187+ expect ( driver . askOpenID ) . toHaveBeenCalledWith ( expect . any ( SimpleObservable ) ) ;
1188+ } ) ;
1189+ } ) ;
1190+
1191+ describe ( 'com.beeper.read_room_account_data action' , ( ) => {
1192+ it ( 'reads room account data' , async ( ) => {
1193+ const type = 'net.example.test' ;
1194+ const roomId = '!room:example.org' ;
1195+
1196+ driver . readRoomAccountData . mockResolvedValue ( [ {
1197+ type,
1198+ room_id : roomId ,
1199+ content : { } ,
1200+ } ] ) ;
1201+
1202+ const event : IReadRoomAccountDataFromWidgetActionRequest = {
1203+ api : WidgetApiDirection . FromWidget ,
1204+ widgetId : 'test' ,
1205+ requestId : '0' ,
1206+ action : WidgetApiFromWidgetAction . BeeperReadRoomAccountData ,
1207+ data : {
1208+ room_ids : [ roomId ] ,
1209+ type,
1210+ } ,
1211+ } ;
1212+
1213+ await loadIframe ( [
1214+ `com.beeper.capabilities.receive.room_account_data:${ type } ` ,
1215+ ] ) ;
1216+
1217+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1218+
1219+ await waitFor ( ( ) => {
1220+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1221+ events : [ {
1222+ type,
1223+ room_id : roomId ,
1224+ content : { } ,
1225+ } ] ,
1226+ } ) ;
1227+ } ) ;
1228+
1229+ expect ( driver . readRoomAccountData ) . toHaveBeenCalledWith ( event . data . type ) ;
1230+ } ) ;
1231+
1232+ it ( 'does not read room account data' , async ( ) => {
1233+ const type = 'net.example.test' ;
1234+ const roomId = '!room:example.org' ;
1235+
1236+ driver . readRoomAccountData . mockResolvedValue ( [ {
1237+ type,
1238+ room_id : roomId ,
1239+ content : { } ,
1240+ } ] ) ;
1241+
1242+ const event : IReadRoomAccountDataFromWidgetActionRequest = {
1243+ api : WidgetApiDirection . FromWidget ,
1244+ widgetId : 'test' ,
1245+ requestId : '0' ,
1246+ action : WidgetApiFromWidgetAction . BeeperReadRoomAccountData ,
1247+ data : {
1248+ room_ids : [ roomId ] ,
1249+ type,
1250+ } ,
1251+ } ;
1252+
1253+ await loadIframe ( [ ] ) ; // Without the required capability
1254+
1255+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1256+
1257+ await waitFor ( ( ) => {
1258+ expect ( transport . reply ) . toHaveBeenCalledWith ( event , {
1259+ error : { message : 'Cannot read room account data of this type' } ,
1260+ } ) ;
1261+ } ) ;
1262+
1263+ expect ( driver . readRoomAccountData ) . toHaveBeenCalledWith ( event . data . type ) ;
1264+ } ) ;
1265+ } ) ;
1266+
11221267 describe ( 'org.matrix.msc2876.read_events action' , ( ) => {
11231268 it ( 'reads state events with any state key' , async ( ) => {
11241269 driver . readStateEvents . mockResolvedValue ( [
@@ -1238,6 +1383,90 @@ describe('ClientWidgetApi', () => {
12381383
12391384 expect ( driver . readStateEvents ) . not . toBeCalled ( ) ;
12401385 } ) ;
1386+
1387+ it ( 'reads state events from specific rooms' , async ( ) => {
1388+ const type = 'net.example.test' ;
1389+ const roomId = '!room:example.org' ;
1390+
1391+ driver . readStateEvents . mockResolvedValue ( [
1392+ createRoomEvent ( { type, state_key : 'A' , room_id : roomId } ) ,
1393+ createRoomEvent ( { type, state_key : 'B' , room_id : roomId } ) ,
1394+ ] ) ;
1395+
1396+ const event : IReadEventFromWidgetActionRequest = {
1397+ api : WidgetApiDirection . FromWidget ,
1398+ widgetId : 'test' ,
1399+ requestId : '0' ,
1400+ action : WidgetApiFromWidgetAction . MSC2876ReadEvents ,
1401+ data : {
1402+ type,
1403+ state_key : true ,
1404+ room_ids : [ roomId ] ,
1405+ } ,
1406+ } ;
1407+
1408+ await loadIframe ( [
1409+ `org.matrix.msc2762.timeline:${ roomId } ` ,
1410+ `org.matrix.msc2762.receive.state_event:${ type } ` ,
1411+ ] ) ;
1412+
1413+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1414+
1415+ await waitFor ( ( ) => {
1416+ expect ( transport . reply ) . toBeCalledWith ( event , {
1417+ events : [
1418+ createRoomEvent ( { type, state_key : 'A' , room_id : roomId } ) ,
1419+ createRoomEvent ( { type, state_key : 'B' , room_id : roomId } ) ,
1420+ ] ,
1421+ } ) ;
1422+ } ) ;
1423+
1424+ expect ( driver . readStateEvents ) . toBeCalledWith (
1425+ type , undefined , 0 , [ roomId ] ,
1426+ ) ;
1427+ } ) ;
1428+
1429+ it ( 'reads state events from any room' , async ( ) => {
1430+ const type = 'net.example.test' ;
1431+ const roomId = '!room:example.org' ;
1432+
1433+ driver . readStateEvents . mockResolvedValue ( [
1434+ createRoomEvent ( { type, state_key : 'A' , room_id : roomId } ) ,
1435+ createRoomEvent ( { type, state_key : 'B' , room_id : roomId } ) ,
1436+ ] ) ;
1437+
1438+ const event : IReadEventFromWidgetActionRequest = {
1439+ api : WidgetApiDirection . FromWidget ,
1440+ widgetId : 'test' ,
1441+ requestId : '0' ,
1442+ action : WidgetApiFromWidgetAction . MSC2876ReadEvents ,
1443+ data : {
1444+ type,
1445+ state_key : true ,
1446+ room_ids : Symbols . AnyRoom ,
1447+ } ,
1448+ } ;
1449+
1450+ await loadIframe ( [
1451+ `org.matrix.msc2762.timeline:${ Symbols . AnyRoom } ` ,
1452+ `org.matrix.msc2762.receive.state_event:${ type } ` ,
1453+ ] ) ;
1454+
1455+ emitEvent ( new CustomEvent ( '' , { detail : event } ) ) ;
1456+
1457+ await waitFor ( ( ) => {
1458+ expect ( transport . reply ) . toBeCalledWith ( event , {
1459+ events : [
1460+ createRoomEvent ( { type, state_key : 'A' , room_id : roomId } ) ,
1461+ createRoomEvent ( { type, state_key : 'B' , room_id : roomId } ) ,
1462+ ] ,
1463+ } ) ;
1464+ } ) ;
1465+
1466+ expect ( driver . readStateEvents ) . toBeCalledWith (
1467+ type , undefined , 0 , [ Symbols . AnyRoom ] ,
1468+ ) ;
1469+ } ) ;
12411470 } ) ;
12421471
12431472 describe ( 'org.matrix.msc3869.read_relations action' , ( ) => {
0 commit comments