@@ -57,6 +57,7 @@ import {
5757import { IOlmDevice } from "../../src/crypto/algorithms/megolm" ;
5858import { QueryDict } from "../../src/utils" ;
5959import { SyncState } from "../../src/sync" ;
60+ import * as featureUtils from "../../src/feature" ;
6061
6162jest . useFakeTimers ( ) ;
6263
@@ -281,6 +282,23 @@ describe("MatrixClient", function () {
281282 client . stopClient ( ) ;
282283 } ) ;
283284
285+ describe ( "getSafeUserId()" , ( ) => {
286+ it ( "returns the logged in user id" , ( ) => {
287+ expect ( client . getSafeUserId ( ) ) . toEqual ( userId ) ;
288+ } ) ;
289+
290+ it ( "throws when there is not logged in user" , ( ) => {
291+ const notLoggedInClient = new MatrixClient ( {
292+ baseUrl : "https://my.home.server" ,
293+ idBaseUrl : identityServerUrl ,
294+ fetchFn : function ( ) { } as any , // NOP
295+ store : store ,
296+ scheduler : scheduler ,
297+ } ) ;
298+ expect ( ( ) => notLoggedInClient . getSafeUserId ( ) ) . toThrow ( "Expected logged in user but found none." ) ;
299+ } ) ;
300+ } ) ;
301+
284302 describe ( "sendEvent" , ( ) => {
285303 const roomId = "!room:example.org" ;
286304 const body = "This is the body" ;
@@ -1828,4 +1846,68 @@ describe("MatrixClient", function () {
18281846 expect ( client . getUseE2eForGroupCall ( ) ) . toBe ( false ) ;
18291847 } ) ;
18301848 } ) ;
1849+
1850+ describe ( "delete account data" , ( ) => {
1851+ afterEach ( ( ) => {
1852+ jest . spyOn ( featureUtils , "buildFeatureSupportMap" ) . mockRestore ( ) ;
1853+ } ) ;
1854+ it ( "makes correct request when deletion is supported by server in unstable versions" , async ( ) => {
1855+ const eventType = "im.vector.test" ;
1856+ const versionsResponse = {
1857+ versions : [ "1" ] ,
1858+ unstable_features : {
1859+ "org.matrix.msc3391" : true ,
1860+ } ,
1861+ } ;
1862+ jest . spyOn ( client . http , "request" ) . mockResolvedValue ( versionsResponse ) ;
1863+ const requestSpy = jest . spyOn ( client . http , "authedRequest" ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
1864+ const unstablePrefix = "/_matrix/client/unstable/org.matrix.msc3391" ;
1865+ const path = `/user/${ encodeURIComponent ( userId ) } /account_data/${ eventType } ` ;
1866+
1867+ // populate version support
1868+ await client . getVersions ( ) ;
1869+ await client . deleteAccountData ( eventType ) ;
1870+
1871+ expect ( requestSpy ) . toHaveBeenCalledWith ( Method . Delete , path , undefined , undefined , {
1872+ prefix : unstablePrefix ,
1873+ } ) ;
1874+ } ) ;
1875+
1876+ it ( "makes correct request when deletion is supported by server based on matrix version" , async ( ) => {
1877+ const eventType = "im.vector.test" ;
1878+ // we don't have a stable version for account data deletion yet to test this code path with
1879+ // so mock the support map to fake stable support
1880+ const stableSupportedDeletionMap = new Map ( ) ;
1881+ stableSupportedDeletionMap . set ( featureUtils . Feature . AccountDataDeletion , featureUtils . ServerSupport . Stable ) ;
1882+ jest . spyOn ( featureUtils , "buildFeatureSupportMap" ) . mockResolvedValue ( new Map ( ) ) ;
1883+ const requestSpy = jest . spyOn ( client . http , "authedRequest" ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
1884+ const path = `/user/${ encodeURIComponent ( userId ) } /account_data/${ eventType } ` ;
1885+
1886+ // populate version support
1887+ await client . getVersions ( ) ;
1888+ await client . deleteAccountData ( eventType ) ;
1889+
1890+ expect ( requestSpy ) . toHaveBeenCalledWith ( Method . Delete , path , undefined , undefined , undefined ) ;
1891+ } ) ;
1892+
1893+ it ( "makes correct request when deletion is not supported by server" , async ( ) => {
1894+ const eventType = "im.vector.test" ;
1895+ const versionsResponse = {
1896+ versions : [ "1" ] ,
1897+ unstable_features : {
1898+ "org.matrix.msc3391" : false ,
1899+ } ,
1900+ } ;
1901+ jest . spyOn ( client . http , "request" ) . mockResolvedValue ( versionsResponse ) ;
1902+ const requestSpy = jest . spyOn ( client . http , "authedRequest" ) . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
1903+ const path = `/user/${ encodeURIComponent ( userId ) } /account_data/${ eventType } ` ;
1904+
1905+ // populate version support
1906+ await client . getVersions ( ) ;
1907+ await client . deleteAccountData ( eventType ) ;
1908+
1909+ // account data updated with empty content
1910+ expect ( requestSpy ) . toHaveBeenCalledWith ( Method . Put , path , undefined , { } ) ;
1911+ } ) ;
1912+ } ) ;
18311913} ) ;
0 commit comments