@@ -21,16 +21,19 @@ import * as testUtils from "../test-utils/test-utils";
2121import { TestClient } from "../TestClient" ;
2222import { logger } from "../../src/logger" ;
2323import {
24+ IClaimOTKsResult ,
2425 IContent ,
26+ IDownloadKeyResult ,
2527 IEvent ,
26- IClaimOTKsResult ,
2728 IJoinedRoom ,
29+ IndexedDBCryptoStore ,
2830 ISyncResponse ,
29- IDownloadKeyResult ,
31+ IUploadKeysRequest ,
3032 MatrixEvent ,
3133 MatrixEventEvent ,
32- IndexedDBCryptoStore ,
3334 Room ,
35+ RoomMember ,
36+ RoomStateEvent ,
3437} from "../../src/matrix" ;
3538import { IDeviceKeys } from "../../src/crypto/dehydration" ;
3639import { DeviceInfo } from "../../src/crypto/deviceinfo" ;
@@ -327,7 +330,9 @@ describe("megolm", () => {
327330 const room = aliceTestClient . client . getRoom ( ROOM_ID ) ! ;
328331 const event = room . getLiveTimeline ( ) . getEvents ( ) [ 0 ] ;
329332 expect ( event . isEncrypted ( ) ) . toBe ( true ) ;
330- const decryptedEvent = await testUtils . awaitDecryption ( event ) ;
333+
334+ // it probably won't be decrypted yet, because it takes a while to process the olm keys
335+ const decryptedEvent = await testUtils . awaitDecryption ( event , { waitOnDecryptionFailure : true } ) ;
331336 expect ( decryptedEvent . getContent ( ) . body ) . toEqual ( '42' ) ;
332337 } ) ;
333338
@@ -873,7 +878,12 @@ describe("megolm", () => {
873878
874879 const room = aliceTestClient . client . getRoom ( ROOM_ID ) ! ;
875880 await room . decryptCriticalEvents ( ) ;
876- expect ( room . getLiveTimeline ( ) . getEvents ( ) [ 0 ] . getContent ( ) . body ) . toEqual ( '42' ) ;
881+
882+ // it probably won't be decrypted yet, because it takes a while to process the olm keys
883+ const decryptedEvent = await testUtils . awaitDecryption (
884+ room . getLiveTimeline ( ) . getEvents ( ) [ 0 ] , { waitOnDecryptionFailure : true } ,
885+ ) ;
886+ expect ( decryptedEvent . getContent ( ) . body ) . toEqual ( '42' ) ;
877887
878888 const exported = await aliceTestClient . client . exportRoomKeys ( ) ;
879889
@@ -1012,7 +1022,9 @@ describe("megolm", () => {
10121022 const room = aliceTestClient . client . getRoom ( ROOM_ID ) ! ;
10131023 const event = room . getLiveTimeline ( ) . getEvents ( ) [ 0 ] ;
10141024 expect ( event . isEncrypted ( ) ) . toBe ( true ) ;
1015- const decryptedEvent = await testUtils . awaitDecryption ( event ) ;
1025+
1026+ // it probably won't be decrypted yet, because it takes a while to process the olm keys
1027+ const decryptedEvent = await testUtils . awaitDecryption ( event , { waitOnDecryptionFailure : true } ) ;
10161028 expect ( decryptedEvent . getRoomId ( ) ) . toEqual ( ROOM_ID ) ;
10171029 expect ( decryptedEvent . getContent ( ) ) . toEqual ( { } ) ;
10181030 expect ( decryptedEvent . getClearContent ( ) ) . toBeUndefined ( ) ;
@@ -1364,4 +1376,90 @@ describe("megolm", () => {
13641376
13651377 await beccaTestClient . stop ( ) ;
13661378 } ) ;
1379+
1380+ it ( "allows sending an encrypted event as soon as room state arrives" , async ( ) => {
1381+ /* Empirically, clients expect to be able to send encrypted events as soon as the
1382+ * RoomStateEvent.NewMember notification is emitted, so test that works correctly.
1383+ */
1384+ const testRoomId = "!testRoom:id" ;
1385+ await aliceTestClient . start ( ) ;
1386+
1387+ aliceTestClient . httpBackend . when ( "POST" , "/keys/query" )
1388+ . respond ( 200 , function ( _path , content : IUploadKeysRequest ) {
1389+ return { device_keys : { } } ;
1390+ } ) ;
1391+
1392+ /* Alice makes the /createRoom call */
1393+ aliceTestClient . httpBackend . when ( "POST" , "/createRoom" )
1394+ . respond ( 200 , { room_id : testRoomId } ) ;
1395+ await Promise . all ( [
1396+ aliceTestClient . client . createRoom ( {
1397+ initial_state : [ {
1398+ type : 'm.room.encryption' ,
1399+ state_key : '' ,
1400+ content : { algorithm : 'm.megolm.v1.aes-sha2' } ,
1401+ } ] ,
1402+ } ) ,
1403+ aliceTestClient . httpBackend . flushAllExpected ( ) ,
1404+ ] ) ;
1405+
1406+ /* The sync arrives in two parts; first the m.room.create... */
1407+ aliceTestClient . httpBackend . when ( "GET" , "/sync" ) . respond ( 200 , {
1408+ rooms : { join : {
1409+ [ testRoomId ] : {
1410+ timeline : { events : [
1411+ {
1412+ type : 'm.room.create' ,
1413+ state_key : '' ,
1414+ event_id : "$create" ,
1415+ } ,
1416+ {
1417+ type : 'm.room.member' ,
1418+ state_key : aliceTestClient . getUserId ( ) ,
1419+ content : { membership : "join" } ,
1420+ event_id : "$alijoin" ,
1421+ } ,
1422+ ] } ,
1423+ } ,
1424+ } } ,
1425+ } ) ;
1426+ await aliceTestClient . flushSync ( ) ;
1427+
1428+ // ... and then the e2e event and an invite ...
1429+ aliceTestClient . httpBackend . when ( "GET" , "/sync" ) . respond ( 200 , {
1430+ rooms : { join : {
1431+ [ testRoomId ] : {
1432+ timeline : { events : [
1433+ {
1434+ type : 'm.room.encryption' ,
1435+ state_key : '' ,
1436+ content : { algorithm : 'm.megolm.v1.aes-sha2' } ,
1437+ event_id : "$e2e" ,
1438+ } ,
1439+ {
1440+ type : 'm.room.member' ,
1441+ state_key : "@other:user" ,
1442+ content : { membership : "invite" } ,
1443+ event_id : "$otherinvite" ,
1444+ } ,
1445+ ] } ,
1446+ } ,
1447+ } } ,
1448+ } ) ;
1449+
1450+ // as soon as the roomMember arrives, try to send a message
1451+ aliceTestClient . client . on ( RoomStateEvent . NewMember , ( _e , _s , member : RoomMember ) => {
1452+ if ( member . userId == "@other:user" ) {
1453+ aliceTestClient . client . sendMessage ( testRoomId , { msgtype : "m.text" , body : "Hello, World" } ) ;
1454+ }
1455+ } ) ;
1456+
1457+ // flush the sync and wait for the /send/ request.
1458+ aliceTestClient . httpBackend . when ( "PUT" , "/send/m.room.encrypted/" )
1459+ . respond ( 200 , ( _path , _content ) => ( { event_id : "asdfgh" } ) ) ;
1460+ await Promise . all ( [
1461+ aliceTestClient . flushSync ( ) ,
1462+ aliceTestClient . httpBackend . flush ( "/send/m.room.encrypted/" , 1 ) ,
1463+ ] ) ;
1464+ } ) ;
13671465} ) ;
0 commit comments