Skip to content

Commit 7201334

Browse files
authored
More sonar tweaks and typing improvements (#2366)
* More sonar tweaks and typing improvements * delint * Write some tests * Attempt to make TS happy * Stash tests * Add tests * Add `istanbul ignore if` around logging special-case for test env * Add test * Comments
1 parent 6f445ca commit 7201334

File tree

8 files changed

+325
-103
lines changed

8 files changed

+325
-103
lines changed

spec/integ/matrix-client-methods.spec.js

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
import * as utils from "../test-utils/test-utils";
218
import { CRYPTO_ENABLED } from "../../src/client";
319
import { MatrixEvent } from "../../src/models/event";
@@ -823,6 +839,171 @@ describe("MatrixClient", function() {
823839
]);
824840
});
825841
});
842+
843+
describe("getThirdpartyUser", () => {
844+
it("should hit the expected API endpoint", async () => {
845+
const response = [{
846+
userid: "@Bob",
847+
protocol: "irc",
848+
fields: {},
849+
}];
850+
851+
const prom = client.getThirdpartyUser("irc", {});
852+
httpBackend.when("GET", "/thirdparty/user/irc").respond(200, response);
853+
await httpBackend.flush();
854+
expect(await prom).toStrictEqual(response);
855+
});
856+
});
857+
858+
describe("getThirdpartyLocation", () => {
859+
it("should hit the expected API endpoint", async () => {
860+
const response = [{
861+
alias: "#alias",
862+
protocol: "irc",
863+
fields: {},
864+
}];
865+
866+
const prom = client.getThirdpartyLocation("irc", {});
867+
httpBackend.when("GET", "/thirdparty/location/irc").respond(200, response);
868+
await httpBackend.flush();
869+
expect(await prom).toStrictEqual(response);
870+
});
871+
});
872+
873+
describe("getPushers", () => {
874+
it("should hit the expected API endpoint", async () => {
875+
const response = {
876+
pushers: [],
877+
};
878+
879+
const prom = client.getPushers();
880+
httpBackend.when("GET", "/pushers").respond(200, response);
881+
await httpBackend.flush();
882+
expect(await prom).toStrictEqual(response);
883+
});
884+
});
885+
886+
describe("getKeyChanges", () => {
887+
it("should hit the expected API endpoint", async () => {
888+
const response = {
889+
changed: [],
890+
left: [],
891+
};
892+
893+
const prom = client.getKeyChanges("old", "new");
894+
httpBackend.when("GET", "/keys/changes").check((req) => {
895+
expect(req.queryParams.from).toEqual("old");
896+
expect(req.queryParams.to).toEqual("new");
897+
}).respond(200, response);
898+
await httpBackend.flush();
899+
expect(await prom).toStrictEqual(response);
900+
});
901+
});
902+
903+
describe("getDevices", () => {
904+
it("should hit the expected API endpoint", async () => {
905+
const response = {
906+
devices: [],
907+
};
908+
909+
const prom = client.getDevices();
910+
httpBackend.when("GET", "/devices").respond(200, response);
911+
await httpBackend.flush();
912+
expect(await prom).toStrictEqual(response);
913+
});
914+
});
915+
916+
describe("getDevice", () => {
917+
it("should hit the expected API endpoint", async () => {
918+
const response = {
919+
device_id: "DEADBEEF",
920+
display_name: "NotAPhone",
921+
last_seen_ip: "127.0.0.1",
922+
last_seen_ts: 1,
923+
};
924+
925+
const prom = client.getDevice("DEADBEEF");
926+
httpBackend.when("GET", "/devices/DEADBEEF").respond(200, response);
927+
await httpBackend.flush();
928+
expect(await prom).toStrictEqual(response);
929+
});
930+
});
931+
932+
describe("getThreePids", () => {
933+
it("should hit the expected API endpoint", async () => {
934+
const response = {
935+
threepids: [],
936+
};
937+
938+
const prom = client.getThreePids();
939+
httpBackend.when("GET", "/account/3pid").respond(200, response);
940+
await httpBackend.flush();
941+
expect(await prom).toStrictEqual(response);
942+
});
943+
});
944+
945+
describe("deleteAlias", () => {
946+
it("should hit the expected API endpoint", async () => {
947+
const response = {};
948+
const prom = client.deleteAlias("#foo:bar");
949+
httpBackend.when("DELETE", "/directory/room/" + encodeURIComponent("#foo:bar")).respond(200, response);
950+
await httpBackend.flush();
951+
expect(await prom).toStrictEqual(response);
952+
});
953+
});
954+
955+
describe("deleteRoomTag", () => {
956+
it("should hit the expected API endpoint", async () => {
957+
const response = {};
958+
const prom = client.deleteRoomTag("!roomId:server", "u.tag");
959+
const url = `/user/${encodeURIComponent(userId)}/rooms/${encodeURIComponent("!roomId:server")}/tags/u.tag`;
960+
httpBackend.when("DELETE", url).respond(200, response);
961+
await httpBackend.flush();
962+
expect(await prom).toStrictEqual(response);
963+
});
964+
});
965+
966+
describe("getRoomTags", () => {
967+
it("should hit the expected API endpoint", async () => {
968+
const response = {
969+
tags: {
970+
"u.tag": {
971+
order: 0.5,
972+
},
973+
},
974+
};
975+
976+
const prom = client.getRoomTags("!roomId:server");
977+
const url = `/user/${encodeURIComponent(userId)}/rooms/${encodeURIComponent("!roomId:server")}/tags`;
978+
httpBackend.when("GET", url).respond(200, response);
979+
await httpBackend.flush();
980+
expect(await prom).toStrictEqual(response);
981+
});
982+
});
983+
984+
describe("requestRegisterEmailToken", () => {
985+
it("should hit the expected API endpoint", async () => {
986+
const response = {
987+
sid: "random_sid",
988+
submit_url: "https://foobar.matrix/_matrix/matrix",
989+
};
990+
991+
httpBackend.when("GET", "/_matrix/client/versions").respond(200, {
992+
versions: ["r0.5.0"],
993+
});
994+
995+
const prom = client.requestRegisterEmailToken("bob@email", "secret", 1);
996+
httpBackend.when("POST", "/register/email/requestToken").check(req => {
997+
expect(req.data).toStrictEqual({
998+
email: "bob@email",
999+
client_secret: "secret",
1000+
send_attempt: 1,
1001+
});
1002+
}).respond(200, response);
1003+
await httpBackend.flush();
1004+
expect(await prom).toStrictEqual(response);
1005+
});
1006+
});
8261007
});
8271008

