@@ -23,12 +23,14 @@ import {
23
23
SecretStorageCallbacks ,
24
24
SecretStorageKeyDescriptionAesV1 ,
25
25
SecretStorageKeyDescriptionCommon ,
26
+ ServerSideSecretStorage ,
26
27
ServerSideSecretStorageImpl ,
27
28
trimTrailingEquals ,
28
29
} from "../../src/secret-storage" ;
29
30
import { randomString } from "../../src/randomstring" ;
30
31
import { SecretInfo } from "../../src/secret-storage.ts" ;
31
- import { AccountDataEvents } from "../../src" ;
32
+ import { AccountDataEvents , ClientEvent , MatrixEvent , TypedEventEmitter } from "../../src" ;
33
+ import { defer , IDeferred } from "../../src/utils" ;
32
34
33
35
declare module "../../src/@types/event" {
34
36
interface SecretStorageAccountDataEvents {
@@ -273,6 +275,78 @@ describe("ServerSideSecretStorageImpl", function () {
273
275
expect ( console . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( "unknown algorithm" ) ) ;
274
276
} ) ;
275
277
} ) ;
278
+
279
+ describe ( "setDefaultKeyId" , function ( ) {
280
+ let secretStorage : ServerSideSecretStorage ;
281
+ let accountDataAdapter : Mocked < AccountDataClient > ;
282
+ let accountDataPromise : IDeferred < void > ;
283
+ beforeEach ( ( ) => {
284
+ accountDataAdapter = mockAccountDataClient ( ) ;
285
+ accountDataPromise = defer ( ) ;
286
+ accountDataAdapter . setAccountData . mockImplementation ( ( ) => {
287
+ accountDataPromise . resolve ( ) ;
288
+ return Promise . resolve ( { } ) ;
289
+ } ) ;
290
+
291
+ secretStorage = new ServerSideSecretStorageImpl ( accountDataAdapter , { } ) ;
292
+ } ) ;
293
+
294
+ it ( "should set the default key id" , async function ( ) {
295
+ const setDefaultPromise = secretStorage . setDefaultKeyId ( "keyId" ) ;
296
+ await accountDataPromise . promise ;
297
+
298
+ expect ( accountDataAdapter . setAccountData ) . toHaveBeenCalledWith ( "m.secret_storage.default_key" , {
299
+ key : "keyId" ,
300
+ } ) ;
301
+
302
+ accountDataAdapter . emit (
303
+ ClientEvent . AccountData ,
304
+ new MatrixEvent ( {
305
+ type : "m.secret_storage.default_key" ,
306
+ content : { key : "keyId" } ,
307
+ } ) ,
308
+ ) ;
309
+ await setDefaultPromise ;
310
+ } ) ;
311
+
312
+ it ( "should set the default key id with a null key id" , async function ( ) {
313
+ const setDefaultPromise = secretStorage . setDefaultKeyId ( null ) ;
314
+ await accountDataPromise . promise ;
315
+
316
+ expect ( accountDataAdapter . setAccountData ) . toHaveBeenCalledWith ( "m.secret_storage.default_key" , { } ) ;
317
+
318
+ accountDataAdapter . emit (
319
+ ClientEvent . AccountData ,
320
+ new MatrixEvent ( {
321
+ type : "m.secret_storage.default_key" ,
322
+ content : { } ,
323
+ } ) ,
324
+ ) ;
325
+ await setDefaultPromise ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ describe ( "getDefaultKeyId" , function ( ) {
330
+ it ( "should return null when there is no key" , async function ( ) {
331
+ const accountDataAdapter = mockAccountDataClient ( ) ;
332
+ const secretStorage = new ServerSideSecretStorageImpl ( accountDataAdapter , { } ) ;
333
+ expect ( await secretStorage . getDefaultKeyId ( ) ) . toBe ( null ) ;
334
+ } ) ;
335
+
336
+ it ( "should return the key id when there is a key" , async function ( ) {
337
+ const accountDataAdapter = mockAccountDataClient ( ) ;
338
+ accountDataAdapter . getAccountDataFromServer . mockResolvedValue ( { key : "keyId" } ) ;
339
+ const secretStorage = new ServerSideSecretStorageImpl ( accountDataAdapter , { } ) ;
340
+ expect ( await secretStorage . getDefaultKeyId ( ) ) . toBe ( "keyId" ) ;
341
+ } ) ;
342
+
343
+ it ( "should return null when an empty object is in the account data" , async function ( ) {
344
+ const accountDataAdapter = mockAccountDataClient ( ) ;
345
+ accountDataAdapter . getAccountDataFromServer . mockResolvedValue ( { } ) ;
346
+ const secretStorage = new ServerSideSecretStorageImpl ( accountDataAdapter , { } ) ;
347
+ expect ( await secretStorage . getDefaultKeyId ( ) ) . toBe ( null ) ;
348
+ } ) ;
349
+ } ) ;
276
350
} ) ;
277
351
278
352
describe ( "trimTrailingEquals" , ( ) => {
@@ -291,8 +365,13 @@ describe("trimTrailingEquals", () => {
291
365
} ) ;
292
366
293
367
function mockAccountDataClient ( ) : Mocked < AccountDataClient > {
368
+ const eventEmitter = new TypedEventEmitter ( ) ;
294
369
return {
295
370
getAccountDataFromServer : jest . fn ( ) . mockResolvedValue ( null ) ,
296
371
setAccountData : jest . fn ( ) . mockResolvedValue ( { } ) ,
372
+ on : eventEmitter . on . bind ( eventEmitter ) ,
373
+ off : eventEmitter . off . bind ( eventEmitter ) ,
374
+ removeListener : eventEmitter . removeListener . bind ( eventEmitter ) ,
375
+ emit : eventEmitter . emit . bind ( eventEmitter ) ,
297
376
} as unknown as Mocked < AccountDataClient > ;
298
377
}
0 commit comments