From 8b079056cce79bee1572986ea08d8a1dd547b5f0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 18 Oct 2022 15:35:42 +0100 Subject: [PATCH 1/3] Fix POST data not being passed for registerWithIdentityServer Due to `any` types this wasn't caught by TSC --- spec/integ/matrix-client-methods.spec.ts | 24 ++++++++++++++++++++++++ src/client.ts | 7 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/spec/integ/matrix-client-methods.spec.ts b/spec/integ/matrix-client-methods.spec.ts index 954df27fd1b..b282e38ceae 100644 --- a/spec/integ/matrix-client-methods.spec.ts +++ b/spec/integ/matrix-client-methods.spec.ts @@ -1296,6 +1296,30 @@ describe("MatrixClient", function() { expect(client.http.opts.accessToken).toBe(token); }); }); + + describe("registerWithIdentityServer", () => { + it("should pass data to POST request", async () => { + const token = { + access_token: "access_token", + token_type: "Bearer", + matrix_server_name: "server_name", + expires_in: 12345, + }; + + httpBackend!.when("POST", "/account/register").check(req => { + expect(req.data).toStrictEqual(token); + }).respond(200, { + access_token: "at", + token: "tt", + }); + + const prom = client!.registerWithIdentityServer(token); + await httpBackend!.flushAllExpected(); + const resp = await prom; + expect(resp.access_token).toBe("at"); + expect(resp.token).toBe("tt"); + }); + }); }); function withThreadId(event: MatrixEvent, newThreadId: string): MatrixEvent { diff --git a/src/client.ts b/src/client.ts index fc049a42089..bd6fea36a81 100644 --- a/src/client.ts +++ b/src/client.ts @@ -8278,13 +8278,16 @@ export class MatrixClient extends TypedEventEmitter { // TODO: Types + public registerWithIdentityServer(hsOpenIdToken: IOpenIDToken): Promise<{ + access_token: string; + token: string; + }> { if (!this.idBaseUrl) { throw new Error("No identity server base URL set"); } const uri = this.http.getUrl("/account/register", undefined, IdentityPrefix.V2, this.idBaseUrl); - return this.http.requestOtherUrl(Method.Post, uri, null, hsOpenIdToken); + return this.http.requestOtherUrl(Method.Post, uri, hsOpenIdToken); } /** From fb64be0a417014cc93415e8c3b2674627f3fef44 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 18 Oct 2022 15:43:35 +0100 Subject: [PATCH 2/3] Fix agreeToTerms sending args as query instead of body --- spec/integ/matrix-client-methods.spec.ts | 14 ++++++++++++++ src/client.ts | 9 ++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/spec/integ/matrix-client-methods.spec.ts b/spec/integ/matrix-client-methods.spec.ts index b282e38ceae..322833f9940 100644 --- a/spec/integ/matrix-client-methods.spec.ts +++ b/spec/integ/matrix-client-methods.spec.ts @@ -1254,6 +1254,20 @@ describe("MatrixClient", function() { }); }); + describe("agreeToTerms", () => { + it("should send `user_accepts` via body of POST request", async () => { + const terms = ["https://vector.im/notice-1"]; + + httpBackend!.when("POST", "/terms").check(req => { + expect(req.data.user_accepts).toStrictEqual(terms); + }).respond(200, {}); + + const prom = client!.agreeToTerms(SERVICE_TYPES.IS, "https://vector.im", "at", terms); + await httpBackend!.flushAllExpected(); + await prom; + }); + }); + describe("publicRooms", () => { it("should use GET request if no server or filter is specified", () => { httpBackend!.when("GET", "/publicRooms").respond(200, {}); diff --git a/src/client.ts b/src/client.ts index bd6fea36a81..e501f7d20db 100644 --- a/src/client.ts +++ b/src/client.ts @@ -8754,15 +8754,14 @@ export class MatrixClient extends TypedEventEmitter { // TODO: Types + ): Promise<{}> { const url = this.termsUrlForService(serviceType, baseUrl); - utils.encodeParams({ - user_accepts: termsUrls, - }, url.searchParams); const headers = { Authorization: "Bearer " + accessToken, }; - return this.http.requestOtherUrl(Method.Post, url, null, { headers }); + return this.http.requestOtherUrl(Method.Post, url, { + user_accepts: termsUrls, + }, { headers }); } /** From 16d34690ef723a08c56968aab36dbdabe5420966 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 18 Oct 2022 15:46:22 +0100 Subject: [PATCH 3/3] Strict ts --- spec/integ/matrix-client-methods.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integ/matrix-client-methods.spec.ts b/spec/integ/matrix-client-methods.spec.ts index 322833f9940..273491e70fe 100644 --- a/spec/integ/matrix-client-methods.spec.ts +++ b/spec/integ/matrix-client-methods.spec.ts @@ -1277,7 +1277,7 @@ describe("MatrixClient", function() { it("should use GET request if only server is specified", () => { httpBackend!.when("GET", "/publicRooms").check(request => { - expect(request.queryParams.server).toBe("server1"); + expect(request.queryParams?.server).toBe("server1"); }).respond(200, {}); client!.publicRooms({ server: "server1" }); return httpBackend!.flushAllExpected();