@@ -23,12 +23,14 @@ import {
2323 SecretStorageCallbacks ,
2424 SecretStorageKeyDescriptionAesV1 ,
2525 SecretStorageKeyDescriptionCommon ,
26+ ServerSideSecretStorage ,
2627 ServerSideSecretStorageImpl ,
2728 trimTrailingEquals ,
2829} from "../../src/secret-storage" ;
2930import { randomString } from "../../src/randomstring" ;
3031import { 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" ;
3234
3335declare module "../../src/@types/event" {
3436 interface SecretStorageAccountDataEvents {
@@ -273,6 +275,78 @@ describe("ServerSideSecretStorageImpl", function () {
273275 expect ( console . warn ) . toHaveBeenCalledWith ( expect . stringContaining ( "unknown algorithm" ) ) ;
274276 } ) ;
275277 } ) ;
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+ } ) ;
276350} ) ;
277351
278352describe ( "trimTrailingEquals" , ( ) => {
@@ -291,8 +365,13 @@ describe("trimTrailingEquals", () => {
291365} ) ;
292366
293367function mockAccountDataClient ( ) : Mocked < AccountDataClient > {
368+ const eventEmitter = new TypedEventEmitter ( ) ;
294369 return {
295370 getAccountDataFromServer : jest . fn ( ) . mockResolvedValue ( null ) ,
296371 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 ) ,
297376 } as unknown as Mocked < AccountDataClient > ;
298377}
0 commit comments