8281009
function withThreadId(event, newThreadId) {

spec/unit/webrtc/call.spec.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
*/
1616

1717
import { TestClient } from '../../TestClient';
18-
import { MatrixCall, CallErrorCode, CallEvent } from '../../../src/webrtc/call';
18+
import { MatrixCall, CallErrorCode, CallEvent, supportsMatrixCall } from '../../../src/webrtc/call';
1919
import { SDPStreamMetadataKey, SDPStreamMetadataPurpose } from '../../../src/webrtc/callEventTypes';
2020
import { RoomMember } from "../../../src";
2121

@@ -505,4 +505,40 @@ describe('Call', function() {
505505
return sender?.track?.kind === "video";
506506
}).track.id).toBe("video_track");
507507
});
508+
509+
describe("supportsMatrixCall", () => {
510+
it("should return true when the environment is right", () => {
511+
expect(supportsMatrixCall()).toBe(true);
512+
});
513+
514+
it("should return false if window or document are undefined", () => {
515+
global.window = undefined;
516+
expect(supportsMatrixCall()).toBe(false);
517+
global.window = prevWindow;
518+
global.document = undefined;
519+
expect(supportsMatrixCall()).toBe(false);
520+
});
521+
522+
it("should return false if RTCPeerConnection throws", () => {
523+
// @ts-ignore - writing to window as we are simulating browser edge-cases
524+
global.window = {};
525+
Object.defineProperty(global.window, "RTCPeerConnection", {
526+
get: () => {
527+
throw Error("Secure mode, naaah!");
528+
},
529+
});
530+
expect(supportsMatrixCall()).toBe(false);
531+
});
532+
533+
it("should return false if RTCPeerConnection & RTCSessionDescription " +
534+
"& RTCIceCandidate & mediaDevices are unavailable",
535+
() => {
536+
global.window.RTCPeerConnection = undefined;
537+
global.window.RTCSessionDescription = undefined;
538+
global.window.RTCIceCandidate = undefined;
539+
// @ts-ignore - writing to a read-only property as we are simulating faulty browsers
540+
global.navigator.mediaDevices = undefined;
541+
expect(supportsMatrixCall()).toBe(false);
542+
});
543+
});
508544
});

src/@types/requests.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,17 @@ export interface IFilterResponse {
190190
filter_id: string;
191191
}
192192

193+
export interface ITagsResponse {
194+
tags: {
195+
[tagId: string]: {
196+
order: number;
197+
};
198+
};
199+
}
200+
201+
export interface IStatusResponse extends IPresenceOpts {
202+
currently_active?: boolean;
203+
last_active_ago?: number;
204+
}
205+
193206
/* eslint-enable camelcase */

0 commit comments

Comments
 (0)