2022-04-16 17:18:12 +02:00
|
|
|
import Substitute, { Arg } from "@fluffy-spoon/substitute";
|
|
|
|
|
2022-06-14 17:10:53 +02:00
|
|
|
import { CryptoService } from "@bitwarden/common/abstractions/crypto.service";
|
|
|
|
import { AttachmentData } from "@bitwarden/common/models/data/attachmentData";
|
|
|
|
import { Attachment } from "@bitwarden/common/models/domain/attachment";
|
|
|
|
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetricCryptoKey";
|
|
|
|
import { ContainerService } from "@bitwarden/common/services/container.service";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-08-05 00:07:24 +02:00
|
|
|
import { makeStaticByteArray, mockEnc } from "../../utils";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
describe("Attachment", () => {
|
|
|
|
let data: AttachmentData;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
data = {
|
|
|
|
id: "id",
|
|
|
|
url: "url",
|
|
|
|
fileName: "fileName",
|
|
|
|
key: "key",
|
|
|
|
size: "1100",
|
|
|
|
sizeName: "1.1 KB",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Convert from empty", () => {
|
|
|
|
const data = new AttachmentData();
|
|
|
|
const attachment = new Attachment(data);
|
|
|
|
|
|
|
|
expect(attachment).toEqual({
|
|
|
|
id: null,
|
|
|
|
url: null,
|
|
|
|
size: undefined,
|
|
|
|
sizeName: null,
|
|
|
|
key: null,
|
|
|
|
fileName: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Convert", () => {
|
|
|
|
const attachment = new Attachment(data);
|
|
|
|
|
|
|
|
expect(attachment).toEqual({
|
|
|
|
size: "1100",
|
|
|
|
id: "id",
|
|
|
|
url: "url",
|
|
|
|
sizeName: "1.1 KB",
|
|
|
|
fileName: { encryptedString: "fileName", encryptionType: 0 },
|
|
|
|
key: { encryptedString: "key", encryptionType: 0 },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toAttachmentData", () => {
|
|
|
|
const attachment = new Attachment(data);
|
|
|
|
expect(attachment.toAttachmentData()).toEqual(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Decrypt", async () => {
|
|
|
|
const attachment = new Attachment();
|
|
|
|
attachment.id = "id";
|
|
|
|
attachment.url = "url";
|
|
|
|
attachment.size = "1100";
|
|
|
|
attachment.sizeName = "1.1 KB";
|
|
|
|
attachment.key = mockEnc("key");
|
|
|
|
attachment.fileName = mockEnc("fileName");
|
|
|
|
|
|
|
|
const cryptoService = Substitute.for<CryptoService>();
|
|
|
|
cryptoService.getOrgKey(null).resolves(null);
|
|
|
|
cryptoService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(32));
|
|
|
|
|
|
|
|
(window as any).bitwardenContainerService = new ContainerService(cryptoService);
|
|
|
|
|
|
|
|
const view = await attachment.decrypt(null);
|
|
|
|
|
|
|
|
expect(view).toEqual({
|
|
|
|
id: "id",
|
|
|
|
url: "url",
|
|
|
|
size: "1100",
|
|
|
|
sizeName: "1.1 KB",
|
|
|
|
fileName: "fileName",
|
|
|
|
key: expect.any(SymmetricCryptoKey),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|