Files
ThreadNet-Web/apps/web/test/unit-tests/Terms-test.ts
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
7.3 KiB
TypeScript
Raw Normal View History

2019-07-11 14:32:04 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2019-07-11 14:32:04 +01:00
Copyright 2019 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2019-07-11 14:32:04 +01:00
*/
2025-02-05 13:25:06 +00:00
import { EventType, MatrixEvent, type Policy, SERVICE_TYPES, type Terms } from "matrix-js-sdk/src/matrix";
import { screen, within } from "jest-matrix-react";
2019-07-11 14:32:04 +01:00
import { dialogTermsInteractionCallback, Service, startTermsFlow } from "../../src/Terms";
2024-10-15 14:57:26 +01:00
import { getMockClientWithEventEmitter } from "../test-utils";
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
2019-07-11 14:32:04 +01:00
const POLICY_ONE = {
version: "six",
en: {
name: "The first policy",
url: "http://example.com/one",
},
} satisfies Policy;
2019-07-11 14:32:04 +01:00
const POLICY_TWO = {
version: "IX",
en: {
name: "The second policy",
url: "http://example.com/two",
},
} satisfies Policy;
2019-07-11 14:32:04 +01:00
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");
2019-07-11 14:32:04 +01:00
describe("Terms", function () {
const mockClient = getMockClientWithEventEmitter({
getAccountData: jest.fn(),
getTerms: jest.fn(),
agreeToTerms: jest.fn(),
setAccountData: jest.fn(),
});
2019-07-11 14:32:04 +01:00
beforeEach(function () {
jest.clearAllMocks();
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue({ policies: {} });
mockClient.setAccountData.mockResolvedValue({});
});
afterAll(() => {
jest.spyOn(MatrixClientPeg, "get").mockRestore();
2019-07-11 14:32:04 +01:00
});
it("should prompt for all terms & services if no account data", async function () {
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue({
2019-07-11 14:32:04 +01:00
policies: {
policy_the_first: POLICY_ONE,
},
});
2019-12-16 11:12:48 +00:00
const interactionCallback = jest.fn().mockResolvedValue([]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
2019-07-11 14:32:04 +01:00
2023-03-01 16:23:35 +01:00
expect(interactionCallback).toHaveBeenCalledWith(
2019-12-16 11:12:48 +00:00
[
2019-07-11 14:32:04 +01:00
{
service: IM_SERVICE_ONE,
policies: {
policy_the_first: POLICY_ONE,
2022-12-12 12:24:14 +01:00
},
2019-07-11 14:32:04 +01:00
},
],
2019-12-16 11:12:48 +00:00
[],
);
2019-07-11 14:32:04 +01:00
});
it("should not prompt if all policies are signed in account data", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
2019-07-11 14:32:04 +01:00
accepted: ["http://example.com/one"],
},
2019-07-11 14:32:04 +01:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
2019-07-11 14:32:04 +01:00
policies: {
policy_the_first: POLICY_ONE,
},
});
2019-12-16 11:12:48 +00:00
const interactionCallback = jest.fn();
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
2019-07-11 14:32:04 +01:00
2019-12-16 11:12:48 +00:00
expect(interactionCallback).not.toHaveBeenCalled();
2023-03-01 16:23:35 +01:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
"http://example.com/one",
2019-12-16 11:12:48 +00:00
]);
2019-07-11 14:32:04 +01:00
});
it("should prompt for only terms that aren't already signed", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
2019-07-11 14:32:04 +01:00
accepted: ["http://example.com/one"],
},
2019-07-11 14:32:04 +01:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
2019-07-11 14:32:04 +01:00
policies: {
policy_the_first: POLICY_ONE,
policy_the_second: POLICY_TWO,
},
});
2019-12-16 11:12:48 +00:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
2019-07-11 14:32:04 +01:00
2023-03-01 16:23:35 +01:00
expect(interactionCallback).toHaveBeenCalledWith(
2019-12-16 11:12:48 +00:00
[
2019-07-11 14:32:04 +01:00
{
service: IM_SERVICE_ONE,
policies: {
policy_the_second: POLICY_TWO,
2022-12-12 12:24:14 +01:00
},
2019-07-11 14:32:04 +01:00
},
2019-12-16 11:12:48 +00:00
],
["http://example.com/one"],
2019-12-16 11:12:48 +00:00
);
2023-03-01 16:23:35 +01:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
2019-12-16 11:12:48 +00:00
"http://example.com/one",
"http://example.com/two",
2022-12-12 12:24:14 +01:00
]);
2019-07-11 14:32:04 +01:00
});
it("should prompt for only services with un-agreed policies", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
2019-07-11 14:32:04 +01:00
accepted: ["http://example.com/one"],
},
2019-07-11 14:32:04 +01:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
2019-07-11 14:32:04 +01:00
mockClient.getTerms.mockImplementation(
async (_serviceTypes: SERVICE_TYPES, baseUrl: string): Promise<Terms> => {
switch (baseUrl) {
case "https://imone.test":
return {
policies: {
policy_the_first: POLICY_ONE,
},
};
case "https://imtwo.test":
return {
policies: {
policy_the_second: POLICY_TWO,
},
};
}
return { policies: {} };
},
);
2019-07-11 14:32:04 +01:00
2019-12-16 11:12:48 +00:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
await startTermsFlow(mockClient, [IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
2019-07-11 14:32:04 +01:00
2023-03-01 16:23:35 +01:00
expect(interactionCallback).toHaveBeenCalledWith(
2019-12-16 11:12:48 +00:00
[
2019-07-11 14:32:04 +01:00
{
service: IM_SERVICE_TWO,
policies: {
policy_the_second: POLICY_TWO,
2022-12-12 12:24:14 +01:00
},
2019-07-11 14:32:04 +01:00
},
2019-12-16 11:12:48 +00:00
],
["http://example.com/one"],
2019-12-16 11:12:48 +00:00
);
2023-03-01 16:23:35 +01:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
"http://example.com/one",
2022-12-12 12:24:14 +01:00
]);
2023-03-01 16:23:35 +01:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imtwo.test", "a token token", [
"http://example.com/two",
2019-12-16 11:12:48 +00:00
]);
2019-07-11 14:32:04 +01:00
});
});
describe("dialogTermsInteractionCallback", () => {
it("should render a dialog with the expected terms", async () => {
dialogTermsInteractionCallback(
[
{
service: new Service(SERVICE_TYPES.IS, "http://base_url", "access_token"),
policies: {
sample: {
version: "VERSION",
en: {
name: "Terms",
url: "http://base_url/terms",
},
},
},
},
],
[],
);
const dialog = await screen.findByRole("dialog");
expect(within(dialog).getByRole("link")).toHaveAttribute("href", "http://base_url/terms");
expect(dialog).toMatchSnapshot();
});
});