element-web-Github/test/unit-tests/Terms-test.tsx
Michael Telatynski c05c429803
Absorb the matrix-react-sdk repository (#28192)
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
Co-authored-by: Kim Brose <kim.brose@nordeck.net>
Co-authored-by: Florian Duros <florianduros@element.io>
Co-authored-by: R Midhun Suresh <hi@midhun.dev>
Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com>
Co-authored-by: ElementRobot <releases@riot.im>
Co-authored-by: dbkr <dbkr@users.noreply.github.com>
Co-authored-by: David Baker <dbkr@users.noreply.github.com>
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: David Langley <davidl@element.io>
Co-authored-by: Michael Weimann <michaelw@matrix.org>
Co-authored-by: Timshel <Timshel@users.noreply.github.com>
Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com>
Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: Hubert Chathi <hubert@uhoreg.ca>
Co-authored-by: Andrew Ferrazzutti <andrewf@element.io>
Co-authored-by: Robin <robin@robin.town>
Co-authored-by: Tulir Asokan <tulir@maunium.net>
2024-10-16 13:31:55 +01:00

184 lines
6.1 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { MatrixEvent, EventType, SERVICE_TYPES } from "matrix-js-sdk/src/matrix";
import { startTermsFlow, Service } from "../../src/Terms";
import { getMockClientWithEventEmitter } from "../test-utils";
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
const POLICY_ONE = {
version: "six",
en: {
name: "The first policy",
url: "http://example.com/one",
},
};
const POLICY_TWO = {
version: "IX",
en: {
name: "The second policy",
url: "http://example.com/two",
},
};
const IM_SERVICE_ONE = new Service(SERVICE_TYPES.IM, "https://imone.test", "a token token");
const IM_SERVICE_TWO = new Service(SERVICE_TYPES.IM, "https://imtwo.test", "a token token");
describe("Terms", function () {
const mockClient = getMockClientWithEventEmitter({
getAccountData: jest.fn(),
getTerms: jest.fn(),
agreeToTerms: jest.fn(),
setAccountData: jest.fn(),
});
beforeEach(function () {
jest.clearAllMocks();
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue(null);
mockClient.setAccountData.mockResolvedValue({});
});
afterAll(() => {
jest.spyOn(MatrixClientPeg, "get").mockRestore();
});
it("should prompt for all terms & services if no account data", async function () {
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue({
policies: {
policy_the_first: POLICY_ONE,
},
});
const interactionCallback = jest.fn().mockResolvedValue([]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
expect(interactionCallback).toHaveBeenCalledWith(
[
{
service: IM_SERVICE_ONE,
policies: {
policy_the_first: POLICY_ONE,
},
},
],
[],
);
});
it("should not prompt if all policies are signed in account data", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
accepted: ["http://example.com/one"],
},
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
policies: {
policy_the_first: POLICY_ONE,
},
});
mockClient.agreeToTerms;
const interactionCallback = jest.fn();
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
expect(interactionCallback).not.toHaveBeenCalled();
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
"http://example.com/one",
]);
});
it("should prompt for only terms that aren't already signed", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
accepted: ["http://example.com/one"],
},
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
policies: {
policy_the_first: POLICY_ONE,
policy_the_second: POLICY_TWO,
},
});
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
expect(interactionCallback).toHaveBeenCalledWith(
[
{
service: IM_SERVICE_ONE,
policies: {
policy_the_second: POLICY_TWO,
},
},
],
["http://example.com/one"],
);
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
"http://example.com/one",
"http://example.com/two",
]);
});
it("should prompt for only services with un-agreed policies", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
accepted: ["http://example.com/one"],
},
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockImplementation(async (_serviceTypes: SERVICE_TYPES, baseUrl: string) => {
switch (baseUrl) {
case "https://imone.test":
return {
policies: {
policy_the_first: POLICY_ONE,
},
};
case "https://imtwo.test":
return {
policies: {
policy_the_second: POLICY_TWO,
},
};
}
});
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
expect(interactionCallback).toHaveBeenCalledWith(
[
{
service: IM_SERVICE_TWO,
policies: {
policy_the_second: POLICY_TWO,
},
},
],
["http://example.com/one"],
);
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
"http://example.com/one",
]);
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imtwo.test", "a token token", [
"http://example.com/two",
]);
});
});