mirror of
https://github.com/bitwarden/browser
synced 2026-02-20 11:24:07 +00:00
* Untrust devices that cannot be rotated * Add tests and only send request on more than 0 failed devices * Address feedback
119 lines
3.4 KiB
TypeScript
119 lines
3.4 KiB
TypeScript
import { mock, MockProxy } from "jest-mock-extended";
|
|
|
|
import { ApiService } from "../../abstractions/api.service";
|
|
import { DeviceResponse } from "../abstractions/devices/responses/device.response";
|
|
|
|
import { DevicesApiServiceImplementation } from "./devices-api.service.implementation";
|
|
|
|
describe("DevicesApiServiceImplementation", () => {
|
|
let devicesApiService: DevicesApiServiceImplementation;
|
|
let apiService: MockProxy<ApiService>;
|
|
|
|
beforeEach(() => {
|
|
apiService = mock<ApiService>();
|
|
devicesApiService = new DevicesApiServiceImplementation(apiService);
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.resetAllMocks();
|
|
});
|
|
|
|
describe("getKnownDevice", () => {
|
|
it("calls api with correct parameters", async () => {
|
|
const email = "test@example.com";
|
|
const deviceIdentifier = "device123";
|
|
apiService.send.mockResolvedValue(true);
|
|
|
|
const result = await devicesApiService.getKnownDevice(email, deviceIdentifier);
|
|
|
|
expect(result).toBe(true);
|
|
expect(apiService.send).toHaveBeenCalledWith(
|
|
"GET",
|
|
"/devices/knowndevice",
|
|
null,
|
|
false,
|
|
true,
|
|
null,
|
|
expect.any(Function),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("getDeviceByIdentifier", () => {
|
|
it("returns device response", async () => {
|
|
const deviceIdentifier = "device123";
|
|
const mockResponse = { id: "123", name: "Test Device" };
|
|
apiService.send.mockResolvedValue(mockResponse);
|
|
|
|
const result = await devicesApiService.getDeviceByIdentifier(deviceIdentifier);
|
|
|
|
expect(result).toBeInstanceOf(DeviceResponse);
|
|
expect(apiService.send).toHaveBeenCalledWith(
|
|
"GET",
|
|
`/devices/identifier/${deviceIdentifier}`,
|
|
null,
|
|
true,
|
|
true,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("updateTrustedDeviceKeys", () => {
|
|
it("updates device keys and returns device response", async () => {
|
|
const deviceIdentifier = "device123";
|
|
const publicKeyEncrypted = "encryptedPublicKey";
|
|
const userKeyEncrypted = "encryptedUserKey";
|
|
const deviceKeyEncrypted = "encryptedDeviceKey";
|
|
const mockResponse = { id: "123", name: "Test Device" };
|
|
apiService.send.mockResolvedValue(mockResponse);
|
|
|
|
const result = await devicesApiService.updateTrustedDeviceKeys(
|
|
deviceIdentifier,
|
|
publicKeyEncrypted,
|
|
userKeyEncrypted,
|
|
deviceKeyEncrypted,
|
|
);
|
|
|
|
expect(result).toBeInstanceOf(DeviceResponse);
|
|
expect(apiService.send).toHaveBeenCalledWith(
|
|
"PUT",
|
|
`/devices/${deviceIdentifier}/keys`,
|
|
{
|
|
encryptedPrivateKey: deviceKeyEncrypted,
|
|
encryptedPublicKey: userKeyEncrypted,
|
|
encryptedUserKey: publicKeyEncrypted,
|
|
},
|
|
true,
|
|
true,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("untrustDevices", () => {
|
|
it("calls api with correct parameters", async () => {
|
|
const deviceIds = ["device1", "device2"];
|
|
apiService.send.mockResolvedValue(true);
|
|
|
|
await devicesApiService.untrustDevices(deviceIds);
|
|
expect(apiService.send).toHaveBeenCalledWith(
|
|
"POST",
|
|
"/devices/untrust",
|
|
{
|
|
devices: deviceIds,
|
|
},
|
|
true,
|
|
false,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("error handling", () => {
|
|
it("propagates api errors", async () => {
|
|
const error = new Error("API Error");
|
|
apiService.send.mockRejectedValue(error);
|
|
|
|
await expect(devicesApiService.getDevices()).rejects.toThrow("API Error");
|
|
});
|
|
});
|
|
});
|