diff --git a/spec/unit/matrix-client.spec.ts b/spec/unit/matrix-client.spec.ts index b9b0081135e..841b7c67be0 100644 --- a/spec/unit/matrix-client.spec.ts +++ b/spec/unit/matrix-client.spec.ts @@ -1026,4 +1026,57 @@ describe("MatrixClient", function() { expect(requestContent).toEqual(content); }); }); + + describe("setPassword", () => { + const auth = { session: 'abcdef', type: 'foo' }; + const newPassword = 'newpassword'; + const callback = () => {}; + + const passwordTest = (expectedRequestContent: any, expectedCallback?: Function) => { + const [callback, method, path, queryParams, requestContent] = client.http.authedRequest.mock.calls[0]; + if (expectedCallback) { + expect(callback).toBe(expectedCallback); + } else { + expect(callback).toBeFalsy(); + } + expect(method).toBe('POST'); + expect(path).toEqual('/account/password'); + expect(queryParams).toBeFalsy(); + expect(requestContent).toEqual(expectedRequestContent); + }; + + beforeEach(() => { + client.http.authedRequest.mockClear().mockResolvedValue({}); + }); + + it("no logout_devices specified", async () => { + await client.setPassword(auth, newPassword); + passwordTest({ auth, new_password: newPassword }); + }); + + it("no logout_devices specified + callback", async () => { + await client.setPassword(auth, newPassword, callback); + passwordTest({ auth, new_password: newPassword }, callback); + }); + + it("overload logoutDevices=true", async () => { + await client.setPassword(auth, newPassword, true); + passwordTest({ auth, new_password: newPassword, logout_devices: true }); + }); + + it("overload logoutDevices=true + callback", async () => { + await client.setPassword(auth, newPassword, true, callback); + passwordTest({ auth, new_password: newPassword, logout_devices: true }, callback); + }); + + it("overload logoutDevices=false", async () => { + await client.setPassword(auth, newPassword, false); + passwordTest({ auth, new_password: newPassword, logout_devices: false }); + }); + + it("overload logoutDevices=false + callback", async () => { + await client.setPassword(auth, newPassword, false, callback); + passwordTest({ auth, new_password: newPassword, logout_devices: false }, callback); + }); + }); }); diff --git a/src/client.ts b/src/client.ts index 1ae008dba2a..918970f697f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7815,18 +7815,45 @@ export class MatrixClient extends TypedEventEmitter { // TODO: Types + public setPassword( + authDict: any, + newPassword: string, + callback?: Callback, + ): Promise<{}>; + public setPassword( + authDict: any, + newPassword: string, + logoutDevices: boolean, + callback?: Callback, + ): Promise<{}>; + public setPassword( + authDict: any, + newPassword: string, + logoutDevices?: Callback | boolean, + callback?: Callback, + ): Promise<{}> { + if (typeof logoutDevices === 'function') { + callback = logoutDevices; + } + if (typeof logoutDevices !== 'boolean') { + // Use backwards compatible behaviour of not specifying logout_devices + // This way it is left up to the server: + logoutDevices = undefined; + } + const path = "/account/password"; const data = { 'auth': authDict, 'new_password': newPassword, + 'logout_devices': logoutDevices, }; - return this.http.authedRequest( + return this.http.authedRequest<{}>( callback, Method.Post, path, null, data, ); }