2022-08-15 12:40:26 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-08-15 12:40:26 +00:00
|
|
|
Copyright 2022 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.
|
2022-08-15 12:40:26 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { act, fireEvent, render } from "jest-matrix-react";
|
2022-08-15 12:40:26 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import SecurityRecommendations from "../../../../../../src/components/views/settings/devices/SecurityRecommendations";
|
|
|
|
import { DeviceSecurityVariation } from "../../../../../../src/components/views/settings/devices/types";
|
2022-08-15 12:40:26 +00:00
|
|
|
|
|
|
|
const MS_DAY = 24 * 60 * 60 * 1000;
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<SecurityRecommendations />", () => {
|
|
|
|
const unverifiedNoMetadata = { device_id: "unverified-no-metadata", isVerified: false };
|
|
|
|
const verifiedNoMetadata = { device_id: "verified-no-metadata", isVerified: true };
|
|
|
|
const hundredDaysOld = { device_id: "100-days-old", isVerified: true, last_seen_ts: Date.now() - MS_DAY * 100 };
|
2022-08-15 12:40:26 +00:00
|
|
|
const hundredDaysOldUnverified = {
|
2022-12-12 11:24:14 +00:00
|
|
|
device_id: "unverified-100-days-old",
|
2022-08-15 12:40:26 +00:00
|
|
|
isVerified: false,
|
2022-12-12 11:24:14 +00:00
|
|
|
last_seen_ts: Date.now() - MS_DAY * 100,
|
2022-08-15 12:40:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
devices: {},
|
2022-08-31 12:34:22 +00:00
|
|
|
goToFilteredList: jest.fn(),
|
2022-12-12 11:24:14 +00:00
|
|
|
currentDeviceId: "abc123",
|
2022-08-15 12:40:26 +00:00
|
|
|
};
|
2022-12-12 11:24:14 +00:00
|
|
|
const getComponent = (props = {}) => <SecurityRecommendations {...defaultProps} {...props} />;
|
2022-08-15 12:40:26 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders null when no devices", () => {
|
2022-08-15 12:40:26 +00:00
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container.firstChild).toBeNull();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders unverified devices section when user has unverified devices", () => {
|
2022-08-15 12:40:26 +00:00
|
|
|
const devices = {
|
|
|
|
[unverifiedNoMetadata.device_id]: unverifiedNoMetadata,
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
[hundredDaysOldUnverified.device_id]: hundredDaysOldUnverified,
|
|
|
|
};
|
|
|
|
const { container } = render(getComponent({ devices }));
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not render unverified devices section when only the current device is unverified", () => {
|
2022-09-05 09:10:12 +00:00
|
|
|
const devices = {
|
|
|
|
[unverifiedNoMetadata.device_id]: unverifiedNoMetadata,
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
};
|
|
|
|
const { container } = render(getComponent({ devices, currentDeviceId: unverifiedNoMetadata.device_id }));
|
|
|
|
// nothing to render
|
|
|
|
expect(container.firstChild).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders inactive devices section when user has inactive devices", () => {
|
2022-08-15 12:40:26 +00:00
|
|
|
const devices = {
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
[hundredDaysOldUnverified.device_id]: hundredDaysOldUnverified,
|
|
|
|
};
|
|
|
|
const { container } = render(getComponent({ devices }));
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders both cards when user has both unverified and inactive devices", () => {
|
2022-08-15 12:40:26 +00:00
|
|
|
const devices = {
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
[hundredDaysOld.device_id]: hundredDaysOld,
|
|
|
|
[unverifiedNoMetadata.device_id]: unverifiedNoMetadata,
|
|
|
|
};
|
|
|
|
const { container } = render(getComponent({ devices }));
|
|
|
|
expect(container).toMatchSnapshot();
|
|
|
|
});
|
2022-08-31 12:34:22 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("clicking view all unverified devices button works", () => {
|
2022-08-31 12:34:22 +00:00
|
|
|
const goToFilteredList = jest.fn();
|
|
|
|
const devices = {
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
[hundredDaysOld.device_id]: hundredDaysOld,
|
|
|
|
[unverifiedNoMetadata.device_id]: unverifiedNoMetadata,
|
|
|
|
};
|
|
|
|
const { getByTestId } = render(getComponent({ devices, goToFilteredList }));
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(getByTestId("unverified-devices-cta"));
|
2022-08-31 12:34:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(goToFilteredList).toHaveBeenCalledWith(DeviceSecurityVariation.Unverified);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("clicking view all inactive devices button works", () => {
|
2022-08-31 12:34:22 +00:00
|
|
|
const goToFilteredList = jest.fn();
|
|
|
|
const devices = {
|
|
|
|
[verifiedNoMetadata.device_id]: verifiedNoMetadata,
|
|
|
|
[hundredDaysOld.device_id]: hundredDaysOld,
|
|
|
|
[unverifiedNoMetadata.device_id]: unverifiedNoMetadata,
|
|
|
|
};
|
|
|
|
const { getByTestId } = render(getComponent({ devices, goToFilteredList }));
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
fireEvent.click(getByTestId("inactive-devices-cta"));
|
2022-08-31 12:34:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(goToFilteredList).toHaveBeenCalledWith(DeviceSecurityVariation.Inactive);
|
|
|
|
});
|
2022-08-15 12:40:26 +00:00
|
|
|
});
|