2019-07-11 13:32:04 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-07-11 13:32:04 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-07-11 13:32:04 +00:00
|
|
|
*/
|
|
|
|
|
2022-05-03 15:09:07 +00:00
|
|
|
import { MatrixEvent, EventType, SERVICE_TYPES } from "matrix-js-sdk/src/matrix";
|
2019-07-11 13:32:04 +00:00
|
|
|
|
|
|
|
import { startTermsFlow, Service } from "../src/Terms";
|
2022-05-03 15:09:07 +00:00
|
|
|
import { getMockClientWithEventEmitter } from "./test-utils";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
2019-07-11 13:32:04 +00:00
|
|
|
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-05-03 15:09:07 +00: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 13:32:04 +00:00
|
|
|
|
|
|
|
describe("Terms", function () {
|
2022-05-03 15:09:07 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getAccountData: jest.fn(),
|
|
|
|
getTerms: jest.fn(),
|
|
|
|
agreeToTerms: jest.fn(),
|
|
|
|
setAccountData: jest.fn(),
|
|
|
|
});
|
|
|
|
|
2019-07-11 13:32:04 +00:00
|
|
|
beforeEach(function () {
|
2022-05-03 15:09:07 +00:00
|
|
|
jest.clearAllMocks();
|
2023-02-16 17:21:44 +00:00
|
|
|
mockClient.getAccountData.mockReturnValue(undefined);
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getTerms.mockResolvedValue(null);
|
|
|
|
mockClient.setAccountData.mockResolvedValue({});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockRestore();
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should prompt for all terms & services if no account data", async function () {
|
2023-02-16 17:21:44 +00:00
|
|
|
mockClient.getAccountData.mockReturnValue(undefined);
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getTerms.mockResolvedValue({
|
2019-07-11 13:32:04 +00:00
|
|
|
policies: {
|
|
|
|
policy_the_first: POLICY_ONE,
|
|
|
|
},
|
|
|
|
});
|
2019-12-16 11:12:48 +00:00
|
|
|
const interactionCallback = jest.fn().mockResolvedValue([]);
|
2023-06-01 13:43:24 +00:00
|
|
|
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(interactionCallback).toHaveBeenCalledWith(
|
2019-12-16 11:12:48 +00:00
|
|
|
[
|
2019-07-11 13:32:04 +00:00
|
|
|
{
|
|
|
|
service: IM_SERVICE_ONE,
|
|
|
|
policies: {
|
|
|
|
policy_the_first: POLICY_ONE,
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
},
|
|
|
|
],
|
2019-12-16 11:12:48 +00:00
|
|
|
[],
|
|
|
|
);
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not prompt if all policies are signed in account data", async function () {
|
2022-05-03 15:09:07 +00:00
|
|
|
const directEvent = new MatrixEvent({
|
|
|
|
type: EventType.Direct,
|
|
|
|
content: {
|
2019-07-11 13:32:04 +00:00
|
|
|
accepted: ["http://example.com/one"],
|
2022-05-03 15:09:07 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
|
|
|
mockClient.getTerms.mockResolvedValue({
|
2019-07-11 13:32:04 +00:00
|
|
|
policies: {
|
|
|
|
policy_the_first: POLICY_ONE,
|
|
|
|
},
|
|
|
|
});
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.agreeToTerms;
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2019-12-16 11:12:48 +00:00
|
|
|
const interactionCallback = jest.fn();
|
2023-06-01 13:43:24 +00:00
|
|
|
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2019-12-16 11:12:48 +00:00
|
|
|
expect(interactionCallback).not.toHaveBeenCalled();
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
|
2019-07-11 13:44:45 +00:00
|
|
|
"http://example.com/one",
|
2019-12-16 11:12:48 +00:00
|
|
|
]);
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should prompt for only terms that aren't already signed", async function () {
|
2022-05-03 15:09:07 +00:00
|
|
|
const directEvent = new MatrixEvent({
|
|
|
|
type: EventType.Direct,
|
|
|
|
content: {
|
2019-07-11 13:32:04 +00:00
|
|
|
accepted: ["http://example.com/one"],
|
2022-05-03 15:09:07 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
|
|
|
|
|
|
|
mockClient.getTerms.mockResolvedValue({
|
2019-07-11 13:32:04 +00: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"]);
|
2023-06-01 13:43:24 +00:00
|
|
|
await startTermsFlow(mockClient, [IM_SERVICE_ONE], interactionCallback);
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(interactionCallback).toHaveBeenCalledWith(
|
2019-12-16 11:12:48 +00:00
|
|
|
[
|
2019-07-11 13:32:04 +00:00
|
|
|
{
|
|
|
|
service: IM_SERVICE_ONE,
|
|
|
|
policies: {
|
|
|
|
policy_the_second: POLICY_TWO,
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
},
|
2019-12-16 11:12:48 +00:00
|
|
|
],
|
2019-07-11 13:44:45 +00:00
|
|
|
["http://example.com/one"],
|
2019-12-16 11:12:48 +00:00
|
|
|
);
|
2023-03-01 15:23:35 +00: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",
|
2019-07-11 13:44:45 +00:00
|
|
|
"http://example.com/two",
|
2022-12-12 11:24:14 +00:00
|
|
|
]);
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should prompt for only services with un-agreed policies", async function () {
|
2022-05-03 15:09:07 +00:00
|
|
|
const directEvent = new MatrixEvent({
|
|
|
|
type: EventType.Direct,
|
|
|
|
content: {
|
2019-07-11 13:32:04 +00:00
|
|
|
accepted: ["http://example.com/one"],
|
2022-05-03 15:09:07 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getAccountData.mockReturnValue(directEvent);
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2022-05-03 15:09:07 +00:00
|
|
|
mockClient.getTerms.mockImplementation(async (_serviceTypes: SERVICE_TYPES, baseUrl: string) => {
|
2019-07-11 13:32:04 +00:00
|
|
|
switch (baseUrl) {
|
|
|
|
case "https://imone.test":
|
|
|
|
return {
|
|
|
|
policies: {
|
|
|
|
policy_the_first: POLICY_ONE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
case "https://imtwo.test":
|
|
|
|
return {
|
|
|
|
policies: {
|
|
|
|
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"]);
|
2023-06-01 13:43:24 +00:00
|
|
|
await startTermsFlow(mockClient, [IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
|
2019-07-11 13:32:04 +00:00
|
|
|
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(interactionCallback).toHaveBeenCalledWith(
|
2019-12-16 11:12:48 +00:00
|
|
|
[
|
2019-07-11 13:32:04 +00:00
|
|
|
{
|
|
|
|
service: IM_SERVICE_TWO,
|
|
|
|
policies: {
|
|
|
|
policy_the_second: POLICY_TWO,
|
2022-12-12 11:24:14 +00:00
|
|
|
},
|
2019-07-11 13:32:04 +00:00
|
|
|
},
|
2019-12-16 11:12:48 +00:00
|
|
|
],
|
2019-07-11 13:44:45 +00:00
|
|
|
["http://example.com/one"],
|
2019-12-16 11:12:48 +00:00
|
|
|
);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
|
2022-05-03 15:09:07 +00:00
|
|
|
"http://example.com/one",
|
2022-12-12 11:24:14 +00:00
|
|
|
]);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imtwo.test", "a token token", [
|
2019-07-11 13:44:45 +00:00
|
|
|
"http://example.com/two",
|
2019-12-16 11:12:48 +00:00
|
|
|
]);
|
2019-07-11 13:32:04 +00:00
|
|
|
});
|
|
|
|
});
|