element-web-Github/test/Terms-test.tsx

192 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-07-11 21:32:04 +08:00
/*
Copyright 2019 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2022-12-12 19:24:14 +08:00
import { MatrixEvent, EventType, SERVICE_TYPES } from "matrix-js-sdk/src/matrix";
2019-07-11 21:32:04 +08:00
2022-12-12 19:24:14 +08:00
import { startTermsFlow, Service } from "../src/Terms";
import { getMockClientWithEventEmitter } from "./test-utils";
import { MatrixClientPeg } from "../src/MatrixClientPeg";
2019-07-11 21:32:04 +08: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-12-12 19:24:14 +08: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 21:32:04 +08:00
2022-12-12 19:24:14 +08:00
describe("Terms", function () {
const mockClient = getMockClientWithEventEmitter({
getAccountData: jest.fn(),
getTerms: jest.fn(),
agreeToTerms: jest.fn(),
setAccountData: jest.fn(),
});
2022-12-12 19:24:14 +08:00
beforeEach(function () {
jest.clearAllMocks();
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue(null);
mockClient.setAccountData.mockResolvedValue({});
});
afterAll(() => {
2022-12-12 19:24:14 +08:00
jest.spyOn(MatrixClientPeg, "get").mockRestore();
2019-07-11 21:32:04 +08:00
});
2022-12-12 19:24:14 +08:00
it("should prompt for all terms & services if no account data", async function () {
mockClient.getAccountData.mockReturnValue(undefined);
mockClient.getTerms.mockResolvedValue({
2019-07-11 21:32:04 +08:00
policies: {
2022-12-12 19:24:14 +08:00
policy_the_first: POLICY_ONE,
2019-07-11 21:32:04 +08:00
},
});
2019-12-16 19:12:48 +08:00
const interactionCallback = jest.fn().mockResolvedValue([]);
2019-07-11 21:32:04 +08:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2023-03-01 23:23:35 +08:00
expect(interactionCallback).toHaveBeenCalledWith(
2022-12-12 19:24:14 +08:00
[
{
service: IM_SERVICE_ONE,
policies: {
policy_the_first: POLICY_ONE,
},
2019-07-11 21:32:04 +08:00
},
2022-12-12 19:24:14 +08:00
],
[],
);
2019-07-11 21:32:04 +08:00
});
2022-12-12 19:24:14 +08: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 21:32:04 +08:00
accepted: ["http://example.com/one"],
},
2019-07-11 21:32:04 +08:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
2019-07-11 21:32:04 +08:00
policies: {
2022-12-12 19:24:14 +08:00
policy_the_first: POLICY_ONE,
2019-07-11 21:32:04 +08:00
},
});
mockClient.agreeToTerms;
2019-07-11 21:32:04 +08:00
2019-12-16 19:12:48 +08:00
const interactionCallback = jest.fn();
2019-07-11 21:32:04 +08:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2019-12-16 19:12:48 +08:00
expect(interactionCallback).not.toHaveBeenCalled();
2023-03-01 23:23:35 +08:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
2022-12-12 19:24:14 +08:00
"http://example.com/one",
]);
2019-07-11 21:32:04 +08:00
});
2022-12-12 19:24:14 +08: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 21:32:04 +08:00
accepted: ["http://example.com/one"],
},
2019-07-11 21:32:04 +08:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
mockClient.getTerms.mockResolvedValue({
2019-07-11 21:32:04 +08:00
policies: {
2022-12-12 19:24:14 +08:00
policy_the_first: POLICY_ONE,
policy_the_second: POLICY_TWO,
2019-07-11 21:32:04 +08:00
},
});
2019-12-16 19:12:48 +08:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
2019-07-11 21:32:04 +08:00
await startTermsFlow([IM_SERVICE_ONE], interactionCallback);
2023-03-01 23:23:35 +08:00
expect(interactionCallback).toHaveBeenCalledWith(
2022-12-12 19:24:14 +08:00
[
{
service: IM_SERVICE_ONE,
policies: {
policy_the_second: POLICY_TWO,
},
2019-07-11 21:32:04 +08:00
},
2022-12-12 19:24:14 +08:00
],
["http://example.com/one"],
2019-12-16 19:12:48 +08:00
);
2023-03-01 23:23:35 +08:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
2022-12-12 19:24:14 +08:00
"http://example.com/one",
"http://example.com/two",
]);
2019-07-11 21:32:04 +08:00
});
2022-12-12 19:24:14 +08:00
it("should prompt for only services with un-agreed policies", async function () {
const directEvent = new MatrixEvent({
type: EventType.Direct,
content: {
2019-07-11 21:32:04 +08:00
accepted: ["http://example.com/one"],
},
2019-07-11 21:32:04 +08:00
});
mockClient.getAccountData.mockReturnValue(directEvent);
2019-07-11 21:32:04 +08:00
mockClient.getTerms.mockImplementation(async (_serviceTypes: SERVICE_TYPES, baseUrl: string) => {
2019-07-11 21:32:04 +08:00
switch (baseUrl) {
2022-12-12 19:24:14 +08:00
case "https://imone.test":
2019-07-11 21:32:04 +08:00
return {
policies: {
2022-12-12 19:24:14 +08:00
policy_the_first: POLICY_ONE,
2019-07-11 21:32:04 +08:00
},
};
2022-12-12 19:24:14 +08:00
case "https://imtwo.test":
2019-07-11 21:32:04 +08:00
return {
policies: {
2022-12-12 19:24:14 +08:00
policy_the_second: POLICY_TWO,
2019-07-11 21:32:04 +08:00
},
};
}
});
2019-12-16 19:12:48 +08:00
const interactionCallback = jest.fn().mockResolvedValue(["http://example.com/one", "http://example.com/two"]);
2019-07-11 21:32:04 +08:00
await startTermsFlow([IM_SERVICE_ONE, IM_SERVICE_TWO], interactionCallback);
2023-03-01 23:23:35 +08:00
expect(interactionCallback).toHaveBeenCalledWith(
2022-12-12 19:24:14 +08:00
[
{
service: IM_SERVICE_TWO,
policies: {
policy_the_second: POLICY_TWO,
},
2019-07-11 21:32:04 +08:00
},
2022-12-12 19:24:14 +08:00
],
["http://example.com/one"],
2019-12-16 19:12:48 +08:00
);
2023-03-01 23:23:35 +08:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imone.test", "a token token", [
2022-12-12 19:24:14 +08:00
"http://example.com/one",
]);
2023-03-01 23:23:35 +08:00
expect(mockClient.agreeToTerms).toHaveBeenCalledWith(SERVICE_TYPES.IM, "https://imtwo.test", "a token token", [
2022-12-12 19:24:14 +08:00
"http://example.com/two",
]);
2019-07-11 21:32:04 +08:00
});
});