From 7f978bee773427350880776436e448b419138a0a Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 10:49:41 +0100 Subject: [PATCH 1/6] Support for MSC2457 logout_devices param for setPassword() --- src/client.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 1ae008dba2a..c1ede23015a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7815,15 +7815,35 @@ export class MatrixClient extends TypedEventEmitter { // TODO: Types + public setPassword( + authDict: any, + newPassword: string, + logoutDevices: boolean, + callback?: Callback + ): Promise; + public setPassword( + authDict: any, + newPassword: string, + logoutDevices?: Callback | boolean, + callback?: Callback, + ): Promise { // TODO: Types + if (typeof logoutDevices !== 'boolean') { + logoutDevices = true; + } + if (typeof logoutDevices === 'function') { + callback = logoutDevices; + } + const path = "/account/password"; const data = { 'auth': authDict, 'new_password': newPassword, + 'logout_devices': logoutDevices, }; return this.http.authedRequest( From 8d708cb193b9be3a21f66b79439f7ce8851c5f63 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 17:09:38 +0100 Subject: [PATCH 2/6] Fix overloading of arguments --- src/client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index c1ede23015a..ca38e997594 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7832,12 +7832,12 @@ export class MatrixClient extends TypedEventEmitter { // TODO: Types - if (typeof logoutDevices !== 'boolean') { - logoutDevices = true; - } if (typeof logoutDevices === 'function') { callback = logoutDevices; } + if (typeof logoutDevices !== 'boolean') { + logoutDevices = true; + } const path = "/account/password"; const data = { From 7bfb5688b56889f473147127e0cbc9c9a22157f7 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 17:09:50 +0100 Subject: [PATCH 3/6] Add missing overload --- src/client.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client.ts b/src/client.ts index ca38e997594..f3cd76a21c3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7820,6 +7820,11 @@ export class MatrixClient extends TypedEventEmitter; public setPassword( authDict: any, newPassword: string, From 4aa561bc55baa70d580c0fe73bf0dc5440f0b394 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 17:10:24 +0100 Subject: [PATCH 4/6] Style fixes --- src/client.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index f3cd76a21c3..7771e856d0c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7823,14 +7823,14 @@ export class MatrixClient extends TypedEventEmitter; public setPassword( authDict: any, newPassword: string, logoutDevices: boolean, - callback?: Callback - ): Promise; + callback?: Callback, + ): Promise; public setPassword( authDict: any, newPassword: string, From 0ac5c7ea4b60f2f2cf06e5ec694e5a0c6f7a94ed Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 17:16:49 +0100 Subject: [PATCH 5/6] Provide return type for setPassword Based on https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3accountpassword --- src/client.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index 7771e856d0c..5230bc002cc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7824,19 +7824,19 @@ export class MatrixClient extends TypedEventEmitter; + ): Promise<{}>; public setPassword( authDict: any, newPassword: string, logoutDevices: boolean, callback?: Callback, - ): Promise; + ): Promise<{}>; public setPassword( authDict: any, newPassword: string, logoutDevices?: Callback | boolean, callback?: Callback, - ): Promise { // TODO: Types + ): Promise<{}> { if (typeof logoutDevices === 'function') { callback = logoutDevices; } @@ -7851,7 +7851,7 @@ export class MatrixClient extends TypedEventEmitter( callback, Method.Post, path, null, data, ); } From 16674e22c7009448bd37b977e55a0a197db10694 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Fri, 8 Apr 2022 18:19:33 +0100 Subject: [PATCH 6/6] Test cases + more backwards compatible behaviour --- spec/unit/matrix-client.spec.ts | 53 +++++++++++++++++++++++++++++++++ src/client.ts | 4 ++- 2 files changed, 56 insertions(+), 1 deletion(-) 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 5230bc002cc..918970f697f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7841,7 +7841,9 @@ export class MatrixClient extends TypedEventEmitter