2021-03-10 01:13:16 +08:00
|
|
|
import '../../../skinned-sdk'; // Must be first for skinning to work
|
2020-02-04 19:25:19 +08:00
|
|
|
import React from 'react';
|
|
|
|
import TestRenderer from 'react-test-renderer';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import * as TestUtils from '../../../test-utils';
|
|
|
|
|
|
|
|
import { MatrixClientPeg } from '../../../../src/MatrixClientPeg';
|
|
|
|
import { MatrixEvent } from 'matrix-js-sdk';
|
|
|
|
import MKeyVerificationConclusion from '../../../../src/components/views/messages/MKeyVerificationConclusion';
|
|
|
|
|
2020-02-04 19:31:16 +08:00
|
|
|
const trustworthy = () => ({ isCrossSigningVerified: () => true });
|
|
|
|
const untrustworthy = () => ({ isCrossSigningVerified: () => false });
|
2020-02-04 19:25:19 +08:00
|
|
|
|
|
|
|
describe("MKeyVerificationConclusion", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
TestUtils.stubClient();
|
|
|
|
const client = MatrixClientPeg.get();
|
2020-02-04 19:31:16 +08:00
|
|
|
client.checkUserTrust = trustworthy;
|
2020-02-04 19:25:19 +08:00
|
|
|
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
client.on = emitter.on.bind(emitter);
|
|
|
|
client.removeListener = emitter.removeListener.bind(emitter);
|
|
|
|
client.emit = emitter.emit.bind(emitter);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if there's no verificationRequest", () => {
|
|
|
|
const event = new MatrixEvent({});
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the verificationRequest is pending", () => {
|
|
|
|
const event = new MatrixEvent({});
|
|
|
|
event.verificationRequest = new EventEmitter();
|
|
|
|
event.verificationRequest.pending = true;
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the event type is cancel but the request type isn't", () => {
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.cancel" });
|
|
|
|
event.verificationRequest = new EventEmitter();
|
|
|
|
event.verificationRequest.cancelled = false;
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the event type is done but the request type isn't", () => {
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = new EventEmitter();
|
|
|
|
event.verificationRequest.done = false;
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("shouldn't render if the user isn't actually trusted", () => {
|
|
|
|
const client = MatrixClientPeg.get();
|
2020-02-04 19:31:16 +08:00
|
|
|
client.checkUserTrust = untrustworthy;
|
2020-02-04 19:25:19 +08:00
|
|
|
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = new EventEmitter();
|
|
|
|
event.verificationRequest.done = true;
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should rerender appropriately if user trust status changes", () => {
|
|
|
|
const client = MatrixClientPeg.get();
|
2020-02-04 19:31:16 +08:00
|
|
|
client.checkUserTrust = untrustworthy;
|
2020-02-04 19:25:19 +08:00
|
|
|
|
|
|
|
const event = new MatrixEvent({ type: "m.key.verification.done" });
|
|
|
|
event.verificationRequest = new EventEmitter();
|
|
|
|
event.verificationRequest.done = true;
|
|
|
|
event.verificationRequest.otherUserId = "@someuser:domain";
|
|
|
|
const renderer = TestRenderer.create(
|
|
|
|
<MKeyVerificationConclusion mxEvent={event} />,
|
|
|
|
);
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
|
2020-02-04 19:31:16 +08:00
|
|
|
client.checkUserTrust = trustworthy;
|
2020-02-04 19:25:19 +08:00
|
|
|
|
|
|
|
/* Ensure we don't rerender for every trust status change of any user */
|
|
|
|
client.emit("userTrustStatusChanged", "@anotheruser:domain");
|
|
|
|
expect(renderer.toJSON()).toBeNull();
|
|
|
|
|
|
|
|
/* But when our user changes, we do rerender */
|
|
|
|
client.emit("userTrustStatusChanged", event.verificationRequest.otherUserId);
|
|
|
|
expect(renderer.toJSON()).not.toBeNull();
|
|
|
|
});
|
|
|
|
});
|