You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-07-30 04:23:07 +03:00
Support MSC3391: Account data deletion (#2967)
* add deleteAccountData endpoint * check server support and test * test current state of memorystore * interpret account data events with empty content as deleted * add handling for (future) stable version of endpoint * add getSafeUserId * user getSafeUserId in deleteAccountData * better jsdoc for throws documentation
This commit is contained in:
@ -57,6 +57,7 @@ import {
|
||||
import { IOlmDevice } from "../../src/crypto/algorithms/megolm";
|
||||
import { QueryDict } from "../../src/utils";
|
||||
import { SyncState } from "../../src/sync";
|
||||
import * as featureUtils from "../../src/feature";
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
@ -281,6 +282,23 @@ describe("MatrixClient", function () {
|
||||
client.stopClient();
|
||||
});
|
||||
|
||||
describe("getSafeUserId()", () => {
|
||||
it("returns the logged in user id", () => {
|
||||
expect(client.getSafeUserId()).toEqual(userId);
|
||||
});
|
||||
|
||||
it("throws when there is not logged in user", () => {
|
||||
const notLoggedInClient = new MatrixClient({
|
||||
baseUrl: "https://my.home.server",
|
||||
idBaseUrl: identityServerUrl,
|
||||
fetchFn: function () {} as any, // NOP
|
||||
store: store,
|
||||
scheduler: scheduler,
|
||||
});
|
||||
expect(() => notLoggedInClient.getSafeUserId()).toThrow("Expected logged in user but found none.");
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendEvent", () => {
|
||||
const roomId = "!room:example.org";
|
||||
const body = "This is the body";
|
||||
@ -1828,4 +1846,68 @@ describe("MatrixClient", function () {
|
||||
expect(client.getUseE2eForGroupCall()).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("delete account data", () => {
|
||||
afterEach(() => {
|
||||
jest.spyOn(featureUtils, "buildFeatureSupportMap").mockRestore();
|
||||
});
|
||||
it("makes correct request when deletion is supported by server in unstable versions", async () => {
|
||||
const eventType = "im.vector.test";
|
||||
const versionsResponse = {
|
||||
versions: ["1"],
|
||||
unstable_features: {
|
||||
"org.matrix.msc3391": true,
|
||||
},
|
||||
};
|
||||
jest.spyOn(client.http, "request").mockResolvedValue(versionsResponse);
|
||||
const requestSpy = jest.spyOn(client.http, "authedRequest").mockImplementation(() => Promise.resolve());
|
||||
const unstablePrefix = "/_matrix/client/unstable/org.matrix.msc3391";
|
||||
const path = `/user/${encodeURIComponent(userId)}/account_data/${eventType}`;
|
||||
|
||||
// populate version support
|
||||
await client.getVersions();
|
||||
await client.deleteAccountData(eventType);
|
||||
|
||||
expect(requestSpy).toHaveBeenCalledWith(Method.Delete, path, undefined, undefined, {
|
||||
prefix: unstablePrefix,
|
||||
});
|
||||
});
|
||||
|
||||
it("makes correct request when deletion is supported by server based on matrix version", async () => {
|
||||
const eventType = "im.vector.test";
|
||||
// we don't have a stable version for account data deletion yet to test this code path with
|
||||
// so mock the support map to fake stable support
|
||||
const stableSupportedDeletionMap = new Map();
|
||||
stableSupportedDeletionMap.set(featureUtils.Feature.AccountDataDeletion, featureUtils.ServerSupport.Stable);
|
||||
jest.spyOn(featureUtils, "buildFeatureSupportMap").mockResolvedValue(new Map());
|
||||
const requestSpy = jest.spyOn(client.http, "authedRequest").mockImplementation(() => Promise.resolve());
|
||||
const path = `/user/${encodeURIComponent(userId)}/account_data/${eventType}`;
|
||||
|
||||
// populate version support
|
||||
await client.getVersions();
|
||||
await client.deleteAccountData(eventType);
|
||||
|
||||
expect(requestSpy).toHaveBeenCalledWith(Method.Delete, path, undefined, undefined, undefined);
|
||||
});
|
||||
|
||||
it("makes correct request when deletion is not supported by server", async () => {
|
||||
const eventType = "im.vector.test";
|
||||
const versionsResponse = {
|
||||
versions: ["1"],
|
||||
unstable_features: {
|
||||
"org.matrix.msc3391": false,
|
||||
},
|
||||
};
|
||||
jest.spyOn(client.http, "request").mockResolvedValue(versionsResponse);
|
||||
const requestSpy = jest.spyOn(client.http, "authedRequest").mockImplementation(() => Promise.resolve());
|
||||
const path = `/user/${encodeURIComponent(userId)}/account_data/${eventType}`;
|
||||
|
||||
// populate version support
|
||||
await client.getVersions();
|
||||
await client.deleteAccountData(eventType);
|
||||
|
||||
// account data updated with empty content
|
||||
expect(requestSpy).toHaveBeenCalledWith(Method.Put, path, undefined, {});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user